From 62d3fc65e0a73645439a9632838197a58c2a9f8c Mon Sep 17 00:00:00 2001 From: MCAxiaz Date: Sat, 11 Jan 2020 12:06:54 -0800 Subject: [PATCH] Filter Extensions By Language (#2275) * add options menu item to filer extensions by languages * resolve merge conflicts changes per pr comments Co-authored-by: arkon --- .../data/preference/PreferencesHelper.kt | 3 +- .../ui/extension/ExtensionController.kt | 25 +++++++++ .../ui/extension/ExtensionPresenter.kt | 11 ++-- .../extension/SettingsExtensionsController.kt | 52 +++++++++++++++++++ app/src/main/res/menu/extension_main.xml | 6 +++ app/src/main/res/values/strings.xml | 1 + 6 files changed, 94 insertions(+), 4 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 68e6371ee7..c988225817 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 823cd77873..33a9085ab5 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 @@ -5,8 +5,13 @@ 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 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 @@ -73,6 +78,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) { @@ -173,4 +197,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..fda29d66e8 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..df9817dc06 --- /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 android.support.v7.preference.PreferenceScreen +import android.support.v7.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.ext_settings + + 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..2d88c505fc 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"/> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7318d62f8e..9065a407dd 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -176,6 +176,7 @@ Version: %1$s Language: %1$s No preferences to edit for this extension + Settings Fullscreen