Add enable toggle to source settings

This commit is contained in:
Koitharu
2024-03-15 11:34:10 +02:00
parent b090652007
commit 83bd390c2a
9 changed files with 43 additions and 4 deletions

View File

@@ -29,6 +29,9 @@ abstract class MangaSourcesDao {
@Query("SELECT * FROM sources ORDER BY sort_key")
abstract fun observeAll(): Flow<List<MangaSourceEntity>>
@Query("SELECT enabled FROM sources WHERE source = :source")
abstract fun observeIsEnabled(source: String): Flow<Boolean>
@Query("SELECT IFNULL(MAX(sort_key),0) FROM sources")
abstract suspend fun getMaxSortKey(): Int

View File

@@ -52,6 +52,10 @@ class MangaSourcesRepository @Inject constructor(
return dao.findAllDisabled().toSources(settings.isNsfwContentDisabled, null)
}
fun observeIsEnabled(source: MangaSource): Flow<Boolean> {
return dao.observeIsEnabled(source.name)
}
fun observeEnabledSourcesCount(): Flow<Int> {
return combine(
observeIsNsfwDisabled(),

View File

@@ -20,6 +20,5 @@ fun errorFooterAD(
bind {
binding.textViewTitle.text = item.exception.getDisplayMessage(context.resources)
binding.imageViewIcon.setImageResource(item.icon)
}
}

View File

@@ -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 {

View File

@@ -83,5 +83,4 @@ fun Throwable.toErrorState(canRetry: Boolean = true) = ErrorState(
fun Throwable.toErrorFooter() = ErrorFooter(
exception = this,
icon = R.drawable.ic_alert_outline,
)

View File

@@ -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<SwitchPreferenceCompat>(KEY_ENABLE)?.run {
setOnPreferenceChangeListener(this@SourceSettingsFragment)
}
findPreference<Preference>(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<Preference>(KEY_AUTH)?.isEnabled = !isLoading
}
viewModel.isEnabled.observe(viewLifecycleOwner) { enabled ->
findPreference<SwitchPreferenceCompat>(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"

View File

@@ -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<MangaSource>(SourceSettingsFragment.EXTRA_SOURCE)
@@ -34,6 +36,7 @@ class SourceSettingsViewModel @Inject constructor(
val onActionDone = MutableEventFlow<ReversibleAction>()
val username = MutableStateFlow<String?>(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 {

View File

@@ -636,4 +636,5 @@
<string name="order_oldest">Oldest</string>
<string name="long_ago_read">Long time ago read</string>
<string name="unread">Unread</string>
<string name="enable_source">Enable source</string>
</resources>

View File

@@ -3,6 +3,14 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<SwitchPreferenceCompat
android:defaultValue="false"
android:key="enable"
android:layout="@layout/preference_toggle_header"
android:order="0"
android:persistent="false"
android:title="@string/enable_source" />
<Preference
android:key="auth"
android:order="100"