Control library badge visibility with props instead

Fixes #8174
This commit is contained in:
arkon 2022-10-16 11:30:54 -04:00
parent 27e5256305
commit 6635dd2990
11 changed files with 183 additions and 149 deletions

View File

@ -103,6 +103,10 @@ fun LibraryScreen(
getDisplayModeForPage = { presenter.categories[it].display }, getDisplayModeForPage = { presenter.categories[it].display },
getColumnsForOrientation = { presenter.getColumnsPreferenceForCurrentOrientation(it) }, getColumnsForOrientation = { presenter.getColumnsPreferenceForCurrentOrientation(it) },
getLibraryForPage = { presenter.getMangaForCategory(page = it) }, getLibraryForPage = { presenter.getMangaForCategory(page = it) },
showDownloadBadges = presenter.showDownloadBadges,
showUnreadBadges = presenter.showUnreadBadges,
showLocalBadges = presenter.showLocalBadges,
showLanguageBadges = presenter.showLanguageBadges,
isIncognitoMode = presenter.isIncognitoMode, isIncognitoMode = presenter.isIncognitoMode,
isDownloadOnly = presenter.isDownloadOnly, isDownloadOnly = presenter.isDownloadOnly,
) )

View File

@ -0,0 +1,53 @@
package eu.kanade.presentation.library.components
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import eu.kanade.presentation.components.Badge
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.ui.library.LibraryItem
@Composable
fun DownloadsBadge(
enabled: Boolean,
item: LibraryItem,
) {
if (enabled && item.downloadCount > 0) {
Badge(
text = "${item.downloadCount}",
color = MaterialTheme.colorScheme.tertiary,
textColor = MaterialTheme.colorScheme.onTertiary,
)
}
}
@Composable
fun UnreadBadge(
enabled: Boolean,
item: LibraryItem,
) {
if (enabled && item.unreadCount > 0) {
Badge(text = "${item.unreadCount}")
}
}
@Composable
fun LanguageBadge(
showLanguage: Boolean,
showLocal: Boolean,
item: LibraryItem,
) {
if (showLocal && item.isLocal) {
Badge(
text = stringResource(R.string.local_source_badge),
color = MaterialTheme.colorScheme.tertiary,
textColor = MaterialTheme.colorScheme.onTertiary,
)
} else if (showLanguage && item.sourceLanguage.isNotEmpty()) {
Badge(
text = item.sourceLanguage.uppercase(),
color = MaterialTheme.colorScheme.tertiary,
textColor = MaterialTheme.colorScheme.onTertiary,
)
}
}

View File

