Skip to content

Commit

Permalink
Merge branch 'feature/bulk-favorite' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
cuong-tran committed Apr 26, 2024
2 parents 5d554b3 + f6be3a4 commit 9bea7b6
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SnackbarResult
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.paging.LoadState
Expand All @@ -23,6 +24,7 @@ import eu.kanade.presentation.browse.components.BrowseSourceList
import eu.kanade.presentation.components.AppBar
import eu.kanade.presentation.util.formattedMessage
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourceScreenModel
import exh.metadata.metadata.RaisedSearchMetadata
import exh.source.isEhBasedSource
import kotlinx.collections.immutable.persistentListOf
Expand Down Expand Up @@ -60,6 +62,7 @@ fun BrowseSourceContent(
onMangaLongClick: (Manga) -> Unit,
// KMK -->
selection: List<Manga>,
browseSourceState: BrowseSourceScreenModel.State,
// KMK <--
) {
val context = LocalContext.current
Expand Down Expand Up @@ -135,9 +138,22 @@ fun BrowseSourceContent(
LoadingScreen(
modifier = Modifier.padding(contentPadding),
)
// KMK -->
browseSourceState.mangaDisplayingList.clear()
// KMK <--
return
}

// KMK -->
for (idx in browseSourceState.mangaDisplayingList.size..<mangaList.itemCount) {
mangaList[idx]?.collectAsState()?.value?.first?.let { manga ->
if (!browseSourceState.mangaDisplayingList.map { it.id }.contains(manga.id)) {
browseSourceState.mangaDisplayingList.add(manga)
}
}
}
// KMK <--

// SY -->
if (source?.isEhBasedSource() == true && ehentaiBrowseDisplayMode) {
BrowseSourceEHentaiList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ fun GlobalSearchScreen(
selectedCount = bulkFavoriteState.selection.size,
onClickClearSelection = bulkFavoriteScreenModel::toggleSelectionMode,
onChangeCategoryClicked = bulkFavoriteScreenModel::addFavorite,
onSelectAll = {
state.filteredItems.forEach { (_, result) ->
when (result) {
is SearchItemResult.Success -> {
result.result.forEach { manga ->
if (!bulkFavoriteState.selection.contains(manga))
bulkFavoriteScreenModel.select(manga)
}
}
else -> {}
}
}
},
)
} else {
// KMK <--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import eu.kanade.presentation.browse.components.GlobalSearchToolbar
import eu.kanade.presentation.components.SelectionToolbar
import eu.kanade.tachiyomi.source.CatalogueSource
import eu.kanade.tachiyomi.ui.browse.BulkFavoriteScreenModel
import eu.kanade.tachiyomi.ui.browse.source.globalsearch.SearchItemResult
import eu.kanade.tachiyomi.ui.browse.source.globalsearch.SearchScreenModel
import eu.kanade.tachiyomi.ui.browse.source.globalsearch.SourceFilter
import tachiyomi.domain.manga.model.Manga
Expand Down Expand Up @@ -42,6 +43,19 @@ fun MigrateSearchScreen(
selectedCount = bulkFavoriteState.selection.size,
onClickClearSelection = bulkFavoriteScreenModel::toggleSelectionMode,
onChangeCategoryClicked = bulkFavoriteScreenModel::addFavorite,
onSelectAll = {
state.filteredItems.forEach { (_, result) ->
when (result) {
is SearchItemResult.Success -> {
result.result.forEach { manga ->
if (!bulkFavoriteState.selection.contains(manga))
bulkFavoriteScreenModel.select(manga)
}
}
else -> {}
}
}
},
)
} else {
// KMK <--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ fun SourceFeedScreen(
selectedCount = bulkFavoriteState.selection.size,
onClickClearSelection = bulkFavoriteScreenModel::toggleSelectionMode,
onChangeCategoryClicked = bulkFavoriteScreenModel::addFavorite,
onSelectAll = {
items.forEach {
it.results?.forEach { manga ->
if (!bulkFavoriteState.selection.contains(manga))
bulkFavoriteScreenModel.select(manga)
}
}
},
)
} else {
// KMK <--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package eu.kanade.presentation.components

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.BookmarkAdd
import androidx.compose.material.icons.filled.SelectAll
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -14,22 +15,38 @@ fun SelectionToolbar(
selectedCount: Int,
onClickClearSelection: () -> Unit,
onChangeCategoryClicked: () -> Unit,
onSelectAll: (() -> Unit)? = null,
) {
AppBar(
titleContent = { Text(text = "$selectedCount") },
actions = {
AppBarActions(
persistentListOf(
AppBar.Action(
title = stringResource(MR.strings.action_bookmark),
icon = Icons.Filled.BookmarkAdd,
onClick = {
if (selectedCount > 0) {
onChangeCategoryClicked()
}
},
),
),
actions = persistentListOf<AppBar.AppBarAction>().builder()
.apply {
if (onSelectAll != null) {
add(
AppBar.Action(
title = stringResource(MR.strings.action_select_all),
icon = Icons.Filled.SelectAll,
onClick = {
onSelectAll()
},
),
)
}
add(
AppBar.Action(
title = stringResource(MR.strings.action_bookmark),
icon = Icons.Filled.BookmarkAdd,
onClick = {
if (selectedCount > 0) {
onChangeCategoryClicked()
}
},
),
)
}
.build(),
)
},
isActionMode = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.zIndex
import dev.icerock.moko.resources.StringResource
import eu.kanade.tachiyomi.ui.browse.BulkFavoriteScreenModel
import eu.kanade.tachiyomi.ui.browse.feed.FeedScreenModel
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.launch
Expand All @@ -40,6 +41,7 @@ fun TabbedScreen(
searchQuery: String? = null,
onChangeSearchQuery: (String?) -> Unit = {},
// KMK -->
feedScreenModel: FeedScreenModel,
bulkFavoriteScreenModel: BulkFavoriteScreenModel,
// KMK <--
) {
Expand All @@ -48,6 +50,7 @@ fun TabbedScreen(
val snackbarHostState = remember { SnackbarHostState() }

// KMK -->
val feedState by feedScreenModel.state.collectAsState()
val bulkFavoriteState by bulkFavoriteScreenModel.state.collectAsState()
// KMK <--

Expand All @@ -67,6 +70,14 @@ fun TabbedScreen(
selectedCount = bulkFavoriteState.selection.size,
onClickClearSelection = bulkFavoriteScreenModel::toggleSelectionMode,
onChangeCategoryClicked = bulkFavoriteScreenModel::addFavorite,
onSelectAll = {
feedState.items?.forEach {
it.results?.forEach { manga ->
if (!bulkFavoriteState.selection.contains(manga))
bulkFavoriteScreenModel.select(manga)
}
}
},
)
} else {
// KMK <--
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/eu/kanade/tachiyomi/ui/browse/BrowseTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import eu.kanade.presentation.util.Tab
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.ui.browse.extension.ExtensionsScreenModel
import eu.kanade.tachiyomi.ui.browse.extension.extensionsTab
import eu.kanade.tachiyomi.ui.browse.feed.FeedScreenModel
import eu.kanade.tachiyomi.ui.browse.feed.feedTab
import eu.kanade.tachiyomi.ui.browse.migration.sources.migrateSourceTab
import eu.kanade.tachiyomi.ui.browse.source.globalsearch.GlobalSearchScreen
Expand Down Expand Up @@ -66,6 +67,7 @@ data class BrowseTab(
val extensionsState by extensionsScreenModel.state.collectAsState()

// KMK -->
val feedScreenModel = rememberScreenModel { FeedScreenModel() }
val bulkFavoriteScreenModel = rememberScreenModel { BulkFavoriteScreenModel() }
// KMK <--

Expand All @@ -80,15 +82,15 @@ data class BrowseTab(
)
} else if (feedTabInFront) {
persistentListOf(
feedTab(bulkFavoriteScreenModel),
feedTab(/* KMK --> */feedScreenModel, bulkFavoriteScreenModel/* KMK <-- */),
sourcesTab(),
extensionsTab(extensionsScreenModel),
migrateSourceTab(),
)
} else {
persistentListOf(
sourcesTab(),
feedTab(bulkFavoriteScreenModel),
feedTab(/* KMK --> */feedScreenModel, bulkFavoriteScreenModel/* KMK <-- */),
extensionsTab(extensionsScreenModel),
migrateSourceTab(),
)
Expand All @@ -98,6 +100,7 @@ data class BrowseTab(
searchQuery = extensionsState.searchQuery,
onChangeSearchQuery = extensionsScreenModel::search,
// KMK -->
feedScreenModel = feedScreenModel,
bulkFavoriteScreenModel = bulkFavoriteScreenModel,
// KMK <--
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,23 @@ class BulkFavoriteScreenModel(
mutableState.update { it.copy(selection = persistentListOf()) }
}

fun toggleSelection(manga: Manga) {
fun select(manga: Manga) {
toggleSelection(manga, toSelectedState = true)
}

fun unselect(manga: Manga) {
toggleSelection(manga, toSelectedState = false)
}

/**
* @param toSelectedState set to true to only Select, set to false to only Unselect
*/
fun toggleSelection(manga: Manga, toSelectedState: Boolean? = null) {
mutableState.update { state ->
val newSelection = state.selection.mutate { list ->
if (list.fastAny { it.id == manga.id }) {
if (toSelectedState != true && list.fastAny { it.id == manga.id }) {
list.removeAll { it.id == manga.id }
} else {
} else if (toSelectedState != false) {
list.add(manga)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.LocalHapticFeedback
import cafe.adriel.voyager.core.model.rememberScreenModel
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.core.stack.StackEvent
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
Expand Down Expand Up @@ -39,13 +37,13 @@ import tachiyomi.i18n.sy.SYMR
import tachiyomi.presentation.core.i18n.stringResource

@Composable
fun Screen.feedTab(
fun feedTab(
// KMK -->
screenModel: FeedScreenModel,
bulkFavoriteScreenModel: BulkFavoriteScreenModel,
// KMK <--
): TabContent {
val navigator = LocalNavigator.currentOrThrow
val screenModel = rememberScreenModel { FeedScreenModel() }
val state by screenModel.state.collectAsState()

// KMK -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ data class SourceSearchScreen(
selectedCount = bulkFavoriteState.selection.size,
onClickClearSelection = bulkFavoriteScreenModel::toggleSelectionMode,
onChangeCategoryClicked = bulkFavoriteScreenModel::addFavorite,
onSelectAll = {
state.mangaDisplayingList.forEach { manga ->
if (!bulkFavoriteState.selection.contains(manga))
bulkFavoriteScreenModel.select(manga)
}
},
)
} else {
// KMK <--
Expand Down Expand Up @@ -150,6 +156,7 @@ data class SourceSearchScreen(
onMangaLongClick = { navigator.push(MangaScreen(it.id, true)) },
// KMK -->
selection = bulkFavoriteState.selection,
browseSourceState = state,
// KMK <--
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ data class BrowseSourceScreen(
selectedCount = bulkFavoriteState.selection.size,
onClickClearSelection = bulkFavoriteScreenModel::toggleSelectionMode,
onChangeCategoryClicked = bulkFavoriteScreenModel::addFavorite,
onSelectAll = {
state.mangaDisplayingList.forEach { manga ->
if (!bulkFavoriteState.selection.contains(manga))
bulkFavoriteScreenModel.select(manga)
}
},
)
} else {
// KMK <--
Expand Down Expand Up @@ -317,6 +323,7 @@ data class BrowseSourceScreen(
},
// KMK -->
selection = bulkFavoriteState.selection,
browseSourceState = state,
// KMK <--
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,9 @@ open class BrowseSourceScreenModel(
val savedSearches: ImmutableList<EXHSavedSearch> = persistentListOf(),
val filterable: Boolean = true,
// SY <--
// KMK -->
val mangaDisplayingList: MutableSet<Manga> = emptySet<Manga>().toMutableSet()
// KMK <--
) {
val isUserQuery get() = listing is Listing.Search && !listing.query.isNullOrEmpty()
}
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/exh/md/follows/MangaDexFollowsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ class MangaDexFollowsScreen(private val sourceId: Long) : Screen() {
selectedCount = bulkFavoriteState.selection.size,
onClickClearSelection = bulkFavoriteScreenModel::toggleSelectionMode,
onChangeCategoryClicked = bulkFavoriteScreenModel::addFavorite,
onSelectAll = {
state.mangaDisplayingList.forEach { manga ->
if (!bulkFavoriteState.selection.contains(manga))
bulkFavoriteScreenModel.select(manga)
}
},
)
} else {
// KMK <--
Expand Down Expand Up @@ -142,6 +148,7 @@ class MangaDexFollowsScreen(private val sourceId: Long) : Screen() {
},
// KMK -->
selection = bulkFavoriteState.selection,
browseSourceState = state,
// KMK <--
)
}
Expand Down
Loading

0 comments on commit 9bea7b6

Please sign in to comment.