Commit ed4ab7a0 authored by Joey's avatar Joey

settings: refactor some preferences

Signed-off-by: Joey's avatarJoey <bevilacquajoey@gmail.com>
parent 7b5b3f36
......@@ -9,6 +9,7 @@
package it.diab.core.util
import android.content.Context
import android.content.Intent
import android.os.Build
import android.preference.PreferenceManager
import androidx.annotation.WorkerThread
......@@ -86,6 +87,12 @@ class PluginManager(context: Context) {
}
}
fun getPickerIntent(): Intent {
return Intent(Intent.ACTION_GET_CONTENT).apply {
type = "application/zip"
}
}
suspend fun fetchSuggestion(glucose: Glucose, onExecuted: (Float) -> Unit) {
delay(1000)
......
......@@ -18,63 +18,53 @@ import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat
import it.diab.core.util.Activities.Settings.PREF_UI_STYLE
import it.diab.core.util.PluginManager
import it.diab.core.util.UIUtils
import it.diab.core.util.extensions.format
import it.diab.core.util.extensions.get
import it.diab.export.ExportService
import it.diab.settings.R
import it.diab.settings.widgets.ExportPreference
import java.util.Date
class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener {
private lateinit var prefs: SharedPreferences
private lateinit var pluginManager: PluginManager
private lateinit var pluginManager: Preference
private lateinit var pluginRemover: Preference
private lateinit var pluginManagerPref: Preference
private lateinit var pluginRemoverPref: Preference
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.settings, rootKey)
prefs = preferenceManager.sharedPreferences
pluginManager = PluginManager(requireContext())
val exportPluginData = findPreference("pref_export_ml_data")
exportPluginData?.onPreferenceClickListener = Preference.OnPreferenceClickListener {
showExportDialog(
R.string.export_ask_ml_title,
R.string.export_ask_ml_message,
REQUEST_ML_EXPORT
)
}
val exportPluginData = findPreference("pref_export_ml_data") as ExportPreference
exportPluginData.bind(object : ExportPreference.ExportPreferenceCallbacks {
override fun getActivity() = activity
override fun requestExport() { requestExport(REQUEST_ML_EXPORT) }
})
val exportXlsx = findPreference("pref_export_xlsx")
exportXlsx?.onPreferenceClickListener = Preference.OnPreferenceClickListener {
showExportDialog(
R.string.export_ask_xlsx_title,
R.string.export_ask_xlsx_message,
REQUEST_XLSX_EXPORT
)
}
val exportXlsx = findPreference("pref_export_xlsx") as ExportPreference
exportXlsx.bind(object : ExportPreference.ExportPreferenceCallbacks {
override fun getActivity() = activity
override fun requestExport() { requestExport(REQUEST_XLSX_EXPORT) }
})
val pluginCategory = findPreference("plugin_category") as PreferenceCategory
pluginManager = pluginCategory.findPreference("pref_plugin_manager")
pluginRemover = pluginCategory.findPreference("pref_plugin_remover")
pluginManagerPref = pluginCategory.findPreference("pref_plugin_manager")
pluginRemoverPref = pluginCategory.findPreference("pref_plugin_remover")
pluginManager.setOnPreferenceClickListener { fetchPluginFile() }
pluginRemover.setOnPreferenceClickListener { askPluginRemoval() }
pluginManagerPref.setOnPreferenceClickListener { fetchPluginFile() }
pluginRemoverPref.setOnPreferenceClickListener { askPluginRemoval() }
prefs.registerOnSharedPreferenceChangeListener(this)
updatePluginPrefs()
val style = findPreference(PREF_UI_STYLE) as ListPreference
setupStylePref(style)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
......@@ -96,27 +86,9 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
when (key) {
PluginManager.LAST_UPDATE -> updatePluginPrefs()
PREF_UI_STYLE -> UIUtils.setStyleMode(prefs[PREF_UI_STYLE, "1"])
}
}
private fun showExportDialog(
@StringRes title: Int,
@StringRes message: Int,
requestCode: Int
): Boolean {
val activity = activity ?: return false
AlertDialog.Builder(activity)
.setTitle(title)
.setMessage(message)
.setPositiveButton(R.string.export_ask_positive) { _, _ -> requestExport(requestCode) }
.setNegativeButton(android.R.string.cancel, null)
.show()
return true
}
private fun requestExport(requestCode: Int) {
val activity = activity ?: return
......@@ -198,11 +170,11 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
val lastUpdated = prefs[PluginManager.LAST_UPDATE, 0L]
if (lastUpdated == 0L) {
pluginRemover.isVisible = false
pluginManager.setSummary(R.string.settings_plugin_manage_summary_new)
pluginRemoverPref.isVisible = false
pluginManagerPref.setSummary(R.string.settings_plugin_manage_summary_new)
} else {
pluginRemover.isVisible = true
pluginManager.summary = getString(
pluginRemoverPref.isVisible = true
pluginManagerPref.summary = getString(
R.string.settings_plugin_manage_summary_installed,
Date(lastUpdated).format("yyyy-MM-dd HH:mm")
)
......@@ -210,11 +182,7 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
}
private fun fetchPluginFile(): Boolean {
val intent = Intent(Intent.ACTION_GET_CONTENT).apply {
type = "application/zip"
}
startActivityForResult(intent, REQUEST_SELECT_PLUGIN)
startActivityForResult(pluginManager.getPickerIntent(), REQUEST_SELECT_PLUGIN)
return true
}
......@@ -227,9 +195,7 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
val uri = data.data ?: return
val iStream = context.contentResolver.openInputStream(uri) ?: return
val manager = PluginManager(context)
manager.install(iStream)
pluginManager.install(iStream)
Toast.makeText(context, R.string.settings_plugin_installing, Toast.LENGTH_SHORT)
.show()
}
......@@ -240,37 +206,13 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
AlertDialog.Builder(activity)
.setTitle(R.string.settings_plugin_remove)
.setMessage(R.string.settings_plugin_remove_confirmation)
.setPositiveButton(R.string.settings_plugin_remove_confirmation_positive) { _, _ -> removePlugin() }
.setPositiveButton(R.string.settings_plugin_remove_confirmation_positive) { _, _ -> pluginManager.uninstall() }
.setNegativeButton(android.R.string.no, null)
.show()
return true
}
private fun removePlugin() {
val context = context ?: return
val manager = PluginManager(context)
manager.uninstall()
}
private fun setupStylePref(preference: ListPreference) {
val supportsAuto = UIUtils.supportsAutoStyleMode()
val entries = if (supportsAuto) arrayOf(
getString(R.string.settings_ui_theme_system),
getString(R.string.settings_ui_theme_light),
getString(R.string.settings_ui_theme_dark)
)
else arrayOf(
getString(R.string.settings_ui_theme_light),
getString(R.string.settings_ui_theme_dark)
)
val values = if (supportsAuto) arrayOf("0", "1", "2") else arrayOf("1", "2")
preference.entries = entries
preference.entryValues = values
}
private fun requestStorageAccess(requestCode: Int) {
requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), requestCode)
}
......
/*
* 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
*/
package it.diab.settings.util.extensions
import android.content.Context
import android.util.AttributeSet
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)
attrArray.recycle()
return required
}
\ No newline at end of file
/*
* 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
*/
package it.diab.settings.widgets
import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.app.AlertDialog
import androidx.preference.Preference
import it.diab.settings.R
import it.diab.settings.util.extensions.getResource
class ExportPreference : Preference {
private lateinit var callbacks: ExportPreferenceCallbacks
@Suppress("unused")
constructor(context: Context, attrs: AttributeSet) :
super(context, attrs) {
init(attrs)
}
@Suppress("unused")
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) :
super(context, attrs, defStyleAttr) {
init(attrs)
}
@Suppress("unused")
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) :
super(context, attrs, defStyleAttr, defStyleRes) {
init(attrs)
}
fun bind(callbacks: ExportPreferenceCallbacks) {
this.callbacks = callbacks
}
private fun init(attrs: AttributeSet) {
onPreferenceClickListener = OnPreferenceClickListener {
showPromptDialog(attrs)
}
}
private fun showPromptDialog(attrs: AttributeSet): 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)
.setPositiveButton(R.string.export_ask_positive) { _, _ ->
callbacks.requestExport()
}
.setNegativeButton(android.R.string.cancel, null)
.show()
return true
}
interface ExportPreferenceCallbacks {
/**
* Get an activity in which display dialogs
*/
fun getActivity(): Activity?
/**
* Export has been confirmed, proceed
*/
fun requestExport()
}
}
\ No newline at end of file
/*
* 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
*/
package it.diab.settings.widgets
import android.content.Context
import android.util.AttributeSet
import androidx.preference.ListPreference
import it.diab.core.util.UIUtils
import it.diab.settings.R
class StylePreference : ListPreference {
@Suppress("unused")
constructor(context: Context) :
super(context)
@Suppress("unused")
constructor(context: Context, attrs: AttributeSet) :
super(context, attrs)
@Suppress("unused")
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) :
super(context, attrs, defStyleAttr)
@Suppress("unused")
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) :
super(context, attrs, defStyleAttr, defStyleRes)
init {
setupEntries()
setupOnChange()
}
private fun setupEntries() {
val supportsAuto = UIUtils.supportsAutoStyleMode()
entries = if (supportsAuto) {
arrayOf(
context.getString(R.string.settings_ui_theme_system),
context.getString(R.string.settings_ui_theme_light),
context.getString(R.string.settings_ui_theme_dark)
)
} else {
arrayOf(
context.getString(R.string.settings_ui_theme_light),
context.getString(R.string.settings_ui_theme_dark)
)
}
entryValues = if (supportsAuto) {
arrayOf("0", "1", "2")
} else {
arrayOf("1", "2")
}
}
private fun setupOnChange() {
setOnPreferenceChangeListener { _, newValue ->
UIUtils.setStyleMode(newValue.toString())
true
}
}
}
\ No newline at end of file
<!--
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
......@@ -9,14 +9,16 @@
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.preference.ListPreference
app:key="pref_ui_style"
<it.diab.settings.widgets.StylePreference
app:defaultValue="1"
app:title="@string/settings_ui_theme_title"
app:summary="%s" />
app:key="pref_ui_style"
app:summary="%s"
app:title="@string/settings_ui_theme_title" />
<androidx.preference.Preference
<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:summary="@string/export_ask_xlsx_summary"
app:title="@string/export_ask_xlsx_title" />
......@@ -54,9 +56,11 @@
app:persistent="false"
app:title="@string/settings_plugin_remove" />
<androidx.preference.Preference
<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: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