Added source icon to filter sources

This commit is contained in:
Jay 2020-04-19 01:20:56 -04:00
parent 1373a0d61c
commit 5e56f80709
4 changed files with 9 additions and 152 deletions

View File

@ -26,23 +26,19 @@ import eu.kanade.tachiyomi.data.preference.EmptyPreferenceDataStore
import eu.kanade.tachiyomi.data.preference.SharedPreferencesDataStore
import eu.kanade.tachiyomi.source.ConfigurableSource
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.online.LoginSource
import eu.kanade.tachiyomi.ui.base.controller.NucleusController
import eu.kanade.tachiyomi.ui.setting.preferenceCategory
import eu.kanade.tachiyomi.util.system.LocaleHelper
import eu.kanade.tachiyomi.util.view.RecyclerWindowInsetsListener
import eu.kanade.tachiyomi.util.view.applyWindowInsetsForController
import eu.kanade.tachiyomi.widget.preference.ListMatPreference
import eu.kanade.tachiyomi.widget.preference.LoginPreference
import eu.kanade.tachiyomi.widget.preference.SourceLoginDialog
import kotlinx.android.synthetic.main.extension_detail_controller.*
@SuppressLint("RestrictedApi")
class ExtensionDetailsController(bundle: Bundle? = null) :
NucleusController<ExtensionDetailsPresenter>(bundle),
PreferenceManager.OnDisplayPreferenceDialogListener,
DialogPreference.TargetFragment,
SourceLoginDialog.Listener {
DialogPreference.TargetFragment {
private var lastOpenPreferencePosition: Int? = null
@ -217,11 +213,6 @@ class ExtensionDetailsController(bundle: Bundle? = null) :
return preferenceScreen!!.getPreference(lastOpenPreferencePosition!!) as T
}
override fun loginDialogClosed(source: LoginSource) {
val lastOpen = lastOpenPreferencePosition ?: return
(preferenceScreen?.getPreference(lastOpen) as? LoginPreference)?.notifyChanged()
}
private companion object {
const val PKGNAME_KEY = "pkg_name"
const val LASTOPENPREFERENCE_KEY = "last_open_preference"

View File

@ -12,19 +12,15 @@ import com.jakewharton.rxbinding.support.v7.widget.queryTextChanges
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.icon
import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.source.online.LoginSource
import eu.kanade.tachiyomi.util.system.LocaleHelper
import eu.kanade.tachiyomi.widget.preference.LoginCheckBoxPreference
import eu.kanade.tachiyomi.widget.preference.SourceLoginDialog
import eu.kanade.tachiyomi.widget.preference.SwitchPreferenceCategory
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.util.TreeMap
class SettingsSourcesController : SettingsController(),
SourceLoginDialog.Listener {
class SettingsSourcesController : SettingsController() {
init {
setHasOptionsMenu(true)
}
@ -115,7 +111,7 @@ class SettingsSourcesController : SettingsController(),
group.addPreference(selectAllPreference)
sources.forEach { source ->
val sourcePreference = LoginCheckBoxPreference(group.context, source).apply {
val sourcePreference = CheckBoxPreference(group.context).apply {
val id = source.id.toString()
title = source.name
key = getSourceKey(source.id)
@ -123,6 +119,11 @@ class SettingsSourcesController : SettingsController(),
isChecked = id !in hiddenCatalogues
isVisible = query.isEmpty() || source.name.contains(query, ignoreCase = true)
val sourceIcon = source.icon()
if (sourceIcon != null) {
icon = sourceIcon
}
onChange { newValue ->
val checked = newValue as Boolean
val current = preferences.hiddenCatalogues().getOrDefault()
@ -136,23 +137,12 @@ class SettingsSourcesController : SettingsController(),
addLanguageSources(group, sortedSources(sources))
true
}
setOnLoginClickListener {
val dialog = SourceLoginDialog(source)
dialog.targetController = this@SettingsSourcesController
dialog.showDialog(router)
}
}
group.addPreference(sourcePreference)
}
}
override fun loginDialogClosed(source: LoginSource) {
val pref = findPreference(getSourceKey(source.id)) as? LoginCheckBoxPreference
pref?.notifyChanged()
}
private fun getSourceKey(sourceId: Long): String {
return "source_$sourceId"
}

View File

@ -1,56 +0,0 @@
package eu.kanade.tachiyomi.widget.preference
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import androidx.preference.CheckBoxPreference
import androidx.preference.PreferenceViewHolder
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.source.online.LoginSource
import eu.kanade.tachiyomi.util.system.getResourceColor
import eu.kanade.tachiyomi.util.view.setVectorCompat
import kotlinx.android.synthetic.main.pref_item_source.view.*
class LoginCheckBoxPreference @JvmOverloads constructor(
context: Context,
val source: HttpSource,
attrs: AttributeSet? = null
) : CheckBoxPreference(context, attrs) {
init {
layoutResource = R.layout.pref_item_source
}
private var onLoginClick: () -> Unit = {}
override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder)
val loginFrame = holder.itemView.login_frame
if (source is LoginSource) {
val tint = if (source.isLogged())
Color.argb(255, 76, 175, 80)
else
context.getResourceColor(android.R.attr.textColorSecondary)
holder.itemView.login.setVectorCompat(R.drawable.ic_account_circle_black_24dp, tint)
loginFrame.visibility = View.VISIBLE
loginFrame.setOnClickListener {
onLoginClick()
}
} else {
loginFrame.visibility = View.GONE
}
}
fun setOnLoginClickListener(block: () -> Unit) {
onLoginClick = block
}
// Make method public
public override fun notifyChanged() {
super.notifyChanged()
}
}

View File

@ -1,68 +0,0 @@
package eu.kanade.tachiyomi.widget.preference
import android.os.Bundle
import android.view.View
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.online.LoginSource
import eu.kanade.tachiyomi.util.system.toast
import kotlinx.android.synthetic.main.pref_account_login.view.*
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
class SourceLoginDialog(bundle: Bundle? = null) : LoginDialogPreference(bundle = bundle) {
private val source = Injekt.get<SourceManager>().get(args.getLong("key")) as LoginSource
constructor(source: Source) : this(Bundle().apply { putLong("key", source.id) })
override fun setCredentialsOnView(view: View) = with(view) {
dialog_title.text = context.getString(R.string.log_in_to_, source.toString())
username.setText(preferences.sourceUsername(source))
password.setText(preferences.sourcePassword(source))
}
override fun checkLogin() {
requestSubscription?.unsubscribe()
v?.apply {
if (username.text.isEmpty() || password.text.isEmpty())
return
login.progress = 1
requestSubscription = source.login(username.text.toString(), password.text.toString())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ logged ->
if (logged) {
preferences.setSourceCredentials(source,
username.text.toString(),
password.text.toString())
dialog?.dismiss()
context.toast(R.string.successfully_logged_in)
} else {
preferences.setSourceCredentials(source, "", "")
login.progress = -1
}
}, { error ->
login.progress = -1
login.setText(R.string.unknown_error)
error.message?.let { context.toast(it) }
})
}
}
override fun onDialogClosed() {
super.onDialogClosed()
(targetController as? Listener)?.loginDialogClosed(source)
}
interface Listener {
fun loginDialogClosed(source: LoginSource)
}
}