Hide catalogues (#466)

Hide catalogues
This commit is contained in:
inorichi
2016-09-18 21:12:12 +02:00
committed by GitHub
parent cb92143613
commit 58a2f7a874
10 changed files with 372 additions and 57 deletions

View File

@@ -16,6 +16,7 @@ import com.afollestad.materialdialogs.MaterialDialog
import com.f2prateek.rx.preferences.Preference
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.source.online.LoginSource
import eu.kanade.tachiyomi.ui.base.adapter.FlexibleViewHolder
import eu.kanade.tachiyomi.ui.base.fragment.BaseRxFragment
import eu.kanade.tachiyomi.ui.main.MainActivity
@@ -45,7 +46,7 @@ class CatalogueFragment : BaseRxFragment<CataloguePresenter>(), FlexibleViewHold
/**
* Spinner shown in the toolbar to change the selected source.
*/
private lateinit var spinner: Spinner
private var spinner: Spinner? = null
/**
* Adapter containing the list of manga from the catalogue.
@@ -125,6 +126,14 @@ class CatalogueFragment : BaseRxFragment<CataloguePresenter>(), FlexibleViewHold
}
override fun onViewCreated(view: View, savedState: Bundle?) {
// If the source list is empty or it only has unlogged sources, return to main screen.
val sources = presenter.sources
if (sources.isEmpty() || sources.all { it is LoginSource && !it.isLogged() }) {
context.toast(R.string.no_valid_sources)
activity.onBackPressed()
return
}
// Initialize adapter, scroll listener and recycler views
adapter = CatalogueAdapter(this)
@@ -166,7 +175,7 @@ class CatalogueFragment : BaseRxFragment<CataloguePresenter>(), FlexibleViewHold
val onItemSelected = IgnoreFirstSpinnerListener { position ->
val source = spinnerAdapter.getItem(position)
if (!presenter.isValidSource(source)) {
spinner.setSelection(selectedIndex)
spinner?.setSelection(selectedIndex)
context.toast(R.string.source_requires_login)
} else if (source != presenter.source) {
selectedIndex = position
@@ -264,7 +273,7 @@ class CatalogueFragment : BaseRxFragment<CataloguePresenter>(), FlexibleViewHold
searchItem?.let {
if (it.isActionViewExpanded) it.collapseActionView()
}
toolbar.removeView(spinner)
spinner?.let { toolbar.removeView(it) }
super.onDestroyView()
}

View File

@@ -21,6 +21,7 @@ import rx.schedulers.Schedulers
import rx.subjects.PublishSubject
import timber.log.Timber
import uy.kohesive.injekt.injectLazy
import java.util.NoSuchElementException
/**
* Presenter of [CatalogueFragment].
@@ -103,7 +104,11 @@ class CataloguePresenter : BasePresenter<CatalogueFragment>() {
override fun onCreate(savedState: Bundle?) {
super.onCreate(savedState)
source = getLastUsedSource()
try {
source = getLastUsedSource()
} catch (error: NoSuchElementException) {
return
}
if (savedState != null) {
query = savedState.getString(CataloguePresenter::query.name, "")
@@ -324,6 +329,7 @@ class CataloguePresenter : BasePresenter<CatalogueFragment>() {
*/
private fun getEnabledSources(): List<OnlineSource> {
val languages = prefs.enabledLanguages().getOrDefault()
val hiddenCatalogues = prefs.hiddenCatalogues().getOrDefault()
// Ensure at least one language
if (languages.isEmpty()) {
@@ -332,6 +338,7 @@ class CataloguePresenter : BasePresenter<CatalogueFragment>() {
return sourceManager.getOnlineSources()
.filter { it.lang.code in languages }
.filterNot { it.id.toString() in hiddenCatalogues }
.sortedBy { "(${it.lang.code}) ${it.name}" }
}

View File

@@ -1,21 +1,19 @@
package eu.kanade.tachiyomi.ui.setting
import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.support.v7.preference.Preference
import android.support.v7.preference.PreferenceGroup
import android.support.v7.preference.XpPreferenceFragment
import android.view.View
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.data.source.Source
import eu.kanade.tachiyomi.data.source.SourceManager
import eu.kanade.tachiyomi.data.source.getLanguages
import eu.kanade.tachiyomi.data.source.online.LoginSource
import eu.kanade.tachiyomi.util.plusAssign
import eu.kanade.tachiyomi.widget.preference.LoginPreference
import eu.kanade.tachiyomi.widget.preference.LoginCheckBoxPreference
import eu.kanade.tachiyomi.widget.preference.SourceLoginDialog
import net.xpece.android.support.preference.MultiSelectListPreference
import eu.kanade.tachiyomi.widget.preference.SwitchPreferenceCategory
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
class SettingsSourcesFragment : SettingsFragment() {
@@ -32,59 +30,105 @@ class SettingsSourcesFragment : SettingsFragment() {
private val preferences: PreferencesHelper by injectLazy()
private val sourceManager: SourceManager by injectLazy()
private val onlineSources by lazy { Injekt.get<SourceManager>().getOnlineSources() }
val languagesPref by lazy { findPreference("pref_source_languages") as MultiSelectListPreference }
val sourcesPref by lazy { findPreference("pref_sources") as PreferenceGroup }
override fun setDivider(divider: Drawable?) {
super.setDivider(null)
}
override fun onViewCreated(view: View, savedState: Bundle?) {
super.onViewCreated(view, savedState)
// Remove dummy preference
preferenceScreen.removeAll()
// Get the list of active language codes.
val activeLangsCodes = preferences.enabledLanguages().getOrDefault()
// Get the list of languages ordered by name.
val langs = getLanguages().sortedBy { it.lang }
val entryKeys = langs.map { it.code }
languagesPref.entries = langs.map { it.lang }.toTypedArray()
languagesPref.entryValues = entryKeys.toTypedArray()
languagesPref.values = preferences.enabledLanguages().getOrDefault()
// Order first by active languages, then inactive ones
val orderedLangs = langs.filter { it.code in activeLangsCodes } +
langs.filterNot { it.code in activeLangsCodes }
subscriptions += preferences.enabledLanguages().asObservable()
.subscribe { languages ->
sourcesPref.removeAll()
val enabledSources = sourceManager.getOnlineSources()
.filter { it.lang.code in languages }
for (source in enabledSources.filterIsInstance(LoginSource::class.java)) {
val pref = createLoginSourceEntry(source)
sourcesPref.addPreference(pref)
}
// Hide category if it doesn't have any child
sourcesPref.isVisible = sourcesPref.preferenceCount > 0
orderedLangs.forEach { lang ->
// Create a preference group and set initial state and change listener
SwitchPreferenceCategory(context).apply {
preferenceScreen.addPreference(this)
title = lang.lang
isPersistent = false
if (lang.code in activeLangsCodes) {
setChecked(true)
addLanguageSources(this)
}
setOnPreferenceChangeListener { preference, any ->
val checked = any as Boolean
val current = preferences.enabledLanguages().getOrDefault()
if (!checked) {
preferences.enabledLanguages().set(current - lang.code)
removeAll()
} else {
preferences.enabledLanguages().set(current + lang.code)
addLanguageSources(this)
}
true
}
}
}
}
fun createLoginSourceEntry(source: Source): Preference {
return LoginPreference(preferenceManager.context).apply {
key = preferences.keys.sourceUsername(source.id)
title = source.toString()
/**
* Adds the source list for the given group (language).
*
* @param group the language category.
*/
private fun addLanguageSources(group: SwitchPreferenceCategory) {
val sources = onlineSources.filter { it.lang.lang == group.title }.sortedBy { it.name }
val hiddenCatalogues = preferences.hiddenCatalogues().getOrDefault()
sources.forEach { source ->
val sourcePreference = LoginCheckBoxPreference(context, source).apply {
val id = source.id.toString()
title = source.name
key = getSourceKey(source.id)
isPersistent = false
isChecked = id !in hiddenCatalogues
setOnPreferenceChangeListener { preference, any ->
val checked = any as Boolean
val current = preferences.hiddenCatalogues().getOrDefault()
preferences.hiddenCatalogues().set(if (checked)
current - id
else
current + id)
true
}
setOnLoginClickListener {
val fragment = SourceLoginDialog.newInstance(source)
fragment.setTargetFragment(this@SettingsSourcesFragment, SOURCE_CHANGE_REQUEST)
fragment.show(fragmentManager, null)
}
setOnPreferenceClickListener {
val fragment = SourceLoginDialog.newInstance(source)
fragment.setTargetFragment(this@SettingsSourcesFragment, SOURCE_CHANGE_REQUEST)
fragment.show(fragmentManager, null)
true
}
group.addPreference(sourcePreference)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == SOURCE_CHANGE_REQUEST) {
val pref = findPreference(preferences.keys.sourceUsername(resultCode)) as? LoginPreference
val pref = findPreference(getSourceKey(resultCode)) as? LoginCheckBoxPreference
pref?.notifyChanged()
}
}
private fun getSourceKey(sourceId: Int): String {
return "source_$sourceId"
}
}