@ -20,6 +20,10 @@ import eu.kanade.tachiyomi.ui.library.LibraryItem
@Composable @Composable
fun LibraryComfortableGrid( fun LibraryComfortableGrid(
items: List<LibraryItem>, items: List<LibraryItem>,
showDownloadBadges: Boolean,
showUnreadBadges: Boolean,
showLocalBadges: Boolean,
showLanguageBadges: Boolean,
columns: Int, columns: Int,
contentPadding: PaddingValues, contentPadding: PaddingValues,
selection: List<LibraryManga>, selection: List<LibraryManga>,
@ -40,10 +44,14 @@ fun LibraryComfortableGrid(
contentType = { "library_comfortable_grid_item" }, contentType = { "library_comfortable_grid_item" },
) { libraryItem -> ) { libraryItem ->
LibraryComfortableGridItem( LibraryComfortableGridItem(
libraryItem, item = libraryItem,
libraryItem.libraryManga in selection, showDownloadBadge = showDownloadBadges,
onClick, showUnreadBadge = showUnreadBadges,
onLongClick, showLocalBadge = showLocalBadges,
showLanguageBadge = showLanguageBadges,
isSelected = libraryItem.libraryManga in selection,
onClick = onClick,
onLongClick = onLongClick,
) )
} }
} }
@ -52,6 +60,10 @@ fun LibraryComfortableGrid(
@Composable @Composable
fun LibraryComfortableGridItem( fun LibraryComfortableGridItem(
item: LibraryItem, item: LibraryItem,
showDownloadBadge: Boolean,
showUnreadBadge: Boolean,
showLocalBadge: Boolean,
showLanguageBadge: Boolean,
isSelected: Boolean, isSelected: Boolean,
onClick: (LibraryManga) -> Unit, onClick: (LibraryManga) -> Unit,
onLongClick: (LibraryManga) -> Unit, onLongClick: (LibraryManga) -> Unit,
@ -78,10 +90,11 @@ fun LibraryComfortableGridItem(
manga.thumbnailUrl, manga.thumbnailUrl,
manga.coverLastModified, manga.coverLastModified,
), ),
downloadCount = item.downloadCount, item = item,
unreadCount = item.unreadCount, showDownloadBadge = showDownloadBadge,
isLocal = item.isLocal, showUnreadBadge = showUnreadBadge,
language = item.sourceLanguage, showLocalBadge = showLocalBadge,
showLanguageBadge = showLanguageBadge,
) )
MangaGridComfortableText( MangaGridComfortableText(
text = manga.title, text = manga.title,

View File

@ -29,6 +29,10 @@ import eu.kanade.tachiyomi.ui.library.LibraryItem
@Composable @Composable
fun LibraryCompactGrid( fun LibraryCompactGrid(
items: List<LibraryItem>, items: List<LibraryItem>,
showDownloadBadges: Boolean,
showUnreadBadges: Boolean,
showLocalBadges: Boolean,
showLanguageBadges: Boolean,
columns: Int, columns: Int,
contentPadding: PaddingValues, contentPadding: PaddingValues,
selection: List<LibraryManga>, selection: List<LibraryManga>,
@ -50,6 +54,10 @@ fun LibraryCompactGrid(
) { libraryItem -> ) { libraryItem ->
LibraryCompactGridItem( LibraryCompactGridItem(
item = libraryItem, item = libraryItem,
showDownloadBadge = showDownloadBadges,
showUnreadBadge = showUnreadBadges,
showLocalBadge = showLocalBadges,
showLanguageBadge = showLanguageBadges,
isSelected = libraryItem.libraryManga in selection, isSelected = libraryItem.libraryManga in selection,
onClick = onClick, onClick = onClick,
onLongClick = onLongClick, onLongClick = onLongClick,
@ -61,6 +69,10 @@ fun LibraryCompactGrid(
@Composable @Composable
fun LibraryCompactGridItem( fun LibraryCompactGridItem(
item: LibraryItem, item: LibraryItem,
showDownloadBadge: Boolean,
showUnreadBadge: Boolean,
showLocalBadge: Boolean,
showLanguageBadge: Boolean,
isSelected: Boolean, isSelected: Boolean,
onClick: (LibraryManga) -> Unit, onClick: (LibraryManga) -> Unit,
onLongClick: (LibraryManga) -> Unit, onLongClick: (LibraryManga) -> Unit,
@ -85,10 +97,11 @@ fun LibraryCompactGridItem(
manga.thumbnailUrl, manga.thumbnailUrl,
manga.coverLastModified, manga.coverLastModified,
), ),
downloadCount = item.downloadCount, item = item,
unreadCount = item.unreadCount, showDownloadBadge = showDownloadBadge,
isLocal = item.isLocal, showUnreadBadge = showUnreadBadge,
language = item.sourceLanguage, showLocalBadge = showLocalBadge,
showLanguageBadge = showLanguageBadge,
) { ) {
Box( Box(
modifier = Modifier modifier = Modifier

View File

@ -22,7 +22,6 @@ import eu.kanade.domain.library.model.LibraryDisplayMode
import eu.kanade.domain.library.model.LibraryManga import eu.kanade.domain.library.model.LibraryManga
import eu.kanade.presentation.components.SwipeRefresh import eu.kanade.presentation.components.SwipeRefresh
import eu.kanade.presentation.library.LibraryState import eu.kanade.presentation.library.LibraryState
import eu.kanade.presentation.util.plus
import eu.kanade.tachiyomi.ui.library.LibraryItem import eu.kanade.tachiyomi.ui.library.LibraryItem
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -33,8 +32,6 @@ fun LibraryContent(
contentPadding: PaddingValues, contentPadding: PaddingValues,
currentPage: () -> Int, currentPage: () -> Int,
isLibraryEmpty: Boolean, isLibraryEmpty: Boolean,
isDownloadOnly: Boolean,
isIncognitoMode: Boolean,
showPageTabs: Boolean, showPageTabs: Boolean,
showMangaCount: Boolean, showMangaCount: Boolean,
onChangeCurrentPage: (Int) -> Unit, onChangeCurrentPage: (Int) -> Unit,
@ -47,6 +44,12 @@ fun LibraryContent(
getDisplayModeForPage: @Composable (Int) -> LibraryDisplayMode, getDisplayModeForPage: @Composable (Int) -> LibraryDisplayMode,
getColumnsForOrientation: (Boolean) -> PreferenceMutableState<Int>, getColumnsForOrientation: (Boolean) -> PreferenceMutableState<Int>,
getLibraryForPage: @Composable (Int) -> List<LibraryItem>, getLibraryForPage: @Composable (Int) -> List<LibraryItem>,
showDownloadBadges: Boolean,
showUnreadBadges: Boolean,
showLocalBadges: Boolean,
showLanguageBadges: Boolean,
isDownloadOnly: Boolean,
isIncognitoMode: Boolean,
) { ) {
Column( Column(
modifier = Modifier.padding( modifier = Modifier.padding(
@ -106,6 +109,10 @@ fun LibraryContent(
getDisplayModeForPage = getDisplayModeForPage, getDisplayModeForPage = getDisplayModeForPage,
getColumnsForOrientation = getColumnsForOrientation, getColumnsForOrientation = getColumnsForOrientation,
getLibraryForPage = getLibraryForPage, getLibraryForPage = getLibraryForPage,
showDownloadBadges = showDownloadBadges,
showUnreadBadges = showUnreadBadges,
showLocalBadges = showLocalBadges,
showLanguageBadges = showLanguageBadges,
onClickManga = onClickManga, onClickManga = onClickManga,
onLongClickManga = onLongClickManga, onLongClickManga = onLongClickManga,
onGlobalSearchClicked = onGlobalSearchClicked, onGlobalSearchClicked = onGlobalSearchClicked,

View File

@ -12,6 +12,10 @@ import eu.kanade.tachiyomi.ui.library.LibraryItem
@Composable @Composable
fun LibraryCoverOnlyGrid( fun LibraryCoverOnlyGrid(
items: List<LibraryItem>, items: List<LibraryItem>,
showDownloadBadges: Boolean,
showUnreadBadges: Boolean,
showLocalBadges: Boolean,
showLanguageBadges: Boolean,
columns: Int, columns: Int,
contentPadding: PaddingValues, contentPadding: PaddingValues,
selection: List<LibraryManga>, selection: List<LibraryManga>,
@ -33,6 +37,10 @@ fun LibraryCoverOnlyGrid(
) { libraryItem -> ) { libraryItem ->
LibraryCoverOnlyGridItem( LibraryCoverOnlyGridItem(
item = libraryItem, item = libraryItem,
showDownloadBadge = showDownloadBadges,
showUnreadBadge = showUnreadBadges,
showLocalBadge = showLocalBadges,
showLanguageBadge = showLanguageBadges,
isSelected = libraryItem.libraryManga in selection, isSelected = libraryItem.libraryManga in selection,
onClick = onClick, onClick = onClick,
onLongClick = onLongClick, onLongClick = onLongClick,
@ -44,6 +52,10 @@ fun LibraryCoverOnlyGrid(
@Composable @Composable
fun LibraryCoverOnlyGridItem( fun LibraryCoverOnlyGridItem(
item: LibraryItem, item: LibraryItem,
showDownloadBadge: Boolean,
showUnreadBadge: Boolean,
showLocalBadge: Boolean,
showLanguageBadge: Boolean,
isSelected: Boolean, isSelected: Boolean,
onClick: (LibraryManga) -> Unit, onClick: (LibraryManga) -> Unit,
onLongClick: (LibraryManga) -> Unit, onLongClick: (LibraryManga) -> Unit,
@ -68,9 +80,10 @@ fun LibraryCoverOnlyGridItem(
manga.thumbnailUrl, manga.thumbnailUrl,
manga.coverLastModified, manga.coverLastModified,
), ),
downloadCount = item.downloadCount, item = item,
unreadCount = item.unreadCount, showDownloadBadge = showDownloadBadge,
isLocal = item.isLocal, showUnreadBadge = showUnreadBadge,
language = item.sourceLanguage, showLocalBadge = showLocalBadge,
showLanguageBadge = showLanguageBadge,
) )
} }

View File

@ -6,16 +6,13 @@ import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.aspectRatio import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import eu.kanade.presentation.components.Badge
import eu.kanade.presentation.components.BadgeGroup import eu.kanade.presentation.components.BadgeGroup
import eu.kanade.presentation.components.MangaCover import eu.kanade.presentation.components.MangaCover
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.ui.library.LibraryItem
@Composable @Composable
fun MangaGridCover( fun MangaGridCover(
@ -56,10 +53,11 @@ fun MangaGridCover(
fun LibraryGridCover( fun LibraryGridCover(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
mangaCover: eu.kanade.domain.manga.model.MangaCover, mangaCover: eu.kanade.domain.manga.model.MangaCover,
downloadCount: Long, item: LibraryItem,
unreadCount: Long, showDownloadBadge: Boolean,
isLocal: Boolean, showUnreadBadge: Boolean,
language: String, showLocalBadge: Boolean,
showLanguageBadge: Boolean,
content: @Composable BoxScope.() -> Unit = {}, content: @Composable BoxScope.() -> Unit = {},
) { ) {
MangaGridCover( MangaGridCover(
@ -71,31 +69,11 @@ fun LibraryGridCover(
) )
}, },
badgesStart = { badgesStart = {
if (downloadCount > 0) { DownloadsBadge(enabled = showDownloadBadge, item = item)
Badge( UnreadBadge(enabled = showUnreadBadge, item = item)
text = "$downloadCount",
color = MaterialTheme.colorScheme.tertiary,
textColor = MaterialTheme.colorScheme.onTertiary,
)
}
if (unreadCount > 0) {
Badge(text = "$unreadCount")
}
}, },
badgesEnd = { badgesEnd = {
if (isLocal) { LanguageBadge(showLanguage = showLanguageBadge, showLocal = showLocalBadge, item = item)
Badge(
text = stringResource(R.string.local_source_badge),
color = MaterialTheme.colorScheme.tertiary,
textColor = MaterialTheme.colorScheme.onTertiary,
)
} else if (language.isNotEmpty()) {
Badge(
text = language,
color = MaterialTheme.colorScheme.tertiary,
textColor = MaterialTheme.colorScheme.onTertiary,
)
}
}, },
content = content, content = content,
) )

View File

@ -21,7 +21,6 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex import androidx.compose.ui.zIndex
import eu.kanade.domain.library.model.LibraryManga import eu.kanade.domain.library.model.LibraryManga
import eu.kanade.domain.manga.model.MangaCover import eu.kanade.domain.manga.model.MangaCover
import eu.kanade.presentation.components.Badge
import eu.kanade.presentation.components.BadgeGroup import eu.kanade.presentation.components.BadgeGroup
import eu.kanade.presentation.components.FastScrollLazyColumn import eu.kanade.presentation.components.FastScrollLazyColumn
import eu.kanade.presentation.components.MangaCover.Square import eu.kanade.presentation.components.MangaCover.Square
@ -35,6 +34,10 @@ import eu.kanade.tachiyomi.ui.library.LibraryItem
@Composable @Composable
fun LibraryList( fun LibraryList(
items: List<LibraryItem>, items: List<LibraryItem>,
showDownloadBadges: Boolean,
showUnreadBadges: Boolean,
showLocalBadges: Boolean,
showLanguageBadges: Boolean,
contentPadding: PaddingValues, contentPadding: PaddingValues,
selection: List<LibraryManga>, selection: List<LibraryManga>,
onClick: (LibraryManga) -> Unit, onClick: (LibraryManga) -> Unit,
@ -63,6 +66,10 @@ fun LibraryList(
) { libraryItem -> ) { libraryItem ->
LibraryListItem( LibraryListItem(
item = libraryItem, item = libraryItem,
showDownloadBadge = showDownloadBadges,
showUnreadBadge = showUnreadBadges,
showLocalBadge = showLocalBadges,
showLanguageBadge = showLanguageBadges,
isSelected = libraryItem.libraryManga in selection, isSelected = libraryItem.libraryManga in selection,
onClick = onClick, onClick = onClick,
onLongClick = onLongClick, onLongClick = onLongClick,
@ -74,6 +81,10 @@ fun LibraryList(
@Composable @Composable
fun LibraryListItem( fun LibraryListItem(
item: LibraryItem, item: LibraryItem,
showDownloadBadge: Boolean,
showUnreadBadge: Boolean,
showLocalBadge: Boolean,
showLanguageBadge: Boolean,
isSelected: Boolean, isSelected: Boolean,
onClick: (LibraryManga) -> Unit, onClick: (LibraryManga) -> Unit,
onLongClick: (LibraryManga) -> Unit, onLongClick: (LibraryManga) -> Unit,
@ -93,30 +104,9 @@ fun LibraryListItem(
onClick = { onClick(libraryManga) }, onClick = { onClick(libraryManga) },
onLongClick = { onLongClick(libraryManga) }, onLongClick = { onLongClick(libraryManga) },
) { ) {
if (item.downloadCount > 0) { DownloadsBadge(enabled = showDownloadBadge, item = item)
Badge( UnreadBadge(enabled = showUnreadBadge, item = item)
text = "${item.downloadCount}", LanguageBadge(showLanguage = showLanguageBadge, showLocal = showLocalBadge, item = item)
color = MaterialTheme.colorScheme.tertiary,
textColor = MaterialTheme.colorScheme.onTertiary,
)
}
if (item.unreadCount > 0) {
Badge(text = "${item.unreadCount}")
}
if (item.isLocal) {
Badge(
text = stringResource(R.string.local_source_badge),
color = MaterialTheme.colorScheme.tertiary,
textColor = MaterialTheme.colorScheme.onTertiary,
)
}
if (item.isLocal.not() && item.sourceLanguage.isNotEmpty()) {
Badge(
text = item.sourceLanguage,
color = MaterialTheme.colorScheme.tertiary,
textColor = MaterialTheme.colorScheme.onTertiary,
)
}
} }
} }

View File

@ -28,6 +28,10 @@ fun LibraryPager(
getDisplayModeForPage: @Composable (Int) -> LibraryDisplayMode, getDisplayModeForPage: @Composable (Int) -> LibraryDisplayMode,
getColumnsForOrientation: (Boolean) -> PreferenceMutableState<Int>, getColumnsForOrientation: (Boolean) -> PreferenceMutableState<Int>,
getLibraryForPage: @Composable (Int) -> List<LibraryItem>, getLibraryForPage: @Composable (Int) -> List<LibraryItem>,
showDownloadBadges: Boolean,
showUnreadBadges: Boolean,
showLocalBadges: Boolean,
showLanguageBadges: Boolean,
onClickManga: (LibraryManga) -> Unit, onClickManga: (LibraryManga) -> Unit,
onLongClickManga: (LibraryManga) -> Unit, onLongClickManga: (LibraryManga) -> Unit,
) { ) {
@ -56,6 +60,10 @@ fun LibraryPager(
LibraryDisplayMode.List -> { LibraryDisplayMode.List -> {
LibraryList( LibraryList(
items = library, items = library,
showDownloadBadges = showDownloadBadges,
showUnreadBadges = showUnreadBadges,
showLocalBadges = showLocalBadges,
showLanguageBadges = showLanguageBadges,
contentPadding = contentPadding, contentPadding = contentPadding,
selection = selectedManga, selection = selectedManga,
onClick = onClickManga, onClick = onClickManga,
@ -67,6 +75,10 @@ fun LibraryPager(
LibraryDisplayMode.CompactGrid -> { LibraryDisplayMode.CompactGrid -> {
LibraryCompactGrid( LibraryCompactGrid(
items = library, items = library,
showDownloadBadges = showDownloadBadges,
showUnreadBadges = showUnreadBadges,
showLocalBadges = showLocalBadges,
showLanguageBadges = showLanguageBadges,
columns = columns, columns = columns,
contentPadding = contentPadding, contentPadding = contentPadding,
selection = selectedManga, selection = selectedManga,
@ -79,6 +91,10 @@ fun LibraryPager(
LibraryDisplayMode.ComfortableGrid -> { LibraryDisplayMode.ComfortableGrid -> {
LibraryComfortableGrid( LibraryComfortableGrid(
items = library, items = library,
showDownloadBadges = showDownloadBadges,
showUnreadBadges = showUnreadBadges,
showLocalBadges = showLocalBadges,
showLanguageBadges = showLanguageBadges,
columns = columns, columns = columns,
contentPadding = contentPadding, contentPadding = contentPadding,
selection = selectedManga, selection = selectedManga,
@ -91,6 +107,10 @@ fun LibraryPager(
LibraryDisplayMode.CoverOnlyGrid -> { LibraryDisplayMode.CoverOnlyGrid -> {
LibraryCoverOnlyGrid( LibraryCoverOnlyGrid(
items = library, items = library,
showDownloadBadges = showDownloadBadges,
showUnreadBadges = showUnreadBadges,
showLocalBadges = showLocalBadges,
showLanguageBadges = showLanguageBadges,
columns = columns, columns = columns,
contentPadding = contentPadding, contentPadding = contentPadding,
selection = selectedManga, selection = selectedManga,

View File

@ -127,9 +127,7 @@ class LibraryController(
when (group) { when (group) {
is LibrarySettingsSheet.Filter.FilterGroup -> onFilterChanged() is LibrarySettingsSheet.Filter.FilterGroup -> onFilterChanged()
is LibrarySettingsSheet.Sort.SortGroup -> onSortChanged() is LibrarySettingsSheet.Sort.SortGroup -> onSortChanged()
is LibrarySettingsSheet.Display.DisplayGroup -> {} else -> {} // Handled via different mechanisms
is LibrarySettingsSheet.Display.BadgeGroup -> onBadgeSettingChanged()
is LibrarySettingsSheet.Display.TabsGroup -> {} // onTabsSettingsChanged()
} }
} }
} }
@ -158,10 +156,6 @@ class LibraryController(
activity?.invalidateOptionsMenu() activity?.invalidateOptionsMenu()
} }
private fun onBadgeSettingChanged() {
presenter.requestBadgesUpdate()
}
private fun onSortChanged() { private fun onSortChanged() {
presenter.requestSortUpdate() presenter.requestSortUpdate()
} }

View File

@ -106,6 +106,11 @@ class LibraryPresenter(
val tabVisibility by libraryPreferences.categoryTabs().asState() val tabVisibility by libraryPreferences.categoryTabs().asState()
val mangaCountVisibility by libraryPreferences.categoryNumberOfItems().asState() val mangaCountVisibility by libraryPreferences.categoryNumberOfItems().asState()
val showDownloadBadges by libraryPreferences.downloadBadge().asState()
val showUnreadBadges by libraryPreferences.unreadBadge().asState()
val showLocalBadges by libraryPreferences.localBadge().asState()
val showLanguageBadges by libraryPreferences.languageBadge().asState()
var activeCategory: Int by libraryPreferences.lastUsedCategory().asState() var activeCategory: Int by libraryPreferences.lastUsedCategory().asState()
val isDownloadOnly: Boolean by preferences.downloadedOnly().asState() val isDownloadOnly: Boolean by preferences.downloadedOnly().asState()
@ -116,11 +121,6 @@ class LibraryPresenter(
*/ */
private val filterTriggerRelay = BehaviorRelay.create(Unit) private val filterTriggerRelay = BehaviorRelay.create(Unit)
/**
* Relay used to apply the UI update to the last emission of the library.
*/
private val badgeTriggerRelay = BehaviorRelay.create(Unit)
/** /**
* Relay used to apply the selected sorting method to the last emission of the library. * Relay used to apply the selected sorting method to the last emission of the library.
*/ */
@ -142,14 +142,11 @@ class LibraryPresenter(
* TODO: Move this to a coroutine world * TODO: Move this to a coroutine world
* - Move filter and sort to getMangaForCategory and only filter and sort the current display category instead of whole library as some has 5000+ items in the library * - Move filter and sort to getMangaForCategory and only filter and sort the current display category instead of whole library as some has 5000+ items in the library
* - Create new db view and new query to just fetch the current category save as needed to instance variable * - Create new db view and new query to just fetch the current category save as needed to instance variable
* - Fetch badges to maps and retrive as needed instead of fetching all of them at once * - Fetch badges to maps and retrieve as needed instead of fetching all of them at once
*/ */
if (librarySubscription == null || librarySubscription!!.isCancelled) { if (librarySubscription == null || librarySubscription!!.isCancelled) {
librarySubscription = presenterScope.launchIO { librarySubscription = presenterScope.launchIO {
getLibraryFlow().asObservable() getLibraryFlow().asObservable()
.combineLatest(badgeTriggerRelay.observeOn(Schedulers.io())) { lib, _ ->
lib.apply { setBadges(mangaMap) }
}
.combineLatest(getFilterObservable()) { lib, tracks -> .combineLatest(getFilterObservable()) { lib, tracks ->
lib.copy(mangaMap = applyFilters(lib.mangaMap, tracks)) lib.copy(mangaMap = applyFilters(lib.mangaMap, tracks))
} }
@ -201,7 +198,7 @@ class LibraryPresenter(
val filterFnUnread: (LibraryItem) -> Boolean = unread@{ item -> val filterFnUnread: (LibraryItem) -> Boolean = unread@{ item ->
if (filterUnread == State.IGNORE.value) return@unread true if (filterUnread == State.IGNORE.value) return@unread true
val isUnread = item.libraryManga.unreadCount != 0L val isUnread = item.libraryManga.unreadCount > 0
return@unread if (filterUnread == State.INCLUDE.value) { return@unread if (filterUnread == State.INCLUDE.value) {
isUnread isUnread
@ -283,50 +280,6 @@ class LibraryPresenter(
return map.mapValues { entry -> entry.value.filter(filterFn) } return map.mapValues { entry -> entry.value.filter(filterFn) }
} }
/**
* Sets downloaded chapter count to each manga.
*
* @param map the map of manga.
*/
private fun setBadges(map: LibraryMap) {
val showDownloadBadges = libraryPreferences.downloadBadge().get()
val showUnreadBadges = libraryPreferences.unreadBadge().get()
val showLocalBadges = libraryPreferences.localBadge().get()
val showLanguageBadges = libraryPreferences.languageBadge().get()
for ((_, itemList) in map) {
for (item in itemList) {
item.downloadCount = if (showDownloadBadges) {
downloadManager.getDownloadCount(item.libraryManga.manga).toLong()
} else {
// Unset download count if not enabled
-1
}
item.unreadCount = if (showUnreadBadges) {
item.libraryManga.unreadCount
} else {
// Unset unread count if not enabled
-1
}
item.isLocal = if (showLocalBadges) {
item.libraryManga.manga.isLocal()
} else {
// Hide / Unset local badge if not enabled
false
}
item.sourceLanguage = if (showLanguageBadges) {
sourceManager.getOrStub(item.libraryManga.manga.source).lang.uppercase()
} else {
// Unset source language if not enabled
""
}
}
}
}
/** /**
* Applies library sorting to the given map of manga. * Applies library sorting to the given map of manga.
* *
@ -434,8 +387,13 @@ class LibraryPresenter(
.map { list -> .map { list ->
list.map { libraryManga -> list.map { libraryManga ->
// Display mode based on user preference: take it from global library setting or category // Display mode based on user preference: take it from global library setting or category
LibraryItem(libraryManga) LibraryItem(libraryManga).apply {
}.groupBy { it.libraryManga.category.toLong() } downloadCount = downloadManager.getDownloadCount(libraryManga.manga).toLong()
unreadCount = libraryManga.unreadCount
isLocal = libraryManga.manga.isLocal()
sourceLanguage = sourceManager.getOrStub(libraryManga.manga.source).lang
}
}.groupBy { it.libraryManga.category }
} }
return combine(categoriesFlow, libraryMangasFlow) { dbCategories, libraryManga -> return combine(categoriesFlow, libraryMangasFlow) { dbCategories, libraryManga ->
val categories = if (libraryManga.isNotEmpty() && libraryManga.containsKey(0).not()) { val categories = if (libraryManga.isNotEmpty() && libraryManga.containsKey(0).not()) {
@ -456,7 +414,7 @@ class LibraryPresenter(
*/ */
private fun getFilterObservable(): Observable<Map<Long, Map<Long, Boolean>>> { private fun getFilterObservable(): Observable<Map<Long, Map<Long, Boolean>>> {
return filterTriggerRelay.observeOn(Schedulers.io()) return filterTriggerRelay.observeOn(Schedulers.io())
.combineLatest(getTracksObservable()) { _, tracks -> tracks } .combineLatest(getTracksFlow().asObservable().observeOn(Schedulers.io())) { _, tracks -> tracks }
} }
/** /**
@ -464,7 +422,7 @@ class LibraryPresenter(
* *
* @return an observable of tracked manga. * @return an observable of tracked manga.
*/ */
private fun getTracksObservable(): Observable<Map<Long, Map<Long, Boolean>>> { private fun getTracksFlow(): Flow<Map<Long, Map<Long, Boolean>>> {
// TODO: Move this to domain/data layer // TODO: Move this to domain/data layer
return getTracks.subscribe() return getTracks.subscribe()
.map { tracks -> .map { tracks ->
@ -477,8 +435,6 @@ class LibraryPresenter(
} }
} }
} }
.asObservable()
.observeOn(Schedulers.io())
} }
/** /**
@ -488,13 +444,6 @@ class LibraryPresenter(
filterTriggerRelay.call(Unit) filterTriggerRelay.call(Unit)
} }
/**
* Requests the library to have download badges added.
*/
fun requestBadgesUpdate() {
badgeTriggerRelay.call(Unit)
}
/** /**
* Requests the library to be sorted. * Requests the library to be sorted.
*/ */