Add enable toggle to source settings

master
Koitharu 2 years ago
parent b090652007
commit 83bd390c2a
Signed by: Koitharu
GPG Key ID: 676DEE768C17A9D7

@ -29,6 +29,9 @@ abstract class MangaSourcesDao {
@Query("SELECT * FROM sources ORDER BY sort_key") @Query("SELECT * FROM sources ORDER BY sort_key")
abstract fun observeAll(): Flow<List<MangaSourceEntity>> 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") @Query("SELECT IFNULL(MAX(sort_key),0) FROM sources")
abstract suspend fun getMaxSortKey(): Int abstract suspend fun getMaxSortKey(): Int

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

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

@ -4,7 +4,6 @@ import androidx.annotation.DrawableRes
data class ErrorFooter( data class ErrorFooter(
val exception: Throwable, val exception: Throwable,
@DrawableRes val icon: Int
) : ListModel { ) : ListModel {
override fun areItemsTheSame(other: ListModel): Boolean { override fun areItemsTheSame(other: ListModel): Boolean {

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

@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.View import android.view.View
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.SwitchPreferenceCompat
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.exceptions.resolve.ExceptionResolver 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 import org.koitharu.kotatsu.settings.sources.auth.SourceAuthActivity
@AndroidEntryPoint @AndroidEntryPoint
class SourceSettingsFragment : BasePreferenceFragment(0) { class SourceSettingsFragment : BasePreferenceFragment(0), Preference.OnPreferenceChangeListener {
private val viewModel: SourceSettingsViewModel by viewModels() private val viewModel: SourceSettingsViewModel by viewModels()
private val exceptionResolver = ExceptionResolver(this) private val exceptionResolver = ExceptionResolver(this)
@ -34,6 +35,9 @@ class SourceSettingsFragment : BasePreferenceFragment(0) {
addPreferencesFromResource(R.xml.pref_source) addPreferencesFromResource(R.xml.pref_source)
addPreferencesFromRepository(viewModel.repository) addPreferencesFromRepository(viewModel.repository)
findPreference<SwitchPreferenceCompat>(KEY_ENABLE)?.run {
setOnPreferenceChangeListener(this@SourceSettingsFragment)
}
findPreference<Preference>(KEY_AUTH)?.run { findPreference<Preference>(KEY_AUTH)?.run {
val authProvider = viewModel.repository.getAuthProvider() val authProvider = viewModel.repository.getAuthProvider()
isVisible = authProvider != null isVisible = authProvider != null
@ -59,6 +63,9 @@ class SourceSettingsFragment : BasePreferenceFragment(0) {
viewModel.isLoading.observe(viewLifecycleOwner) { isLoading -> viewModel.isLoading.observe(viewLifecycleOwner) { isLoading ->
findPreference<Preference>(KEY_AUTH)?.isEnabled = !isLoading findPreference<Preference>(KEY_AUTH)?.isEnabled = !isLoading
} }
viewModel.isEnabled.observe(viewLifecycleOwner) { enabled ->
findPreference<SwitchPreferenceCompat>(KEY_ENABLE)?.isChecked = enabled
}
viewModel.onActionDone.observeEvent(viewLifecycleOwner, ReversibleActionObserver(listView)) viewModel.onActionDone.observeEvent(viewLifecycleOwner, ReversibleActionObserver(listView))
} }
@ -68,6 +75,7 @@ class SourceSettingsFragment : BasePreferenceFragment(0) {
startActivity(SourceAuthActivity.newIntent(preference.context, viewModel.source)) startActivity(SourceAuthActivity.newIntent(preference.context, viewModel.source))
true true
} }
AppSettings.KEY_COOKIES_CLEAR -> { AppSettings.KEY_COOKIES_CLEAR -> {
viewModel.clearCookies() viewModel.clearCookies()
true 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 { companion object {
private const val KEY_AUTH = "auth" private const val KEY_AUTH = "auth"
private const val KEY_ENABLE = "enable"
const val EXTRA_SOURCE = "source" const val EXTRA_SOURCE = "source"

@ -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.MutableEventFlow
import org.koitharu.kotatsu.core.util.ext.call import org.koitharu.kotatsu.core.util.ext.call
import org.koitharu.kotatsu.core.util.ext.require 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.config.ConfigKey
import org.koitharu.kotatsu.parsers.exception.AuthRequiredException import org.koitharu.kotatsu.parsers.exception.AuthRequiredException
import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.parsers.model.MangaSource
@ -27,6 +28,7 @@ class SourceSettingsViewModel @Inject constructor(
savedStateHandle: SavedStateHandle, savedStateHandle: SavedStateHandle,
mangaRepositoryFactory: MangaRepository.Factory, mangaRepositoryFactory: MangaRepository.Factory,
private val cookieJar: MutableCookieJar, private val cookieJar: MutableCookieJar,
private val mangaSourcesRepository: MangaSourcesRepository,
) : BaseViewModel(), SharedPreferences.OnSharedPreferenceChangeListener { ) : BaseViewModel(), SharedPreferences.OnSharedPreferenceChangeListener {
val source = savedStateHandle.require<MangaSource>(SourceSettingsFragment.EXTRA_SOURCE) val source = savedStateHandle.require<MangaSource>(SourceSettingsFragment.EXTRA_SOURCE)
@ -34,6 +36,7 @@ class SourceSettingsViewModel @Inject constructor(
val onActionDone = MutableEventFlow<ReversibleAction>() val onActionDone = MutableEventFlow<ReversibleAction>()
val username = MutableStateFlow<String?>(null) val username = MutableStateFlow<String?>(null)
val isEnabled = mangaSourcesRepository.observeIsEnabled(source)
private var usernameLoadJob: Job? = null private var usernameLoadJob: Job? = null
init { init {
@ -70,6 +73,12 @@ class SourceSettingsViewModel @Inject constructor(
} }
} }
fun setEnabled(value: Boolean) {
launchJob(Dispatchers.Default) {
mangaSourcesRepository.setSourceEnabled(source, value)
}
}
private fun loadUsername() { private fun loadUsername() {
launchLoadingJob(Dispatchers.Default) { launchLoadingJob(Dispatchers.Default) {
try { try {

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

@ -3,6 +3,14 @@
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> 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 <Preference
android:key="auth" android:key="auth"
android:order="100" android:order="100"

Loading…
Cancel
Save