Commit f20b9204 authored by Joey's avatar Joey

settings: ExportPreference: simplify attrs

* Reuse DialogPreference's dialogMessage and dialogTitle
Signed-off-by: Joey's avatarJoey <bevilacquajoey@gmail.com>
parent 59211e91
Pipeline #58836306 passed with stages
in 17 minutes and 34 seconds
......@@ -10,11 +10,13 @@ package it.diab.settings.util.extensions
import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import androidx.annotation.StyleableRes
fun AttributeSet.getResource(resource: Int, @StyleableRes styleable: IntArray, context: Context): Int {
val attrArray = context.obtainStyledAttributes(this, styleable)
val required = attrArray.getResourceId(resource, 0)
val value = TypedValue()
attrArray.getValue(resource, value)
attrArray.recycle()
return required
return value.resourceId
}
\ No newline at end of file
......@@ -12,57 +12,37 @@ import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.app.AlertDialog
import androidx.preference.Preference
import androidx.preference.DialogPreference
import it.diab.settings.R
import it.diab.settings.util.extensions.getResource
class ExportPreference : Preference {
class ExportPreference : DialogPreference {
private lateinit var callbacks: ExportPreferenceCallbacks
@Suppress("unused")
constructor(context: Context, attrs: AttributeSet) :
super(context, attrs) {
init(attrs)
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
@Suppress("unused")
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) :
super(context, attrs, defStyleAttr) {
init(attrs)
}
super(context, attrs, defStyleAttr)
@Suppress("unused")
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) :
super(context, attrs, defStyleAttr, defStyleRes) {
init(attrs)
super(context, attrs, defStyleAttr, defStyleRes)
override fun onClick() {
showPromptDialog()
}
fun bind(callbacks: ExportPreferenceCallbacks) {
this.callbacks = callbacks
}
private fun init(attrs: AttributeSet) {
onPreferenceClickListener = OnPreferenceClickListener {
showPromptDialog(attrs)
}
}
private fun showPromptDialog(attrs: AttributeSet): Boolean {
private fun showPromptDialog(): Boolean {
val activity = callbacks.getActivity() ?: return false
val title = attrs.getResource(
R.styleable.ExportPreference_promptDialogTitle,
R.styleable.ExportPreference,
context
)
val message = attrs.getResource(
R.styleable.ExportPreference_promptDialogMessage,
R.styleable.ExportPreference,
context
)
AlertDialog.Builder(activity)
.setTitle(title)
.setMessage(message)
.setTitle(dialogTitle)
.setMessage(dialogMessage)
.setPositiveButton(R.string.export_ask_positive) { _, _ ->
callbacks.requestExport()
}
......
<!--
Copyright (c) 2019 Bevilacqua Joey
Licensed under the GNU GPLv3 license
The text of the license can be found in the LICENSE file
or at https://www.gnu.org/licenses/gpl.txt
-->
<resources>
<declare-styleable name="ExportPreference">
<attr name="promptDialogMessage" format="string" />
<attr name="promptDialogTitle" format="string" />
</declare-styleable>
</resources>
\ No newline at end of file
......@@ -17,8 +17,8 @@
<it.diab.settings.widgets.ExportPreference
app:key="pref_export_xlsx"
app:promptDialogMessage="@string/export_ask_xlsx_message"
app:promptDialogTitle="@string/export_ask_xlsx_title"
app:dialogMessage="@string/export_ask_xlsx_message"
app:dialogTitle="@string/export_ask_xlsx_title"
app:summary="@string/export_ask_xlsx_summary"
app:title="@string/export_ask_xlsx_title" />
......@@ -59,8 +59,8 @@
<it.diab.settings.widgets.ExportPreference
app:key="pref_export_ml_data"
app:persistent="false"
app:promptDialogMessage="@string/export_ask_ml_message"
app:promptDialogTitle="@string/export_ask_ml_title"
app:dialogMessage="@string/export_ask_ml_message"
app:dialogTitle="@string/export_ask_ml_title"
app:title="@string/export_ask_ml_title" />
</androidx.preference.PreferenceCategory>
</androidx.preference.PreferenceScreen>
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