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 <arkon@users.noreply.github.com>
This commit is contained in:
MCAxiaz 2020-01-11 12:06:54 -08:00 committed by arkon
parent 262ad45b79
commit 62d3fc65e0
6 changed files with 94 additions and 4 deletions

View File

@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.track.TrackService import eu.kanade.tachiyomi.data.track.TrackService
import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.Source
import java.io.File import java.io.File
import java.util.Locale
import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
fun <T> Preference<T>.getOrDefault(): T = get() ?: defaultValue()!! fun <T> Preference<T>.getOrDefault(): T = get() ?: defaultValue()!!
@ -95,7 +96,7 @@ class PreferencesHelper(val context: Context) {
fun catalogueAsList() = rxPrefs.getBoolean(Keys.catalogueAsList, false) 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), "") fun sourceUsername(source: Source) = prefs.getString(Keys.sourceUsername(source.id), "")

View File

@ -5,8 +5,13 @@ import androidx.appcompat.widget.SearchView
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.Menu import android.view.Menu
import android.view.MenuInflater import android.view.MenuInflater
import android.view.MenuItem
import android.view.View import android.view.View
import android.view.ViewGroup 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.v4.widget.refreshes
import com.jakewharton.rxbinding.support.v7.widget.queryTextChanges import com.jakewharton.rxbinding.support.v7.widget.queryTextChanges
import eu.davidea.flexibleadapter.FlexibleAdapter import eu.davidea.flexibleadapter.FlexibleAdapter
@ -73,6 +78,25 @@ open class ExtensionController : NucleusController<ExtensionPresenter>(),
super.onDestroyView(view) 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) { override fun onButtonClick(position: Int) {
val extension = (adapter?.getItem(position) as? ExtensionItem)?.extension ?: return val extension = (adapter?.getItem(position) as? ExtensionItem)?.extension ?: return
when (extension) { when (extension) {
@ -173,4 +197,5 @@ open class ExtensionController : NucleusController<ExtensionPresenter>(),
presenter.uninstallExtension(pkgName) presenter.uninstallExtension(pkgName)
} }
class SettingsExtensionsFadeChangeHandler : FadeChangeHandler()
} }

View File

@ -3,6 +3,8 @@ package eu.kanade.tachiyomi.ui.extension
import android.app.Application import android.app.Application
import android.os.Bundle import android.os.Bundle
import eu.kanade.tachiyomi.R 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.ExtensionManager
import eu.kanade.tachiyomi.extension.model.Extension import eu.kanade.tachiyomi.extension.model.Extension
import eu.kanade.tachiyomi.extension.model.InstallStep import eu.kanade.tachiyomi.extension.model.InstallStep
@ -22,7 +24,8 @@ private typealias ExtensionTuple
* Presenter of [ExtensionController]. * Presenter of [ExtensionController].
*/ */
open class ExtensionPresenter( open class ExtensionPresenter(
private val extensionManager: ExtensionManager = Injekt.get() private val extensionManager: ExtensionManager = Injekt.get(),
private val preferences: PreferencesHelper = Injekt.get()
) : BasePresenter<ExtensionController>() { ) : BasePresenter<ExtensionController>() {
private var extensions = emptyList<ExtensionItem>() private var extensions = emptyList<ExtensionItem>()
@ -53,6 +56,7 @@ open class ExtensionPresenter(
@Synchronized @Synchronized
private fun toItems(tuple: ExtensionTuple): List<ExtensionItem> { private fun toItems(tuple: ExtensionTuple): List<ExtensionItem> {
val context = Injekt.get<Application>() val context = Injekt.get<Application>()
val activeLangs = preferences.enabledLanguages().getOrDefault()
val (installed, untrusted, available) = tuple val (installed, untrusted, available) = tuple
@ -61,9 +65,10 @@ open class ExtensionPresenter(
val installedSorted = installed.sortedWith(compareBy({ !it.hasUpdate }, { it.pkgName })) val installedSorted = installed.sortedWith(compareBy({ !it.hasUpdate }, { it.pkgName }))
val untrustedSorted = untrusted.sortedBy { it.pkgName } val untrustedSorted = untrusted.sortedBy { it.pkgName }
val availableSorted = available val availableSorted = available
// Filter out already installed extensions // Filter out already installed extensions and disabled languages
.filter { avail -> installed.none { it.pkgName == avail.pkgName } .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 } .sortedBy { it.pkgName }
if (installedSorted.isNotEmpty() || untrustedSorted.isNotEmpty()) { if (installedSorted.isNotEmpty() || untrustedSorted.isNotEmpty()) {

View File

@ -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<ExtensionManager>().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
}
}
}
}
}

View File

@ -8,4 +8,10 @@
app:showAsAction="collapseActionView|ifRoom" app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="androidx.appcompat.widget.SearchView"/> app:actionViewClass="androidx.appcompat.widget.SearchView"/>
<item
android:id="@+id/action_settings"
android:title="@string/ext_settings"
android:icon="@drawable/ic_settings_white_24dp"
app:showAsAction="always"/>
</menu> </menu>

View File

@ -176,6 +176,7 @@
<string name="ext_version_info">Version: %1$s</string> <string name="ext_version_info">Version: %1$s</string>
<string name="ext_language_info">Language: %1$s</string> <string name="ext_language_info">Language: %1$s</string>
<string name="ext_empty_preferences">No preferences to edit for this extension</string> <string name="ext_empty_preferences">No preferences to edit for this extension</string>
<string name="ext_settings">Settings</string>
<!-- Reader section --> <!-- Reader section -->
<string name="pref_fullscreen">Fullscreen</string> <string name="pref_fullscreen">Fullscreen</string>