Skip to content

Commit

Permalink
Convert NumberPickerPreference
Browse files Browse the repository at this point in the history
32/40
  • Loading branch information
HMBSbige committed Nov 4, 2019
1 parent c020294 commit 812df1c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 247 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.github.shadowsocks.preferences

import android.content.*
import android.content.res.*
import android.os.*
import android.util.*
import android.view.*
import android.widget.*
import com.github.shadowsocks.*

class NumberPickerPreference(context: Context, attrs: AttributeSet) : SummaryDialogPreference(context, attrs)
{
private val picker: NumberPicker = NumberPicker(context)

var value: Int = 0
set(i)
{
if (i == value)
{
return
}
picker.value = i
field = picker.value
persistInt(this.value)
notifyChanged()
}

var min: Int
get() = picker.minValue
set(value)
{
picker.minValue = value
}

init
{
val a = context.obtainStyledAttributes(attrs, R.styleable.NumberPickerPreference)
min = a.getInt(R.styleable.NumberPickerPreference_min, 0)
setMax(a.getInt(R.styleable.NumberPickerPreference_max, Integer.MAX_VALUE - 1))
a.recycle()
}

private fun setMax(value: Int)
{
picker.maxValue = value
}

override fun showDialog(state: Bundle?)
{
super.showDialog(state)
val window = dialog.window
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
}

override fun onCreateDialogView(): View
{
val parent = picker.parent as ViewGroup?
parent?.removeView(picker)
return picker
}

override fun onDialogClosed(positiveResult: Boolean)
{
picker.clearFocus()
super.onDialogClosed(positiveResult)

if (positiveResult)
{
val value = picker.value
if (callChangeListener(value))
{
this.value = value
return
}
}
picker.value = this.value
}

override fun onGetDefaultValue(a: TypedArray, index: Int): Any
{
return a.getInt(index, min)
}

override fun onSetInitialValue(restorePersistedValue: Boolean, defaultValue: Any)
{
val defValue = defaultValue as Int
this.value = if (restorePersistedValue) getPersistedInt(defValue) else defValue
}

override val summaryValue: Any?
get() = value
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.shadowsocks.preferences

import android.content.*
import android.preference.*
import android.util.*
import java.util.*

abstract class SummaryDialogPreference internal constructor(context: Context, attrs: AttributeSet) : DialogPreference(context, attrs)
{
abstract val summaryValue: Any?

override fun getSummary(): CharSequence
{
return String.format(Locale.ENGLISH, super.getSummary().toString(), summaryValue)
}
}

0 comments on commit 812df1c

Please sign in to comment.