mirror of
https://github.com/mihonapp/mihon.git
synced 2025-10-30 05:47:57 +01:00
@@ -0,0 +1,56 @@
|
||||
package eu.kanade.tachiyomi.widget.preference
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Color
|
||||
import android.support.v7.preference.PreferenceViewHolder
|
||||
import android.util.AttributeSet
|
||||
import android.view.View
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.data.source.online.LoginSource
|
||||
import eu.kanade.tachiyomi.data.source.online.OnlineSource
|
||||
import eu.kanade.tachiyomi.util.setVectorCompat
|
||||
import kotlinx.android.synthetic.main.pref_item_source.view.*
|
||||
import net.xpece.android.support.preference.CheckBoxPreference
|
||||
|
||||
class LoginCheckBoxPreference @JvmOverloads constructor(
|
||||
context: Context,
|
||||
val source: OnlineSource,
|
||||
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
|
||||
Color.argb(97, 0, 0, 0)
|
||||
|
||||
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
|
||||
override public fun notifyChanged() {
|
||||
super.notifyChanged()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package eu.kanade.tachiyomi.widget.preference
|
||||
|
||||
import android.annotation.TargetApi
|
||||
import android.content.Context
|
||||
import android.content.res.TypedArray
|
||||
import android.os.Build
|
||||
import android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH
|
||||
import android.support.v7.preference.PreferenceViewHolder
|
||||
import android.support.v7.widget.SwitchCompat
|
||||
import android.util.AttributeSet
|
||||
import android.view.View
|
||||
import android.widget.Checkable
|
||||
import android.widget.CompoundButton
|
||||
import android.widget.Switch
|
||||
import eu.kanade.tachiyomi.util.getResourceColor
|
||||
import net.xpece.android.support.preference.PreferenceCategory
|
||||
import net.xpece.android.support.preference.R
|
||||
|
||||
class SwitchPreferenceCategory @JvmOverloads constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet? = null)
|
||||
: PreferenceCategory(
|
||||
context,
|
||||
attrs,
|
||||
R.attr.switchPreferenceCompatStyle,
|
||||
R.style.Preference_Material_SwitchPreferenceCompat),
|
||||
CompoundButton.OnCheckedChangeListener {
|
||||
|
||||
init {
|
||||
setTitleTextColor(context.theme.getResourceColor(R.attr.colorAccent))
|
||||
}
|
||||
|
||||
private var mChecked = false
|
||||
|
||||
private var mCheckedSet = false
|
||||
|
||||
override fun onBindViewHolder(holder: PreferenceViewHolder) {
|
||||
super.onBindViewHolder(holder)
|
||||
syncSwitchView(holder)
|
||||
}
|
||||
|
||||
private fun syncSwitchView(holder: PreferenceViewHolder) {
|
||||
val switchView = holder.findViewById(R.id.switchWidget)
|
||||
syncSwitchView(switchView)
|
||||
}
|
||||
|
||||
@TargetApi(ICE_CREAM_SANDWICH)
|
||||
private fun syncSwitchView(view: View) {
|
||||
if (view is Checkable) {
|
||||
val isChecked = view.isChecked
|
||||
if (isChecked == mChecked) return
|
||||
|
||||
if (view is SwitchCompat) {
|
||||
view.setOnCheckedChangeListener(null)
|
||||
} else if (NATIVE_SWITCH_CAPABLE && view is Switch) {
|
||||
view.setOnCheckedChangeListener(null)
|
||||
}
|
||||
|
||||
view.toggle()
|
||||
|
||||
if (view is SwitchCompat) {
|
||||
view.setOnCheckedChangeListener(this)
|
||||
} else if (NATIVE_SWITCH_CAPABLE && view is Switch) {
|
||||
view.setOnCheckedChangeListener(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
|
||||
if (!callChangeListener(isChecked)) {
|
||||
buttonView.isChecked = !isChecked
|
||||
} else {
|
||||
setChecked(isChecked)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onClick() {
|
||||
super.onClick()
|
||||
|
||||
val newValue = !isChecked()
|
||||
if (callChangeListener(newValue)) {
|
||||
setChecked(newValue)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the checked state and saves it to the [SharedPreferences].
|
||||
*
|
||||
* @param checked The checked state.
|
||||
*/
|
||||
fun setChecked(checked: Boolean) {
|
||||
// Always persist/notify the first time; don't assume the field's default of false.
|
||||
val changed = mChecked != checked
|
||||
if (changed || !mCheckedSet) {
|
||||
mChecked = checked
|
||||
mCheckedSet = true
|
||||
persistBoolean(checked)
|
||||
if (changed) {
|
||||
notifyDependencyChange(shouldDisableDependents())
|
||||
notifyChanged()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the checked state.
|
||||
*
|
||||
* @return The checked state.
|
||||
*/
|
||||
fun isChecked(): Boolean {
|
||||
return mChecked
|
||||
}
|
||||
|
||||
override fun isEnabled(): Boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
override fun shouldDisableDependents(): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
override fun onGetDefaultValue(a: TypedArray, index: Int): Any {
|
||||
return a.getBoolean(index, false)
|
||||
}
|
||||
|
||||
override fun onSetInitialValue(restoreValue: Boolean, defaultValue: Any?) {
|
||||
setChecked(if (restoreValue)
|
||||
getPersistedBoolean(mChecked)
|
||||
else
|
||||
defaultValue as Boolean)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val NATIVE_SWITCH_CAPABLE = Build.VERSION.SDK_INT >= ICE_CREAM_SANDWICH
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user