From 83bd390c2a7588a50933184332838334bb4c6dc2 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Fri, 15 Mar 2024 11:34:10 +0200 Subject: [PATCH] Add enable toggle to source settings --- .../kotatsu/core/db/dao/MangaSourcesDao.kt | 3 +++ .../explore/data/MangaSourcesRepository.kt | 4 ++++ .../kotatsu/list/ui/adapter/ErrorFooterAD.kt | 1 - .../kotatsu/list/ui/model/ErrorFooter.kt | 1 - .../list/ui/model/ListModelConversionExt.kt | 1 - .../sources/SourceSettingsFragment.kt | 19 ++++++++++++++++++- .../sources/SourceSettingsViewModel.kt | 9 +++++++++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/pref_source.xml | 8 ++++++++ 9 files changed, 43 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/db/dao/MangaSourcesDao.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/db/dao/MangaSourcesDao.kt index 9401d5030..342050f63 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/db/dao/MangaSourcesDao.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/db/dao/MangaSourcesDao.kt @@ -29,6 +29,9 @@ abstract class MangaSourcesDao { @Query("SELECT * FROM sources ORDER BY sort_key") abstract fun observeAll(): Flow> + @Query("SELECT enabled FROM sources WHERE source = :source") + abstract fun observeIsEnabled(source: String): Flow + @Query("SELECT IFNULL(MAX(sort_key),0) FROM sources") abstract suspend fun getMaxSortKey(): Int diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt index 884689c9a..7bd81a89d 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt @@ -52,6 +52,10 @@ class MangaSourcesRepository @Inject constructor( return dao.findAllDisabled().toSources(settings.isNsfwContentDisabled, null) } + fun observeIsEnabled(source: MangaSource): Flow { + return dao.observeIsEnabled(source.name) + } + fun observeEnabledSourcesCount(): Flow { return combine( observeIsNsfwDisabled(), diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/adapter/ErrorFooterAD.kt b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/adapter/ErrorFooterAD.kt index aa1a018f7..2bdf5a434 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/adapter/ErrorFooterAD.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/adapter/ErrorFooterAD.kt @@ -20,6 +20,5 @@ fun errorFooterAD( bind { binding.textViewTitle.text = item.exception.getDisplayMessage(context.resources) - binding.imageViewIcon.setImageResource(item.icon) } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ErrorFooter.kt b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ErrorFooter.kt index 4cdbb9c1b..55e414919 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ErrorFooter.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ErrorFooter.kt @@ -4,7 +4,6 @@ import androidx.annotation.DrawableRes data class ErrorFooter( val exception: Throwable, - @DrawableRes val icon: Int ) : ListModel { override fun areItemsTheSame(other: ListModel): Boolean { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ListModelConversionExt.kt b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ListModelConversionExt.kt index 2ecb9c6db..a4fbe8e18 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ListModelConversionExt.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ListModelConversionExt.kt @@ -83,5 +83,4 @@ fun Throwable.toErrorState(canRetry: Boolean = true) = ErrorState( fun Throwable.toErrorFooter() = ErrorFooter( exception = this, - icon = R.drawable.ic_alert_outline, ) 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 3e0ddf42d..460bdabcc 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 @@ -4,6 +4,7 @@ import android.os.Bundle import android.view.View import androidx.fragment.app.viewModels import androidx.preference.Preference +import androidx.preference.SwitchPreferenceCompat import dagger.hilt.android.AndroidEntryPoint import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.exceptions.resolve.ExceptionResolver @@ -18,7 +19,7 @@ import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.settings.sources.auth.SourceAuthActivity @AndroidEntryPoint -class SourceSettingsFragment : BasePreferenceFragment(0) { +class SourceSettingsFragment : BasePreferenceFragment(0), Preference.OnPreferenceChangeListener { private val viewModel: SourceSettingsViewModel by viewModels() private val exceptionResolver = ExceptionResolver(this) @@ -34,6 +35,9 @@ class SourceSettingsFragment : BasePreferenceFragment(0) { addPreferencesFromResource(R.xml.pref_source) addPreferencesFromRepository(viewModel.repository) + findPreference(KEY_ENABLE)?.run { + setOnPreferenceChangeListener(this@SourceSettingsFragment) + } findPreference(KEY_AUTH)?.run { val authProvider = viewModel.repository.getAuthProvider() isVisible = authProvider != null @@ -59,6 +63,9 @@ class SourceSettingsFragment : BasePreferenceFragment(0) { viewModel.isLoading.observe(viewLifecycleOwner) { isLoading -> findPreference(KEY_AUTH)?.isEnabled = !isLoading } + viewModel.isEnabled.observe(viewLifecycleOwner) { enabled -> + findPreference(KEY_ENABLE)?.isChecked = enabled + } viewModel.onActionDone.observeEvent(viewLifecycleOwner, ReversibleActionObserver(listView)) } @@ -68,6 +75,7 @@ class SourceSettingsFragment : BasePreferenceFragment(0) { startActivity(SourceAuthActivity.newIntent(preference.context, viewModel.source)) true } + AppSettings.KEY_COOKIES_CLEAR -> { viewModel.clearCookies() true @@ -77,9 +85,18 @@ class SourceSettingsFragment : BasePreferenceFragment(0) { } } + override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean { + when (preference.key) { + KEY_ENABLE -> viewModel.setEnabled(newValue == true) + else -> return false + } + return true + } + companion object { private const val KEY_AUTH = "auth" + private const val KEY_ENABLE = "enable" const val EXTRA_SOURCE = "source" 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 484547b76..d30e43f89 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 @@ -17,6 +17,7 @@ import org.koitharu.kotatsu.core.ui.util.ReversibleAction import org.koitharu.kotatsu.core.util.ext.MutableEventFlow import org.koitharu.kotatsu.core.util.ext.call import org.koitharu.kotatsu.core.util.ext.require +import org.koitharu.kotatsu.explore.data.MangaSourcesRepository import org.koitharu.kotatsu.parsers.config.ConfigKey import org.koitharu.kotatsu.parsers.exception.AuthRequiredException import org.koitharu.kotatsu.parsers.model.MangaSource @@ -27,6 +28,7 @@ class SourceSettingsViewModel @Inject constructor( savedStateHandle: SavedStateHandle, mangaRepositoryFactory: MangaRepository.Factory, private val cookieJar: MutableCookieJar, + private val mangaSourcesRepository: MangaSourcesRepository, ) : BaseViewModel(), SharedPreferences.OnSharedPreferenceChangeListener { val source = savedStateHandle.require(SourceSettingsFragment.EXTRA_SOURCE) @@ -34,6 +36,7 @@ class SourceSettingsViewModel @Inject constructor( val onActionDone = MutableEventFlow() val username = MutableStateFlow(null) + val isEnabled = mangaSourcesRepository.observeIsEnabled(source) private var usernameLoadJob: Job? = null init { @@ -70,6 +73,12 @@ class SourceSettingsViewModel @Inject constructor( } } + fun setEnabled(value: Boolean) { + launchJob(Dispatchers.Default) { + mangaSourcesRepository.setSourceEnabled(source, value) + } + } + private fun loadUsername() { launchLoadingJob(Dispatchers.Default) { try { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f13ebc64f..42451c9b5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -636,4 +636,5 @@ Oldest Long time ago read Unread + Enable source diff --git a/app/src/main/res/xml/pref_source.xml b/app/src/main/res/xml/pref_source.xml index 21e5cca6c..e27b1eb4a 100644 --- a/app/src/main/res/xml/pref_source.xml +++ b/app/src/main/res/xml/pref_source.xml @@ -3,6 +3,14 @@ xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> + +