Configure shelf sections
parent
cec19c3db3
commit
c663d10515
@ -1,6 +0,0 @@
|
||||
package org.koitharu.kotatsu.core.prefs
|
||||
|
||||
enum class AppSection {
|
||||
|
||||
LOCAL, FAVOURITES, HISTORY, FEED, SUGGESTIONS
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package org.koitharu.kotatsu.shelf.domain
|
||||
|
||||
import org.koitharu.kotatsu.core.model.FavouriteCategory
|
||||
import org.koitharu.kotatsu.history.domain.MangaWithHistory
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
|
||||
class ShelfContent(
|
||||
val history: List<MangaWithHistory>,
|
||||
val favourites: Map<FavouriteCategory, List<Manga>>,
|
||||
val updated: Map<Manga, Int>,
|
||||
val local: List<Manga>,
|
||||
) {
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
other as ShelfContent
|
||||
|
||||
if (history != other.history) return false
|
||||
if (favourites != other.favourites) return false
|
||||
if (updated != other.updated) return false
|
||||
if (local != other.local) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = history.hashCode()
|
||||
result = 31 * result + favourites.hashCode()
|
||||
result = 31 * result + updated.hashCode()
|
||||
result = 31 * result + local.hashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
package org.koitharu.kotatsu.shelf.domain
|
||||
|
||||
enum class ShelfSection {
|
||||
|
||||
HISTORY, LOCAL, UPDATED, FAVORITES;
|
||||
}
|
||||
@ -1,32 +0,0 @@
|
||||
package org.koitharu.kotatsu.shelf.ui.config.categories
|
||||
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter
|
||||
import org.koitharu.kotatsu.base.ui.list.OnListItemClickListener
|
||||
import org.koitharu.kotatsu.core.model.FavouriteCategory
|
||||
|
||||
class ShelfCategoriesConfigAdapter(
|
||||
listener: OnListItemClickListener<FavouriteCategory>,
|
||||
) : AsyncListDifferDelegationAdapter<FavouriteCategory>(DiffCallback()) {
|
||||
|
||||
init {
|
||||
delegatesManager.addDelegate(shelfCategoryAD(listener))
|
||||
}
|
||||
|
||||
class DiffCallback : DiffUtil.ItemCallback<FavouriteCategory>() {
|
||||
|
||||
override fun areItemsTheSame(oldItem: FavouriteCategory, newItem: FavouriteCategory): Boolean {
|
||||
return oldItem.id == newItem.id
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(oldItem: FavouriteCategory, newItem: FavouriteCategory): Boolean {
|
||||
return oldItem.isVisibleInLibrary == newItem.isVisibleInLibrary && oldItem.title == newItem.title
|
||||
}
|
||||
|
||||
override fun getChangePayload(oldItem: FavouriteCategory, newItem: FavouriteCategory): Any? {
|
||||
return if (oldItem.isVisibleInLibrary == newItem.isVisibleInLibrary) {
|
||||
super.getChangePayload(oldItem, newItem)
|
||||
} else Unit
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,30 +0,0 @@
|
||||
package org.koitharu.kotatsu.shelf.ui.config.categories
|
||||
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import org.koitharu.kotatsu.base.ui.BaseViewModel
|
||||
import org.koitharu.kotatsu.core.model.FavouriteCategory
|
||||
import org.koitharu.kotatsu.favourites.domain.FavouritesRepository
|
||||
import org.koitharu.kotatsu.utils.ext.asLiveDataDistinct
|
||||
|
||||
@HiltViewModel
|
||||
class ShelfCategoriesConfigViewModel @Inject constructor(
|
||||
private val favouritesRepository: FavouritesRepository,
|
||||
) : BaseViewModel() {
|
||||
|
||||
val content = favouritesRepository.observeCategories()
|
||||
.asLiveDataDistinct(viewModelScope.coroutineContext + Dispatchers.Default, emptyList())
|
||||
|
||||
private var updateJob: Job? = null
|
||||
|
||||
fun toggleItem(category: FavouriteCategory) {
|
||||
val prevJob = updateJob
|
||||
updateJob = launchJob(Dispatchers.Default) {
|
||||
prevJob?.join()
|
||||
favouritesRepository.updateCategory(category.id, !category.isVisibleInLibrary)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
package org.koitharu.kotatsu.shelf.ui.config.categories
|
||||
|
||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||
import org.koitharu.kotatsu.base.ui.list.AdapterDelegateClickListenerAdapter
|
||||
import org.koitharu.kotatsu.base.ui.list.OnListItemClickListener
|
||||
import org.koitharu.kotatsu.core.model.FavouriteCategory
|
||||
import org.koitharu.kotatsu.databinding.ItemCategoryCheckableMultipleBinding
|
||||
|
||||
fun shelfCategoryAD(
|
||||
listener: OnListItemClickListener<FavouriteCategory>,
|
||||
) = adapterDelegateViewBinding<FavouriteCategory, FavouriteCategory, ItemCategoryCheckableMultipleBinding>(
|
||||
{ layoutInflater, parent -> ItemCategoryCheckableMultipleBinding.inflate(layoutInflater, parent, false) },
|
||||
) {
|
||||
val eventListener = AdapterDelegateClickListenerAdapter(this, listener)
|
||||
itemView.setOnClickListener(eventListener)
|
||||
|
||||
bind {
|
||||
binding.root.text = item.title
|
||||
binding.root.isChecked = item.isVisibleInLibrary
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,51 @@
|
||||
package org.koitharu.kotatsu.shelf.ui.config.categories
|
||||
|
||||
import androidx.core.view.updatePaddingRelative
|
||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.base.ui.list.AdapterDelegateClickListenerAdapter
|
||||
import org.koitharu.kotatsu.base.ui.list.OnListItemClickListener
|
||||
import org.koitharu.kotatsu.databinding.ItemCategoryCheckableMultipleBinding
|
||||
import org.koitharu.kotatsu.shelf.domain.ShelfSection
|
||||
|
||||
fun shelfSectionAD(
|
||||
listener: OnListItemClickListener<ShelfConfigModel>,
|
||||
) = adapterDelegateViewBinding<ShelfConfigModel.Section, ShelfConfigModel, ItemCategoryCheckableMultipleBinding>(
|
||||
{ layoutInflater, parent -> ItemCategoryCheckableMultipleBinding.inflate(layoutInflater, parent, false) },
|
||||
) {
|
||||
|
||||
val eventListener = AdapterDelegateClickListenerAdapter(this, listener)
|
||||
itemView.setOnClickListener(eventListener)
|
||||
|
||||
bind {
|
||||
binding.root.setText(item.section.titleResId)
|
||||
binding.root.isChecked = item.isChecked
|
||||
}
|
||||
}
|
||||
|
||||
fun shelfCategoryAD(
|
||||
listener: OnListItemClickListener<ShelfConfigModel>,
|
||||
) =
|
||||
adapterDelegateViewBinding<ShelfConfigModel.FavouriteCategory, ShelfConfigModel, ItemCategoryCheckableMultipleBinding>(
|
||||
{ layoutInflater, parent -> ItemCategoryCheckableMultipleBinding.inflate(layoutInflater, parent, false) },
|
||||
) {
|
||||
val eventListener = AdapterDelegateClickListenerAdapter(this, listener)
|
||||
itemView.setOnClickListener(eventListener)
|
||||
binding.root.updatePaddingRelative(
|
||||
start = binding.root.paddingStart * 2,
|
||||
end = binding.root.paddingStart,
|
||||
)
|
||||
|
||||
bind {
|
||||
binding.root.text = item.title
|
||||
binding.root.isChecked = item.isChecked
|
||||
}
|
||||
}
|
||||
|
||||
private val ShelfSection.titleResId: Int
|
||||
get() = when (this) {
|
||||
ShelfSection.HISTORY -> R.string.history
|
||||
ShelfSection.LOCAL -> R.string.local_storage
|
||||
ShelfSection.UPDATED -> R.string.updated
|
||||
ShelfSection.FAVORITES -> R.string.favourites
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
package org.koitharu.kotatsu.shelf.ui.config.categories
|
||||
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter
|
||||
import org.koitharu.kotatsu.base.ui.list.OnListItemClickListener
|
||||
|
||||
class ShelfConfigAdapter(
|
||||
listener: OnListItemClickListener<ShelfConfigModel>,
|
||||
) : AsyncListDifferDelegationAdapter<ShelfConfigModel>(DiffCallback()) {
|
||||
|
||||
init {
|
||||
delegatesManager.addDelegate(shelfCategoryAD(listener))
|
||||
.addDelegate(shelfSectionAD(listener))
|
||||
}
|
||||
|
||||
class DiffCallback : DiffUtil.ItemCallback<ShelfConfigModel>() {
|
||||
|
||||
override fun areItemsTheSame(oldItem: ShelfConfigModel, newItem: ShelfConfigModel): Boolean {
|
||||
return when {
|
||||
oldItem is ShelfConfigModel.Section && newItem is ShelfConfigModel.Section -> {
|
||||
oldItem.section == newItem.section
|
||||
}
|
||||
|
||||
oldItem is ShelfConfigModel.FavouriteCategory && newItem is ShelfConfigModel.FavouriteCategory -> {
|
||||
oldItem.id == newItem.id
|
||||
}
|
||||
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(oldItem: ShelfConfigModel, newItem: ShelfConfigModel): Boolean {
|
||||
return oldItem == newItem
|
||||
}
|
||||
|
||||
override fun getChangePayload(oldItem: ShelfConfigModel, newItem: ShelfConfigModel): Any? {
|
||||
return if (oldItem.isChecked == newItem.isChecked) {
|
||||
super.getChangePayload(oldItem, newItem)
|
||||
} else Unit
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,60 @@
|
||||
package org.koitharu.kotatsu.shelf.ui.config.categories
|
||||
|
||||
import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||
import org.koitharu.kotatsu.shelf.domain.ShelfSection
|
||||
|
||||
sealed interface ShelfConfigModel : ListModel {
|
||||
|
||||
val isChecked: Boolean
|
||||
|
||||
class Section(
|
||||
val section: ShelfSection,
|
||||
override val isChecked: Boolean,
|
||||
) : ShelfConfigModel {
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
other as Section
|
||||
|
||||
if (section != other.section) return false
|
||||
if (isChecked != other.isChecked) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = section.hashCode()
|
||||
result = 31 * result + isChecked.hashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
class FavouriteCategory(
|
||||
val id: Long,
|
||||
val title: String,
|
||||
override val isChecked: Boolean,
|
||||
) : ShelfConfigModel {
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
other as FavouriteCategory
|
||||
|
||||
if (id != other.id) return false
|
||||
if (title != other.title) return false
|
||||
if (isChecked != other.isChecked) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = id.hashCode()
|
||||
result = 31 * result + title.hashCode()
|
||||
result = 31 * result + isChecked.hashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,69 @@
|
||||
package org.koitharu.kotatsu.shelf.ui.config.categories
|
||||
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import org.koitharu.kotatsu.base.ui.BaseViewModel
|
||||
import org.koitharu.kotatsu.core.model.FavouriteCategory
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.core.prefs.observeAsFlow
|
||||
import org.koitharu.kotatsu.favourites.domain.FavouritesRepository
|
||||
import org.koitharu.kotatsu.shelf.domain.ShelfSection
|
||||
import org.koitharu.kotatsu.utils.asFlowLiveData
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class ShelfConfigViewModel @Inject constructor(
|
||||
private val favouritesRepository: FavouritesRepository,
|
||||
private val settings: AppSettings,
|
||||
) : BaseViewModel() {
|
||||
|
||||
val content = combine(
|
||||
settings.observeAsFlow(AppSettings.KEY_SHELF_SECTIONS) { shelfSections },
|
||||
favouritesRepository.observeCategories(),
|
||||
) { sections, categories ->
|
||||
buildList(sections, categories)
|
||||
}.asFlowLiveData(viewModelScope.coroutineContext + Dispatchers.Default, emptyList())
|
||||
|
||||
private var updateJob: Job? = null
|
||||
|
||||
fun toggleItem(item: ShelfConfigModel) {
|
||||
val prevJob = updateJob
|
||||
updateJob = launchJob(Dispatchers.Default) {
|
||||
prevJob?.join()
|
||||
when (item) {
|
||||
is ShelfConfigModel.FavouriteCategory -> {
|
||||
favouritesRepository.updateCategory(item.id, !item.isChecked)
|
||||
}
|
||||
|
||||
is ShelfConfigModel.Section -> {
|
||||
if (item.isChecked) {
|
||||
settings.shelfSections -= item.section
|
||||
} else {
|
||||
settings.shelfSections += item.section
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildList(sections: Set<ShelfSection>, categories: List<FavouriteCategory>): List<ShelfConfigModel> {
|
||||
val result = ArrayList<ShelfConfigModel>()
|
||||
for (section in ShelfSection.values()) {
|
||||
val isEnabled = section in sections
|
||||
result.add(ShelfConfigModel.Section(section, isEnabled))
|
||||
if (section == ShelfSection.FAVORITES && isEnabled) {
|
||||
categories.mapTo(result) {
|
||||
ShelfConfigModel.FavouriteCategory(
|
||||
id = it.id,
|
||||
title = it.title,
|
||||
isChecked = it.isVisibleInLibrary,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue