-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
library/src/main/java/de/Maxr1998/modernpreferences/preferences/EditTextPreference.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package de.Maxr1998.modernpreferences.preferences | ||
|
||
import android.app.Dialog | ||
import android.content.Context | ||
import android.text.InputType | ||
import android.view.ViewGroup | ||
import android.view.ViewGroup.LayoutParams.MATCH_PARENT | ||
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT | ||
import android.widget.FrameLayout | ||
import androidx.annotation.StringRes | ||
import androidx.appcompat.app.AlertDialog | ||
import androidx.appcompat.widget.AppCompatEditText | ||
|
||
class EditTextPreference(key: String) : DialogPreference(key) { | ||
|
||
var currentInput: CharSequence? = null | ||
private set | ||
|
||
/** | ||
* The [InputType] applied to the contained [EditText][AppCompatEditText] | ||
*/ | ||
var textInputType: Int = InputType.TYPE_NULL | ||
|
||
@StringRes | ||
var textInputHintRes: Int = -1 | ||
var textInputHint: CharSequence? = null | ||
|
||
var textChangeListener: OnTextChangeListener? = null | ||
|
||
/** | ||
* Allows to override the summary, providing the current input value when called. | ||
* | ||
* Summary falls back to [summary] or [summaryRes] when null is returned. | ||
*/ | ||
var summaryProvider: (CharSequence?) -> CharSequence? = { null } | ||
|
||
override fun onAttach() { | ||
super.onAttach() | ||
if (currentInput == null) | ||
currentInput = getString() | ||
} | ||
|
||
override fun createDialog(context: Context): Dialog = AlertDialog.Builder(context).apply { | ||
if (titleRes != -1) setTitle(titleRes) else setTitle(title) | ||
val editText = AppCompatEditText(context).apply { | ||
if (textInputType != InputType.TYPE_NULL) { | ||
inputType = textInputType | ||
} | ||
when { | ||
textInputHintRes != -1 -> setHint(textInputHintRes) | ||
textInputHint != null -> hint = textInputHint | ||
} | ||
setText(currentInput) | ||
} | ||
setView(FrameLayout(context).apply { | ||
val layoutParams = ViewGroup.MarginLayoutParams(MATCH_PARENT, WRAP_CONTENT).apply { | ||
val tenDp = (10 * context.resources.displayMetrics.density).toInt() | ||
marginStart = 2 * tenDp | ||
marginEnd = 2 * tenDp | ||
topMargin = tenDp | ||
} | ||
addView(editText, layoutParams) | ||
}) | ||
setCancelable(false) | ||
setPositiveButton(android.R.string.ok) { _, _ -> | ||
editText.text?.let(::persist) | ||
requestRebind() | ||
} | ||
setNegativeButton(android.R.string.cancel) { _, _ -> | ||
editText.setText(currentInput) | ||
} | ||
}.create() | ||
|
||
private fun persist(input: CharSequence) { | ||
if (textChangeListener?.onTextChange(this, input) != false) { | ||
currentInput = input | ||
commitString(input.toString()) | ||
} | ||
} | ||
|
||
override fun resolveSummary(context: Context): CharSequence? { | ||
return summaryProvider(currentInput) ?: super.resolveSummary(context) | ||
} | ||
|
||
fun interface OnTextChangeListener { | ||
/** | ||
* Notified when the value of the connected [EditTextPreference] changes, | ||
* meaning after the user closes the dialog by pressing "ok". | ||
* This is called before the change gets persisted and can be prevented by returning false. | ||
* | ||
* @param text the new value | ||
* | ||
* @return true to commit the new value to [SharedPreferences][android.content.SharedPreferences] | ||
*/ | ||
fun onTextChange(preference: EditTextPreference, text: CharSequence): Boolean | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters