Fix list mode changing
This commit is contained in:
@@ -55,7 +55,7 @@ class FavouritesListViewModel @AssistedInject constructor(
|
|||||||
} else {
|
} else {
|
||||||
repository.observeAll(categoryId)
|
repository.observeAll(categoryId)
|
||||||
},
|
},
|
||||||
createListModeFlow(),
|
listModeFlow,
|
||||||
) { list, mode ->
|
) { list, mode ->
|
||||||
when {
|
when {
|
||||||
list.isEmpty() -> listOf(
|
list.isEmpty() -> listOf(
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ class HistoryListViewModel @Inject constructor(
|
|||||||
override val content = combine(
|
override val content = combine(
|
||||||
repository.observeAllWithHistory(),
|
repository.observeAllWithHistory(),
|
||||||
historyGrouping,
|
historyGrouping,
|
||||||
createListModeFlow(),
|
listModeFlow,
|
||||||
) { list, grouped, mode ->
|
) { list, grouped, mode ->
|
||||||
when {
|
when {
|
||||||
list.isEmpty() -> listOf(
|
list.isEmpty() -> listOf(
|
||||||
|
|||||||
@@ -49,6 +49,9 @@ class ListModeBottomSheet :
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onButtonChecked(group: MaterialButtonToggleGroup?, checkedId: Int, isChecked: Boolean) {
|
override fun onButtonChecked(group: MaterialButtonToggleGroup?, checkedId: Int, isChecked: Boolean) {
|
||||||
|
if (!isChecked) {
|
||||||
|
return
|
||||||
|
}
|
||||||
val mode = when (checkedId) {
|
val mode = when (checkedId) {
|
||||||
R.id.button_list -> ListMode.LIST
|
R.id.button_list -> ListMode.LIST
|
||||||
R.id.button_list_detailed -> ListMode.DETAILED_LIST
|
R.id.button_list_detailed -> ListMode.DETAILED_LIST
|
||||||
|
|||||||
@@ -1,26 +1,29 @@
|
|||||||
package org.koitharu.kotatsu.list.ui
|
package org.koitharu.kotatsu.list.ui
|
||||||
|
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
|
import kotlinx.coroutines.flow.stateIn
|
||||||
|
import kotlinx.coroutines.plus
|
||||||
import org.koitharu.kotatsu.base.ui.BaseViewModel
|
import org.koitharu.kotatsu.base.ui.BaseViewModel
|
||||||
import org.koitharu.kotatsu.base.ui.util.ReversibleAction
|
import org.koitharu.kotatsu.base.ui.util.ReversibleAction
|
||||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||||
import org.koitharu.kotatsu.core.prefs.ListMode
|
|
||||||
import org.koitharu.kotatsu.core.prefs.observeAsFlow
|
import org.koitharu.kotatsu.core.prefs.observeAsFlow
|
||||||
import org.koitharu.kotatsu.core.prefs.observeAsLiveData
|
import org.koitharu.kotatsu.core.prefs.observeAsLiveData
|
||||||
import org.koitharu.kotatsu.list.ui.model.ListModel
|
import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||||
import org.koitharu.kotatsu.parsers.model.MangaTag
|
import org.koitharu.kotatsu.parsers.model.MangaTag
|
||||||
import org.koitharu.kotatsu.utils.SingleLiveEvent
|
import org.koitharu.kotatsu.utils.SingleLiveEvent
|
||||||
|
import org.koitharu.kotatsu.utils.asFlowLiveData
|
||||||
|
|
||||||
abstract class MangaListViewModel(
|
abstract class MangaListViewModel(
|
||||||
private val settings: AppSettings,
|
private val settings: AppSettings,
|
||||||
) : BaseViewModel() {
|
) : BaseViewModel() {
|
||||||
|
|
||||||
abstract val content: LiveData<List<ListModel>>
|
abstract val content: LiveData<List<ListModel>>
|
||||||
val listMode = MutableLiveData<ListMode>()
|
protected val listModeFlow = settings.observeAsFlow(AppSettings.KEY_LIST_MODE) { listMode }
|
||||||
|
.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Lazily, settings.listMode)
|
||||||
|
val listMode = listModeFlow.asFlowLiveData(viewModelScope.coroutineContext)
|
||||||
val onActionDone = SingleLiveEvent<ReversibleAction>()
|
val onActionDone = SingleLiveEvent<ReversibleAction>()
|
||||||
val gridScale = settings.observeAsLiveData(
|
val gridScale = settings.observeAsLiveData(
|
||||||
context = viewModelScope.coroutineContext + Dispatchers.Default,
|
context = viewModelScope.coroutineContext + Dispatchers.Default,
|
||||||
@@ -30,13 +33,6 @@ abstract class MangaListViewModel(
|
|||||||
|
|
||||||
open fun onUpdateFilter(tags: Set<MangaTag>) = Unit
|
open fun onUpdateFilter(tags: Set<MangaTag>) = Unit
|
||||||
|
|
||||||
protected fun createListModeFlow() = settings.observeAsFlow(AppSettings.KEY_LIST_MODE) { listMode }
|
|
||||||
.onEach {
|
|
||||||
if (listMode.value != it) {
|
|
||||||
listMode.postValue(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract fun onRefresh()
|
abstract fun onRefresh()
|
||||||
|
|
||||||
abstract fun onRetry()
|
abstract fun onRetry()
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ class LocalListViewModel @Inject constructor(
|
|||||||
|
|
||||||
override val content = combine(
|
override val content = combine(
|
||||||
mangaList,
|
mangaList,
|
||||||
createListModeFlow(),
|
listModeFlow,
|
||||||
sortOrder.asFlow(),
|
sortOrder.asFlow(),
|
||||||
selectedTags,
|
selectedTags,
|
||||||
listError,
|
listError,
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ class RemoteListViewModel @AssistedInject constructor(
|
|||||||
|
|
||||||
override val content = combine(
|
override val content = combine(
|
||||||
mangaList,
|
mangaList,
|
||||||
createListModeFlow(),
|
listModeFlow,
|
||||||
createHeaderFlow(),
|
createHeaderFlow(),
|
||||||
listError,
|
listError,
|
||||||
hasNextPage,
|
hasNextPage,
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ class SearchViewModel @AssistedInject constructor(
|
|||||||
|
|
||||||
override val content = combine(
|
override val content = combine(
|
||||||
mangaList,
|
mangaList,
|
||||||
createListModeFlow(),
|
listModeFlow,
|
||||||
listError,
|
listError,
|
||||||
hasNextPage,
|
hasNextPage,
|
||||||
) { list, mode, error, hasNext ->
|
) { list, mode, error, hasNext ->
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package org.koitharu.kotatsu.suggestions.ui
|
|||||||
|
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import javax.inject.Inject
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.catch
|
import kotlinx.coroutines.flow.catch
|
||||||
import kotlinx.coroutines.flow.combine
|
import kotlinx.coroutines.flow.combine
|
||||||
@@ -17,6 +16,7 @@ import org.koitharu.kotatsu.list.ui.model.toUi
|
|||||||
import org.koitharu.kotatsu.suggestions.domain.SuggestionRepository
|
import org.koitharu.kotatsu.suggestions.domain.SuggestionRepository
|
||||||
import org.koitharu.kotatsu.utils.asFlowLiveData
|
import org.koitharu.kotatsu.utils.asFlowLiveData
|
||||||
import org.koitharu.kotatsu.utils.ext.onFirst
|
import org.koitharu.kotatsu.utils.ext.onFirst
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class SuggestionsViewModel @Inject constructor(
|
class SuggestionsViewModel @Inject constructor(
|
||||||
@@ -26,7 +26,7 @@ class SuggestionsViewModel @Inject constructor(
|
|||||||
|
|
||||||
override val content = combine(
|
override val content = combine(
|
||||||
repository.observeAll(),
|
repository.observeAll(),
|
||||||
createListModeFlow(),
|
listModeFlow,
|
||||||
) { list, mode ->
|
) { list, mode ->
|
||||||
when {
|
when {
|
||||||
list.isEmpty() -> listOf(
|
list.isEmpty() -> listOf(
|
||||||
@@ -37,6 +37,7 @@ class SuggestionsViewModel @Inject constructor(
|
|||||||
actionStringRes = 0,
|
actionStringRes = 0,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
else -> list.toUi(mode)
|
else -> list.toUi(mode)
|
||||||
}
|
}
|
||||||
}.onStart {
|
}.onStart {
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ class UpdatesViewModel @Inject constructor(
|
|||||||
|
|
||||||
override val content = combine(
|
override val content = combine(
|
||||||
repository.observeUpdatedManga(),
|
repository.observeUpdatedManga(),
|
||||||
createListModeFlow(),
|
listModeFlow,
|
||||||
) { mangaMap, mode ->
|
) { mangaMap, mode ->
|
||||||
when {
|
when {
|
||||||
mangaMap.isEmpty() -> listOf(
|
mangaMap.isEmpty() -> listOf(
|
||||||
|
|||||||
Reference in New Issue
Block a user