Skip to content

Commit

Permalink
MR-140 Use MainActivity as splash screen
Browse files Browse the repository at this point in the history
  • Loading branch information
khoi-nguyen-2359 committed Sep 26, 2023
1 parent 6a1aae5 commit a5f00b7
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 126 deletions.
10 changes: 2 additions & 8 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,16 @@
android:exported="false" />

<activity
android:name=".feature.splash.SplashActivity"
android:name=".feature.main.MainActivity"
android:theme="@style/AppTheme.SplashScreen"
android:screenOrientation="portrait"
android:exported="true">
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".feature.main.MainActivity"
android:theme="@style/AppTheme.SemiTransparentSystemBars"
android:screenOrientation="portrait"
android:exported="false" />

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
Expand Down
74 changes: 73 additions & 1 deletion app/src/main/java/akio/apps/myrun/feature/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ package akio.apps.myrun.feature.main

import akio.apps.myrun.R
import akio.apps.myrun.data.activity.api.model.BaseActivityModel
import akio.apps.myrun.data.authentication.api.model.SignInSuccessResult
import akio.apps.myrun.feature.activitydetail.ActivityExportService
import akio.apps.myrun.feature.core.DialogDelegate
import akio.apps.myrun.feature.core.ktx.getParcelableExtraExt
import akio.apps.myrun.feature.core.ktx.lazyViewModelProvider
import akio.apps.myrun.feature.core.navigation.OnBoardingNavigation
import akio.apps.myrun.feature.main.di.DaggerMainActivityComponent
import akio.apps.myrun.feature.main.ui.MainNavHost
import akio.apps.myrun.feature.registration.SignInActivity
import akio.apps.myrun.feature.route.RoutePlanningFacade
import akio.apps.myrun.feature.tracking.LocationPermissionChecker
import akio.apps.myrun.feature.tracking.RouteTrackingActivity
Expand All @@ -14,23 +21,85 @@ import android.widget.Toast
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.splashscreen.SplashScreen
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.WindowCompat
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import timber.log.Timber

class MainActivity : AppCompatActivity() {

private val locationPermissionChecker: LocationPermissionChecker =
LocationPermissionChecker(activity = this)

private val mainViewModel: MainViewModel by lazyViewModelProvider {
DaggerMainActivityComponent.factory().create(application).mainViewModel()
}

private val dialogDelegate by lazy { DialogDelegate(this) }

private lateinit var splashScreen: SplashScreen
private var splashStartTime: Long = 0

override fun onCreate(savedInstanceState: Bundle?) {
splashScreen = installSplashScreen().apply {
setKeepOnScreenCondition { true }
}
splashStartTime = System.currentTimeMillis()
super.onCreate(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false)

lifecycleScope.launch {
mainViewModel.isUserSignedIn.collect(::onUserSignIn)
}
dialogDelegate.collectLaunchCatchingError(this, mainViewModel)
}

@Suppress("DEPRECATION")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
RC_SIGN_IN -> verifySignInResult(resultCode, data)
}
}

private fun verifySignInResult(resultCode: Int, data: Intent?) {
if (resultCode == RESULT_CANCELED || data == null) {
finish()
return
}

data.getParcelableExtraExt<SignInSuccessResult>(SignInActivity.RESULT_SIGN_RESULT_DATA)
?: return

goHome()
}

private fun onUserSignIn(isSignedIn: Boolean) = lifecycleScope.launch {
Timber.d("onUserSignIn $isSignedIn")
val delayMore = SPLASH_MIN_SHOWTIME - (System.currentTimeMillis() - splashStartTime)
if (delayMore > 0) {
delay(delayMore)
}
if (isSignedIn) {
splashScreen.setKeepOnScreenCondition { false }
goHome()
} else {
val intent = OnBoardingNavigation.createSignInIntent(this@MainActivity)
?: return@launch
@Suppress("DEPRECATION")
startActivityForResult(intent, RC_SIGN_IN)
}
}

private fun goHome() {
setContent {
MainNavHost(
onClickFloatingActionButton = ::openRouteTrackingOrCheckRequiredPermission,
onClickExportActivityFile = ::startActivityExportService
) { RoutePlanningFacade.startRoutePlanning(this) }
) { RoutePlanningFacade.startRoutePlanning(this@MainActivity) }
}
}

