Skip to content

Commit

Permalink
Splash screen update (#282)
Browse files Browse the repository at this point in the history
* Updated splash screen logic & animation

* Resolved PR comments

* Update unit tests

---------

Co-authored-by: ShiftHackZ <[email protected]>
  • Loading branch information
ComicSAS and ShiftHackZ authored Sep 1, 2024
1 parent f29557e commit 8a4b143
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.shifthackz.aisdv1.presentation.activity

import android.animation.ObjectAnimator
import android.os.Bundle
import android.view.View
import androidx.activity.compose.BackHandler
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.core.animation.doOnEnd
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.currentBackStackEntryAsState
Expand Down Expand Up @@ -48,11 +52,25 @@ class AiStableDiffusionActivity : AppCompatActivity() {
}

override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)
actionBar?.hide()
PermissionUtil.checkNotificationPermission(this, notificationPermission::launch)
PermissionUtil.checkStoragePermission(this, storagePermission::launch)
splashScreen.setKeepOnScreenCondition { viewModel.state.value.isShowSplash }
splashScreen.setOnExitAnimationListener { splashScreenViewProvider ->
val fadeOutAnimation = ObjectAnimator.ofFloat(
splashScreenViewProvider.view,
View.ALPHA,
1f,
0f
)
fadeOutAnimation.duration = 500L
fadeOutAnimation.doOnEnd {
PermissionUtil.checkNotificationPermission(this, notificationPermission::launch)
PermissionUtil.checkStoragePermission(this, storagePermission::launch)
splashScreenViewProvider.remove()
}
fadeOutAnimation.start()
}
setContent {
val navController = rememberNavController()
val backStackEntry by navController.currentBackStackEntryAsState()
Expand All @@ -65,6 +83,13 @@ class AiStableDiffusionActivity : AppCompatActivity() {
scope.launch { drawerState.close() }
}

LaunchedEffect(backStackEntry) {
if (!viewModel.state.value.isShowSplash) return@LaunchedEffect
if (backStackEntry?.destination?.route != Constants.ROUTE_SPLASH) {
viewModel.processIntent(AppIntent.HideSplash)
}
}

AiSdAppTheme {
MviComponent(
viewModel = viewModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,9 @@ class AiStableDiffusionViewModel(
is AppIntent.HomeRoute -> {
homeRouter.navigateToRoute(intent.route)
}

AppIntent.HideSplash -> updateState { state ->
state.copy(isShowSplash = false)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.shifthackz.android.core.mvi.MviIntent
sealed interface AppIntent : MviIntent {

data object GrantStoragePermission : AppIntent
data object HideSplash : AppIntent

data class HomeRoute(val route: String) : AppIntent
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ import com.shifthackz.android.core.mvi.MviState
@Immutable
data class AppState(
val drawerItems: List<NavItem> = mainDrawerNavItems(),
val isShowSplash: Boolean = true,
) : MviState
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal class MainRouterImpl : MainRouter {
override fun navigateToServerSetup(source: LaunchSource) {
effectSubject.onNext(NavigationEffect.Navigate.RouteBuilder("${Constants.ROUTE_SERVER_SETUP}/${source.ordinal}") {
if (source == LaunchSource.SPLASH) {
popUpTo(Constants.ROUTE_ONBOARDING) {
popUpTo(Constants.ROUTE_SPLASH) {
inclusive = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ import io.mockk.mockk
import io.mockk.verify
import io.reactivex.rxjava3.core.Flowable
import io.reactivex.rxjava3.subjects.BehaviorSubject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.Assert
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -76,7 +73,7 @@ class AiStableDiffusionViewModelTest : CoreViewModelTest<AiStableDiffusionViewMo
}

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

@Test
fun `given received HideSplash intent, expected VM sets isShowSplash to false in UI state`() {
with(viewModel) {
processIntent(AppIntent.HideSplash)
Assert.assertFalse(state.value.isShowSplash)
}
}

@Test
fun `given route event from main router, expected domain model delivered to effect collector`() {
stubNavigationEffect.onNext(NavigationEffect.Navigate.Route("route"))
Expand Down Expand Up @@ -132,7 +137,6 @@ class AiStableDiffusionViewModelTest : CoreViewModelTest<AiStableDiffusionViewMo

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

0 comments on commit 8a4b143

Please sign in to comment.