Skip to content

Commit 8a4b143

Browse files
ComicSASShiftHackZ
andauthored
Splash screen update (#282)
* Updated splash screen logic & animation * Resolved PR comments * Update unit tests --------- Co-authored-by: ShiftHackZ <[email protected]>
1 parent f29557e commit 8a4b143

File tree

6 files changed

+44
-9
lines changed

6 files changed

+44
-9
lines changed

presentation/src/main/java/com/shifthackz/aisdv1/presentation/activity/AiStableDiffusionActivity.kt

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package com.shifthackz.aisdv1.presentation.activity
22

3+
import android.animation.ObjectAnimator
34
import android.os.Bundle
5+
import android.view.View
46
import androidx.activity.compose.BackHandler
57
import androidx.activity.compose.setContent
68
import androidx.activity.result.contract.ActivityResultContracts
79
import androidx.appcompat.app.AppCompatActivity
810
import androidx.compose.material3.DrawerValue
911
import androidx.compose.material3.rememberDrawerState
12+
import androidx.compose.runtime.LaunchedEffect
1013
import androidx.compose.runtime.getValue
1114
import androidx.compose.runtime.mutableStateOf
1215
import androidx.compose.runtime.remember
1316
import androidx.compose.runtime.rememberCoroutineScope
1417
import androidx.compose.runtime.setValue
18+
import androidx.core.animation.doOnEnd
1519
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
1620
import androidx.navigation.compose.NavHost
1721
import androidx.navigation.compose.currentBackStackEntryAsState
@@ -48,11 +52,25 @@ class AiStableDiffusionActivity : AppCompatActivity() {
4852
}
4953

5054
override fun onCreate(savedInstanceState: Bundle?) {
51-
installSplashScreen()
55+
val splashScreen = installSplashScreen()
5256
super.onCreate(savedInstanceState)
5357
actionBar?.hide()
54-
PermissionUtil.checkNotificationPermission(this, notificationPermission::launch)
55-
PermissionUtil.checkStoragePermission(this, storagePermission::launch)
58+
splashScreen.setKeepOnScreenCondition { viewModel.state.value.isShowSplash }
59+
splashScreen.setOnExitAnimationListener { splashScreenViewProvider ->
60+
val fadeOutAnimation = ObjectAnimator.ofFloat(
61+
splashScreenViewProvider.view,
62+
View.ALPHA,
63+
1f,
64+
0f
65+
)
66+
fadeOutAnimation.duration = 500L
67+
fadeOutAnimation.doOnEnd {
68+
PermissionUtil.checkNotificationPermission(this, notificationPermission::launch)
69+
PermissionUtil.checkStoragePermission(this, storagePermission::launch)
70+
splashScreenViewProvider.remove()
71+
}
72+
fadeOutAnimation.start()
73+
}
5674
setContent {
5775
val navController = rememberNavController()
5876
val backStackEntry by navController.currentBackStackEntryAsState()
@@ -65,6 +83,13 @@ class AiStableDiffusionActivity : AppCompatActivity() {
6583
scope.launch { drawerState.close() }
6684
}
6785

86+
LaunchedEffect(backStackEntry) {
87+
if (!viewModel.state.value.isShowSplash) return@LaunchedEffect
88+
if (backStackEntry?.destination?.route != Constants.ROUTE_SPLASH) {
89+
viewModel.processIntent(AppIntent.HideSplash)
90+
}
91+
}
92+
6893
AiSdAppTheme {
6994
MviComponent(
7095
viewModel = viewModel,

presentation/src/main/java/com/shifthackz/aisdv1/presentation/activity/AiStableDiffusionViewModel.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,9 @@ class AiStableDiffusionViewModel(
5858
is AppIntent.HomeRoute -> {
5959
homeRouter.navigateToRoute(intent.route)
6060
}
61+
62+
AppIntent.HideSplash -> updateState { state ->
63+
state.copy(isShowSplash = false)
64+
}
6165
}
6266
}

presentation/src/main/java/com/shifthackz/aisdv1/presentation/activity/AppIntent.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.shifthackz.android.core.mvi.MviIntent
55
sealed interface AppIntent : MviIntent {
66

77
data object GrantStoragePermission : AppIntent
8+
data object HideSplash : AppIntent
89

910
data class HomeRoute(val route: String) : AppIntent
1011
}

presentation/src/main/java/com/shifthackz/aisdv1/presentation/activity/AppState.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ import com.shifthackz.android.core.mvi.MviState
88
@Immutable
99
data class AppState(
1010
val drawerItems: List<NavItem> = mainDrawerNavItems(),
11+
val isShowSplash: Boolean = true,
1112
) : MviState

presentation/src/main/java/com/shifthackz/aisdv1/presentation/navigation/router/main/MainRouterImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ internal class MainRouterImpl : MainRouter {
4747
override fun navigateToServerSetup(source: LaunchSource) {
4848
effectSubject.onNext(NavigationEffect.Navigate.RouteBuilder("${Constants.ROUTE_SERVER_SETUP}/${source.ordinal}") {
4949
if (source == LaunchSource.SPLASH) {
50-
popUpTo(Constants.ROUTE_ONBOARDING) {
50+
popUpTo(Constants.ROUTE_SPLASH) {
5151
inclusive = true
5252
}
5353
}

presentation/src/test/java/com/shifthackz/aisdv1/presentation/activity/AiStableDiffusionViewModelTest.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ import io.mockk.mockk
1919
import io.mockk.verify
2020
import io.reactivex.rxjava3.core.Flowable
2121
import io.reactivex.rxjava3.subjects.BehaviorSubject
22-
import kotlinx.coroutines.Dispatchers
2322
import kotlinx.coroutines.ExperimentalCoroutinesApi
2423
import kotlinx.coroutines.flow.firstOrNull
25-
import kotlinx.coroutines.test.StandardTestDispatcher
2624
import kotlinx.coroutines.test.runTest
27-
import kotlinx.coroutines.test.setMain
2825
import org.junit.Assert
2926
import org.junit.Before
3027
import org.junit.Test
@@ -76,7 +73,7 @@ class AiStableDiffusionViewModelTest : CoreViewModelTest<AiStableDiffusionViewMo
7673
}
7774

7875
@Test
79-
fun `given onStoragePermissionsGranted was called, expected VM sets field saveToMediaStore with true in preference manager`() {
76+
fun `given received GrantStoragePermission intent, expected VM sets field saveToMediaStore with true in preference manager`() {
8077
every {
8178
stubPreferenceManager::saveToMediaStore.set(any())
8279
} returns Unit
@@ -88,6 +85,14 @@ class AiStableDiffusionViewModelTest : CoreViewModelTest<AiStableDiffusionViewMo
8885
}
8986
}
9087

88+
@Test
89+
fun `given received HideSplash intent, expected VM sets isShowSplash to false in UI state`() {
90+
with(viewModel) {
91+
processIntent(AppIntent.HideSplash)
92+
Assert.assertFalse(state.value.isShowSplash)
93+
}
94+
}
95+
9196
@Test
9297
fun `given route event from main router, expected domain model delivered to effect collector`() {
9398
stubNavigationEffect.onNext(NavigationEffect.Navigate.Route("route"))
@@ -132,7 +137,6 @@ class AiStableDiffusionViewModelTest : CoreViewModelTest<AiStableDiffusionViewMo
132137

133138
@Test
134139
fun `given route then back events from main router, expected two domain models delivered to effect collector in same order`() {
135-
Dispatchers.setMain(StandardTestDispatcher())
136140
runTest {
137141
viewModel.effect.test {
138142
stubNavigationEffect.onNext(NavigationEffect.Navigate.Route("route2"))

0 commit comments

Comments
 (0)