fix: event dispatch; closes #348 (#373)

This commit is contained in:
Diego Beraldin 2023-12-25 10:00:13 +01:00 committed by GitHub
parent 8d4decf608
commit 6ce62e3e61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 80 additions and 63 deletions

View File

@ -27,7 +27,7 @@ class InboxViewModel(
override fun onStarted() {
mvi.onStarted()
mvi.scope?.launch(Dispatchers.IO) {
mvi.scope?.launch {
identityRepository.isLogged.onEach { logged ->
mvi.updateState { it.copy(isLogged = logged) }
}.launchIn(this)

View File

@ -17,7 +17,6 @@ class ProfileMainViewModel(
override fun onStarted() {
mvi.onStarted()
mvi.scope?.launch {
identityRepository.isLogged.onEach { logged ->
mvi.updateState { it.copy(logged = logged) }

View File

@ -57,7 +57,7 @@ class ExploreViewModel(
instance = apiConfigRepository.instance.value,
)
}
mvi.scope?.launch(Dispatchers.Main) {
mvi.scope?.launch {
identityRepository.isLogged.onEach { isLogged ->
mvi.updateState {
it.copy(isLogged = isLogged ?: false)

View File

@ -32,6 +32,7 @@ import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.toSortType
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.GetSortTypesUseCase
import com.github.diegoberaldin.raccoonforlemmy.resources.LanguageRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
@ -55,7 +56,7 @@ class SettingsViewModel(
override fun onStarted() {
mvi.onStarted()
mvi.scope?.launch(Dispatchers.Main) {
mvi.scope?.launch {
themeRepository.uiTheme.onEach { value ->
mvi.updateState { it.copy(uiTheme = value) }
}.launchIn(this)
@ -342,7 +343,7 @@ class SettingsViewModel(
private fun changeTheme(value: UiTheme?) {
themeRepository.changeUiTheme(value)
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
theme = value?.toInt()
)
@ -352,7 +353,7 @@ class SettingsViewModel(
private fun changeFontFamily(value: UiFontFamily) {
themeRepository.changeUiFontFamily(value)
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
uiFontFamily = value.toInt()
)
@ -362,7 +363,7 @@ class SettingsViewModel(
private fun changeUiFontScale(value: Float) {
themeRepository.changeUiFontScale(value)
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
uiFontScale = value
)
@ -372,7 +373,7 @@ class SettingsViewModel(
private fun changeContentFontScale(value: Float) {
themeRepository.changeContentFontScale(value)
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
contentFontScale = value
)
@ -382,7 +383,7 @@ class SettingsViewModel(
private fun changeContentFontFamily(value: UiFontFamily) {
themeRepository.changeContentFontFamily(value)
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
contentFontFamily = value.toInt()
)
@ -392,7 +393,7 @@ class SettingsViewModel(
private fun changeLanguage(value: String) {
languageRepository.changeLanguage(value)
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
locale = value
)
@ -402,7 +403,7 @@ class SettingsViewModel(
private fun changeDefaultListingType(value: ListingType) {
mvi.updateState { it.copy(defaultListingType = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
defaultListingType = value.toInt()
)
@ -414,7 +415,7 @@ class SettingsViewModel(
private fun changeDefaultPostSortType(value: SortType) {
mvi.updateState { it.copy(defaultPostSortType = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
defaultPostSortType = value.toInt()
)
@ -426,7 +427,7 @@ class SettingsViewModel(
private fun changeDefaultCommentSortType(value: SortType) {
mvi.updateState { it.copy(defaultCommentSortType = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
defaultCommentSortType = value.toInt()
)
@ -436,7 +437,7 @@ class SettingsViewModel(
private fun changeNavBarTitlesVisible(value: Boolean) {
themeRepository.changeNavItemTitles(value)
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
navigationTitlesVisible = value
)
@ -446,7 +447,7 @@ class SettingsViewModel(
private fun changeIncludeNsfw(value: Boolean) {
mvi.updateState { it.copy(includeNsfw = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
includeNsfw = value
)
@ -456,7 +457,7 @@ class SettingsViewModel(
private fun changeBlurNsfw(value: Boolean) {
mvi.updateState { it.copy(blurNsfw = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
blurNsfw = value
)
@ -466,7 +467,7 @@ class SettingsViewModel(
private fun changeDynamicColors(value: Boolean) {
themeRepository.changeDynamicColors(value)
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
dynamicColors = value
)
@ -476,7 +477,7 @@ class SettingsViewModel(
private fun changeCustomSeedColor(value: Color?) {
themeRepository.changeCustomSeedColor(value)
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
customSeedColor = value?.toArgb()
)
@ -486,7 +487,7 @@ class SettingsViewModel(
private fun changeUpvoteColor(value: Color?) {
themeRepository.changeUpvoteColor(value)
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
upvoteColor = value?.toArgb()
)
@ -496,7 +497,7 @@ class SettingsViewModel(
private fun changeDownvoteColor(value: Color?) {
themeRepository.changeDownvoteColor(value)
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
downvoteColor = value?.toArgb()
)
@ -506,7 +507,7 @@ class SettingsViewModel(
private fun changeOpenUrlsInExternalBrowser(value: Boolean) {
mvi.updateState { it.copy(openUrlsInExternalBrowser = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
openUrlsInExternalBrowser = value
)
@ -516,7 +517,7 @@ class SettingsViewModel(
private fun changeEnableSwipeActions(value: Boolean) {
mvi.updateState { it.copy(enableSwipeActions = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
enableSwipeActions = value
)
@ -526,7 +527,7 @@ class SettingsViewModel(
private fun changeEnableDoubleTapAction(value: Boolean) {
mvi.updateState { it.copy(enableDoubleTapAction = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
enableDoubleTapAction = value
)
@ -536,7 +537,7 @@ class SettingsViewModel(
private fun changePostLayout(value: PostLayout) {
themeRepository.changePostLayout(value)
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
postLayout = value.toInt()
)
@ -552,7 +553,7 @@ class SettingsViewModel(
private fun changeVoteFormat(value: VoteFormat) {
mvi.updateState { it.copy(voteFormat = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
voteFormat = value
)
@ -562,7 +563,7 @@ class SettingsViewModel(
private fun changeAutoLoadImages(value: Boolean) {
mvi.updateState { it.copy(autoLoadImages = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
autoLoadImages = value
)
@ -572,7 +573,7 @@ class SettingsViewModel(
private fun changeAutoExpandComments(value: Boolean) {
mvi.updateState { it.copy(autoExpandComments = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
autoExpandComments = value
)
@ -582,7 +583,7 @@ class SettingsViewModel(
private fun changeFullHeightImages(value: Boolean) {
mvi.updateState { it.copy(fullHeightImages = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
fullHeightImages = value
)
@ -592,7 +593,7 @@ class SettingsViewModel(
private fun changeHideNavigationBarWhileScrolling(value: Boolean) {
mvi.updateState { it.copy(hideNavigationBarWhileScrolling = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
hideNavigationBarWhileScrolling = value
)
@ -602,7 +603,7 @@ class SettingsViewModel(
private fun changeMarkAsReadWhileScrolling(value: Boolean) {
mvi.updateState { it.copy(markAsReadWhileScrolling = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
markAsReadWhileScrolling = value
)
@ -612,7 +613,7 @@ class SettingsViewModel(
private fun changeZombieModeInterval(value: Duration) {
mvi.updateState { it.copy(zombieModeInterval = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
zombieModeInterval = value
)
@ -622,7 +623,7 @@ class SettingsViewModel(
private fun changeZombieModeScrollAmount(value: Float) {
mvi.updateState { it.copy(zombieModeScrollAmount = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
zombieModeScrollAmount = value
)
@ -632,7 +633,7 @@ class SettingsViewModel(
private fun changeDefaultInboxUnreadOnly(value: Boolean) {
mvi.updateState { it.copy(defaultInboxUnreadOnly = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
defaultInboxType = value.toInboxDefaultType(),
)
@ -642,7 +643,7 @@ class SettingsViewModel(
private fun changeCommentBarTheme(value: CommentBarTheme) {
themeRepository.changeCommentBarTheme(value)
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
commentBarTheme = value.toInt()
)
@ -652,7 +653,7 @@ class SettingsViewModel(
private fun changeSearchPostTitleOnly(value: Boolean) {
mvi.updateState { it.copy(searchPostTitleOnly = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.currentSettings.value.copy(
searchPostTitleOnly = value
)
@ -667,7 +668,7 @@ class SettingsViewModel(
}
private fun handleLogout() {
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val settings = settingsRepository.getSettings(null)
mvi.updateState {
it.copy(

View File

@ -209,7 +209,7 @@ class InboxChatViewModel(
private fun submitNewMessage(text: String) {
val editedMessageId = uiState.value.editedMessageId
if (text.isNotEmpty()) {
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val auth = identityRepository.authToken.value
if (editedMessageId == null) {
messageRepository.create(

View File

@ -68,7 +68,7 @@ class CommunityDetailViewModel(
)
}
mvi.scope?.launch(Dispatchers.IO) {
mvi.scope?.launch {
themeRepository.postLayout.onEach { layout ->
mvi.updateState { it.copy(postLayout = layout) }
}.launchIn(this)
@ -274,6 +274,9 @@ class CommunityDetailViewModel(
}
private fun applySortType(value: SortType) {
if (uiState.value.sortType == value) {
return
}
mvi.updateState { it.copy(sortType = value) }
mvi.scope?.launch(Dispatchers.IO) {
mvi.emitEffect(CommunityDetailMviModel.Effect.BackToTop)

View File

@ -5,8 +5,6 @@ import com.github.diegoberaldin.raccoonforlemmy.core.architecture.MviModel
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.repository.SettingsRepository
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.CommunityModel
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.CommunityRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
@ -23,7 +21,7 @@ class CommunityInfoViewModel(
mvi.onStarted()
mvi.updateState { it.copy(community = community) }
mvi.scope?.launch(Dispatchers.IO) {
mvi.scope?.launch {
settingsRepository.currentSettings.onEach {
mvi.updateState { it.copy(autoLoadImages = it.autoLoadImages) }
}.launchIn(this)

View File

@ -27,6 +27,7 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import kotlinx.coroutines.yield
@ -46,7 +47,7 @@ class ModalDrawerViewModel(
@OptIn(FlowPreview::class)
override fun onStarted() {
mvi.onStarted()
mvi.scope?.launch(Dispatchers.Main) {
mvi.scope?.launch {
apiConfigurationRepository.instance.onEach { instance ->
mvi.updateState {
it.copy(instance = instance)
@ -62,7 +63,7 @@ class ModalDrawerViewModel(
observeChangesInFavoriteCommunities()
mvi.scope?.launch {
withContext(Dispatchers.IO) {
delay(250)
refreshUser()
refresh()

View File

@ -35,7 +35,7 @@ class InstanceInfoViewModel(
override fun onStarted() {
mvi.onStarted()
mvi.scope?.launch(Dispatchers.IO) {
mvi.scope?.launch {
settingsRepository.currentSettings.onEach { settings ->
mvi.updateState { it.copy(autoLoadImages = settings.autoLoadImages) }
}.launchIn(this)

View File

@ -104,7 +104,7 @@ class ManageSubscriptionsViewModel(
}
private fun handleUnsubscription(community: CommunityModel) {
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
val auth = identityRepository.authToken.value
communityRepository.unsubscribe(
auth = auth, id = community.id

View File

@ -36,7 +36,6 @@ class InboxMentionsViewModel(
override fun onStarted() {
mvi.onStarted()
mvi.scope?.launch {
coordinator.events.onEach {
when (it) {

View File

@ -62,7 +62,7 @@ class ModlogViewModel(
initial = initial,
)
}
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
loadNextPage()
}
}

View File

@ -48,7 +48,6 @@ class MultiCommunityViewModel(
override fun onStarted() {
mvi.onStarted()
mvi.scope?.launch {
themeRepository.postLayout.onEach { layout ->
mvi.updateState { it.copy(postLayout = layout) }
@ -209,6 +208,9 @@ class MultiCommunityViewModel(
}
private fun applySortType(value: SortType) {
if (uiState.value.sortType == value) {
return
}
mvi.updateState { it.copy(sortType = value) }
refresh()
}

View File

@ -52,7 +52,7 @@ class ProfileLoggedViewModel(
override fun onStarted() {
mvi.onStarted()
mvi.updateState { it.copy(instance = apiConfigurationRepository.instance.value) }
mvi.scope?.launch(Dispatchers.IO) {
mvi.scope?.launch {
themeRepository.postLayout.onEach { layout ->
mvi.updateState { it.copy(postLayout = layout) }
}.launchIn(this)

View File

@ -59,7 +59,7 @@ class PostDetailViewModel(
?: apiConfigurationRepository.instance.value,
)
}
mvi.scope?.launch(Dispatchers.Main) {
mvi.scope?.launch {
notificationCenter.subscribe(NotificationCenterEvent.PostUpdated::class).onEach { evt ->
handlePostUpdate(evt.model)
}.launchIn(this)
@ -131,9 +131,7 @@ class PostDetailViewModel(
val auth = identityRepository.authToken.value.orEmpty()
val user = siteRepository.getCurrentUser(auth)
mvi.updateState {
it.copy(
currentUserId = user?.id ?: 0,
)
it.copy(currentUserId = user?.id ?: 0)
}
}
@ -211,7 +209,7 @@ class PostDetailViewModel(
} else {
// comment to highlight found
commentWasHighlighted = true
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.Main) {
mvi.emitEffect(PostDetailMviModel.Effect.ScrollToComment(indexOfHighlight))
}
}
@ -394,6 +392,9 @@ class PostDetailViewModel(
}
private fun applySortType(value: SortType) {
if (uiState.value.sortType == value) {
return
}
mvi.updateState { it.copy(sortType = value) }
mvi.scope?.launch(Dispatchers.IO) {
mvi.emitEffect(PostDetailMviModel.Effect.BackToTop)
@ -641,7 +642,7 @@ class PostDetailViewModel(
}
private fun toggleExpanded(comment: CommentModel) {
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.Main) {
val commentId = comment.id
val newExpanded = !comment.expanded
mvi.updateState {

View File

@ -58,8 +58,7 @@ class PostListViewModel(
override fun onStarted() {
mvi.onStarted()
mvi.scope?.launch(Dispatchers.Main) {
mvi.scope?.launch {
apiConfigurationRepository.instance.onEach { instance ->
mvi.updateState {
it.copy(instance = instance)
@ -321,16 +320,22 @@ class PostListViewModel(
}
private fun applySortType(value: SortType) {
if (uiState.value.sortType == value) {
return
}
mvi.updateState { it.copy(sortType = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
mvi.emitEffect(PostListMviModel.Effect.BackToTop)
refresh()
}
}
private fun applyListingType(value: ListingType) {
if (uiState.value.listingType == value) {
return
}
mvi.updateState { it.copy(listingType = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
mvi.emitEffect(PostListMviModel.Effect.BackToTop)
refresh()
}

View File

@ -39,7 +39,6 @@ class InboxRepliesViewModel(
override fun onStarted() {
mvi.onStarted()
mvi.scope?.launch {
coordinator.events.onEach {
when (it) {

View File

@ -105,7 +105,7 @@ class ReportListViewModel(
initial = initial,
)
}
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.IO) {
loadNextPage()
}
}

View File

@ -224,6 +224,9 @@ class SavedItemsViewModel(
}
private fun applySortType(value: SortType) {
if (uiState.value.sortType == value) {
return
}
mvi.updateState { it.copy(sortType = value) }
refresh()
}

View File

@ -28,6 +28,7 @@ import kotlinx.coroutines.IO
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class UserDetailViewModel(
private val mvi: DefaultMviModel<UserDetailMviModel.Intent, UserDetailMviModel.UiState, UserDetailMviModel.Effect>,
@ -108,7 +109,9 @@ class UserDetailViewModel(
if (uiState.value.posts.isEmpty()) {
updateAvailableSortTypes()
refresh(initial = true)
withContext(Dispatchers.IO) {
refresh(initial = true)
}
}
}
}
@ -192,8 +195,11 @@ class UserDetailViewModel(
}
private fun applySortType(value: SortType) {
if (uiState.value.sortType == value) {
return
}
mvi.updateState { it.copy(sortType = value) }
mvi.scope?.launch {
mvi.scope?.launch(Dispatchers.Main) {
mvi.emitEffect(UserDetailMviModel.Effect.BackToTop)
}
}