Shelf settings
parent
0e5221fa6e
commit
38d4274ece
@ -1,51 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.shelf.ui.config
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
@ -1,42 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.shelf.ui.config
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,53 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.shelf.ui.config
|
|
||||||
|
|
||||||
import android.os.Bundle
|
|
||||||
import android.view.LayoutInflater
|
|
||||||
import android.view.View
|
|
||||||
import android.view.ViewGroup
|
|
||||||
import androidx.core.view.isVisible
|
|
||||||
import androidx.fragment.app.FragmentManager
|
|
||||||
import androidx.fragment.app.viewModels
|
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
|
||||||
import org.koitharu.kotatsu.R
|
|
||||||
import org.koitharu.kotatsu.base.ui.BaseBottomSheet
|
|
||||||
import org.koitharu.kotatsu.base.ui.list.OnListItemClickListener
|
|
||||||
import org.koitharu.kotatsu.databinding.SheetBaseBinding
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
|
||||||
class ShelfConfigSheet :
|
|
||||||
BaseBottomSheet<SheetBaseBinding>(),
|
|
||||||
OnListItemClickListener<ShelfConfigModel>,
|
|
||||||
View.OnClickListener {
|
|
||||||
|
|
||||||
private val viewModel by viewModels<ShelfConfigViewModel>()
|
|
||||||
|
|
||||||
override fun onInflateView(inflater: LayoutInflater, container: ViewGroup?): SheetBaseBinding {
|
|
||||||
return SheetBaseBinding.inflate(inflater, container, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
||||||
super.onViewCreated(view, savedInstanceState)
|
|
||||||
binding.headerBar.setTitle(R.string.settings)
|
|
||||||
binding.buttonDone.isVisible = true
|
|
||||||
binding.buttonDone.setOnClickListener(this)
|
|
||||||
val adapter = ShelfConfigAdapter(this)
|
|
||||||
binding.recyclerView.adapter = adapter
|
|
||||||
|
|
||||||
viewModel.content.observe(viewLifecycleOwner) { adapter.items = it }
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onItemClick(item: ShelfConfigModel, view: View) {
|
|
||||||
viewModel.toggleItem(item)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onClick(v: View?) {
|
|
||||||
dismiss()
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
|
|
||||||
private const val TAG = "ShelfCategoriesConfigSheet"
|
|
||||||
|
|
||||||
fun show(fm: FragmentManager) = ShelfConfigSheet().show(fm, TAG)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,74 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.shelf.ui.config
|
|
||||||
|
|
||||||
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 -> {
|
|
||||||
val sections = settings.shelfSections
|
|
||||||
settings.shelfSections = if (item.isChecked) {
|
|
||||||
if (sections.size > 1) {
|
|
||||||
sections - item.section
|
|
||||||
} else {
|
|
||||||
return@launchJob
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
sections + 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,101 @@
|
|||||||
|
package org.koitharu.kotatsu.shelf.ui.config
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import androidx.activity.viewModels
|
||||||
|
import androidx.core.graphics.Insets
|
||||||
|
import androidx.core.view.updateLayoutParams
|
||||||
|
import androidx.core.view.updatePadding
|
||||||
|
import androidx.recyclerview.widget.ItemTouchHelper
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import org.koitharu.kotatsu.base.ui.BaseActivity
|
||||||
|
import org.koitharu.kotatsu.databinding.ActivityShelfSettingsBinding
|
||||||
|
|
||||||
|
@AndroidEntryPoint
|
||||||
|
class ShelfSettingsActivity :
|
||||||
|
BaseActivity<ActivityShelfSettingsBinding>(),
|
||||||
|
View.OnClickListener, ShelfSettingsListener {
|
||||||
|
|
||||||
|
private val viewModel by viewModels<ShelfSettingsViewModel>()
|
||||||
|
private lateinit var reorderHelper: ItemTouchHelper
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
setContentView(ActivityShelfSettingsBinding.inflate(layoutInflater))
|
||||||
|
supportActionBar?.run {
|
||||||
|
setDisplayHomeAsUpEnabled(true)
|
||||||
|
setHomeAsUpIndicator(com.google.android.material.R.drawable.abc_ic_clear_material)
|
||||||
|
}
|
||||||
|
binding.buttonDone.setOnClickListener(this)
|
||||||
|
val settingsAdapter = ShelfSettingsAdapter(this)
|
||||||
|
with(binding.recyclerView) {
|
||||||
|
setHasFixedSize(true)
|
||||||
|
adapter = settingsAdapter
|
||||||
|
reorderHelper = ItemTouchHelper(SectionsReorderCallback()).also {
|
||||||
|
it.attachToRecyclerView(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
viewModel.content.observe(this) { settingsAdapter.items = it }
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onItemCheckedChanged(item: ShelfSettingsItemModel, isChecked: Boolean) {
|
||||||
|
viewModel.setItemChecked(item, isChecked)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDragHandleTouch(holder: RecyclerView.ViewHolder) {
|
||||||
|
reorderHelper.startDrag(holder)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onClick(v: View?) {
|
||||||
|
finishAfterTransition()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onWindowInsetsChanged(insets: Insets) {
|
||||||
|
binding.root.updatePadding(
|
||||||
|
left = insets.left,
|
||||||
|
right = insets.right,
|
||||||
|
)
|
||||||
|
binding.recyclerView.updatePadding(
|
||||||
|
bottom = insets.bottom,
|
||||||
|
)
|
||||||
|
binding.toolbar.updateLayoutParams<ViewGroup.MarginLayoutParams> {
|
||||||
|
topMargin = insets.top
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private inner class SectionsReorderCallback : ItemTouchHelper.SimpleCallback(
|
||||||
|
ItemTouchHelper.DOWN or ItemTouchHelper.UP,
|
||||||
|
0,
|
||||||
|
) {
|
||||||
|
|
||||||
|
override fun onMove(
|
||||||
|
recyclerView: RecyclerView,
|
||||||
|
viewHolder: RecyclerView.ViewHolder,
|
||||||
|
target: RecyclerView.ViewHolder,
|
||||||
|
): Boolean = viewHolder.itemViewType == target.itemViewType && viewModel.reorderSections(
|
||||||
|
viewHolder.bindingAdapterPosition,
|
||||||
|
target.bindingAdapterPosition,
|
||||||
|
)
|
||||||
|
|
||||||
|
override fun canDropOver(
|
||||||
|
recyclerView: RecyclerView,
|
||||||
|
current: RecyclerView.ViewHolder,
|
||||||
|
target: RecyclerView.ViewHolder,
|
||||||
|
): Boolean = current.itemViewType == target.itemViewType
|
||||||
|
|
||||||
|
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) = Unit
|
||||||
|
|
||||||
|
override fun isLongPressDragEnabled() = false
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
fun newIntent(context: Context) = Intent(context, ShelfSettingsActivity::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,41 @@
|
|||||||
|
package org.koitharu.kotatsu.shelf.ui.config
|
||||||
|
|
||||||
|
import androidx.recyclerview.widget.DiffUtil
|
||||||
|
import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter
|
||||||
|
|
||||||
|
class ShelfSettingsAdapter(
|
||||||
|
listener: ShelfSettingsListener,
|
||||||
|
) : AsyncListDifferDelegationAdapter<ShelfSettingsItemModel>(DiffCallback()) {
|
||||||
|
|
||||||
|
init {
|
||||||
|
delegatesManager.addDelegate(shelfCategoryAD(listener))
|
||||||
|
.addDelegate(shelfSectionAD(listener))
|
||||||
|
}
|
||||||
|
|
||||||
|
class DiffCallback : DiffUtil.ItemCallback<ShelfSettingsItemModel>() {
|
||||||
|
|
||||||
|
override fun areItemsTheSame(oldItem: ShelfSettingsItemModel, newItem: ShelfSettingsItemModel): Boolean {
|
||||||
|
return when {
|
||||||
|
oldItem is ShelfSettingsItemModel.Section && newItem is ShelfSettingsItemModel.Section -> {
|
||||||
|
oldItem.section == newItem.section
|
||||||
|
}
|
||||||
|
|
||||||
|
oldItem is ShelfSettingsItemModel.FavouriteCategory && newItem is ShelfSettingsItemModel.FavouriteCategory -> {
|
||||||
|
oldItem.id == newItem.id
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun areContentsTheSame(oldItem: ShelfSettingsItemModel, newItem: ShelfSettingsItemModel): Boolean {
|
||||||
|
return oldItem == newItem
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getChangePayload(oldItem: ShelfSettingsItemModel, newItem: ShelfSettingsItemModel): Any? {
|
||||||
|
return if (oldItem.isChecked == newItem.isChecked) {
|
||||||
|
super.getChangePayload(oldItem, newItem)
|
||||||
|
} else Unit
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,75 @@
|
|||||||
|
package org.koitharu.kotatsu.shelf.ui.config
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
|
import android.view.MotionEvent
|
||||||
|
import android.view.View
|
||||||
|
import android.widget.CompoundButton
|
||||||
|
import androidx.core.view.updatePaddingRelative
|
||||||
|
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.databinding.ItemCategoryCheckableMultipleBinding
|
||||||
|
import org.koitharu.kotatsu.databinding.ItemShelfSectionDraggableBinding
|
||||||
|
import org.koitharu.kotatsu.shelf.domain.ShelfSection
|
||||||
|
|
||||||
|
@SuppressLint("ClickableViewAccessibility")
|
||||||
|
fun shelfSectionAD(
|
||||||
|
listener: ShelfSettingsListener,
|
||||||
|
) =
|
||||||
|
adapterDelegateViewBinding<ShelfSettingsItemModel.Section, ShelfSettingsItemModel, ItemShelfSectionDraggableBinding>(
|
||||||
|
{ layoutInflater, parent -> ItemShelfSectionDraggableBinding.inflate(layoutInflater, parent, false) },
|
||||||
|
) {
|
||||||
|
|
||||||
|
val eventListener = object :
|
||||||
|
View.OnTouchListener,
|
||||||
|
CompoundButton.OnCheckedChangeListener {
|
||||||
|
|
||||||
|
override fun onTouch(v: View?, event: MotionEvent): Boolean {
|
||||||
|
return if (event.actionMasked == MotionEvent.ACTION_DOWN) {
|
||||||
|
listener.onDragHandleTouch(this@adapterDelegateViewBinding)
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
|
||||||
|
listener.onItemCheckedChanged(item, isChecked)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
binding.switchToggle.setOnCheckedChangeListener(eventListener)
|
||||||
|
binding.imageViewHandle.setOnTouchListener(eventListener)
|
||||||
|
|
||||||
|
bind {
|
||||||
|
binding.textViewTitle.setText(item.section.titleResId)
|
||||||
|
binding.switchToggle.isChecked = item.isChecked
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun shelfCategoryAD(
|
||||||
|
listener: ShelfSettingsListener,
|
||||||
|
) =
|
||||||
|
adapterDelegateViewBinding<ShelfSettingsItemModel.FavouriteCategory, ShelfSettingsItemModel, ItemCategoryCheckableMultipleBinding>(
|
||||||
|
{ layoutInflater, parent -> ItemCategoryCheckableMultipleBinding.inflate(layoutInflater, parent, false) },
|
||||||
|
) {
|
||||||
|
itemView.setOnClickListener {
|
||||||
|
listener.onItemCheckedChanged(item, !item.isChecked)
|
||||||
|
}
|
||||||
|
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,10 @@
|
|||||||
|
package org.koitharu.kotatsu.shelf.ui.config
|
||||||
|
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
|
||||||
|
interface ShelfSettingsListener {
|
||||||
|
|
||||||
|
fun onItemCheckedChanged(item: ShelfSettingsItemModel, isChecked: Boolean)
|
||||||
|
|
||||||
|
fun onDragHandleTouch(holder: RecyclerView.ViewHolder)
|
||||||
|
}
|
||||||
@ -0,0 +1,101 @@
|
|||||||
|
package org.koitharu.kotatsu.shelf.ui.config
|
||||||
|
|
||||||
|
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 org.koitharu.kotatsu.utils.ext.move
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@HiltViewModel
|
||||||
|
class ShelfSettingsViewModel @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 setItemChecked(item: ShelfSettingsItemModel, isChecked: Boolean) {
|
||||||
|
val prevJob = updateJob
|
||||||
|
updateJob = launchJob(Dispatchers.Default) {
|
||||||
|
prevJob?.join()
|
||||||
|
when (item) {
|
||||||
|
is ShelfSettingsItemModel.FavouriteCategory -> {
|
||||||
|
favouritesRepository.updateCategory(item.id, isChecked)
|
||||||
|
}
|
||||||
|
|
||||||
|
is ShelfSettingsItemModel.Section -> {
|
||||||
|
val sections = settings.shelfSections
|
||||||
|
settings.shelfSections = if (isChecked) {
|
||||||
|
sections + item.section
|
||||||
|
} else {
|
||||||
|
if (sections.size > 1) {
|
||||||
|
sections - item.section
|
||||||
|
} else {
|
||||||
|
return@launchJob
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun reorderSections(oldPos: Int, newPos: Int): Boolean {
|
||||||
|
val snapshot = content.value?.toMutableList() ?: return false
|
||||||
|
snapshot.move(oldPos, newPos)
|
||||||
|
settings.shelfSections = snapshot.sections()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun buildList(
|
||||||
|
sections: List<ShelfSection>,
|
||||||
|
categories: List<FavouriteCategory>
|
||||||
|
): List<ShelfSettingsItemModel> {
|
||||||
|
val result = ArrayList<ShelfSettingsItemModel>()
|
||||||
|
val sectionsList = ShelfSection.values().toMutableList()
|
||||||
|
for (section in sections) {
|
||||||
|
sectionsList.remove(section)
|
||||||
|
result.addSection(section, true, categories)
|
||||||
|
}
|
||||||
|
for (section in sectionsList) {
|
||||||
|
result.addSection(section, false, categories)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun MutableList<in ShelfSettingsItemModel>.addSection(
|
||||||
|
section: ShelfSection,
|
||||||
|
isEnabled: Boolean,
|
||||||
|
favouriteCategories: List<FavouriteCategory>,
|
||||||
|
) {
|
||||||
|
add(ShelfSettingsItemModel.Section(section, isEnabled))
|
||||||
|
if (isEnabled && section == ShelfSection.FAVORITES) {
|
||||||
|
favouriteCategories.mapTo(this) {
|
||||||
|
ShelfSettingsItemModel.FavouriteCategory(
|
||||||
|
id = it.id,
|
||||||
|
title = it.title,
|
||||||
|
isChecked = it.isVisibleInLibrary,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun List<ShelfSettingsItemModel>.sections(): List<ShelfSection> {
|
||||||
|
return mapNotNull { (it as? ShelfSettingsItemModel.Section)?.takeIf { x -> x.isChecked }?.section }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,36 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<com.google.android.material.appbar.MaterialToolbar
|
||||||
|
android:id="@+id/toolbar"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="?attr/actionBarSize"
|
||||||
|
tools:navigationIcon="@drawable/abc_ic_clear_material"
|
||||||
|
tools:title="@string/settings">
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/button_done"
|
||||||
|
style="@style/Widget.Material3.Button.UnelevatedButton"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="end"
|
||||||
|
android:layout_marginHorizontal="@dimen/toolbar_button_margin"
|
||||||
|
android:text="@string/done" />
|
||||||
|
|
||||||
|
</com.google.android.material.appbar.MaterialToolbar>
|
||||||
|
|
||||||
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
|
android:id="@+id/recyclerView"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:clipToPadding="false"
|
||||||
|
android:overScrollMode="ifContentScrolls"
|
||||||
|
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
@ -0,0 +1,36 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="?android:windowBackground"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
android:minHeight="58dp"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/imageView_handle"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:paddingHorizontal="?listPreferredItemPaddingStart"
|
||||||
|
android:scaleType="center"
|
||||||
|
android:src="@drawable/ic_reorder_handle" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/textView_title"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:ellipsize="end"
|
||||||
|
android:singleLine="true"
|
||||||
|
android:textAppearance="?attr/textAppearanceBodyLarge"
|
||||||
|
tools:text="@tools:sample/lorem[15]" />
|
||||||
|
|
||||||
|
<com.google.android.material.materialswitch.MaterialSwitch
|
||||||
|
android:id="@+id/switch_toggle"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingHorizontal="?listPreferredItemPaddingEnd" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
Loading…
Reference in New Issue