Skip to content

Commit

Permalink
Collect flows lifecycle aware
Browse files Browse the repository at this point in the history
  • Loading branch information
sunkup committed Apr 18, 2024
1 parent b252c4c commit 9bc712e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ dependencies {
implementation(libs.androidx.appCompat)
implementation(libs.androidx.core)
implementation(libs.androidx.lifecycle.viewmodel)
implementation(libs.androidx.lifecycle.runtime.compose)
implementation(libs.androidx.work.runtime)

// Jetpack Compose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
Expand All @@ -48,6 +47,7 @@ import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import at.bitfire.icsdroid.Constants
import at.bitfire.icsdroid.HttpClient
import at.bitfire.icsdroid.R
Expand Down Expand Up @@ -104,21 +104,21 @@ class AddCalendarActivity : AppCompatActivity() {
val context = LocalContext.current
val pagerState = rememberPagerState { 2 }

val url: String? by subscriptionSettingsModel.url.collectAsState()
val fileName: String? by subscriptionSettingsModel.fileName.collectAsState()
val urlError: String? by subscriptionSettingsModel.urlError.collectAsState()
val supportsAuthentication: Boolean by subscriptionSettingsModel.supportsAuthentication.collectAsState()
val title by subscriptionSettingsModel.title.collectAsState()
val color by subscriptionSettingsModel.color.collectAsState()
val ignoreAlerts by subscriptionSettingsModel.ignoreAlerts.collectAsState()
val defaultAlarmMinutes by subscriptionSettingsModel.defaultAlarmMinutes.collectAsState()
val defaultAllDayAlarmMinutes by subscriptionSettingsModel.defaultAllDayAlarmMinutes.collectAsState()
val ignoreDescription by subscriptionSettingsModel.ignoreDescription.collectAsState()

val requiresAuth: Boolean by credentialsModel.requiresAuth.collectAsState()
val username: String? by credentialsModel.username.collectAsState()
val password: String? by credentialsModel.password.collectAsState()
val isInsecure: Boolean by credentialsModel.isInsecure.collectAsState()
val url: String? by subscriptionSettingsModel.url.collectAsStateWithLifecycle()
val fileName: String? by subscriptionSettingsModel.fileName.collectAsStateWithLifecycle()
val urlError: String? by subscriptionSettingsModel.urlError.collectAsStateWithLifecycle()
val supportsAuthentication: Boolean by subscriptionSettingsModel.supportsAuthentication.collectAsStateWithLifecycle()
val title by subscriptionSettingsModel.title.collectAsStateWithLifecycle()
val color by subscriptionSettingsModel.color.collectAsStateWithLifecycle()
val ignoreAlerts by subscriptionSettingsModel.ignoreAlerts.collectAsStateWithLifecycle()
val defaultAlarmMinutes by subscriptionSettingsModel.defaultAlarmMinutes.collectAsStateWithLifecycle()
val defaultAllDayAlarmMinutes by subscriptionSettingsModel.defaultAllDayAlarmMinutes.collectAsStateWithLifecycle()
val ignoreDescription by subscriptionSettingsModel.ignoreDescription.collectAsStateWithLifecycle()

val requiresAuth: Boolean by credentialsModel.requiresAuth.collectAsStateWithLifecycle()
val username: String? by credentialsModel.username.collectAsStateWithLifecycle()
val password: String? by credentialsModel.password.collectAsStateWithLifecycle()
val isInsecure: Boolean by credentialsModel.isInsecure.collectAsStateWithLifecycle()

val isVerifyingUrl: Boolean by validationModel.isVerifyingUrl
val validationResult: ResourceInfo? by validationModel.result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -37,6 +36,7 @@ import androidx.compose.ui.unit.dp
import androidx.core.app.ShareCompat
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import at.bitfire.icsdroid.R
import at.bitfire.icsdroid.db.dao.SubscriptionsDao
import at.bitfire.icsdroid.db.entity.Credential
Expand Down Expand Up @@ -210,15 +210,15 @@ class EditCalendarActivity: AppCompatActivity() {

@Composable
private fun EditCalendarComposable() {
val url by subscriptionSettingsModel.url.collectAsState()
val title by subscriptionSettingsModel.title.collectAsState()
val color by subscriptionSettingsModel.color.collectAsState()
val ignoreAlerts by subscriptionSettingsModel.ignoreAlerts.collectAsState()
val defaultAlarmMinutes by subscriptionSettingsModel.defaultAlarmMinutes.collectAsState()
val defaultAllDayAlarmMinutes by subscriptionSettingsModel.defaultAllDayAlarmMinutes.collectAsState()
val ignoreDescription by subscriptionSettingsModel.ignoreDescription.collectAsState()
val inputValid by inputValid.collectAsState(false)
val modelsDirty by modelsDirty.collectAsState(false)
val url by subscriptionSettingsModel.url.collectAsStateWithLifecycle()
val title by subscriptionSettingsModel.title.collectAsStateWithLifecycle()
val color by subscriptionSettingsModel.color.collectAsStateWithLifecycle()
val ignoreAlerts by subscriptionSettingsModel.ignoreAlerts.collectAsStateWithLifecycle()
val defaultAlarmMinutes by subscriptionSettingsModel.defaultAlarmMinutes.collectAsStateWithLifecycle()
val defaultAllDayAlarmMinutes by subscriptionSettingsModel.defaultAllDayAlarmMinutes.collectAsStateWithLifecycle()
val ignoreDescription by subscriptionSettingsModel.ignoreDescription.collectAsStateWithLifecycle()
val inputValid by inputValid.collectAsStateWithLifecycle(false)
val modelsDirty by modelsDirty.collectAsStateWithLifecycle(false)
Scaffold(
topBar = { AppBarComposable(inputValid, modelsDirty) }
) { paddingValues ->
Expand Down Expand Up @@ -251,10 +251,10 @@ class EditCalendarActivity: AppCompatActivity() {
isCreating = false,
modifier = Modifier.fillMaxWidth()
)
val supportsAuthentication by subscriptionSettingsModel.supportsAuthentication.collectAsState(false)
val requiresAuth: Boolean by credentialsModel.requiresAuth.collectAsState(false)
val username: String? by credentialsModel.username.collectAsState(null)
val password: String? by credentialsModel.password.collectAsState(null)
val supportsAuthentication by subscriptionSettingsModel.supportsAuthentication.collectAsStateWithLifecycle()
val requiresAuth: Boolean by credentialsModel.requiresAuth.collectAsStateWithLifecycle()
val username: String? by credentialsModel.username.collectAsStateWithLifecycle()
val password: String? by credentialsModel.password.collectAsStateWithLifecycle()
AnimatedVisibility(visible = supportsAuthentication) {
LoginCredentialsComposable(
requiresAuth,
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ androidx-appCompat = { module = "androidx.appcompat:appcompat", version.ref = "a
androidx-arch-core-testing = { module = "androidx.arch.core:core-testing", version.ref = "androidx-archCore" }
androidx-core = { module = "androidx.core:core-ktx", version.ref = "androidx-core" }
androidx-lifecycle-viewmodel = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "androidx-lifecycle" }
androidx-lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "androidx-lifecycle" }
androidx-test-rules = { module = "androidx.test:rules", version.ref = "androidx-test-rules" }
androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidx-test-runner" }
androidx-work-runtime = { module = "androidx.work:work-runtime-ktx", version.ref = "androidx-work" }
Expand Down

0 comments on commit 9bc712e

Please sign in to comment.