wireguard-android/ui/src/main/java/com/wireguard/android/widget/ToggleSwitch.kt
Jason A. Donenfeld 1054e54c89 widget: rewrite in kotlin
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-03-20 02:46:01 -06:00

45 lines
1.4 KiB
Kotlin

/*
* Copyright © 2013 The Android Open Source Project
* Copyright © 2017-2019 WireGuard LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package com.wireguard.android.widget
import android.content.Context
import android.os.Parcelable
import android.util.AttributeSet
import android.widget.Switch
class ToggleSwitch @JvmOverloads constructor(context: Context?, attrs: AttributeSet? = null) : Switch(context, attrs) {
private var isRestoringState = false
private var listener: OnBeforeCheckedChangeListener? = null
override fun onRestoreInstanceState(state: Parcelable) {
isRestoringState = true
super.onRestoreInstanceState(state)
isRestoringState = false
}
override fun setChecked(checked: Boolean) {
if (checked == isChecked) return
if (isRestoringState || listener == null) {
super.setChecked(checked)
return
}
isEnabled = false
listener!!.onBeforeCheckedChanged(this, checked)
}
fun setCheckedInternal(checked: Boolean) {
super.setChecked(checked)
isEnabled = true
}
fun setOnBeforeCheckedChangeListener(listener: OnBeforeCheckedChangeListener?) {
this.listener = listener
}
interface OnBeforeCheckedChangeListener {
fun onBeforeCheckedChanged(toggleSwitch: ToggleSwitch?, checked: Boolean)
}
}