From dd77926dcb73c4f42f5005d2f1ea02755deb3665 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Wed, 30 Jul 2025 14:39:12 +0300 Subject: [PATCH] Improve sources settings --- .../kotatsu/core/prefs/SourceSettings.kt | 5 ++- .../kotatsu/details/ui/ReadButtonDelegate.kt | 2 +- .../sources/SourceSettingsFragment.kt | 43 +++++++++++++++++++ .../sources/SourceSettingsViewModel.kt | 3 +- 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/SourceSettings.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/SourceSettings.kt index ab620658d..7a0180859 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/SourceSettings.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/SourceSettings.kt @@ -66,8 +66,9 @@ class SourceSettings(context: Context, source: MangaSource) : MangaSourceConfig companion object { - const val KEY_SORT_ORDER = "sort_order" - const val KEY_SLOWDOWN = "slowdown" + const val KEY_DOMAIN = "domain" const val KEY_NO_CAPTCHA = "no_captcha" + const val KEY_SLOWDOWN = "slowdown" + const val KEY_SORT_ORDER = "sort_order" } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/ReadButtonDelegate.kt b/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/ReadButtonDelegate.kt index 2a1054ac8..a697e873b 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/ReadButtonDelegate.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/ReadButtonDelegate.kt @@ -106,7 +106,7 @@ class ReadButtonDelegate( } private fun openReader(isIncognitoMode: Boolean) { - val manga = viewModel.manga.value ?: return + val manga = viewModel.getMangaOrNull() ?: return if (viewModel.historyInfo.value.isChapterMissing) { Snackbar.make(buttonRead, R.string.chapter_is_missing, Snackbar.LENGTH_SHORT) .show() // TODO diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourceSettingsFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourceSettingsFragment.kt index 7bf90f703..73040d6cd 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourceSettingsFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourceSettingsFragment.kt @@ -2,7 +2,10 @@ package org.koitharu.kotatsu.settings.sources import android.os.Bundle import android.view.View +import androidx.appcompat.app.AlertDialog import androidx.fragment.app.viewModels +import androidx.preference.EditTextPreference +import androidx.preference.EditTextPreferenceDialogFragmentCompat import androidx.preference.Preference import androidx.preference.SwitchPreferenceCompat import dagger.hilt.android.AndroidEntryPoint @@ -112,6 +115,20 @@ class SourceSettingsFragment : BasePreferenceFragment(0), Preference.OnPreferenc } } + override fun onDisplayPreferenceDialog(preference: Preference) { + if (preference.key == SourceSettings.KEY_DOMAIN) { + if (parentFragmentManager.findFragmentByTag(DomainDialogFragment.DIALOG_FRAGMENT_TAG) != null) { + return + } + val f = DomainDialogFragment.newInstance(preference.key) + @Suppress("DEPRECATION") + f.setTargetFragment(this, 0) + f.show(parentFragmentManager, DomainDialogFragment.DIALOG_FRAGMENT_TAG) + return + } + super.onDisplayPreferenceDialog(preference) + } + override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean { when (preference.key) { KEY_ENABLE -> viewModel.setEnabled(newValue == true) @@ -120,6 +137,32 @@ class SourceSettingsFragment : BasePreferenceFragment(0), Preference.OnPreferenc return true } + class DomainDialogFragment : EditTextPreferenceDialogFragmentCompat() { + + override fun onPrepareDialogBuilder(builder: AlertDialog.Builder) { + super.onPrepareDialogBuilder(builder) + builder.setNeutralButton(R.string.reset) { _, _ -> + resetValue() + } + } + + private fun resetValue() { + val editTextPreference = preference as EditTextPreference + if (editTextPreference.callChangeListener("")) { + editTextPreference.text = "" + } + } + + companion object { + + const val DIALOG_FRAGMENT_TAG: String = "androidx.preference.PreferenceFragment.DIALOG" + + fun newInstance(key: String) = DomainDialogFragment().withArgs(1) { + putString(ARG_KEY, key) + } + } + } + companion object { private const val KEY_AUTH = "auth" diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourceSettingsViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourceSettingsViewModel.kt index bcd57bc61..d92546aa1 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourceSettingsViewModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourceSettingsViewModel.kt @@ -14,7 +14,6 @@ import org.koitharu.kotatsu.core.network.cookies.MutableCookieJar import org.koitharu.kotatsu.core.parser.CachingMangaRepository import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.parser.ParserMangaRepository -import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.SourceSettings import org.koitharu.kotatsu.core.ui.BaseViewModel import org.koitharu.kotatsu.core.ui.util.ReversibleAction @@ -69,7 +68,7 @@ class SourceSettingsViewModel @Inject constructor( } } if (repository is ParserMangaRepository) { - if (key == AppSettings.KEY_OPEN_BROWSER) { + if (key == SourceSettings.KEY_DOMAIN) { browserUrl.value = "https://${repository.domain}" } }