Skip to content

Commit

Permalink
Add connection of splash screen and onboarding screen
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzsout committed Nov 7, 2024
1 parent fa21098 commit 654dacb
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ fun LoginScreenRoute(

val loginState by viewModel.loginState.collectAsStateWithLifecycle()

BackHandler(enabled = true) {
navigateBack.invoke()
}

Scaffold(
modifier = modifier.fillMaxSize()
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package com.oguzdogdu.walliescompose.features.splash

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
Expand All @@ -25,14 +19,13 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LifecycleEventEffect
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.oguzdogdu.walliescompose.R
import kotlinx.coroutines.delay

@Composable
fun SplashScreenRoute(
modifier: Modifier = Modifier,
viewModel: SplashViewModel = hiltViewModel(),
goToLoginFlow: () -> Unit,
goToContentScreen: () -> Unit
goToContentScreen: () -> Unit,
goToOnboarding: () -> Unit
) {
val state by viewModel.splashState.collectAsStateWithLifecycle()

Expand All @@ -42,22 +35,18 @@ fun SplashScreenRoute(

LaunchedEffect(state) {
when(state) {
SplashScreenState.StartFlow -> {
}
SplashScreenState.UserNotSigned -> {
goToLoginFlow.invoke()
}
SplashScreenState.UserSignedIn -> {
goToContentScreen.invoke()
}
SplashScreenState.StartFlow -> {}
SplashScreenState.UserNotSigned -> goToLoginFlow.invoke()
SplashScreenState.UserSignedIn -> goToContentScreen.invoke()
SplashScreenState.GoToOnboarding -> goToOnboarding.invoke()
}
}

SplashScreenContent(modifier = modifier)
SplashScreenContent()
}

@Composable
fun SplashScreenContent(modifier: Modifier) {
fun SplashScreenContent(modifier: Modifier = Modifier) {
Box(modifier = modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
Icon(
painter = painterResource(id = R.drawable.logo), contentDescription = stringResource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ fun NavController.navigateToSplashScreen() = navigate(Screens.SplashScreenRoute)

fun NavGraphBuilder.splashScreen(
goToLoginFlow: () -> Unit,
goToContentScreen: () -> Unit
goToContentScreen: () -> Unit,
goToOnboarding: () -> Unit
) {
composable<Screens.SplashScreenRoute> {
SplashScreenRoute(goToLoginFlow = {
goToLoginFlow.invoke()
}, goToContentScreen = {
goToContentScreen.invoke()
}, goToOnboarding = {
goToOnboarding.invoke()
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ sealed class SplashScreenState {
data object StartFlow : SplashScreenState()
data object UserSignedIn : SplashScreenState()
data object UserNotSigned : SplashScreenState()
data object GoToOnboarding : SplashScreenState()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@ package com.oguzdogdu.walliescompose.features.splash

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.oguzdogdu.walliescompose.data.repository.AppSettingsRepositoryImpl.Companion.ONBOARDING
import com.oguzdogdu.walliescompose.domain.repository.AppSettingsRepository
import com.oguzdogdu.walliescompose.domain.repository.UserAuthenticationRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.single
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class SplashViewModel @Inject constructor(
private val authenticationRepository: UserAuthenticationRepository
private val authenticationRepository: UserAuthenticationRepository,
private val appSettingsRepository: AppSettingsRepository
) :
ViewModel() {

Expand All @@ -23,6 +30,10 @@ class SplashViewModel @Inject constructor(
)
val splashState = _splashState.asStateFlow()

private val visibilityOfOnboarding: StateFlow<Boolean> =
appSettingsRepository.getOnboardingShow(ONBOARDING).map { it }
.stateIn(viewModelScope, started = SharingStarted.Eagerly, false)

fun handleUIEvent(event: SplashScreenEvent) {
when (event) {
is SplashScreenEvent.CheckAuthState -> checkSignIn()
Expand All @@ -33,10 +44,14 @@ class SplashViewModel @Inject constructor(
viewModelScope.launch {
delay(2000)
val authState = authenticationRepository.isUserAuthenticatedInFirebase().single()
if (authState) {
_splashState.update { SplashScreenState.UserSignedIn }
if (!visibilityOfOnboarding.value) {
_splashState.update { SplashScreenState.GoToOnboarding }
} else {
_splashState.update { SplashScreenState.UserNotSigned }
if (authState) {
_splashState.update { SplashScreenState.UserSignedIn }
} else {
_splashState.update { SplashScreenState.UserNotSigned }
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.oguzdogdu.walliescompose.navigation

import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.navigation
import com.oguzdogdu.walliescompose.features.onboarding.onboardingScreen
import com.oguzdogdu.walliescompose.features.splash.splashScreen
import kotlinx.serialization.Serializable

@Serializable
object RootGraph

fun NavGraphBuilder.navigationRootGraph(
navHostController: NavHostController,
) {
navigation<RootGraph>(startDestination = Screens.SplashScreenRoute) {
splashScreen(goToLoginFlow = {
navHostController.navigate(AuthGraph) {
popUpTo(Screens.SplashScreenRoute) { inclusive = true }
}
}, goToContentScreen = {
navHostController.navigate(NavigationBarGraph) {
popUpTo(Screens.SplashScreenRoute) { inclusive = true }
}
}, goToOnboarding = {
navHostController.navigate(Screens.OnboardingScreenNavigationRoute) {
popUpTo(Screens.SplashScreenRoute) { inclusive = true }
}
})

onboardingScreen(goToLoginFlow = {
navHostController.navigate(AuthGraph) {
popUpTo(Screens.OnboardingScreenNavigationRoute) { inclusive = true }
}
}, goToContentScreen = {
navHostController.navigate(NavigationBarGraph) {
popUpTo(Screens.OnboardingScreenNavigationRoute) { inclusive = true }
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import com.oguzdogdu.walliescompose.features.login.googlesignin.GoogleAuthUiClient
import com.oguzdogdu.walliescompose.features.splash.splashScreen

@OptIn(ExperimentalSharedTransitionApi::class)
@Composable
Expand All @@ -17,29 +16,17 @@ fun WalliesNavHost(
SharedTransitionLayout {
NavHost(
navController = navController,
startDestination = Screens.SplashScreenRoute,
startDestination = RootGraph,
) {
splashScreen(goToLoginFlow = {
navController.navigate(AuthGraph) {
popUpTo(Screens.SplashScreenRoute) {
inclusive = true
}
}
}, goToContentScreen = {
navController.navigate(NavigationBarGraph) {
popUpTo(Screens.SplashScreenRoute) {
inclusive = true
}
}
})
navigationBarGraph(
navHostController = navController,
scope = this@SharedTransitionLayout
)
navigationRootGraph(navHostController = navController)
navigationAuthGraph(
navHostController = navController,
googleAuthUiClient = googleAuthUiClient
)
navigationBarGraph(
navHostController = navController,
scope = this@SharedTransitionLayout
)
navigationHomeGraph(
navHostController = navController,
sharedTransitionScope = this@SharedTransitionLayout
Expand Down

0 comments on commit 654dacb

Please sign in to comment.