Skip to content

Commit

Permalink
Refresh log on resume to display newly created, deleted or modified e…
Browse files Browse the repository at this point in the history
…ntries
  • Loading branch information
Faltenreich committed Feb 4, 2024
1 parent bfb6a47 commit 07b4abe
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 1 deletion.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ app-android-sdk-target = "34"

activity-compose = "1.8.2"
android-gradle = "8.2.2"
android-lifecycle-compose = "2.7.0"
android-test-runner = "1.5.2"
android-test-junit = "1.1.5"
compose = "1.6.0-beta01"
Expand Down Expand Up @@ -42,6 +43,7 @@ activity-compose = { module = "androidx.activity:activity-compose", version.ref
androidx-compose-material3 = { module = "androidx.compose.material3:material3", version.ref = "compose-material3" }
androidx-datastore = { module = "androidx.datastore:datastore-preferences", version.ref = "datastore" }
androidx-emojipicker = { module = "androidx.emoji2:emoji2-emojipicker", version.ref = "emojipicker" }
androidx-lifecycle-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "android-lifecycle-compose" }
androidx-test-runner = { module = "androidx.test:runner", version.ref = "android-test-runner" }
androidx-test-junit = { module = "androidx.test.ext:junit", version.ref = "android-test-junit" }
androidx-test-junit-ktx = { module = "androidx.test.ext:junit-ktx", version.ref = "android-test-junit" }
Expand Down
1 change: 1 addition & 0 deletions shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ kotlin {
implementation(libs.androidx.compose.material3)
implementation(libs.androidx.datastore)
implementation(libs.androidx.emojipicker)
implementation(libs.androidx.lifecycle.compose)
implementation(libs.koin.android)
implementation(libs.ktor.android)
implementation(libs.paging.android)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.faltenreich.diaguard.shared.view

import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.Lifecycle

@Composable
actual fun rememberLifecycleState(): LifecycleState {
val state by LocalLifecycleOwner.current.lifecycle.currentStateFlow.collectAsState()
return state.toDomain()
}

private fun Lifecycle.State.toDomain(): LifecycleState {
return when (this) {
Lifecycle.State.DESTROYED -> LifecycleState.DESTROYED
Lifecycle.State.INITIALIZED -> LifecycleState.INITIALIZED
Lifecycle.State.CREATED -> LifecycleState.CREATED
Lifecycle.State.STARTED -> LifecycleState.STARTED
Lifecycle.State.RESUMED -> LifecycleState.RESUMED
}
}
11 changes: 10 additions & 1 deletion shared/src/commonMain/kotlin/com/faltenreich/diaguard/log/Log.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import com.faltenreich.diaguard.log.item.LogEntry
import com.faltenreich.diaguard.log.item.LogItem
import com.faltenreich.diaguard.log.item.LogMonth
import com.faltenreich.diaguard.shared.di.inject
import com.faltenreich.diaguard.shared.view.LifecycleState
import com.faltenreich.diaguard.shared.view.Skeleton
import com.faltenreich.diaguard.shared.view.rememberLifecycleState
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filterNotNull

Expand All @@ -35,7 +37,6 @@ fun Log(
modifier: Modifier = Modifier,
viewModel: LogViewModel = inject(),
) {
// FIXME: Gets not updated on entry change
val state = viewModel.collectState() ?: return
val paginationItems = viewModel.pagingData.collectAsLazyPagingItems()
val listState = rememberLazyListState()
Expand All @@ -45,6 +46,14 @@ fun Log(
listState.scrollBy(-state.monthHeaderSize.height.toFloat())
}

val lifecycleState = rememberLifecycleState()
LaunchedEffect(lifecycleState) {
if (lifecycleState == LifecycleState.RESUMED) {
// FIXME: Scrolls down, maybe due to wrong refreshKey
paginationItems.refresh()
}
}

LaunchedEffect(state) {
snapshotFlow {
listState.layoutInfo.visibleItemsInfo
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.faltenreich.diaguard.shared.view

import androidx.compose.runtime.Composable

@Composable
expect fun rememberLifecycleState(): LifecycleState
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.faltenreich.diaguard.shared.view

enum class LifecycleState {
DESTROYED,
INITIALIZED,
CREATED,
STARTED,
RESUMED,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.faltenreich.diaguard.shared.view

import androidx.compose.runtime.Composable

@Composable
actual fun rememberLifecycleState(): LifecycleState {
TODO("Not yet implemented")
}

0 comments on commit 07b4abe

Please sign in to comment.