using live counts for the invitations

This commit is contained in:
Adam Brown 2022-03-13 19:42:33 +00:00
parent 50e12c8c54
commit f79804e5ef
5 changed files with 27 additions and 16 deletions

View File

@ -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) }
}

View File

@ -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

View File

@ -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()
}
}

View File

@ -18,7 +18,7 @@ private val SERVICE_KEY = SyncService::class
interface SyncService : MatrixService {
suspend fun invites(): Flow<InviteState>
fun invites(): Flow<InviteState>
fun overview(): Flow<OverviewState>
fun room(roomId: RoomId): Flow<RoomState>
fun startSyncing(): Flow<Unit>

View File

@ -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