From db977b8109a8e305b31202327a88e71836ba7467 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 11 Sep 2020 09:43:10 +0200 Subject: [PATCH] Simplify Dialog UI and code --- .../BackgroundSyncModeChooserDialog.kt | 145 +++--------------- .../layout/dialog_background_sync_mode.xml | 84 +++++++++- .../layout/item_custom_dialog_radio_line.xml | 57 ------- 3 files changed, 103 insertions(+), 183 deletions(-) delete mode 100644 vector/src/main/res/layout/item_custom_dialog_radio_line.xml diff --git a/vector/src/main/java/im/vector/app/features/settings/BackgroundSyncModeChooserDialog.kt b/vector/src/main/java/im/vector/app/features/settings/BackgroundSyncModeChooserDialog.kt index 079be58fbc..8b45a06f23 100644 --- a/vector/src/main/java/im/vector/app/features/settings/BackgroundSyncModeChooserDialog.kt +++ b/vector/src/main/java/im/vector/app/features/settings/BackgroundSyncModeChooserDialog.kt @@ -17,147 +17,52 @@ package im.vector.app.features.settings import android.app.Dialog -import android.content.Context import android.os.Bundle -import android.view.LayoutInflater import android.view.View -import android.view.ViewGroup -import android.widget.ArrayAdapter -import android.widget.ListView -import android.widget.RadioButton -import android.widget.TextView import androidx.appcompat.app.AlertDialog -import androidx.core.view.isVisible import androidx.fragment.app.DialogFragment -import androidx.fragment.app.FragmentActivity import im.vector.app.R class BackgroundSyncModeChooserDialog : DialogFragment() { - var interactionListener: InteractionListener? = null + private var interactionListener: InteractionListener? = null override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - return activity?.let { activity: FragmentActivity -> - val builder = AlertDialog.Builder(activity) - // Get the layout inflater - val inflater = activity.layoutInflater + val initialMode = BackgroundSyncMode.fromString(arguments?.getString(ARG_INITIAL_MODE)) - // Inflate and set the layout for the dialog - // Pass null as the parent view because its going in the dialog layout - val view = inflater.inflate(R.layout.dialog_background_sync_mode, null) - view.findViewById(R.id.dialog_background_sync_list)?.let { - it.adapter = Adapter( - activity, - BackgroundSyncMode.fromString(arguments?.getString(ARG_INITIAL_MODE)) - ) - } - builder.setView(view) - // Add action buttons - .setPositiveButton(R.string.ok) { dialog, _ -> - val mode = getSelectedOption() - if (mode.name == arguments?.getString(ARG_INITIAL_MODE)) { - // it's like a cancel, no changes - dialog.cancel() - } else { - interactionListener?.onOptionSelected(mode) - dialog.dismiss() - } - } - .setNegativeButton(R.string.cancel) { dialog, _ -> - interactionListener?.onCancel() - dialog.cancel() - } - builder.create() - } ?: throw IllegalStateException("Activity cannot be null") - } + val view = requireActivity().layoutInflater.inflate(R.layout.dialog_background_sync_mode, null) + val dialog = AlertDialog.Builder(requireActivity()) + .setTitle(R.string.settings_background_fdroid_sync_mode) + .setView(view) + .setPositiveButton(R.string.cancel, null) + .create() - private fun getSelectedOption(): BackgroundSyncMode { - options.forEach { - if (it.isSelected) return it.mode + view.findViewById(R.id.backgroundSyncModeBattery).setOnClickListener { + interactionListener + ?.takeIf { initialMode != BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_BATTERY } + ?.onOptionSelected(BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_BATTERY) + dialog.dismiss() } - // an item is always selected, should not happen - return options[0].mode - } - - data class SyncMode(val mode: BackgroundSyncMode, val title: Int, val description: Int, var isSelected: Boolean) - - private class Adapter(context: Context, val initialMode: BackgroundSyncMode) : ArrayAdapter(context, 0, options) { - init { - // mark the currently selected option - var initialModeFound = false - options.forEach { - it.isSelected = initialMode == it.mode - initialModeFound = true - } - if (!initialModeFound) { - options[0].isSelected = true - } + view.findViewById(R.id.backgroundSyncModeReal).setOnClickListener { + interactionListener + ?.takeIf { initialMode != BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_REALTIME } + ?.onOptionSelected(BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_REALTIME) + dialog.dismiss() } - - override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { - val syncMode = getItem(position)!! - - // Only 3 items, let's keep it like that - val itemView = convertView - ?: LayoutInflater.from(context).inflate(R.layout.item_custom_dialog_radio_line, parent, false) - - // Lookup view for data population - itemView?.findViewById(R.id.item_generic_title_text)?.let { - it.text = context.getString(syncMode.title) - } - itemView?.findViewById(R.id.item_generic_description_text)?.let { - it.text = context.getString(syncMode.description) - it.isVisible = true - } - itemView?.findViewById(R.id.item_generic_radio)?.let { - it.isChecked = syncMode.isSelected - it.isVisible = true - // let the item click handle that - it.setOnClickListener { - toggleChangeAtPosition(position) - } - } - - itemView?.setOnClickListener { - toggleChangeAtPosition(position) - } - - // Populate the data into the template view using the data object - - return itemView - } - - private fun toggleChangeAtPosition(position: Int) { - if (getItem(position)?.isSelected == true) { - // nop - } else { - for (i in 0 until count) { - // we change the single selection - getItem(i)?.isSelected = i == position - } - notifyDataSetChanged() - } + view.findViewById(R.id.backgroundSyncModeOff).setOnClickListener { + interactionListener + ?.takeIf { initialMode != BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_DISABLED } + ?.onOptionSelected(BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_DISABLED) + dialog.dismiss() } + return dialog } interface InteractionListener { - fun onCancel() {} - fun onOptionSelected(mode: BackgroundSyncMode) {} + fun onOptionSelected(mode: BackgroundSyncMode) } companion object { - private val options = listOf( - SyncMode(BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_BATTERY, - R.string.settings_background_fdroid_sync_mode_battery, - R.string.settings_background_fdroid_sync_mode_battery_description, false), - SyncMode(BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_REALTIME, - R.string.settings_background_fdroid_sync_mode_real_time, - R.string.settings_background_fdroid_sync_mode_real_time_description, false), - SyncMode(BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_DISABLED, - R.string.settings_background_fdroid_sync_mode_disabled, - R.string.settings_background_fdroid_sync_mode_disabled_description, false) - ) - private const val ARG_INITIAL_MODE = "ARG_INITIAL_MODE" fun newInstance(selectedMode: BackgroundSyncMode, interactionListener: InteractionListener): BackgroundSyncModeChooserDialog { diff --git a/vector/src/main/res/layout/dialog_background_sync_mode.xml b/vector/src/main/res/layout/dialog_background_sync_mode.xml index 89b2aaefde..1ddaa3a3f6 100644 --- a/vector/src/main/res/layout/dialog_background_sync_mode.xml +++ b/vector/src/main/res/layout/dialog_background_sync_mode.xml @@ -1,13 +1,85 @@ - - + android:orientation="vertical"> - + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/vector/src/main/res/layout/item_custom_dialog_radio_line.xml b/vector/src/main/res/layout/item_custom_dialog_radio_line.xml deleted file mode 100644 index 17d7be08ed..0000000000 --- a/vector/src/main/res/layout/item_custom_dialog_radio_line.xml +++ /dev/null @@ -1,57 +0,0 @@ - - - - - - - - - - -