Correct the usage of flowOn

This commit is contained in:
fanchao 2024-06-04 10:41:15 +10:00
parent 9b6fa0db01
commit 172edde628

View File

@ -2,7 +2,6 @@ package org.thoughtcrime.securesms.home
import android.content.ContentResolver import android.content.ContentResolver
import android.content.Context import android.content.Context
import android.util.Log
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.asFlow import androidx.lifecycle.asFlow
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
@ -22,9 +21,9 @@ import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.merge import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.withContext
import org.session.libsession.utilities.TextSecurePreferences import org.session.libsession.utilities.TextSecurePreferences
import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.database.DatabaseContentProviders import org.thoughtcrime.securesms.database.DatabaseContentProviders
@ -60,12 +59,10 @@ class HomeViewModel @Inject constructor(
observeTypingStatus(), observeTypingStatus(),
messageRequests(), messageRequests(),
::Data ::Data
) ).stateIn(viewModelScope, SharingStarted.Eagerly, null)
.stateIn(viewModelScope, SharingStarted.Eagerly, null)
private fun hasHiddenMessageRequests() = TextSecurePreferences.events private fun hasHiddenMessageRequests() = TextSecurePreferences.events
.filter { it == TextSecurePreferences.HAS_HIDDEN_MESSAGE_REQUESTS } .filter { it == TextSecurePreferences.HAS_HIDDEN_MESSAGE_REQUESTS }
.flowOn(Dispatchers.IO)
.map { prefs.hasHiddenMessageRequests() } .map { prefs.hasHiddenMessageRequests() }
.onStart { emit(prefs.hasHiddenMessageRequests()) } .onStart { emit(prefs.hasHiddenMessageRequests()) }
@ -81,7 +78,7 @@ class HomeViewModel @Inject constructor(
hasHiddenMessageRequests(), hasHiddenMessageRequests(),
latestUnapprovedConversationTimestamp(), latestUnapprovedConversationTimestamp(),
::createMessageRequests ::createMessageRequests
) ).flowOn(Dispatchers.IO)
private fun unapprovedConversationCount() = reloadTriggersAndContentChanges() private fun unapprovedConversationCount() = reloadTriggersAndContentChanges()
.map { threadDb.unapprovedConversationCount } .map { threadDb.unapprovedConversationCount }
@ -96,13 +93,13 @@ class HomeViewModel @Inject constructor(
threadDb.readerFor(openCursor).run { generateSequence { next }.toList() } threadDb.readerFor(openCursor).run { generateSequence { next }.toList() }
} }
} }
.flowOn(Dispatchers.IO)
@OptIn(FlowPreview::class) @OptIn(FlowPreview::class)
private fun reloadTriggersAndContentChanges() = merge( private fun reloadTriggersAndContentChanges() = merge(
manualReloadTrigger, manualReloadTrigger,
contentResolver.observeChanges(DatabaseContentProviders.ConversationList.CONTENT_URI) contentResolver.observeChanges(DatabaseContentProviders.ConversationList.CONTENT_URI)
) )
.flowOn(Dispatchers.IO)
.debounce(CHANGE_NOTIFICATION_DEBOUNCE_MILLS) .debounce(CHANGE_NOTIFICATION_DEBOUNCE_MILLS)
.onStart { emit(Unit) } .onStart { emit(Unit) }
@ -114,7 +111,7 @@ class HomeViewModel @Inject constructor(
val messageRequests: MessageRequests? = null val messageRequests: MessageRequests? = null
) )
fun createMessageRequests( private fun createMessageRequests(
count: Int, count: Int,
hidden: Boolean, hidden: Boolean,
timestamp: Long timestamp: Long