From dd214f98fc3353a0ed046bce5c46ca36789a7254 Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 16 Dec 2019 20:37:34 -0800 Subject: [PATCH] Filter Extensions By Language Co-Authored-By: MCAxiaz --- .../data/preference/PreferencesHelper.kt | 3 +- .../ui/extension/ExtensionController.kt | 28 +++++++++- .../ui/extension/ExtensionPresenter.kt | 11 ++-- .../extension/SettingsExtensionsController.kt | 52 +++++++++++++++++++ app/src/main/res/menu/extension_main.xml | 6 +++ 5 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/extension/SettingsExtensionsController.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index 69cee9c5a9..dad085346b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.track.TrackService import eu.kanade.tachiyomi.source.Source import java.io.File +import java.util.Locale import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys fun Preference.getOrDefault(): T = get() ?: defaultValue()!! @@ -95,7 +96,7 @@ class PreferencesHelper(val context: Context) { fun catalogueAsList() = rxPrefs.getBoolean(Keys.catalogueAsList, false) - fun enabledLanguages() = rxPrefs.getStringSet(Keys.enabledLanguages, setOf("en")) + fun enabledLanguages() = rxPrefs.getStringSet(Keys.enabledLanguages, setOf("en", Locale.getDefault().language)) fun sourceUsername(source: Source) = prefs.getString(Keys.sourceUsername(source.id), "") diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionController.kt index 26e539e71b..71b7999307 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionController.kt @@ -1,12 +1,16 @@ package eu.kanade.tachiyomi.ui.extension -import androidx.recyclerview.widget.LinearLayoutManager -import androidx.appcompat.widget.SearchView import android.view.LayoutInflater import android.view.Menu import android.view.MenuInflater +import android.view.MenuItem import android.view.View import android.view.ViewGroup +import androidx.appcompat.widget.SearchView +import com.bluelinelabs.conductor.ControllerChangeHandler +import com.bluelinelabs.conductor.ControllerChangeType +import com.bluelinelabs.conductor.RouterTransaction +import com.bluelinelabs.conductor.changehandler.FadeChangeHandler import com.jakewharton.rxbinding.support.v4.widget.refreshes import com.jakewharton.rxbinding.support.v7.widget.queryTextChanges import eu.davidea.flexibleadapter.FlexibleAdapter @@ -75,6 +79,25 @@ open class ExtensionController : NucleusController(), super.onDestroyView(view) } + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.action_settings -> { + router.pushController((RouterTransaction.with(SettingsExtensionsController())) + .popChangeHandler(SettingsExtensionsFadeChangeHandler()) + .pushChangeHandler(FadeChangeHandler())) + } + else -> return super.onOptionsItemSelected(item) + } + return true + } + + override fun onChangeStarted(handler: ControllerChangeHandler, type: ControllerChangeType) { + super.onChangeStarted(handler, type) + if (!type.isPush && handler is SettingsExtensionsFadeChangeHandler) { + presenter.findAvailableExtensions() + } + } + override fun onButtonClick(position: Int) { val extension = (adapter?.getItem(position) as? ExtensionItem)?.extension ?: return when (extension) { @@ -175,4 +198,5 @@ open class ExtensionController : NucleusController(), presenter.uninstallExtension(pkgName) } + class SettingsExtensionsFadeChangeHandler : FadeChangeHandler() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionPresenter.kt index 04ff5f3ce7..3dd819bfb6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionPresenter.kt @@ -3,6 +3,8 @@ package eu.kanade.tachiyomi.ui.extension import android.app.Application import android.os.Bundle import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.extension.ExtensionManager import eu.kanade.tachiyomi.extension.model.Extension import eu.kanade.tachiyomi.extension.model.InstallStep @@ -22,7 +24,8 @@ private typealias ExtensionTuple * Presenter of [ExtensionController]. */ open class ExtensionPresenter( - private val extensionManager: ExtensionManager = Injekt.get() + private val extensionManager: ExtensionManager = Injekt.get(), + private val preferences: PreferencesHelper = Injekt.get() ) : BasePresenter() { private var extensions = emptyList() @@ -53,6 +56,7 @@ open class ExtensionPresenter( @Synchronized private fun toItems(tuple: ExtensionTuple): List { val context = Injekt.get() + val activeLangs = preferences.enabledLanguages().getOrDefault() val (installed, untrusted, available) = tuple @@ -61,9 +65,10 @@ open class ExtensionPresenter( val installedSorted = installed.sortedWith(compareBy({ !it.hasUpdate }, { it.pkgName })) val untrustedSorted = untrusted.sortedBy { it.pkgName } val availableSorted = available - // Filter out already installed extensions + // Filter out already installed extensions and disabled languages .filter { avail -> installed.none { it.pkgName == avail.pkgName } - && untrusted.none { it.pkgName == avail.pkgName } } + && untrusted.none { it.pkgName == avail.pkgName } + && (avail.lang in activeLangs || avail.lang == "all")} .sortedBy { it.pkgName } if (installedSorted.isNotEmpty() || untrustedSorted.isNotEmpty()) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/extension/SettingsExtensionsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/extension/SettingsExtensionsController.kt new file mode 100644 index 0000000000..378d87b894 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/extension/SettingsExtensionsController.kt @@ -0,0 +1,52 @@ +package eu.kanade.tachiyomi.ui.extension + +import androidx.preference.PreferenceScreen +import androidx.preference.SwitchPreference +import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.preference.getOrDefault +import eu.kanade.tachiyomi.extension.ExtensionManager +import eu.kanade.tachiyomi.ui.setting.SettingsController +import eu.kanade.tachiyomi.ui.setting.onChange +import eu.kanade.tachiyomi.ui.setting.titleRes +import eu.kanade.tachiyomi.util.LocaleHelper +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get + +class SettingsExtensionsController: SettingsController() { + + override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) { + titleRes = R.string.action_filter + + val activeLangs = preferences.enabledLanguages().getOrDefault() + + val availableLangs = + Injekt.get().availableExtensions.groupBy { + it.lang + }.keys.minus("all").partition { + it in activeLangs + }.let { + it.first + it.second + } + + availableLangs.forEach { + SwitchPreference(context).apply { + preferenceScreen.addPreference(this) + title = LocaleHelper.getDisplayName(it, context) + isPersistent = false + isChecked = it in activeLangs + + onChange { newValue -> + val checked = newValue as Boolean + val currentActiveLangs = preferences.enabledLanguages().getOrDefault() + + if (checked) { + preferences.enabledLanguages().set(currentActiveLangs + it) + } else { + preferences.enabledLanguages().set(currentActiveLangs - it) + } + true + } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/res/menu/extension_main.xml b/app/src/main/res/menu/extension_main.xml index 994ae2fbc2..7a6ffffbeb 100644 --- a/app/src/main/res/menu/extension_main.xml +++ b/app/src/main/res/menu/extension_main.xml @@ -8,4 +8,10 @@ app:showAsAction="collapseActionView|ifRoom" app:actionViewClass="androidx.appcompat.widget.SearchView"/> + +