-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
479 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.stslex.aproselection.di | ||
|
||
import com.stslex.aproselection.ui.InitialAppViewModel | ||
import org.koin.androidx.viewmodel.dsl.viewModelOf | ||
import org.koin.dsl.module | ||
|
||
val appModule = module { | ||
viewModelOf(::InitialAppViewModel) | ||
} |
39 changes: 36 additions & 3 deletions
39
app/src/main/java/com/stslex/aproselection/navigation/NavigationHost.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,78 @@ | ||
package com.stslex.aproselection.navigation | ||
|
||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.navigation.NavHostController | ||
import androidx.navigation.compose.NavHost | ||
import androidx.navigation.compose.composable | ||
import com.stslex.aproselection.core.ui.navigation.AppDestination | ||
import com.stslex.aproselection.core.ui.navigation.NavigationScreen | ||
import com.stslex.aproselection.feature.auth.ui.navigation.authRouter | ||
import com.stslex.aproselection.feature.home.ui.navigation.homeRouter | ||
import com.stslex.aproselection.ui.InitialAppViewModel | ||
import org.koin.androidx.compose.koinViewModel | ||
import org.koin.core.parameter.parametersOf | ||
|
||
@Composable | ||
fun NavigationHost( | ||
navController: NavHostController, | ||
modifier: Modifier = Modifier, | ||
startDestination: AppDestination = AppDestination.AUTH | ||
startDestination: AppDestination = AppDestination.SPLASH | ||
) { | ||
val navigator: (NavigationScreen) -> Unit = { screen -> | ||
when (screen) { | ||
is NavigationScreen.PopBackStack -> navController.popBackStack() | ||
else -> navController.navigateScreen(screen) | ||
} | ||
} | ||
|
||
val viewModel = koinViewModel<InitialAppViewModel>( | ||
parameters = { parametersOf(navigator) } | ||
) | ||
val isAuth by remember { | ||
viewModel.isAuth | ||
}.collectAsState() | ||
|
||
NavHost( | ||
navController = navController, | ||
startDestination = startDestination.route | ||
) { | ||
composable(NavigationScreen.Splash.screenRoute) { | ||
Box( | ||
contentAlignment = Alignment.Center | ||
) { | ||
Text(text = "splash") // TODO replace with norm navigator | ||
} | ||
val screen = when (isAuth) { | ||
true -> NavigationScreen.Home | ||
false -> NavigationScreen.Auth | ||
null -> null | ||
} | ||
screen?.let(navigator) | ||
} | ||
authRouter(modifier, navigator) | ||
homeRouter(modifier, navigator) | ||
} | ||
} | ||
|
||
fun NavHostController.navigateScreen(screen: NavigationScreen) { | ||
val currentRoute = this.currentDestination?.route | ||
if (currentRoute == screen.screenRoute) return | ||
navigate(screen.screenRoute) { | ||
if (screen.isSingleTop.not()) return@navigate | ||
graph.startDestinationRoute?.let { route -> | ||
currentRoute?.let { route -> | ||
popUpTo(route) { | ||
inclusive = true | ||
saveState = true | ||
} | ||
} | ||
|
||
launchSingleTop = true | ||
restoreState = false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
app/src/main/java/com/stslex/aproselection/ui/InitialAppViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.stslex.aproselection.ui | ||
|
||
import androidx.lifecycle.viewModelScope | ||
import com.stslex.aproselection.core.datastore.AppDataStore | ||
import com.stslex.aproselection.core.network.client.NetworkClient | ||
import com.stslex.aproselection.core.ui.base.BaseViewModel | ||
import com.stslex.aproselection.core.ui.navigation.NavigationScreen | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
|
||
class InitialAppViewModel( | ||
dataStore: AppDataStore, | ||
navigate: (NavigationScreen) -> Unit, | ||
networkClient: NetworkClient, | ||
) : BaseViewModel() { | ||
|
||
private val _isAuth = MutableStateFlow<Boolean?>(null) | ||
val isAuth: StateFlow<Boolean?> | ||
get() = _isAuth.asStateFlow() | ||
|
||
init { | ||
dataStore.token | ||
.catch { error -> | ||
handleError(error) | ||
} | ||
.onEach { token -> | ||
if (token.isBlank()) { | ||
networkClient.regenerateToken() | ||
} | ||
} | ||
.launchIn(viewModelScope) | ||
|
||
dataStore.uuid | ||
.catch { error -> | ||
handleError(error) | ||
} | ||
.onEach { uuid -> | ||
_isAuth.emit(uuid.isNotBlank()) | ||
if (uuid.isBlank()) { | ||
navigate(NavigationScreen.Auth) | ||
} | ||
} | ||
.launchIn(viewModelScope) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 12 additions & 7 deletions
19
core/ui/src/main/java/com/stslex/aproselection/core/ui/navigation/NavigationScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.