-
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.
Merge pull request #7 from stslex/dev
Dev
- Loading branch information
Showing
40 changed files
with
1,054 additions
and
300 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/stslex/aproselection/SelectApplication.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
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/stslex/aproselection/controller/AuthController.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,11 @@ | ||
package com.stslex.aproselection.controller | ||
|
||
import kotlinx.coroutines.flow.StateFlow | ||
|
||
interface AuthController { | ||
|
||
val isAuth: StateFlow<Boolean?> | ||
|
||
fun init() | ||
} | ||
|
46 changes: 46 additions & 0 deletions
46
app/src/main/java/com/stslex/aproselection/controller/AuthControllerImpl.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,46 @@ | ||
package com.stslex.aproselection.controller | ||
|
||
import com.stslex.aproselection.core.datastore.AppDataStore | ||
import com.stslex.aproselection.core.network.client.NetworkClient | ||
import com.stslex.aproselection.core.ui.core.Logger | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.SupervisorJob | ||
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 AuthControllerImpl( | ||
private val dataStore: AppDataStore, | ||
private val networkClient: NetworkClient, | ||
) : AuthController { | ||
|
||
private val scope = CoroutineScope(SupervisorJob()) | ||
private val _isAuth = MutableStateFlow<Boolean?>(null) | ||
override val isAuth: StateFlow<Boolean?> | ||
get() = _isAuth.asStateFlow() | ||
|
||
override fun init() { | ||
dataStore.token | ||
.catch { error -> | ||
Logger.exception(error) | ||
} | ||
.onEach { token -> | ||
if (token.isBlank()) { | ||
networkClient.regenerateToken() | ||
} | ||
} | ||
.launchIn(scope) | ||
|
||
dataStore.uuid | ||
.catch { error -> | ||
Logger.exception(error) | ||
} | ||
.onEach { uuid -> | ||
_isAuth.emit(uuid.isNotBlank()) | ||
} | ||
.launchIn(scope) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/stslex/aproselection/di/AppModule.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,9 +1,23 @@ | ||
package com.stslex.aproselection.di | ||
|
||
import androidx.navigation.NavHostController | ||
import com.stslex.aproselection.controller.AuthController | ||
import com.stslex.aproselection.controller.AuthControllerImpl | ||
import com.stslex.aproselection.core.ui.navigation.navigator.Navigator | ||
import com.stslex.aproselection.core.ui.navigation.navigator.NavigatorImpl | ||
import com.stslex.aproselection.ui.InitialAppViewModel | ||
import org.koin.androidx.viewmodel.dsl.viewModelOf | ||
import org.koin.core.module.dsl.bind | ||
import org.koin.core.module.dsl.singleOf | ||
import org.koin.dsl.module | ||
|
||
val appModule = module { | ||
singleOf(::AuthControllerImpl) { bind<AuthController>() } | ||
} | ||
|
||
fun navigationModule( | ||
navHostController: NavHostController | ||
) = module { | ||
single<Navigator> { NavigatorImpl(navHostController) } | ||
viewModelOf(::InitialAppViewModel) | ||
} |
62 changes: 4 additions & 58 deletions
62
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,78 +1,24 @@ | ||
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.core.ui.navigation.destination.AppDestination | ||
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, | ||
startDestination: AppDestination, | ||
modifier: Modifier = Modifier, | ||
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) | ||
authRouter(modifier) | ||
homeRouter(modifier) | ||
} | ||
} | ||
|
||
fun NavHostController.navigateScreen(screen: NavigationScreen) { | ||
val currentRoute = this.currentDestination?.route | ||
if (currentRoute == screen.screenRoute) return | ||
navigate(screen.screenRoute) { | ||
if (screen.isSingleTop.not()) return@navigate | ||
currentRoute?.let { route -> | ||
popUpTo(route) { | ||
inclusive = true | ||
saveState = true | ||
} | ||
} | ||
|
||
launchSingleTop = true | ||
} | ||
} |
43 changes: 25 additions & 18 deletions
43
app/src/main/java/com/stslex/aproselection/ui/InitialApp.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,27 +1,34 @@ | ||
package com.stslex.aproselection.ui | ||
|
||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.navigation.compose.rememberNavController | ||
import com.stslex.aproselection.core.ui.theme.AppTheme | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.remember | ||
import androidx.navigation.NavHostController | ||
import com.stslex.aproselection.core.ui.navigation.destination.AppDestination | ||
import com.stslex.aproselection.navigation.NavigationHost | ||
import org.koin.androidx.compose.koinViewModel | ||
|
||
@Composable | ||
fun InitialApp() { | ||
val navController = rememberNavController() | ||
fun InitialApp( | ||
navController: NavHostController | ||
) { | ||
val viewModel: InitialAppViewModel = koinViewModel() | ||
val isInitialAuth by remember { | ||
viewModel.isInitialAuth | ||
}.collectAsState() | ||
|
||
Box { | ||
NavigationHost( | ||
navController = navController, | ||
) | ||
LaunchedEffect(Unit) { | ||
viewModel.init() | ||
} | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Composable | ||
fun InitialAppPreview() { | ||
AppTheme { | ||
InitialApp() | ||
} | ||
} | ||
AppDestination | ||
.getStartDestination(isInitialAuth) | ||
?.let { destination -> | ||
NavigationHost( | ||
navController = navController, | ||
startDestination = destination | ||
) | ||
} | ||
} |
45 changes: 15 additions & 30 deletions
45
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
32 changes: 20 additions & 12 deletions
32
app/src/main/java/com/stslex/aproselection/ui/MainActivity.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,30 +1,38 @@ | ||
package com.stslex.aproselection.ui | ||
|
||
import android.os.Bundle | ||
import android.util.Log | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import androidx.lifecycle.lifecycleScope | ||
import com.stslex.aproselection.core.datastore.AppDataStore | ||
import com.stslex.aproselection.core.network.client.NetworkClient | ||
import androidx.compose.runtime.Composable | ||
import androidx.navigation.NavHostController | ||
import androidx.navigation.compose.rememberNavController | ||
import com.stslex.aproselection.core.ui.theme.AppTheme | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import org.koin.android.ext.android.inject | ||
import com.stslex.aproselection.di.navigationModule | ||
import org.koin.androidx.compose.getKoin | ||
|
||
class MainActivity : ComponentActivity() { | ||
|
||
private val dataStore by inject<AppDataStore>() | ||
private val networkClient by inject<NetworkClient>() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
setContent { | ||
val navHostController = rememberNavController() | ||
SetupComposeDependencies(navHostController) | ||
AppTheme { | ||
InitialApp() | ||
InitialApp( | ||
navController = navHostController | ||
) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun SetupComposeDependencies( | ||
navController: NavHostController | ||
) { | ||
val navModule = navigationModule(navController) | ||
getKoin().loadModules( | ||
listOf(navModule) | ||
) | ||
} | ||
} |
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.