Simplify Dialog UI and code
This commit is contained in:
parent
4dc28a9d62
commit
db977b8109
|
@ -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<ListView>(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<View>(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<SyncMode>(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<View>(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<TextView>(R.id.item_generic_title_text)?.let {
|
||||
it.text = context.getString(syncMode.title)
|
||||
}
|
||||
itemView?.findViewById<TextView>(R.id.item_generic_description_text)?.let {
|
||||
it.text = context.getString(syncMode.description)
|
||||
it.isVisible = true
|
||||
}
|
||||
itemView?.findViewById<RadioButton>(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<View>(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 {
|
||||
|
|
|
@ -1,13 +1,85 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<ListView
|
||||
android:id="@+id/dialog_background_sync_list"
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
tools:listitem="@layout/item_custom_dialog_radio_line" />
|
||||
android:orientation="vertical">
|
||||
|
||||
</LinearLayout>
|
||||
<LinearLayout
|
||||
android:id="@+id/backgroundSyncModeBattery"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:foreground="?attr/selectableItemBackground"
|
||||
android:orientation="vertical"
|
||||
android:padding="16dp">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/settings_background_fdroid_sync_mode_battery"
|
||||
android:textColor="?riotx_text_primary"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginEnd="16dp"
|
||||
android:text="@string/settings_background_fdroid_sync_mode_battery_description"
|
||||
android:textColor="?riotx_text_secondary" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/backgroundSyncModeReal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:foreground="?attr/selectableItemBackground"
|
||||
android:orientation="vertical"
|
||||
android:padding="16dp">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/settings_background_fdroid_sync_mode_real_time"
|
||||
android:textColor="?riotx_text_primary"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginEnd="16dp"
|
||||
android:text="@string/settings_background_fdroid_sync_mode_real_time_description"
|
||||
android:textColor="?riotx_text_secondary" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/backgroundSyncModeOff"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:foreground="?attr/selectableItemBackground"
|
||||
android:orientation="vertical"
|
||||
android:padding="16dp">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/settings_background_fdroid_sync_mode_disabled"
|
||||
android:textColor="?riotx_text_primary"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginEnd="16dp"
|
||||
android:text="@string/settings_background_fdroid_sync_mode_disabled_description"
|
||||
android:textColor="?riotx_text_secondary" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</ScrollView>
|
||||
|
|
|
@ -1,57 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?android:attr/colorBackground"
|
||||
android:minHeight="50dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/item_generic_title_text"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="16dp"
|
||||
android:layout_marginLeft="16dp"
|
||||
android:layout_marginEnd="16dp"
|
||||
android:layout_marginRight="16dp"
|
||||
android:paddingTop="4dp"
|
||||
android:paddingBottom="2dp"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintBottom_toTopOf="@+id/item_generic_description_text"
|
||||
app:layout_constraintEnd_toStartOf="@+id/item_generic_radio"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:text="Item Title"
|
||||
tools:textSize="14sp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/item_generic_description_text"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="16dp"
|
||||
android:layout_marginLeft="16dp"
|
||||
android:layout_marginEnd="16dp"
|
||||
android:layout_marginRight="16dp"
|
||||
android:paddingTop="2dp"
|
||||
android:paddingBottom="4dp"
|
||||
android:textColor="?android:attr/textColorSecondary"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toStartOf="@+id/item_generic_radio"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/item_generic_title_text"
|
||||
tools:text="At totam delectus et aliquid dolorem. Consectetur voluptas tempore et non blanditiis id optio. Dolorum impedit quidem minus nihil. "
|
||||
tools:visibility="visible" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/item_generic_radio"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="16dp"
|
||||
app:layout_constraintBottom_toBottomOf="@+id/item_generic_description_text"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@+id/item_generic_title_text" />
|
||||
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in New Issue