Skip to content

Instantly share code, notes, and snippets.

@onliniak
Last active September 15, 2021 18:53
Show Gist options
  • Save onliniak/9c55b4e4e2656960799115f5c7c9ef83 to your computer and use it in GitHub Desktop.
Save onliniak/9c55b4e4e2656960799115f5c7c9ef83 to your computer and use it in GitHub Desktop.
Jetpack Compose + Router + Flow
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val context: Context = this
val isWordPressKnew = runBlocking { context.settingsDataStore.data.first().websiteUrl }
setContent {
if(isWordPressKnew.isNullOrEmpty()){
Router(start = "profile")
Butt("Android", this, rememberCoroutineScope())
}else{
Router(start = "friendslist")
}
WordPressTheme {
// A surface container using the 'background' color from the theme
Surface(color = MaterialTheme.colors.background) {
}
}
}
}
}
@Composable
fun Greeting(name: String) {
Text(text = "Hello $name!")
}
@Composable
fun Butt(name: String, context: Context, lifecycleScope: CoroutineScope) {
Button(onClick = {
lifecycleScope.launch { incrementCounter(name, context) }
}) {
Text(text = "kliknij")
}
}
suspend fun incrementCounter(txt: String, context: Context) {
Log.i("TAG", txt)
context.settingsDataStore.updateData { currentSettings ->
currentSettings.toBuilder()
.setWebsiteUrl(txt)
.build()
}
Log.i("TAG", runBlocking { context.settingsDataStore.data.first().websiteUrl })
}
@Composable
fun Router(start: String){
NavHost(navController = rememberNavController(), start) {
composable("profile") { Greeting(name = "") }
composable("friendslist") { Greeting(name = "2") }
/*...*/
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment