From f79804e5ef0e7bc74fe474b4694291b71bbef12f Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Sun, 13 Mar 2022 19:42:33 +0000 Subject: [PATCH] using live counts for the invitations --- .../st/domain/sync/OverviewPersistence.kt | 2 +- .../app/dapk/st/profile/ProfileScreen.kt | 8 ++--- .../app/dapk/st/profile/ProfileViewModel.kt | 29 +++++++++++++------ .../app/dapk/st/matrix/sync/SyncService.kt | 2 +- .../sync/internal/DefaultSyncService.kt | 2 +- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/domains/store/src/main/kotlin/app/dapk/st/domain/sync/OverviewPersistence.kt b/domains/store/src/main/kotlin/app/dapk/st/domain/sync/OverviewPersistence.kt index 7c6a9c6..1b1ee08 100644 --- a/domains/store/src/main/kotlin/app/dapk/st/domain/sync/OverviewPersistence.kt +++ b/domains/store/src/main/kotlin/app/dapk/st/domain/sync/OverviewPersistence.kt @@ -59,7 +59,7 @@ internal class OverviewPersistence( } override suspend fun retrieve(): OverviewState { - return withContext(Dispatchers.IO) { + return dispatchers.withIoContext { val overviews = database.overviewStateQueries.selectAll().executeAsList() overviews.map { json.decodeFromString(RoomOverview.serializer(), it.blob) } } diff --git a/features/profile/src/main/kotlin/app/dapk/st/profile/ProfileScreen.kt b/features/profile/src/main/kotlin/app/dapk/st/profile/ProfileScreen.kt index 5587329..8bc21f3 100644 --- a/features/profile/src/main/kotlin/app/dapk/st/profile/ProfileScreen.kt +++ b/features/profile/src/main/kotlin/app/dapk/st/profile/ProfileScreen.kt @@ -20,7 +20,6 @@ import app.dapk.st.core.LifecycleEffect import app.dapk.st.core.StartObserving import app.dapk.st.core.components.CenteredLoading import app.dapk.st.design.components.CircleishAvatar -import app.dapk.st.design.components.Spider import app.dapk.st.design.components.TextRow import app.dapk.st.design.components.percentOfHeight import app.dapk.st.settings.SettingsActivity @@ -29,9 +28,10 @@ import app.dapk.st.settings.SettingsActivity fun ProfileScreen(viewModel: ProfileViewModel) { viewModel.ObserveEvents() - LifecycleEffect(onStart = { - viewModel.start() - }) + LifecycleEffect( + onStart = { viewModel.start() }, + onStop = { viewModel.stop() } + ) val context = LocalContext.current diff --git a/features/profile/src/main/kotlin/app/dapk/st/profile/ProfileViewModel.kt b/features/profile/src/main/kotlin/app/dapk/st/profile/ProfileViewModel.kt index 3bb17b2..7a6e2ee 100644 --- a/features/profile/src/main/kotlin/app/dapk/st/profile/ProfileViewModel.kt +++ b/features/profile/src/main/kotlin/app/dapk/st/profile/ProfileViewModel.kt @@ -4,8 +4,11 @@ import androidx.lifecycle.viewModelScope import app.dapk.st.matrix.room.ProfileService import app.dapk.st.matrix.sync.SyncService import app.dapk.st.viewmodel.DapkViewModel -import kotlinx.coroutines.flow.firstOrNull -import kotlinx.coroutines.launch +import kotlinx.coroutines.Job +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach class ProfileViewModel( private val profileService: ProfileService, @@ -14,14 +17,18 @@ class ProfileViewModel( initialState = ProfileScreenState.Loading ) { - fun start() { - viewModelScope.launch { - val invitationsCount = syncService.invites().firstOrNull()?.size ?: 0 - val me = profileService.me(forceRefresh = true) - state = ProfileScreenState.Content(me, invitationsCount = invitationsCount) - } - } + private var syncingJob: Job? = null + fun start() { + syncingJob = combine( + syncService.startSyncing(), + flow { emit(profileService.me(forceRefresh = true)) }, + syncService.invites(), + transform = { _, me, invites -> me to invites } + ) + .onEach { (me, invites) -> state = ProfileScreenState.Content(me, invitationsCount = invites.size) } + .launchIn(viewModelScope) + } fun updateDisplayName() { // TODO @@ -31,4 +38,8 @@ class ProfileViewModel( // TODO } + fun stop() { + syncingJob?.cancel() + } + } diff --git a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/SyncService.kt b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/SyncService.kt index 725d13a..7fc345c 100644 --- a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/SyncService.kt +++ b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/SyncService.kt @@ -18,7 +18,7 @@ private val SERVICE_KEY = SyncService::class interface SyncService : MatrixService { - suspend fun invites(): Flow + fun invites(): Flow fun overview(): Flow fun room(roomId: RoomId): Flow fun startSyncing(): Flow diff --git a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/DefaultSyncService.kt b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/DefaultSyncService.kt index eff24ac..c51ecd7 100644 --- a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/DefaultSyncService.kt +++ b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/DefaultSyncService.kt @@ -100,7 +100,7 @@ internal class DefaultSyncService( } override fun startSyncing() = syncFlow - override suspend fun invites() = overviewStore.latestInvites() + override fun invites() = overviewStore.latestInvites() override fun overview() = overviewStore.latest() override fun room(roomId: RoomId) = roomStore.latest(roomId) override fun events() = syncEventsFlow