Skip to content

Commit

Permalink
Attach LibraryFilterState memory to Library filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Doomsdayrs committed Jan 16, 2023
1 parent 5debbc8 commit d12fc57
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package app.shosetsu.android.common

import app.shosetsu.android.common.enums.MarkingType
import app.shosetsu.android.domain.model.local.LibrarySortFilterEntity
import app.shosetsu.android.domain.model.local.LibraryFilterState
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

Expand Down Expand Up @@ -274,7 +274,7 @@ sealed class SettingKey<T : Any>(val name: String, val default: T) {
object VerifyCheckSum : BooleanKey("verifyCheckSum", false)

object LibraryFilter :
StringKey("libraryFilter", Json.encodeToString(LibrarySortFilterEntity()))
StringKey("libraryFilter", Json.encodeToString(LibraryFilterState()))

object RequireDoubleBackToExit : BooleanKey("requireDoubleBackToExit", false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ val useCaseModule: DI.Module = DI.Module("useCase") {
SetNovelsCategoriesUseCase(instance())
}

bind<UpdateLibraryFilterSettingsUseCase>() with provider {
UpdateLibraryFilterSettingsUseCase(instance())
bind<UpdateLibraryFilterStateUseCase>() with provider {
UpdateLibraryFilterStateUseCase(instance())
}

bind<GetExtListingNamesUseCase>() with provider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ val viewModelsModule: DI.Module = DI.Module("view_models_module") {
loadNovelUIColumnsH = instance(),
loadNovelUIColumnsP = instance(),
loadNovelUIBadgeToast = instance(),
toggleNovelPin = instance()
toggleNovelPin = instance(),
loadLibraryFilterSettings = instance(),
_updateLibraryFilterState = instance()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app.shosetsu.android.domain.model.local
import app.shosetsu.android.common.enums.InclusionState
import app.shosetsu.android.common.enums.NovelSortType
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json

/*
* This file is part of Shosetsu.
Expand All @@ -25,13 +26,22 @@ import kotlinx.serialization.Serializable
* 03 / 01 / 2021
*/
@Serializable
data class LibrarySortFilterEntity(
var sortType: NovelSortType = NovelSortType.BY_TITLE,
var reversedSort: Boolean = false,
var unreadInclusion: InclusionState? = null,
data class LibraryFilterState(
val sortType: NovelSortType = NovelSortType.BY_TITLE,
val reversedSort: Boolean = false,
val unreadInclusion: InclusionState? = null,

var genreFilter: Map<String, InclusionState> = emptyMap(),
var authorFilter: Map<String, InclusionState> = emptyMap(),
var artistFilter: Map<String, InclusionState> = emptyMap(),
var tagFilter: Map<String, InclusionState> = emptyMap(),
)
val genreFilter: Map<String, InclusionState> = emptyMap(),
val authorFilter: Map<String, InclusionState> = emptyMap(),
val artistFilter: Map<String, InclusionState> = emptyMap(),
val tagFilter: Map<String, InclusionState> = emptyMap(),
val arePinsOnTop: Boolean = true,
val downloadedOnly: InclusionState? = null
) {
companion object {
val libraryFilterStateJson = Json {
encodeDefaults = true
ignoreUnknownKeys = true
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package app.shosetsu.android.domain.usecases.load

import app.shosetsu.android.common.SettingKey
import app.shosetsu.android.domain.model.local.LibrarySortFilterEntity
import app.shosetsu.android.domain.model.local.LibraryFilterState
import app.shosetsu.android.domain.repository.base.ISettingsRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

/*
* This file is part of Shosetsu.
Expand All @@ -29,11 +30,11 @@ import kotlinx.serialization.json.Json
class LoadLibraryFilterSettingsUseCase(
private val iSettingsRepository: ISettingsRepository
) {
suspend operator fun invoke(): LibrarySortFilterEntity {
return iSettingsRepository.getString(
operator fun invoke(): Flow<LibraryFilterState> {
return iSettingsRepository.getStringFlow(
SettingKey.LibraryFilter
).let {
Json.decodeFromString(it)
).map {
LibraryFilterState.libraryFilterStateJson.decodeFromString(it)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package app.shosetsu.android.domain.usecases.update

import app.shosetsu.android.common.SettingKey
import app.shosetsu.android.domain.model.local.LibrarySortFilterEntity
import app.shosetsu.android.domain.model.local.LibraryFilterState
import app.shosetsu.android.domain.repository.base.ISettingsRepository
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

/*
* This file is part of Shosetsu.
Expand All @@ -26,13 +25,13 @@ import kotlinx.serialization.json.Json
/**
* 09 / 03 / 2021
*/
class UpdateLibraryFilterSettingsUseCase(
class UpdateLibraryFilterStateUseCase(
private val iSettingsRepository: ISettingsRepository
) {
suspend operator fun invoke(librarySortFilterEntity: LibrarySortFilterEntity) {
suspend operator fun invoke(librarySortFilterEntity: LibraryFilterState) {
iSettingsRepository.setString(
SettingKey.LibraryFilter,
Json.encodeToString(librarySortFilterEntity)
LibraryFilterState.libraryFilterStateJson.encodeToString(librarySortFilterEntity)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ import app.shosetsu.android.common.enums.NovelSortType
import app.shosetsu.android.common.ext.launchIO
import app.shosetsu.android.common.ext.logE
import app.shosetsu.android.common.utils.copy
import app.shosetsu.android.domain.model.local.LibraryFilterState
import app.shosetsu.android.domain.usecases.IsOnlineUseCase
import app.shosetsu.android.domain.usecases.SetNovelsCategoriesUseCase
import app.shosetsu.android.domain.usecases.ToggleNovelPinUseCase
import app.shosetsu.android.domain.usecases.load.*
import app.shosetsu.android.domain.usecases.settings.SetNovelUITypeUseCase
import app.shosetsu.android.domain.usecases.start.StartUpdateWorkerUseCase
import app.shosetsu.android.domain.usecases.update.UpdateBookmarkedNovelUseCase
import app.shosetsu.android.domain.usecases.update.UpdateLibraryFilterStateUseCase
import app.shosetsu.android.view.uimodels.model.CategoryUI
import app.shosetsu.android.view.uimodels.model.LibraryNovelUI
import app.shosetsu.android.view.uimodels.model.LibraryUI
Expand Down Expand Up @@ -63,7 +65,9 @@ class LibraryViewModel(
private val loadNovelUIBadgeToast: LoadNovelUIBadgeToastUseCase,
private val setNovelUITypeUseCase: SetNovelUITypeUseCase,
private val setNovelsCategoriesUseCase: SetNovelsCategoriesUseCase,
private val toggleNovelPin: ToggleNovelPinUseCase
private val toggleNovelPin: ToggleNovelPinUseCase,
private val loadLibraryFilterSettings: LoadLibraryFilterSettingsUseCase,
private val _updateLibraryFilterState: UpdateLibraryFilterStateUseCase
) : ALibraryViewModel() {

private val selectedNovels = MutableStateFlow<Map<Int, Map<Int, Boolean>>>(emptyMap())
Expand Down Expand Up @@ -189,47 +193,54 @@ class LibraryViewModel(
.stateIn(viewModelScopeIO, SharingStarted.Lazily, NovelCardType.NORMAL)
}

private val novelSortTypeFlow: MutableStateFlow<NovelSortType> by lazy {
MutableStateFlow(
NovelSortType.BY_TITLE
)
private val libraryMemory: StateFlow<LibraryFilterState> by lazy {
loadLibraryFilterSettings()
.stateIn(viewModelScopeIO, SharingStarted.Lazily, LibraryFilterState())
}
private val areNovelsReversedFlow: MutableStateFlow<Boolean> by lazy {
MutableStateFlow(
false
)

private val novelSortTypeFlow: StateFlow<NovelSortType> by lazy {
libraryMemory.map { it.sortType }
.stateIn(viewModelScopeIO, SharingStarted.Lazily, NovelSortType.BY_TITLE)
}
private val genreFilterFlow: MutableStateFlow<Map<String, InclusionState>> by lazy {
MutableStateFlow(
hashMapOf()
)

private val areNovelsReversedFlow: StateFlow<Boolean> by lazy {
libraryMemory.map { it.reversedSort }
.stateIn(viewModelScopeIO, SharingStarted.Lazily, false)
}
private val authorFilterFlow: MutableStateFlow<Map<String, InclusionState>> by lazy {
MutableStateFlow(
hashMapOf()
)

private val genreFilterFlow: StateFlow<Map<String, InclusionState>> by lazy {
libraryMemory.map { it.genreFilter }
.stateIn(viewModelScopeIO, SharingStarted.Lazily, hashMapOf())
}
private val artistFilterFlow: MutableStateFlow<Map<String, InclusionState>> by lazy {
MutableStateFlow(
hashMapOf()
)

private val authorFilterFlow: StateFlow<Map<String, InclusionState>> by lazy {
libraryMemory.map { it.authorFilter }
.stateIn(viewModelScopeIO, SharingStarted.Lazily, hashMapOf())
}
private val tagFilterFlow: MutableStateFlow<Map<String, InclusionState>> by lazy {
MutableStateFlow(
hashMapOf()
)

private val artistFilterFlow: StateFlow<Map<String, InclusionState>> by lazy {
libraryMemory.map { it.artistFilter }
.stateIn(viewModelScopeIO, SharingStarted.Lazily, hashMapOf())
}
private val unreadStatusFlow: MutableStateFlow<InclusionState?> by lazy {
MutableStateFlow(null)

private val tagFilterFlow: StateFlow<Map<String, InclusionState>> by lazy {
libraryMemory.map { it.tagFilter }
.stateIn(viewModelScopeIO, SharingStarted.Lazily, hashMapOf())
}
private val arePinsOnTop: MutableStateFlow<Boolean> by lazy {
MutableStateFlow(
true
)

private val unreadStatusFlow: StateFlow<InclusionState?> by lazy {
libraryMemory.map { it.unreadInclusion }
.stateIn(viewModelScopeIO, SharingStarted.Lazily, null)
}

private val arePinsOnTop: StateFlow<Boolean> by lazy {
libraryMemory.map { it.arePinsOnTop }
.stateIn(viewModelScopeIO, SharingStarted.Lazily, true)
}

private val downloadedFlow: MutableStateFlow<InclusionState?> by lazy {
MutableStateFlow(null)
private val downloadedFlow: StateFlow<InclusionState?> by lazy {
libraryMemory.map { it.downloadedOnly }
.stateIn(viewModelScopeIO, SharingStarted.Lazily, null)
}

/**
Expand Down Expand Up @@ -516,23 +527,44 @@ class LibraryViewModel(
}
}

@Synchronized
private fun updateLibraryFilterState(mem: LibraryFilterState) {
launchIO {
_updateLibraryFilterState(
mem
)
}
}

override fun getSortType(): Flow<NovelSortType> = novelSortTypeFlow

override fun setSortType(novelSortType: NovelSortType) {
novelSortTypeFlow.value = novelSortType
areNovelsReversedFlow.value = false
updateLibraryFilterState(
libraryMemory.value.copy(
sortType = novelSortType,
reversedSort = false
)
)
}

override fun isSortReversed(): Flow<Boolean> = areNovelsReversedFlow

override fun setIsSortReversed(reversed: Boolean) {
areNovelsReversedFlow.value = reversed
updateLibraryFilterState(
libraryMemory.value.copy(
reversedSort = reversed
)
)
}

override fun isPinnedOnTop(): Flow<Boolean> = arePinsOnTop

override fun setPinnedOnTop(onTop: Boolean) {
arePinsOnTop.value = onTop
updateLibraryFilterState(
libraryMemory.value.copy(
arePinsOnTop = onTop
)
)
}

override fun cycleFilterGenreState(genre: String, currentState: ToggleableState) {
Expand All @@ -541,7 +573,11 @@ class LibraryViewModel(
currentState.toInclusionState().cycle()?.let {
map[genre] = it
} ?: map.remove(genre)
genreFilterFlow.value = map
_updateLibraryFilterState(
libraryMemory.value.copy(
genreFilter = map
)
)
}
}

Expand All @@ -555,7 +591,11 @@ class LibraryViewModel(
currentState.toInclusionState().cycle()?.let {
map[author] = it
} ?: map.remove(author)
authorFilterFlow.value = map
_updateLibraryFilterState(
libraryMemory.value.copy(
authorFilter = map
)
)
}
}

Expand All @@ -569,7 +609,11 @@ class LibraryViewModel(
currentState.toInclusionState().cycle()?.let {
map[artist] = it
} ?: map.remove(artist)
artistFilterFlow.value = map
_updateLibraryFilterState(
libraryMemory.value.copy(
artistFilter = map
)
)
}
}

Expand All @@ -583,7 +627,11 @@ class LibraryViewModel(
currentState.toInclusionState().cycle()?.let {
map[tag] = it
} ?: map.remove(tag)
tagFilterFlow.value = map
_updateLibraryFilterState(
libraryMemory.value.copy(
tagFilter = map
)
)
}
}

Expand All @@ -592,13 +640,7 @@ class LibraryViewModel(
}

override fun resetSortAndFilters() {
genreFilterFlow.value = hashMapOf()
tagFilterFlow.value = hashMapOf()
authorFilterFlow.value = hashMapOf()
artistFilterFlow.value = hashMapOf()

novelSortTypeFlow.value = NovelSortType.BY_TITLE
areNovelsReversedFlow.value = false
updateLibraryFilterState(LibraryFilterState())
}

override fun setViewType(cardType: NovelCardType) {
Expand All @@ -613,14 +655,22 @@ class LibraryViewModel(
}

override fun cycleUnreadFilter(currentState: ToggleableState) {
unreadStatusFlow.value = currentState.toInclusionState().cycle()
updateLibraryFilterState(
libraryMemory.value.copy(
unreadInclusion = currentState.toInclusionState().cycle()
)
)
}

override fun getUnreadFilter(): Flow<ToggleableState> =
unreadStatusFlow.map { it.toToggleableState() }

override fun cycleDownloadedFilter(currentState: ToggleableState) {
downloadedFlow.value = currentState.toInclusionState().cycle()
updateLibraryFilterState(
libraryMemory.value.copy(
downloadedOnly = currentState.toInclusionState().cycle()
)
)
}

override fun getDownloadedFilter(): Flow<ToggleableState> =
Expand Down

0 comments on commit d12fc57

Please sign in to comment.