Skip to content

Instantly share code, notes, and snippets.

@motorro
Last active September 22, 2022 07:39
Show Gist options
  • Save motorro/4d84252e86991b8d7e9973cdffedb60c to your computer and use it in GitHub Desktop.
Save motorro/4d84252e86991b8d7e9973cdffedb60c to your computer and use it in GitHub Desktop.
Main activity setup
@AndroidEntryPoint
class MainActivity : BaseActivity() {
// The model to run authentication and content visibility
private val model: MainScreenModel by viewModels
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MainContent(model) {
LaunchedEffect(key1 = Unit) {
finish()
}
}
}
}
@Composable
private fun MainContent(model: MainScreenModel, onTerminate: @Composable () -> Unit) {
// Create the nav-controller here (see below)
val navController = rememberNavController(bottomSheetNavigator)
// Observer the content-wrapping state
val viewState by model.state.observeAsState(MainScreenUiState.Loading())
val onGesture: (MainScreenGesture) -> Unit = model::update
val onBack = { onGesture(MainScreenGesture.Back) }
@Composable
fun MainScreenView(viewState: MainScreenUiState) = when(viewState) {
is MainScreenUiState.Loading -> {
SplashScreen(viewState.message, onBack)
}
MainScreenUiState.Content -> {
// Content state ruled by navigation graph
NavigationGraph(navController = navController)
}
is MainScreenUiState.Error -> ErrorScreen(
error = viewState.error,
onBack = onBack,
onAction = { onGesture(MainScreenGesture.DismissError) }
)
is MainScreenUiState.Login -> CodeLoginScreen(
viewState = viewState.loginState,
onGesture = { onGesture(MainScreenGesture.Login(it)) },
)
MainScreenUiState.Terminated -> onTerminate()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment