Commit 546a5d01 authored by Mygod's avatar Mygod

Refine design for preference package

parent cbb839ec
package be.mygod.preference
import android.app.DialogFragment
import android.support.v7.preference.DialogPreference
trait DialogPreferencePlus extends DialogPreference {
def createDialog(): DialogFragment
}
...@@ -11,10 +11,12 @@ import android.util.AttributeSet ...@@ -11,10 +11,12 @@ import android.util.AttributeSet
* Based on: https://github.com/Gericop/Android-Support-Preference-V7-Fix/tree/master/app/src/main/java/android/support/v7/preference * Based on: https://github.com/Gericop/Android-Support-Preference-V7-Fix/tree/master/app/src/main/java/android/support/v7/preference
*/ */
class EditTextPreference(context: Context, attrs: AttributeSet = null) extends Parent(context, attrs) class EditTextPreference(context: Context, attrs: AttributeSet = null) extends Parent(context, attrs)
with SummaryPreference { with DialogPreferencePlus with SummaryPreference {
val editText = new AppCompatEditText(context, attrs) val editText = new AppCompatEditText(context, attrs)
editText.setId(android.R.id.edit) editText.setId(android.R.id.edit)
override def createDialog() = new EditTextPreferenceDialogFragment()
override protected def getSummaryValue = { override protected def getSummaryValue = {
var text = getText var text = getText
if (text == null) text = "" if (text == null) text = ""
......
...@@ -9,7 +9,7 @@ import android.widget.NumberPicker ...@@ -9,7 +9,7 @@ import android.widget.NumberPicker
import com.github.shadowsocks.R import com.github.shadowsocks.R
class NumberPickerPreference(private val context: Context, attrs: AttributeSet = null) class NumberPickerPreference(private val context: Context, attrs: AttributeSet = null)
extends DialogPreference(context, attrs) with SummaryPreference { extends DialogPreference(context, attrs) with DialogPreferencePlus with SummaryPreference {
private[preference] val picker = new NumberPicker(new ContextThemeWrapper(context, R.style.NumberPickerStyle)) private[preference] val picker = new NumberPicker(new ContextThemeWrapper(context, R.style.NumberPickerStyle))
private var value: Int = _ private var value: Int = _
...@@ -20,6 +20,8 @@ class NumberPickerPreference(private val context: Context, attrs: AttributeSet = ...@@ -20,6 +20,8 @@ class NumberPickerPreference(private val context: Context, attrs: AttributeSet =
a.recycle a.recycle
} }
override def createDialog() = new NumberPickerPreferenceDialogFragment()
def getValue = value def getValue = value
def getMin = if (picker == null) 0 else picker.getMinValue def getMin = if (picker == null) 0 else picker.getMinValue
def getMax = picker.getMaxValue def getMax = picker.getMaxValue
......
...@@ -3,7 +3,7 @@ package be.mygod.preference ...@@ -3,7 +3,7 @@ package be.mygod.preference
import android.app.DialogFragment import android.app.DialogFragment
import android.os.Bundle import android.os.Bundle
import android.support.v14.preference.{PreferenceFragment => Base} import android.support.v14.preference.{PreferenceFragment => Base}
import android.support.v7.preference.PreferenceScreen import android.support.v7.preference.{Preference, PreferenceScreen}
import android.view.{LayoutInflater, ViewGroup} import android.view.{LayoutInflater, ViewGroup}
abstract class PreferenceFragment extends Base { abstract class PreferenceFragment extends Base {
...@@ -20,6 +20,11 @@ abstract class PreferenceFragment extends Base { ...@@ -20,6 +20,11 @@ abstract class PreferenceFragment extends Base {
.commitAllowingStateLoss() .commitAllowingStateLoss()
} }
override def onDisplayPreferenceDialog(preference: Preference) = preference match {
case dpp: DialogPreferencePlus => displayPreferenceDialog(preference.getKey, dpp.createDialog())
case _ => super.onDisplayPreferenceDialog(preference)
}
override protected def onCreateAdapter(screen: PreferenceScreen) = new PreferenceGroupAdapter(screen) override protected def onCreateAdapter(screen: PreferenceScreen) = new PreferenceGroupAdapter(screen)
override def onResume { override def onResume {
......
...@@ -203,12 +203,8 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan ...@@ -203,12 +203,8 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan
}) })
} }
override def onDisplayPreferenceDialog(preference: Preference) = preference match { override def onDisplayPreferenceDialog(preference: Preference) = preference.getKey match {
case _: EditTextPreference => displayPreferenceDialog(preference.getKey, case Key.kcpcli => displayPreferenceDialog (Key.kcpcli, new KcpCliPreferenceDialogFragment () )
if (preference.getKey == Key.kcpcli) new KcpCliPreferenceDialogFragment()
else new EditTextPreferenceDialogFragment())
case _: NumberPickerPreference =>
displayPreferenceDialog(preference.getKey, new NumberPickerPreferenceDialogFragment())
case _ => super.onDisplayPreferenceDialog(preference) case _ => super.onDisplayPreferenceDialog(preference)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment