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/MultiselectableRelativeLayout.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/MultiselectableRelativeLayout.kt')
-rw-r--r-- | ui/src/main/java/com/wireguard/android/widget/MultiselectableRelativeLayout.kt | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/ui/src/main/java/com/wireguard/android/widget/MultiselectableRelativeLayout.kt b/ui/src/main/java/com/wireguard/android/widget/MultiselectableRelativeLayout.kt new file mode 100644 index 00000000..69ffefc0 --- /dev/null +++ b/ui/src/main/java/com/wireguard/android/widget/MultiselectableRelativeLayout.kt @@ -0,0 +1,49 @@ +/* + * 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.util.AttributeSet +import android.view.View +import android.widget.RelativeLayout +import com.wireguard.android.R + +class MultiselectableRelativeLayout : RelativeLayout { + private var multiselected = false + + constructor(context: Context?) : super(context) {} + constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {} + constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {} + constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {} + + override fun onCreateDrawableState(extraSpace: Int): IntArray { + if (multiselected) { + val drawableState = super.onCreateDrawableState(extraSpace + 1) + View.mergeDrawableStates(drawableState, STATE_MULTISELECTED) + return drawableState + } + return super.onCreateDrawableState(extraSpace) + } + + fun setMultiSelected(on: Boolean) { + if (!multiselected) { + multiselected = true + refreshDrawableState() + } + isActivated = on + } + + fun setSingleSelected(on: Boolean) { + if (multiselected) { + multiselected = false + refreshDrawableState() + } + isActivated = on + } + + companion object { + private val STATE_MULTISELECTED = intArrayOf(R.attr.state_multiselected) + } +} |