Filter Extensions By Language

Co-Authored-By: MCAxiaz <mcaxiaz@gmail.com>
This commit is contained in:
Jay 2019-12-16 20:37:34 -08:00
parent af8ddac007
commit dd214f98fc
5 changed files with 94 additions and 6 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

@ -1,12 +1,16 @@
package eu.kanade.tachiyomi.ui.extension package eu.kanade.tachiyomi.ui.extension
import androidx.recyclerview.widget.LinearLayoutManager
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 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.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
@ -75,6 +79,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) {
@ -175,4 +198,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 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<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/action_filter"
android:icon="@drawable/ic_filter_list_white_24dp"
app:showAsAction="always"/>
</menu> </menu>