diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-03-20 02:46:01 -0600 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-03-20 02:46:01 -0600 |
commit | 1054e54c8976e2ed5aef48a485b6ef28ae4ddcb8 (patch) | |
tree | 9552fde641c2b1c3e7c2d405dd6205f2b2978d9b /ui/src/main/java/com/wireguard/android/widget/ToggleSwitch.kt | |
parent | 2fe5b92035934a24369197c41598c0a3972c44c5 (diff) |
widget: rewrite in kotlin
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'ui/src/main/java/com/wireguard/android/widget/ToggleSwitch.kt')
-rw-r--r-- | ui/src/main/java/com/wireguard/android/widget/ToggleSwitch.kt | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/ui/src/main/java/com/wireguard/android/widget/ToggleSwitch.kt b/ui/src/main/java/com/wireguard/android/widget/ToggleSwitch.kt new file mode 100644 index 00000000..c97cb934 --- /dev/null +++ b/ui/src/main/java/com/wireguard/android/widget/ToggleSwitch.kt @@ -0,0 +1,44 @@ +/* + * 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) + } +} |