Ver código fonte

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 <[email protected]>
MCAxiaz 5 anos atrás
pai
commit
62d3fc65e0

+ 2 - 1
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 <T> Preference<T>.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), "")
 

+ 25 - 0
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<ExtensionPresenter>(),
         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<ExtensionPresenter>(),
         presenter.uninstallExtension(pkgName)
     }
 
+    class SettingsExtensionsFadeChangeHandler : FadeChangeHandler()
 }

+ 8 - 3
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<ExtensionController>() {
 
     private var extensions = emptyList<ExtensionItem>()
@@ -53,6 +56,7 @@ open class ExtensionPresenter(
     @Synchronized
     private fun toItems(tuple: ExtensionTuple): List<ExtensionItem> {
         val context = Injekt.get<Application>()
+        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()) {

+ 52 - 0
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<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
+                }
+            }
+        }
+    }
+}

+ 6 - 0
app/src/main/res/menu/extension_main.xml

@@ -8,4 +8,10 @@
         app:showAsAction="collapseActionView|ifRoom"
         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>

+ 1 - 0
app/src/main/res/values/strings.xml

@@ -176,6 +176,7 @@
     <string name="ext_version_info">Version: %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_settings">Settings</string>
 
       <!-- Reader section -->
     <string name="pref_fullscreen">Fullscreen</string>