Expand Down Expand Up @@ -73,5 +142,8 @@ class MainActivity : AppCompatActivity() {
companion object {
fun clearTaskIntent(context: Context): Intent = Intent(context, MainActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)

private const val SPLASH_MIN_SHOWTIME = 700
private const val RC_SIGN_IN = 1
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package akio.apps.myrun.feature.splash
package akio.apps.myrun.feature.main

import akio.apps.myrun.data.authentication.api.UserAuthenticationState
import akio.apps.myrun.feature.core.launchcatching.LaunchCatchingDelegate
Expand All @@ -8,7 +8,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow

class SplashViewModel @Inject constructor(
class MainViewModel @Inject constructor(
private val userAuthenticationState: UserAuthenticationState,
private val launchCatchingDelegate: LaunchCatchingDelegate,
) : ViewModel(), LaunchCatchingDelegate by launchCatchingDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import dagger.Component

@FeatureScope
@Component(dependencies = [TrackingDataComponent::class])
interface HomeTabFeatureComponent {
interface HomeTabComponent {
fun homeTabViewModel(): HomeTabViewModel

@Component.Factory
Expand All @@ -19,6 +19,6 @@ interface HomeTabFeatureComponent {
@BindsInstance application: Application,
trackingDataComponent: TrackingDataComponent =
DaggerTrackingDataComponent.factory().create(application),
): HomeTabFeatureComponent
): HomeTabComponent
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package akio.apps.myrun.feature.splash.di
package akio.apps.myrun.feature.main.di

import akio.apps.myrun.base.di.FeatureScope
import akio.apps.myrun.data.authentication.di.AuthenticationDataComponent
import akio.apps.myrun.data.authentication.di.DaggerAuthenticationDataComponent
import akio.apps.myrun.feature.core.launchcatching.LaunchCatchingModule
import akio.apps.myrun.feature.splash.SplashViewModel
import akio.apps.myrun.feature.main.MainViewModel
import android.app.Application
import dagger.BindsInstance
import dagger.Component
Expand All @@ -16,15 +16,15 @@ import dagger.Component
],
dependencies = [AuthenticationDataComponent::class]
)
interface SplashFeatureComponent {
fun splashViewModel(): SplashViewModel
interface MainActivityComponent {
fun mainViewModel(): MainViewModel

@Component.Factory
interface Factory {
fun create(
@BindsInstance application: Application,
authenticationDataComponent: AuthenticationDataComponent =
DaggerAuthenticationDataComponent.factory().create(application),
): SplashFeatureComponent
): MainActivityComponent
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import akio.apps.myrun.feature.core.ui.AppTheme
import akio.apps.myrun.feature.core.ui.NavigationBarSpacer
import akio.apps.myrun.feature.feed.ui.ActivityFeedComposable
import akio.apps.myrun.feature.main.HomeTabViewModel
import akio.apps.myrun.feature.main.di.DaggerHomeTabFeatureComponent
import akio.apps.myrun.feature.main.di.DaggerHomeTabComponent
import akio.apps.myrun.feature.userstats.ui.CurrentUserStatsComposable
import android.app.Application
import androidx.annotation.StringRes
Expand Down Expand Up @@ -147,7 +147,7 @@ fun HomeTabComposable(
private fun rememberViewModel(): HomeTabViewModel {
val application = LocalContext.current.applicationContext as Application
return remember {
DaggerHomeTabFeatureComponent.factory().create(application).homeTabViewModel()
DaggerHomeTabComponent.factory().create(application).homeTabViewModel()
}
}

Expand Down
90 changes: 0 additions & 90 deletions app/src/main/java/akio/apps/myrun/feature/splash/SplashActivity.kt

This file was deleted.

2 changes: 1 addition & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
<style name="AppTheme.SplashScreen" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/colorPrimary</item>
<item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher_round</item>
<item name="postSplashScreenTheme">@style/AppTheme</item>
<item name="postSplashScreenTheme">@style/AppTheme.SemiTransparentSystemBars</item>
</style>
</resources>
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package akio.apps.myrun.feature.core.navigation

import akio.apps.myrun.feature.core.navigation.OnBoardingNavigation.SIGNIN_ACTIVITY_CLASS_NAME
import akio.apps.myrun.feature.core.navigation.OnBoardingNavigation.SPLASH_ACTIVITY_CLASS_NAME
import akio.apps.myrun.feature.registration.SignInActivity
import akio.apps.myrun.feature.splash.SplashActivity
import kotlin.test.assertEquals
import org.junit.Test

Expand All @@ -15,12 +13,4 @@ class OnBoardingNavigationTest {
SignInActivity::class.java.name
)
}

@Test
fun testSplashActivityClassNameChangedCorrectly() {
assertEquals(
SPLASH_ACTIVITY_CLASS_NAME,
SplashActivity::class.java.name
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package akio.apps.myrun.feature.splash

import akio.apps.myrun.data.authentication.api.UserAuthenticationState
import akio.apps.myrun.feature.core.launchcatching.LaunchCatchingDelegate
import akio.apps.myrun.feature.main.MainViewModel
import app.cash.turbine.test
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
Expand All @@ -12,9 +13,9 @@ import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever

@OptIn(ExperimentalCoroutinesApi::class)
class SplashViewModelTest {
class MainViewModelTest {

private lateinit var splashViewModel: SplashViewModel
private lateinit var mainViewModel: MainViewModel
private lateinit var mockedUserAuthState: UserAuthenticationState
private lateinit var mockLaunchCatchingDelegate: LaunchCatchingDelegate

Expand All @@ -30,8 +31,8 @@ class SplashViewModelTest {
whenever(mockedUserAuthState.isSignedIn())
.then { throw userAuthStateError }

splashViewModel = SplashViewModel(mockedUserAuthState, mockLaunchCatchingDelegate)
splashViewModel.isUserSignedIn.test {
mainViewModel = MainViewModel(mockedUserAuthState, mockLaunchCatchingDelegate)
mainViewModel.isUserSignedIn.test {
awaitComplete()
verify(mockLaunchCatchingDelegate).setLaunchCatchingError(userAuthStateError)
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ workmanager = "2.8.0"
paging = "3.1.1"
room = "2.5.0"
lifecycle = "2.5.1"
splashscreen = "1.0.0"
splashscreen = "1.0.1"
androidx-annotation = "1.6.0"
multidex = "2.0.1"
androidx-coretesting = "2.2.0"
Expand Down

0 comments on commit a5f00b7

Please sign in to comment.