Merge branch 'devel' into feature/anilist
commit
46786e32a3
@ -1,18 +1,17 @@
|
||||
package org.koitharu.kotatsu.core.model
|
||||
|
||||
import java.util.*
|
||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||
import org.koitharu.kotatsu.parsers.util.toTitleCase
|
||||
import java.util.Locale
|
||||
|
||||
fun MangaSource.getLocaleTitle(): String? {
|
||||
val lc = Locale(locale ?: return null)
|
||||
return lc.getDisplayLanguage(lc).toTitleCase(lc)
|
||||
}
|
||||
|
||||
@Suppress("FunctionName")
|
||||
fun MangaSource(name: String): MangaSource? {
|
||||
fun MangaSource(name: String): MangaSource {
|
||||
MangaSource.values().forEach {
|
||||
if (it.name == name) return it
|
||||
}
|
||||
return null
|
||||
return MangaSource.DUMMY
|
||||
}
|
||||
@ -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,34 @@
|
||||
package org.koitharu.kotatsu.utils.ext
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.ResponseBody
|
||||
import org.koitharu.kotatsu.utils.progress.ProgressResponseBody
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
suspend fun InputStream.copyToSuspending(
|
||||
out: OutputStream,
|
||||
bufferSize: Int = DEFAULT_BUFFER_SIZE
|
||||
): Long = withContext(Dispatchers.IO) {
|
||||
val job = currentCoroutineContext()[Job]
|
||||
var bytesCopied: Long = 0
|
||||
val buffer = ByteArray(bufferSize)
|
||||
var bytes = read(buffer)
|
||||
while (bytes >= 0) {
|
||||
out.write(buffer, 0, bytes)
|
||||
bytesCopied += bytes
|
||||
job?.ensureActive()
|
||||
bytes = read(buffer)
|
||||
job?.ensureActive()
|
||||
}
|
||||
bytesCopied
|
||||
}
|
||||
|
||||
fun ResponseBody.withProgress(progressState: MutableStateFlow<Float>): ResponseBody {
|
||||
return ProgressResponseBody(this, progressState)
|
||||
}
|
||||
@ -0,0 +1,51 @@
|
||||
package org.koitharu.kotatsu.utils.progress
|
||||
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import okhttp3.MediaType
|
||||
import okhttp3.ResponseBody
|
||||
import okio.Buffer
|
||||
import okio.BufferedSource
|
||||
import okio.ForwardingSource
|
||||
import okio.Source
|
||||
import okio.buffer
|
||||
|
||||
class ProgressResponseBody(
|
||||
private val delegate: ResponseBody,
|
||||
private val progressState: MutableStateFlow<Float>,
|
||||
) : ResponseBody() {
|
||||
|
||||
private var bufferedSource: BufferedSource? = null
|
||||
|
||||
override fun close() {
|
||||
super.close()
|
||||
delegate.close()
|
||||
}
|
||||
|
||||
override fun contentLength(): Long = delegate.contentLength()
|
||||
|
||||
override fun contentType(): MediaType? = delegate.contentType()
|
||||
|
||||
override fun source(): BufferedSource {
|
||||
return bufferedSource ?: ProgressSource(delegate.source(), contentLength(), progressState).buffer().also {
|
||||
bufferedSource = it
|
||||
}
|
||||
}
|
||||
|
||||
private class ProgressSource(
|
||||
delegate: Source,
|
||||
private val contentLength: Long,
|
||||
private val progressState: MutableStateFlow<Float>,
|
||||
) : ForwardingSource(delegate) {
|
||||
|
||||
private var totalBytesRead = 0L
|
||||
|
||||
override fun read(sink: Buffer, byteCount: Long): Long {
|
||||
val bytesRead = super.read(sink, byteCount)
|
||||
if (contentLength > 0) {
|
||||
totalBytesRead += if (bytesRead != -1L) bytesRead else 0
|
||||
progressState.value = (totalBytesRead.toDouble() / contentLength.toDouble()).toFloat()
|
||||
}
|
||||
return bytesRead
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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>
|
||||
@ -1,34 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<style name="Theme.Kotatsu.DialogWhenLarge">
|
||||
<item name="windowFixedWidthMajor">@dimen/abc_dialog_fixed_width_major</item>
|
||||
<item name="windowFixedWidthMinor">@dimen/abc_dialog_fixed_width_minor</item>
|
||||
<item name="windowFixedHeightMajor">@dimen/abc_dialog_fixed_height_major</item>
|
||||
<item name="windowFixedHeightMinor">@dimen/abc_dialog_fixed_height_minor</item>
|
||||
<item name="android:windowElevation">@dimen/abc_floating_window_z</item>
|
||||
<item name="android:colorBackground">?attr/colorBackgroundFloating</item>
|
||||
<item name="android:colorBackgroundCacheHint">@null</item>
|
||||
|
||||
<item name="android:windowFrame">@null</item>
|
||||
<item name="android:windowTitleStyle">@style/RtlOverlay.DialogWindowTitle.AppCompat</item>
|
||||
<item name="android:windowTitleBackgroundStyle">@style/Base.DialogWindowTitleBackground.AppCompat</item>
|
||||
<item name="android:windowBackground">@drawable/abc_dialog_material_background</item>
|
||||
<item name="android:windowIsFloating">true</item>
|
||||
<item name="android:backgroundDimEnabled">true</item>
|
||||
<item name="android:windowContentOverlay">@null</item>
|
||||
<item name="android:windowAnimationStyle">@style/Animation.AppCompat.Dialog</item>
|
||||
<item name="android:windowSoftInputMode">stateUnspecified|adjustResize</item>
|
||||
|
||||
<item name="windowActionBar">false</item>
|
||||
<item name="windowActionModeOverlay">true</item>
|
||||
|
||||
<item name="listPreferredItemPaddingLeft">24dip</item>
|
||||
<item name="listPreferredItemPaddingRight">24dip</item>
|
||||
|
||||
<item name="android:listDivider">@null</item>
|
||||
|
||||
<item name="android:buttonBarStyle">@style/Widget.AppCompat.ButtonBar.AlertDialog</item>
|
||||
<item name="android:borderlessButtonStyle">@style/Widget.AppCompat.Button.Borderless</item>
|
||||
<item name="android:windowCloseOnTouchOutside">true</item>
|
||||
</style>
|
||||
</resources>
|
||||
@ -0,0 +1,39 @@
|
||||
package org.koitharu.kotatsu.core.parser
|
||||
|
||||
import org.koitharu.kotatsu.parsers.MangaLoaderContext
|
||||
import org.koitharu.kotatsu.parsers.MangaParser
|
||||
import org.koitharu.kotatsu.parsers.config.ConfigKey
|
||||
import org.koitharu.kotatsu.parsers.exception.NotFoundException
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.parsers.model.MangaChapter
|
||||
import org.koitharu.kotatsu.parsers.model.MangaPage
|
||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||
import org.koitharu.kotatsu.parsers.model.MangaTag
|
||||
import org.koitharu.kotatsu.parsers.model.SortOrder
|
||||
import java.util.EnumSet
|
||||
|
||||
class DummyParser(override val context: MangaLoaderContext) : MangaParser(MangaSource.DUMMY) {
|
||||
|
||||
override val configKeyDomain: ConfigKey.Domain
|
||||
get() = ConfigKey.Domain("localhost", null)
|
||||
|
||||
override val sortOrders: Set<SortOrder>
|
||||
get() = EnumSet.allOf(SortOrder::class.java)
|
||||
|
||||
override suspend fun getDetails(manga: Manga): Manga = stub()
|
||||
|
||||
override suspend fun getList(
|
||||
offset: Int,
|
||||
query: String?,
|
||||
tags: Set<MangaTag>?,
|
||||
sortOrder: SortOrder,
|
||||
): List<Manga> = stub()
|
||||
|
||||
override suspend fun getPages(chapter: MangaChapter): List<MangaPage> = stub()
|
||||
|
||||
override suspend fun getTags(): Set<MangaTag> = stub()
|
||||
|
||||
private fun stub(): Nothing {
|
||||
throw NotFoundException("Usage of Dummy parser in release build", "")
|
||||
}
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
package org.koitharu.kotatsu.core.parser
|
||||
|
||||
import org.koitharu.kotatsu.parsers.MangaLoaderContext
|
||||
import org.koitharu.kotatsu.parsers.MangaParser
|
||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||
import org.koitharu.kotatsu.parsers.newParser
|
||||
|
||||
fun MangaParser(source: MangaSource, loaderContext: MangaLoaderContext): MangaParser {
|
||||
return source.newParser(loaderContext)
|
||||
}
|
||||
Loading…
Reference in New Issue