New download dialog
parent
1e22e8de45
commit
557b69d73f
@ -1,67 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.details.ui
|
|
||||||
|
|
||||||
import android.content.DialogInterface
|
|
||||||
import android.view.View
|
|
||||||
import org.koitharu.kotatsu.R
|
|
||||||
import org.koitharu.kotatsu.core.model.ids
|
|
||||||
import org.koitharu.kotatsu.core.ui.dialog.buildAlertDialog
|
|
||||||
import org.koitharu.kotatsu.core.ui.dialog.setRecyclerViewList
|
|
||||||
import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
|
|
||||||
import org.koitharu.kotatsu.download.ui.dialog.DownloadOption
|
|
||||||
import org.koitharu.kotatsu.download.ui.dialog.downloadOptionAD
|
|
||||||
import org.koitharu.kotatsu.settings.SettingsActivity
|
|
||||||
|
|
||||||
class DownloadDialogHelper(
|
|
||||||
private val host: View,
|
|
||||||
private val viewModel: DetailsViewModel,
|
|
||||||
) {
|
|
||||||
|
|
||||||
fun show(callback: OnListItemClickListener<DownloadOption>) {
|
|
||||||
val branch = viewModel.selectedBranchValue
|
|
||||||
val allChapters = viewModel.manga.value?.chapters ?: return
|
|
||||||
val branchChapters = viewModel.manga.value?.getChapters(branch).orEmpty()
|
|
||||||
val history = viewModel.history.value
|
|
||||||
|
|
||||||
val options = buildList {
|
|
||||||
add(DownloadOption.WholeManga(allChapters.ids()))
|
|
||||||
if (branch != null && branchChapters.isNotEmpty()) {
|
|
||||||
add(DownloadOption.AllChapters(branch, branchChapters.ids()))
|
|
||||||
}
|
|
||||||
|
|
||||||
if (history != null) {
|
|
||||||
val unreadChapters = branchChapters.dropWhile { it.id != history.chapterId }
|
|
||||||
if (unreadChapters.isNotEmpty() && unreadChapters.size < branchChapters.size) {
|
|
||||||
add(DownloadOption.AllUnreadChapters(unreadChapters.ids(), branch))
|
|
||||||
if (unreadChapters.size > 5) {
|
|
||||||
add(DownloadOption.NextUnreadChapters(unreadChapters.take(5).ids()))
|
|
||||||
if (unreadChapters.size > 10) {
|
|
||||||
add(DownloadOption.NextUnreadChapters(unreadChapters.take(10).ids()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (branchChapters.size > 5) {
|
|
||||||
add(DownloadOption.FirstChapters(branchChapters.take(5).ids()))
|
|
||||||
if (branchChapters.size > 10) {
|
|
||||||
add(DownloadOption.FirstChapters(branchChapters.take(10).ids()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
add(DownloadOption.SelectionHint())
|
|
||||||
}
|
|
||||||
var dialog: DialogInterface? = null
|
|
||||||
val listener = OnListItemClickListener<DownloadOption> { item, _ ->
|
|
||||||
callback.onItemClick(item, host)
|
|
||||||
dialog?.dismiss()
|
|
||||||
}
|
|
||||||
dialog = buildAlertDialog(host.context) {
|
|
||||||
setCancelable(true)
|
|
||||||
setTitle(R.string.download)
|
|
||||||
setNegativeButton(android.R.string.cancel, null)
|
|
||||||
setNeutralButton(R.string.settings) { _, _ ->
|
|
||||||
host.context.startActivity(SettingsActivity.newDownloadsSettingsIntent(host.context))
|
|
||||||
}
|
|
||||||
setRecyclerViewList(options, downloadOptionAD(listener))
|
|
||||||
}.also { it.show() }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.dialog
|
||||||
|
|
||||||
|
data class ChapterSelectOptions(
|
||||||
|
val wholeManga: ChaptersSelectMacro.WholeManga,
|
||||||
|
val wholeBranch: ChaptersSelectMacro.WholeBranch?,
|
||||||
|
val firstChapters: ChaptersSelectMacro.FirstChapters?,
|
||||||
|
val unreadChapters: ChaptersSelectMacro.UnreadChapters?,
|
||||||
|
)
|
||||||
@ -0,0 +1,97 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.dialog
|
||||||
|
|
||||||
|
import androidx.collection.ArraySet
|
||||||
|
import androidx.collection.LongLongMap
|
||||||
|
import org.koitharu.kotatsu.parsers.model.MangaChapter
|
||||||
|
import org.koitharu.kotatsu.parsers.util.mapNotNullToSet
|
||||||
|
|
||||||
|
interface ChaptersSelectMacro {
|
||||||
|
|
||||||
|
fun getChaptersIds(mangaId: Long, chapters: List<MangaChapter>): Set<Long>?
|
||||||
|
|
||||||
|
class WholeManga(
|
||||||
|
val chaptersCount: Int,
|
||||||
|
) : ChaptersSelectMacro {
|
||||||
|
|
||||||
|
override fun getChaptersIds(mangaId: Long, chapters: List<MangaChapter>): Set<Long>? = null
|
||||||
|
}
|
||||||
|
|
||||||
|
class WholeBranch(
|
||||||
|
val branches: Map<String?, Int>,
|
||||||
|
val selectedBranch: String?,
|
||||||
|
) : ChaptersSelectMacro {
|
||||||
|
|
||||||
|
val chaptersCount: Int = branches[selectedBranch] ?: 0
|
||||||
|
|
||||||
|
override fun getChaptersIds(
|
||||||
|
mangaId: Long,
|
||||||
|
chapters: List<MangaChapter>
|
||||||
|
): Set<Long> = chapters.mapNotNullToSet { c ->
|
||||||
|
if (c.branch == selectedBranch) {
|
||||||
|
c.id
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun copy(branch: String?) = WholeBranch(branches, branch)
|
||||||
|
}
|
||||||
|
|
||||||
|
class FirstChapters(
|
||||||
|
val chaptersCount: Int,
|
||||||
|
val maxAvailableCount: Int,
|
||||||
|
val branch: String?,
|
||||||
|
) : ChaptersSelectMacro {
|
||||||
|
|
||||||
|
override fun getChaptersIds(mangaId: Long, chapters: List<MangaChapter>): Set<Long> {
|
||||||
|
val result = ArraySet<Long>(chaptersCount)
|
||||||
|
for (c in chapters) {
|
||||||
|
if (c.branch == branch) {
|
||||||
|
result.add(c.id)
|
||||||
|
if (result.size >= chaptersCount) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun copy(count: Int) = FirstChapters(count, maxAvailableCount, branch)
|
||||||
|
}
|
||||||
|
|
||||||
|
class UnreadChapters(
|
||||||
|
val chaptersCount: Int,
|
||||||
|
val maxAvailableCount: Int,
|
||||||
|
private val currentChaptersIds: LongLongMap,
|
||||||
|
) : ChaptersSelectMacro {
|
||||||
|
|
||||||
|
override fun getChaptersIds(mangaId: Long, chapters: List<MangaChapter>): Set<Long>? {
|
||||||
|
if (chapters.isEmpty()) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
val currentChapterId = currentChaptersIds.getOrDefault(mangaId, chapters.first().id)
|
||||||
|
var branch: String? = null
|
||||||
|
var isAdding = false
|
||||||
|
val result = ArraySet<Long>(chaptersCount)
|
||||||
|
for (c in chapters) {
|
||||||
|
if (!isAdding) {
|
||||||
|
if (c.id == currentChapterId) {
|
||||||
|
branch = c.branch
|
||||||
|
isAdding = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isAdding) {
|
||||||
|
if (c.branch == branch) {
|
||||||
|
result.add(c.id)
|
||||||
|
if (result.size >= chaptersCount) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun copy(count: Int) = UnreadChapters(count, maxAvailableCount, currentChaptersIds)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,41 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.dialog
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.ArrayAdapter
|
||||||
|
import android.widget.TextView
|
||||||
|
import androidx.core.view.isVisible
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.textAndVisible
|
||||||
|
import org.koitharu.kotatsu.databinding.ItemStorageConfigBinding
|
||||||
|
import org.koitharu.kotatsu.settings.storage.DirectoryModel
|
||||||
|
|
||||||
|
class DestinationsAdapter(context: Context, dataset: List<DirectoryModel>) :
|
||||||
|
ArrayAdapter<DirectoryModel>(context, android.R.layout.simple_spinner_dropdown_item, android.R.id.text1, dataset) {
|
||||||
|
|
||||||
|
init {
|
||||||
|
setDropDownViewResource(R.layout.item_storage_config)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
||||||
|
val view = convertView ?: LayoutInflater.from(parent.context)
|
||||||
|
.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false)
|
||||||
|
val item = getItem(position) ?: return view
|
||||||
|
view.findViewById<TextView>(android.R.id.text1).text = item.title ?: view.context.getString(item.titleRes)
|
||||||
|
return view
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
|
||||||
|
val view = convertView ?: LayoutInflater.from(parent.context)
|
||||||
|
.inflate(R.layout.item_storage_config, parent, false)
|
||||||
|
val item = getItem(position) ?: return view
|
||||||
|
val binding =
|
||||||
|
view.tag as? ItemStorageConfigBinding ?: ItemStorageConfigBinding.bind(view).also { view.tag = it }
|
||||||
|
binding.imageViewRemove.isVisible = false
|
||||||
|
binding.textViewTitle.text = item.title ?: view.context.getString(item.titleRes)
|
||||||
|
binding.textViewSubtitle.textAndVisible = item.file?.path
|
||||||
|
return view
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,359 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.dialog
|
||||||
|
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.Menu
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.Spinner
|
||||||
|
import androidx.appcompat.widget.PopupMenu
|
||||||
|
import androidx.core.view.isVisible
|
||||||
|
import androidx.fragment.app.Fragment
|
||||||
|
import androidx.fragment.app.FragmentActivity
|
||||||
|
import androidx.fragment.app.FragmentManager
|
||||||
|
import androidx.fragment.app.FragmentResultListener
|
||||||
|
import androidx.fragment.app.setFragmentResult
|
||||||
|
import androidx.fragment.app.viewModels
|
||||||
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
|
import com.google.android.material.snackbar.Snackbar
|
||||||
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.core.model.parcelable.ParcelableManga
|
||||||
|
import org.koitharu.kotatsu.core.prefs.DownloadFormat
|
||||||
|
import org.koitharu.kotatsu.core.ui.AlertDialogFragment
|
||||||
|
import org.koitharu.kotatsu.core.ui.widgets.TwoLinesItemView
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.findActivity
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.getDisplayMessage
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.joinToStringWithLimit
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.mapToArray
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.observe
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.observeEvent
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.parentView
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.showDistinct
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.showOrHide
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.withArgs
|
||||||
|
import org.koitharu.kotatsu.databinding.DialogDownloadBinding
|
||||||
|
import org.koitharu.kotatsu.download.ui.list.DownloadsActivity
|
||||||
|
import org.koitharu.kotatsu.main.ui.owners.BottomNavOwner
|
||||||
|
import org.koitharu.kotatsu.parsers.model.Manga
|
||||||
|
import org.koitharu.kotatsu.parsers.util.format
|
||||||
|
import org.koitharu.kotatsu.settings.storage.DirectoryModel
|
||||||
|
|
||||||
|
@AndroidEntryPoint
|
||||||
|
class DownloadDialogFragment : AlertDialogFragment<DialogDownloadBinding>(), View.OnClickListener {
|
||||||
|
|
||||||
|
private val viewModel by viewModels<DownloadDialogViewModel>()
|
||||||
|
private var optionViews: Array<out TwoLinesItemView>? = null
|
||||||
|
|
||||||
|
override fun onCreateViewBinding(inflater: LayoutInflater, container: ViewGroup?) =
|
||||||
|
DialogDownloadBinding.inflate(inflater, container, false)
|
||||||
|
|
||||||
|
override fun onBuildDialog(builder: MaterialAlertDialogBuilder): MaterialAlertDialogBuilder {
|
||||||
|
return super.onBuildDialog(builder)
|
||||||
|
.setTitle(R.string.save_manga)
|
||||||
|
.setCancelable(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onViewBindingCreated(binding: DialogDownloadBinding, savedInstanceState: Bundle?) {
|
||||||
|
super.onViewBindingCreated(binding, savedInstanceState)
|
||||||
|
optionViews = arrayOf(
|
||||||
|
binding.optionWholeManga,
|
||||||
|
binding.optionWholeBranch,
|
||||||
|
binding.optionFirstChapters,
|
||||||
|
binding.optionUnreadChapters,
|
||||||
|
).onEach {
|
||||||
|
it.setOnClickListener(this)
|
||||||
|
it.setOnButtonClickListener(this)
|
||||||
|
}
|
||||||
|
binding.buttonCancel.setOnClickListener(this)
|
||||||
|
binding.buttonConfirm.setOnClickListener(this)
|
||||||
|
binding.textViewMore.setOnClickListener(this)
|
||||||
|
|
||||||
|
binding.textViewSummary.text = viewModel.manga.joinToStringWithLimit(binding.root.context, 120) { it.title }
|
||||||
|
|
||||||
|
viewModel.isLoading.observe(viewLifecycleOwner, this::onLoadingStateChanged)
|
||||||
|
viewModel.onScheduled.observeEvent(viewLifecycleOwner, this::onDownloadScheduled)
|
||||||
|
viewModel.onError.observeEvent(viewLifecycleOwner, this::onError)
|
||||||
|
viewModel.defaultFormat.observe(viewLifecycleOwner, this::onDefaultFormatChanged)
|
||||||
|
viewModel.availableDestinations.observe(viewLifecycleOwner, this::onDestinationsChanged)
|
||||||
|
viewModel.chaptersSelectOptions.observe(viewLifecycleOwner, this::onChapterSelectOptionsChanged)
|
||||||
|
viewModel.isOptionsLoading.observe(viewLifecycleOwner, binding.progressBar::showOrHide)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onViewStateRestored(savedInstanceState: Bundle?) {
|
||||||
|
super.onViewStateRestored(savedInstanceState)
|
||||||
|
showMoreOptions(requireViewBinding().textViewMore.isChecked)
|
||||||
|
setCheckedOption(
|
||||||
|
savedInstanceState?.getInt(KEY_CHECKED_OPTION, R.id.option_whole_manga) ?: R.id.option_whole_manga,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onSaveInstanceState(outState: Bundle) {
|
||||||
|
super.onSaveInstanceState(outState)
|
||||||
|
optionViews?.find { it.isChecked }?.let {
|
||||||
|
outState.putInt(KEY_CHECKED_OPTION, it.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroyView() {
|
||||||
|
super.onDestroyView()
|
||||||
|
optionViews = null
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onClick(v: View) {
|
||||||
|
when (v.id) {
|
||||||
|
R.id.button_cancel -> dialog?.cancel()
|
||||||
|
R.id.button_confirm -> viewBinding?.run {
|
||||||
|
val options = viewModel.chaptersSelectOptions.value
|
||||||
|
viewModel.confirm(
|
||||||
|
startNow = switchStart.isChecked,
|
||||||
|
chaptersMacro = when {
|
||||||
|
optionWholeManga.isChecked -> options.wholeManga
|
||||||
|
optionWholeBranch.isChecked -> options.wholeBranch ?: return@run
|
||||||
|
optionFirstChapters.isChecked -> options.firstChapters ?: return@run
|
||||||
|
optionUnreadChapters.isChecked -> options.unreadChapters ?: return@run
|
||||||
|
else -> return@run
|
||||||
|
},
|
||||||
|
format = DownloadFormat.entries.getOrNull(spinnerFormat.selectedItemPosition),
|
||||||
|
destination = viewModel.availableDestinations.value.getOrNull(spinnerDestination.selectedItemPosition),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
R.id.textView_more -> {
|
||||||
|
val binding = viewBinding ?: return
|
||||||
|
binding.textViewMore.toggle()
|
||||||
|
showMoreOptions(binding.textViewMore.isChecked)
|
||||||
|
}
|
||||||
|
|
||||||
|
R.id.button -> when (v.parentView?.id ?: return) {
|
||||||
|
R.id.option_whole_branch -> showBranchSelection(v)
|
||||||
|
R.id.option_first_chapters -> showFirstChaptersCountSelection(v)
|
||||||
|
R.id.option_unread_chapters -> showUnreadChaptersCountSelection(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> if (v is TwoLinesItemView) {
|
||||||
|
setCheckedOption(v.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun onError(e: Throwable) {
|
||||||
|
MaterialAlertDialogBuilder(context ?: return)
|
||||||
|
.setNegativeButton(R.string.close, null)
|
||||||
|
.setTitle(R.string.error)
|
||||||
|
.setMessage(e.getDisplayMessage(resources))
|
||||||
|
.show()
|
||||||
|
dismiss()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun onLoadingStateChanged(value: Boolean) {
|
||||||
|
with(requireViewBinding()) {
|
||||||
|
buttonConfirm.isEnabled = !value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun onDefaultFormatChanged(format: DownloadFormat?) {
|
||||||
|
val spinner = viewBinding?.spinnerFormat ?: return
|
||||||
|
spinner.setSelection(format?.ordinal ?: Spinner.INVALID_POSITION)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun onDestinationsChanged(directories: List<DirectoryModel>) {
|
||||||
|
viewBinding?.spinnerDestination?.run {
|
||||||
|
adapter = DestinationsAdapter(context, directories)
|
||||||
|
setSelection(directories.indexOfFirst { it.isChecked })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun onChapterSelectOptionsChanged(options: ChapterSelectOptions) {
|
||||||
|
with(viewBinding ?: return) {
|
||||||
|
// Whole manga
|
||||||
|
optionWholeManga.subtitle = if (options.wholeManga.chaptersCount > 0) {
|
||||||
|
resources.getQuantityString(
|
||||||
|
R.plurals.chapters,
|
||||||
|
options.wholeManga.chaptersCount,
|
||||||
|
options.wholeManga.chaptersCount,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
// All chapters for branch
|
||||||
|
optionWholeBranch.isVisible = options.wholeBranch != null
|
||||||
|
options.wholeBranch?.let {
|
||||||
|
optionWholeBranch.title = resources.getString(
|
||||||
|
R.string.download_option_all_chapters,
|
||||||
|
it.selectedBranch,
|
||||||
|
)
|
||||||
|
optionWholeBranch.subtitle = if (it.chaptersCount > 0) {
|
||||||
|
resources.getQuantityString(
|
||||||
|
R.plurals.chapters,
|
||||||
|
it.chaptersCount,
|
||||||
|
it.chaptersCount,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// First N chapters
|
||||||
|
optionFirstChapters.isVisible = options.firstChapters != null
|
||||||
|
options.firstChapters?.let {
|
||||||
|
optionFirstChapters.title = resources.getString(
|
||||||
|
R.string.download_option_first_n_chapters,
|
||||||
|
resources.getQuantityString(
|
||||||
|
R.plurals.chapters,
|
||||||
|
it.chaptersCount,
|
||||||
|
it.chaptersCount,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
optionFirstChapters.subtitle = it.branch
|
||||||
|
}
|
||||||
|
// Next N unread chapters
|
||||||
|
optionUnreadChapters.isVisible = options.unreadChapters != null
|
||||||
|
options.unreadChapters?.let {
|
||||||
|
optionUnreadChapters.title = if (it.chaptersCount == Int.MAX_VALUE) {
|
||||||
|
resources.getString(R.string.download_option_all_unread)
|
||||||
|
} else {
|
||||||
|
resources.getString(
|
||||||
|
R.string.download_option_next_unread_n_chapters,
|
||||||
|
resources.getQuantityString(
|
||||||
|
R.plurals.chapters,
|
||||||
|
it.chaptersCount,
|
||||||
|
it.chaptersCount,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun onDownloadScheduled(isStarted: Boolean) {
|
||||||
|
val bundle = Bundle(1)
|
||||||
|
bundle.putBoolean(ARG_STARTED, isStarted)
|
||||||
|
setFragmentResult(RESULT_KEY, bundle)
|
||||||
|
dismiss()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun showMoreOptions(isVisible: Boolean) = viewBinding?.apply {
|
||||||
|
cardFormat.isVisible = isVisible
|
||||||
|
textViewFormat.isVisible = isVisible
|
||||||
|
cardDestination.isVisible = isVisible
|
||||||
|
textViewDestination.isVisible = isVisible
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun setCheckedOption(id: Int) {
|
||||||
|
for (optionView in optionViews ?: return) {
|
||||||
|
optionView.isChecked = id == optionView.id
|
||||||
|
optionView.isButtonEnabled = optionView.isChecked
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun showBranchSelection(v: View) {
|
||||||
|
val option = viewModel.chaptersSelectOptions.value.wholeBranch ?: return
|
||||||
|
val branches = option.branches.keys.toList()
|
||||||
|
if (branches.size <= 1) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val menu = PopupMenu(v.context, v)
|
||||||
|
for ((i, branch) in branches.withIndex()) {
|
||||||
|
menu.menu.add(Menu.NONE, Menu.NONE, i, branch ?: getString(R.string.unknown))
|
||||||
|
}
|
||||||
|
menu.setOnMenuItemClickListener {
|
||||||
|
viewModel.setSelectedBranch(branches.getOrNull(it.order))
|
||||||
|
true
|
||||||
|
}
|
||||||
|
menu.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun showFirstChaptersCountSelection(v: View) {
|
||||||
|
val option = viewModel.chaptersSelectOptions.value.firstChapters ?: return
|
||||||
|
val menu = PopupMenu(v.context, v)
|
||||||
|
chaptersCount(option.maxAvailableCount).forEach { i ->
|
||||||
|
menu.menu.add(i.format())
|
||||||
|
}
|
||||||
|
menu.setOnMenuItemClickListener {
|
||||||
|
viewModel.setFirstChaptersCount(
|
||||||
|
it.title?.toString()?.toIntOrNull() ?: return@setOnMenuItemClickListener false,
|
||||||
|
)
|
||||||
|
true
|
||||||
|
}
|
||||||
|
menu.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun showUnreadChaptersCountSelection(v: View) {
|
||||||
|
val option = viewModel.chaptersSelectOptions.value.unreadChapters ?: return
|
||||||
|
val menu = PopupMenu(v.context, v)
|
||||||
|
chaptersCount(option.maxAvailableCount).forEach { i ->
|
||||||
|
menu.menu.add(i.format())
|
||||||
|
}
|
||||||
|
menu.menu.add(getString(R.string.chapters_all))
|
||||||
|
menu.setOnMenuItemClickListener {
|
||||||
|
viewModel.setUnreadChaptersCount(it.title?.toString()?.toIntOrNull() ?: Int.MAX_VALUE)
|
||||||
|
true
|
||||||
|
}
|
||||||
|
menu.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun chaptersCount(max: Int) = sequence {
|
||||||
|
yield(1)
|
||||||
|
var seed = 5
|
||||||
|
var step = 5
|
||||||
|
while (seed + step <= max) {
|
||||||
|
yield(seed)
|
||||||
|
step = when {
|
||||||
|
seed < 20 -> 5
|
||||||
|
seed < 60 -> 10
|
||||||
|
else -> 20
|
||||||
|
}
|
||||||
|
seed += step
|
||||||
|
}
|
||||||
|
if (seed < max) {
|
||||||
|
yield(max)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class SnackbarResultListener(private val host: View) : FragmentResultListener {
|
||||||
|
|
||||||
|
override fun onFragmentResult(requestKey: String, result: Bundle) {
|
||||||
|
val isStarted = result.getBoolean(ARG_STARTED, true)
|
||||||
|
val snackbar = Snackbar.make(
|
||||||
|
host,
|
||||||
|
if (isStarted) R.string.download_started else R.string.download_added,
|
||||||
|
Snackbar.LENGTH_LONG,
|
||||||
|
)
|
||||||
|
(host.context.findActivity() as? BottomNavOwner)?.let {
|
||||||
|
snackbar.anchorView = it.bottomNav
|
||||||
|
}
|
||||||
|
snackbar.setAction(R.string.details) {
|
||||||
|
it.context.startActivity(Intent(it.context, DownloadsActivity::class.java))
|
||||||
|
}
|
||||||
|
snackbar.show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
private const val TAG = "DownloadDialogFragment"
|
||||||
|
private const val RESULT_KEY = "DOWNLOAD_STARTED"
|
||||||
|
private const val ARG_STARTED = "started"
|
||||||
|
private const val KEY_CHECKED_OPTION = "checked_opt"
|
||||||
|
const val ARG_MANGA = "manga"
|
||||||
|
|
||||||
|
fun show(fm: FragmentManager, manga: Collection<Manga>) = DownloadDialogFragment().withArgs(1) {
|
||||||
|
putParcelableArray(ARG_MANGA, manga.mapToArray { ParcelableManga(it) })
|
||||||
|
}.showDistinct(fm, TAG)
|
||||||
|
|
||||||
|
fun registerCallback(activity: FragmentActivity, snackbarHost: View) =
|
||||||
|
activity.supportFragmentManager.setFragmentResultListener(
|
||||||
|
RESULT_KEY,
|
||||||
|
activity,
|
||||||
|
SnackbarResultListener(snackbarHost),
|
||||||
|
)
|
||||||
|
|
||||||
|
fun registerCallback(fragment: Fragment, snackbarHost: View) =
|
||||||
|
fragment.childFragmentManager.setFragmentResultListener(
|
||||||
|
RESULT_KEY,
|
||||||
|
fragment.viewLifecycleOwner,
|
||||||
|
SnackbarResultListener(snackbarHost),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,241 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.dialog
|
||||||
|
|
||||||
|
import androidx.collection.ArrayMap
|
||||||
|
import androidx.collection.ArraySet
|
||||||
|
import androidx.collection.MutableLongLongMap
|
||||||
|
import androidx.lifecycle.SavedStateHandle
|
||||||
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.async
|
||||||
|
import kotlinx.coroutines.awaitAll
|
||||||
|
import kotlinx.coroutines.coroutineScope
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.core.model.getPreferredBranch
|
||||||
|
import org.koitharu.kotatsu.core.model.parcelable.ParcelableManga
|
||||||
|
import org.koitharu.kotatsu.core.parser.MangaDataRepository
|
||||||
|
import org.koitharu.kotatsu.core.parser.MangaRepository
|
||||||
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||||
|
import org.koitharu.kotatsu.core.prefs.DownloadFormat
|
||||||
|
import org.koitharu.kotatsu.core.ui.BaseViewModel
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.MutableEventFlow
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.call
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.require
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.sizeOrZero
|
||||||
|
import org.koitharu.kotatsu.download.ui.worker.DownloadTask
|
||||||
|
import org.koitharu.kotatsu.download.ui.worker.DownloadWorker
|
||||||
|
import org.koitharu.kotatsu.history.data.HistoryRepository
|
||||||
|
import org.koitharu.kotatsu.local.data.LocalMangaRepository
|
||||||
|
import org.koitharu.kotatsu.local.data.LocalStorageManager
|
||||||
|
import org.koitharu.kotatsu.parsers.model.Manga
|
||||||
|
import org.koitharu.kotatsu.parsers.util.SuspendLazy
|
||||||
|
import org.koitharu.kotatsu.parsers.util.mapToSet
|
||||||
|
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
|
||||||
|
import org.koitharu.kotatsu.settings.storage.DirectoryModel
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@HiltViewModel
|
||||||
|
class DownloadDialogViewModel @Inject constructor(
|
||||||
|
savedStateHandle: SavedStateHandle,
|
||||||
|
private val mangaDataRepository: MangaDataRepository,
|
||||||
|
private val scheduler: DownloadWorker.Scheduler,
|
||||||
|
private val localStorageManager: LocalStorageManager,
|
||||||
|
private val localMangaRepository: LocalMangaRepository,
|
||||||
|
private val mangaRepositoryFactory: MangaRepository.Factory,
|
||||||
|
private val historyRepository: HistoryRepository,
|
||||||
|
private val settings: AppSettings,
|
||||||
|
) : BaseViewModel() {
|
||||||
|
|
||||||
|
val manga = savedStateHandle.require<Array<ParcelableManga>>(DownloadDialogFragment.ARG_MANGA).map {
|
||||||
|
it.manga
|
||||||
|
}
|
||||||
|
private val mangaDetails = SuspendLazy {
|
||||||
|
coroutineScope {
|
||||||
|
manga.map { m ->
|
||||||
|
async { m.getDetails() }
|
||||||
|
}.awaitAll()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val onScheduled = MutableEventFlow<Boolean>()
|
||||||
|
val defaultFormat = MutableStateFlow<DownloadFormat?>(null)
|
||||||
|
val availableDestinations = MutableStateFlow(listOf(defaultDestination()))
|
||||||
|
val chaptersSelectOptions = MutableStateFlow(
|
||||||
|
ChapterSelectOptions(
|
||||||
|
wholeManga = ChaptersSelectMacro.WholeManga(0),
|
||||||
|
wholeBranch = null,
|
||||||
|
firstChapters = null,
|
||||||
|
unreadChapters = null,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
val isOptionsLoading = MutableStateFlow(true)
|
||||||
|
|
||||||
|
init {
|
||||||
|
launchJob(Dispatchers.Default) {
|
||||||
|
defaultFormat.value = settings.preferredDownloadFormat
|
||||||
|
}
|
||||||
|
launchJob(Dispatchers.Default) {
|
||||||
|
try {
|
||||||
|
loadAvailableOptions()
|
||||||
|
} finally {
|
||||||
|
isOptionsLoading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
loadAvailableDestinations()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun confirm(
|
||||||
|
startNow: Boolean,
|
||||||
|
chaptersMacro: ChaptersSelectMacro,
|
||||||
|
format: DownloadFormat?,
|
||||||
|
destination: DirectoryModel?,
|
||||||
|
) {
|
||||||
|
launchLoadingJob(Dispatchers.Default) {
|
||||||
|
val tasks = mangaDetails.get().map { m ->
|
||||||
|
val chapters = checkNotNull(m.chapters) { "Manga \"${m.title}\" cannot be loaded" }
|
||||||
|
mangaDataRepository.storeManga(m)
|
||||||
|
DownloadTask(
|
||||||
|
mangaId = m.id,
|
||||||
|
isPaused = !startNow,
|
||||||
|
isSilent = false,
|
||||||
|
chaptersIds = chaptersMacro.getChaptersIds(m.id, chapters)?.toLongArray(),
|
||||||
|
destination = destination?.file,
|
||||||
|
format = format,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
scheduler.schedule(tasks)
|
||||||
|
onScheduled.call(startNow)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setSelectedBranch(branch: String?) {
|
||||||
|
val snapshot = chaptersSelectOptions.value
|
||||||
|
chaptersSelectOptions.value = snapshot.copy(
|
||||||
|
wholeBranch = snapshot.wholeBranch?.copy(branch),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setFirstChaptersCount(count: Int) {
|
||||||
|
val snapshot = chaptersSelectOptions.value
|
||||||
|
chaptersSelectOptions.value = snapshot.copy(
|
||||||
|
firstChapters = snapshot.firstChapters?.copy(count),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setUnreadChaptersCount(count: Int) {
|
||||||
|
val snapshot = chaptersSelectOptions.value
|
||||||
|
chaptersSelectOptions.value = snapshot.copy(
|
||||||
|
unreadChapters = snapshot.unreadChapters?.copy(count),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun defaultDestination() = DirectoryModel(
|
||||||
|
title = null,
|
||||||
|
titleRes = R.string.system_default,
|
||||||
|
file = null,
|
||||||
|
isRemovable = false,
|
||||||
|
isChecked = true,
|
||||||
|
isAvailable = true,
|
||||||
|
)
|
||||||
|
|
||||||
|
private suspend fun loadAvailableOptions() {
|
||||||
|
val details = mangaDetails.get()
|
||||||
|
var totalChapters = 0
|
||||||
|
val branches = ArrayMap<String?, Int>()
|
||||||
|
var maxChapters = 0
|
||||||
|
var maxUnreadChapters = 0
|
||||||
|
val preferredBranches = ArraySet<String?>(details.size)
|
||||||
|
val currentChaptersIds = MutableLongLongMap(details.size)
|
||||||
|
|
||||||
|
details.forEach { m ->
|
||||||
|
val history = historyRepository.getOne(m)
|
||||||
|
if (history != null) {
|
||||||
|
currentChaptersIds[m.id] = history.chapterId
|
||||||
|
val unreadChaptersCount = m.chapters?.dropWhile { it.id != history.chapterId }.sizeOrZero()
|
||||||
|
maxUnreadChapters = maxOf(maxUnreadChapters, unreadChaptersCount)
|
||||||
|
} else {
|
||||||
|
maxUnreadChapters = maxOf(maxUnreadChapters, m.chapters.sizeOrZero())
|
||||||
|
}
|
||||||
|
maxChapters = maxOf(maxChapters, m.chapters.sizeOrZero())
|
||||||
|
preferredBranches.add(m.getPreferredBranch(history))
|
||||||
|
m.chapters?.forEach { c ->
|
||||||
|
totalChapters++
|
||||||
|
branches.increment(c.branch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val defaultBranch = preferredBranches.firstOrNull()
|
||||||
|
chaptersSelectOptions.value = ChapterSelectOptions(
|
||||||
|
wholeManga = ChaptersSelectMacro.WholeManga(totalChapters),
|
||||||
|
wholeBranch = if (branches.size > 1) {
|
||||||
|
ChaptersSelectMacro.WholeBranch(
|
||||||
|
branches = branches,
|
||||||
|
selectedBranch = defaultBranch,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
},
|
||||||
|
firstChapters = if (maxChapters > 0) {
|
||||||
|
ChaptersSelectMacro.FirstChapters(
|
||||||
|
chaptersCount = minOf(5, maxChapters),
|
||||||
|
maxAvailableCount = maxChapters,
|
||||||
|
branch = defaultBranch,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
},
|
||||||
|
unreadChapters = if (currentChaptersIds.isNotEmpty()) {
|
||||||
|
ChaptersSelectMacro.UnreadChapters(
|
||||||
|
chaptersCount = minOf(5, maxUnreadChapters),
|
||||||
|
maxAvailableCount = maxUnreadChapters,
|
||||||
|
currentChaptersIds = currentChaptersIds,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun loadAvailableDestinations() = launchJob(Dispatchers.Default) {
|
||||||
|
val defaultDir = manga.mapToSet {
|
||||||
|
localMangaRepository.getOutputDir(it, null)
|
||||||
|
}.singleOrNull()
|
||||||
|
val dirs = localStorageManager.getWriteableDirs()
|
||||||
|
availableDestinations.value = buildList(dirs.size + 1) {
|
||||||
|
if (defaultDir == null) {
|
||||||
|
add(defaultDestination())
|
||||||
|
} else if (defaultDir !in dirs) {
|
||||||
|
add(
|
||||||
|
DirectoryModel(
|
||||||
|
title = localStorageManager.getDirectoryDisplayName(defaultDir, isFullPath = false),
|
||||||
|
titleRes = 0,
|
||||||
|
file = defaultDir,
|
||||||
|
isChecked = true,
|
||||||
|
isAvailable = true,
|
||||||
|
isRemovable = false,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
dirs.mapTo(this) { dir ->
|
||||||
|
DirectoryModel(
|
||||||
|
title = localStorageManager.getDirectoryDisplayName(dir, isFullPath = false),
|
||||||
|
titleRes = 0,
|
||||||
|
file = dir,
|
||||||
|
isChecked = dir == defaultDir,
|
||||||
|
isAvailable = true,
|
||||||
|
isRemovable = false,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun Manga.getDetails(): Manga = runCatchingCancellable {
|
||||||
|
mangaRepositoryFactory.create(source).getDetails(this)
|
||||||
|
}.onFailure { e ->
|
||||||
|
e.printStackTraceDebug()
|
||||||
|
}.getOrDefault(this)
|
||||||
|
|
||||||
|
private fun <T> MutableMap<T, Int>.increment(key: T) {
|
||||||
|
put(key, getOrDefault(key, 0) + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,99 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.download.ui.dialog
|
|
||||||
|
|
||||||
import android.content.res.Resources
|
|
||||||
import androidx.annotation.DrawableRes
|
|
||||||
import org.koitharu.kotatsu.R
|
|
||||||
import java.util.Locale
|
|
||||||
import com.google.android.material.R as materialR
|
|
||||||
|
|
||||||
sealed interface DownloadOption {
|
|
||||||
|
|
||||||
val chaptersIds: Set<Long>
|
|
||||||
|
|
||||||
@get:DrawableRes
|
|
||||||
val iconResId: Int
|
|
||||||
|
|
||||||
val chaptersCount: Int
|
|
||||||
get() = chaptersIds.size
|
|
||||||
|
|
||||||
fun getLabel(resources: Resources): CharSequence
|
|
||||||
|
|
||||||
class AllChapters(
|
|
||||||
val branch: String,
|
|
||||||
override val chaptersIds: Set<Long>,
|
|
||||||
) : DownloadOption {
|
|
||||||
|
|
||||||
override val iconResId = R.drawable.ic_select_group
|
|
||||||
|
|
||||||
override fun getLabel(resources: Resources): CharSequence {
|
|
||||||
return resources.getString(R.string.download_option_all_chapters, branch)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class WholeManga(
|
|
||||||
override val chaptersIds: Set<Long>,
|
|
||||||
) : DownloadOption {
|
|
||||||
|
|
||||||
override val iconResId = materialR.drawable.abc_ic_menu_selectall_mtrl_alpha
|
|
||||||
|
|
||||||
override fun getLabel(resources: Resources): CharSequence {
|
|
||||||
return resources.getString(R.string.download_option_whole_manga)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class FirstChapters(
|
|
||||||
override val chaptersIds: Set<Long>,
|
|
||||||
) : DownloadOption {
|
|
||||||
|
|
||||||
override val iconResId = R.drawable.ic_list_start
|
|
||||||
|
|
||||||
override fun getLabel(resources: Resources): CharSequence {
|
|
||||||
return resources.getString(
|
|
||||||
R.string.download_option_first_n_chapters,
|
|
||||||
resources.getQuantityString(R.plurals.chapters, chaptersCount, chaptersCount)
|
|
||||||
.lowercase(Locale.getDefault()),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class AllUnreadChapters(
|
|
||||||
override val chaptersIds: Set<Long>,
|
|
||||||
val branch: String?,
|
|
||||||
) : DownloadOption {
|
|
||||||
|
|
||||||
override val iconResId = R.drawable.ic_list_end
|
|
||||||
|
|
||||||
override fun getLabel(resources: Resources): CharSequence {
|
|
||||||
return if (branch == null) {
|
|
||||||
resources.getString(R.string.download_option_all_unread)
|
|
||||||
} else {
|
|
||||||
resources.getString(R.string.download_option_all_unread_b, branch)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class NextUnreadChapters(
|
|
||||||
override val chaptersIds: Set<Long>,
|
|
||||||
) : DownloadOption {
|
|
||||||
|
|
||||||
override val iconResId = R.drawable.ic_list_next
|
|
||||||
|
|
||||||
override fun getLabel(resources: Resources): CharSequence {
|
|
||||||
return resources.getString(
|
|
||||||
R.string.download_option_next_unread_n_chapters,
|
|
||||||
resources.getQuantityString(R.plurals.chapters, chaptersCount, chaptersCount)
|
|
||||||
.lowercase(Locale.getDefault()),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class SelectionHint : DownloadOption {
|
|
||||||
|
|
||||||
override val chaptersIds: Set<Long> = emptySet()
|
|
||||||
override val iconResId = R.drawable.ic_tap
|
|
||||||
|
|
||||||
override fun getLabel(resources: Resources): CharSequence {
|
|
||||||
return resources.getString(R.string.download_option_manual_selection)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,27 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.download.ui.dialog
|
|
||||||
|
|
||||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
|
||||||
import org.koitharu.kotatsu.R
|
|
||||||
import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
|
|
||||||
import org.koitharu.kotatsu.databinding.ItemDownloadOptionBinding
|
|
||||||
|
|
||||||
fun downloadOptionAD(
|
|
||||||
onClickListener: OnListItemClickListener<DownloadOption>,
|
|
||||||
) = adapterDelegateViewBinding<DownloadOption, DownloadOption, ItemDownloadOptionBinding>(
|
|
||||||
{ layoutInflater, parent -> ItemDownloadOptionBinding.inflate(layoutInflater, parent, false) },
|
|
||||||
) {
|
|
||||||
|
|
||||||
binding.root.setOnClickListener { v -> onClickListener.onItemClick(item, v) }
|
|
||||||
|
|
||||||
bind {
|
|
||||||
with(binding.root) {
|
|
||||||
title = item.getLabel(resources)
|
|
||||||
subtitle = if (item.chaptersCount == 0) null else resources.getQuantityString(
|
|
||||||
R.plurals.chapters,
|
|
||||||
item.chaptersCount,
|
|
||||||
item.chaptersCount,
|
|
||||||
)
|
|
||||||
setIconResource(item.iconResId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,73 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.worker
|
||||||
|
|
||||||
|
import android.os.Parcelable
|
||||||
|
import androidx.work.Data
|
||||||
|
import kotlinx.parcelize.Parcelize
|
||||||
|
import org.koitharu.kotatsu.core.prefs.DownloadFormat
|
||||||
|
import org.koitharu.kotatsu.parsers.util.find
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
@Parcelize
|
||||||
|
class DownloadTask(
|
||||||
|
val mangaId: Long,
|
||||||
|
val isPaused: Boolean,
|
||||||
|
val isSilent: Boolean,
|
||||||
|
val chaptersIds: LongArray?,
|
||||||
|
val destination: File?,
|
||||||
|
val format: DownloadFormat?,
|
||||||
|
) : Parcelable {
|
||||||
|
|
||||||
|
constructor(data: Data) : this(
|
||||||
|
mangaId = data.getLong(MANGA_ID, 0L),
|
||||||
|
isPaused = data.getBoolean(START_PAUSED, false),
|
||||||
|
isSilent = data.getBoolean(IS_SILENT, false),
|
||||||
|
chaptersIds = data.getLongArray(CHAPTERS)?.takeUnless(LongArray::isEmpty),
|
||||||
|
destination = data.getString(DESTINATION)?.let { File(it) },
|
||||||
|
format = data.getString(FORMAT)?.let { DownloadFormat.entries.find(it) },
|
||||||
|
)
|
||||||
|
|
||||||
|
fun toData(): Data = Data.Builder()
|
||||||
|
.putLong(MANGA_ID, mangaId)
|
||||||
|
.putBoolean(START_PAUSED, isPaused)
|
||||||
|
.putBoolean(IS_SILENT, isSilent)
|
||||||
|
.putLongArray(CHAPTERS, chaptersIds ?: LongArray(0))
|
||||||
|
.putString(DESTINATION, destination?.path)
|
||||||
|
.putString(FORMAT, format?.name)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
if (this === other) return true
|
||||||
|
if (javaClass != other?.javaClass) return false
|
||||||
|
|
||||||
|
other as DownloadTask
|
||||||
|
|
||||||
|
if (mangaId != other.mangaId) return false
|
||||||
|
if (isPaused != other.isPaused) return false
|
||||||
|
if (isSilent != other.isSilent) return false
|
||||||
|
if (!(chaptersIds contentEquals other.chaptersIds)) return false
|
||||||
|
if (destination != other.destination) return false
|
||||||
|
if (format != other.format) return false
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
var result = mangaId.hashCode()
|
||||||
|
result = 31 * result + isPaused.hashCode()
|
||||||
|
result = 31 * result + isSilent.hashCode()
|
||||||
|
result = 31 * result + (chaptersIds?.contentHashCode() ?: 0)
|
||||||
|
result = 31 * result + (destination?.hashCode() ?: 0)
|
||||||
|
result = 31 * result + (format?.hashCode() ?: 0)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
private companion object {
|
||||||
|
|
||||||
|
const val MANGA_ID = "manga_id"
|
||||||
|
const val IS_SILENT = "silent"
|
||||||
|
const val START_PAUSED = "paused"
|
||||||
|
const val CHAPTERS = "chapters"
|
||||||
|
const val DESTINATION = "dest"
|
||||||
|
const val FORMAT = "format"
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,224 @@
|
|||||||
|
<?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"
|
||||||
|
android:paddingBottom="?dialogPreferredPadding">
|
||||||
|
|
||||||
|
<ScrollView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:scrollIndicators="top|bottom"
|
||||||
|
tools:ignore="UnusedAttribute">
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/textView_summary"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="@dimen/margin_small"
|
||||||
|
android:paddingHorizontal="@dimen/margin_normal"
|
||||||
|
android:textAppearance="?textAppearanceBody2"
|
||||||
|
tools:text="@tools:sample/lorem[15]" />
|
||||||
|
|
||||||
|
<org.koitharu.kotatsu.core.ui.widgets.TwoLinesItemView
|
||||||
|
android:id="@+id/option_whole_manga"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="@dimen/margin_small"
|
||||||
|
android:checked="true"
|
||||||
|
android:drawablePadding="?android:listPreferredItemPaddingStart"
|
||||||
|
android:minHeight="?android:listPreferredItemHeightSmall"
|
||||||
|
android:paddingStart="?android:listPreferredItemPaddingStart"
|
||||||
|
android:paddingEnd="?android:listPreferredItemPaddingEnd"
|
||||||
|
app:icon="?android:listChoiceIndicatorSingle"
|
||||||
|
app:title="@string/download_option_whole_manga"
|
||||||
|
tools:subtitle="@string/no_chapters" />
|
||||||
|
|
||||||
|
<org.koitharu.kotatsu.core.ui.widgets.TwoLinesItemView
|
||||||
|
android:id="@+id/option_whole_branch"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:button="@drawable/ic_expand_more"
|
||||||
|
android:drawablePadding="?android:listPreferredItemPaddingStart"
|
||||||
|
android:minHeight="?android:listPreferredItemHeightSmall"
|
||||||
|
android:paddingStart="?android:listPreferredItemPaddingStart"
|
||||||
|
android:paddingEnd="?android:listPreferredItemPaddingEnd"
|
||||||
|
android:visibility="gone"
|
||||||
|
app:icon="?android:listChoiceIndicatorSingle"
|
||||||
|
tools:subtitle="@string/no_chapters"
|
||||||
|
tools:title="@string/download_option_all_chapters"
|
||||||
|
tools:visibility="visible" />
|
||||||
|
|
||||||
|
<org.koitharu.kotatsu.core.ui.widgets.TwoLinesItemView
|
||||||
|
android:id="@+id/option_first_chapters"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:button="@drawable/ic_expand_more"
|
||||||
|
android:drawablePadding="?android:listPreferredItemPaddingStart"
|
||||||
|
android:minHeight="?android:listPreferredItemHeightSmall"
|
||||||
|
android:paddingStart="?android:listPreferredItemPaddingStart"
|
||||||
|
android:paddingEnd="?android:listPreferredItemPaddingEnd"
|
||||||
|
android:visibility="gone"
|
||||||
|
app:icon="?android:listChoiceIndicatorSingle"
|
||||||
|
tools:title="@string/download_option_first_n_chapters"
|
||||||
|
tools:visibility="visible" />
|
||||||
|
|
||||||
|
<org.koitharu.kotatsu.core.ui.widgets.TwoLinesItemView
|
||||||
|
android:id="@+id/option_unread_chapters"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:button="@drawable/ic_expand_more"
|
||||||
|
android:drawablePadding="?android:listPreferredItemPaddingStart"
|
||||||
|
android:minHeight="?android:listPreferredItemHeightSmall"
|
||||||
|
android:paddingStart="?android:listPreferredItemPaddingStart"
|
||||||
|
android:paddingEnd="?android:listPreferredItemPaddingEnd"
|
||||||
|
android:visibility="gone"
|
||||||
|
app:icon="?android:listChoiceIndicatorSingle"
|
||||||
|
tools:title="@string/download_option_next_unread_n_chapters"
|
||||||
|
tools:visibility="visible" />
|
||||||
|
|
||||||
|
<com.google.android.material.progressindicator.LinearProgressIndicator
|
||||||
|
android:id="@+id/progressBar"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginHorizontal="@dimen/margin_normal"
|
||||||
|
android:layout_marginTop="@dimen/margin_small"
|
||||||
|
android:text="@string/chapter_selection_hint"
|
||||||
|
android:textAppearance="?attr/textAppearanceBodySmall" />
|
||||||
|
|
||||||
|
<com.google.android.material.materialswitch.MaterialSwitch
|
||||||
|
android:id="@+id/switch_start"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="?android:listPreferredItemHeightSmall"
|
||||||
|
android:layout_marginTop="@dimen/margin_normal"
|
||||||
|
android:checked="true"
|
||||||
|
android:drawablePadding="?android:listPreferredItemPaddingStart"
|
||||||
|
android:ellipsize="end"
|
||||||
|
android:paddingStart="?android:listPreferredItemPaddingStart"
|
||||||
|
android:paddingEnd="?android:listPreferredItemPaddingEnd"
|
||||||
|
android:singleLine="true"
|
||||||
|
android:text="@string/start_download"
|
||||||
|
android:textAppearance="?attr/textAppearanceButton"
|
||||||
|
android:textColor="?colorOnSurfaceVariant" />
|
||||||
|
|
||||||
|
<CheckedTextView
|
||||||
|
android:id="@+id/textView_more"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="?android:listPreferredItemHeightSmall"
|
||||||
|
android:background="@drawable/list_selector"
|
||||||
|
android:checked="false"
|
||||||
|
android:drawableEnd="@drawable/ic_expand_collapse"
|
||||||
|
android:drawablePadding="?android:listPreferredItemPaddingStart"
|
||||||
|
android:ellipsize="end"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
android:paddingStart="?android:listPreferredItemPaddingStart"
|
||||||
|
android:paddingEnd="?android:listPreferredItemPaddingEnd"
|
||||||
|
android:singleLine="true"
|
||||||
|
android:text="@string/more_options"
|
||||||
|
android:textAppearance="?attr/textAppearanceButton"
|
||||||
|
android:textColor="?colorOnSurfaceVariant" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/textView_destination"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="@dimen/margin_normal"
|
||||||
|
android:paddingHorizontal="@dimen/margin_normal"
|
||||||
|
android:text="@string/destination_directory"
|
||||||
|
android:textAppearance="?textAppearanceTitleSmall"
|
||||||
|
android:visibility="gone"
|
||||||
|
tools:visibility="visible" />
|
||||||
|
|
||||||
|
<com.google.android.material.card.MaterialCardView
|
||||||
|
android:id="@+id/card_destination"
|
||||||
|
style="?materialCardViewOutlinedStyle"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginHorizontal="16dp"
|
||||||
|
android:layout_marginTop="@dimen/margin_normal"
|
||||||
|
android:visibility="gone"
|
||||||
|
tools:visibility="visible">
|
||||||
|
|
||||||
|
<Spinner
|
||||||
|
android:id="@+id/spinner_destination"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:minHeight="@dimen/spinner_height"
|
||||||
|
android:paddingHorizontal="8dp" />
|
||||||
|
|
||||||
|
</com.google.android.material.card.MaterialCardView>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/textView_format"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="@dimen/margin_normal"
|
||||||
|
android:paddingHorizontal="@dimen/margin_normal"
|
||||||
|
android:text="@string/preferred_download_format"
|
||||||
|
android:textAppearance="?textAppearanceTitleSmall"
|
||||||
|
android:visibility="gone"
|
||||||
|
tools:visibility="visible" />
|
||||||
|
|
||||||
|
<com.google.android.material.card.MaterialCardView
|
||||||
|
android:id="@+id/card_format"
|
||||||
|
style="?materialCardViewOutlinedStyle"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginHorizontal="16dp"
|
||||||
|
android:layout_marginTop="@dimen/margin_normal"
|
||||||
|
android:visibility="gone"
|
||||||
|
tools:visibility="visible">
|
||||||
|
|
||||||
|
<Spinner
|
||||||
|
android:id="@+id/spinner_format"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:entries="@array/download_formats"
|
||||||
|
android:minHeight="@dimen/spinner_height"
|
||||||
|
android:paddingHorizontal="8dp" />
|
||||||
|
|
||||||
|
</com.google.android.material.card.MaterialCardView>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</ScrollView>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
style="?buttonBarStyle"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginHorizontal="?dialogPreferredPadding"
|
||||||
|
android:layout_marginTop="@dimen/margin_small"
|
||||||
|
android:gravity="end"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/button_cancel"
|
||||||
|
style="?buttonBarButtonStyle"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="@android:string/cancel" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/button_confirm"
|
||||||
|
style="?buttonBarButtonStyle"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="@string/save" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
@ -1,14 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<org.koitharu.kotatsu.core.ui.widgets.TwoLinesItemView
|
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
|
||||||
android:id="@+id/button_file"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:drawablePadding="?android:listPreferredItemPaddingStart"
|
|
||||||
android:minHeight="?android:listPreferredItemHeightSmall"
|
|
||||||
android:paddingStart="?android:listPreferredItemPaddingStart"
|
|
||||||
android:paddingEnd="?android:listPreferredItemPaddingEnd"
|
|
||||||
tools:subtitle="@string/chapters"
|
|
||||||
tools:title="@string/download_option_whole_manga" />
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue