Save notes when dialog closes (screen rotation, screen turned off, user accidentally clicked outside the dialog)

pull/2129/head
simon 6 months ago committed by Simon
parent 897a236501
commit 6467257ee6

@ -38,10 +38,13 @@ import org.isoron.uhabits.utils.sres
class CheckmarkDialog : AppCompatDialogFragment() { class CheckmarkDialog : AppCompatDialogFragment() {
var onToggle: (Int, String) -> Unit = { _, _ -> } var onToggle: (Int, String) -> Unit = { _, _ -> }
private var wasSaved = false
private lateinit var view: CheckmarkPopupBinding
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val appComponent = (requireActivity().application as HabitsApplication).component val appComponent = (requireActivity().application as HabitsApplication).component
val prefs = appComponent.preferences val prefs = appComponent.preferences
val view = CheckmarkPopupBinding.inflate(LayoutInflater.from(context)) view = CheckmarkPopupBinding.inflate(LayoutInflater.from(context))
val color = requireArguments().getInt("color") val color = requireArguments().getInt("color")
arrayOf(view.yesBtn, view.skipBtn).forEach { arrayOf(view.yesBtn, view.skipBtn).forEach {
it.setTextColor(color) it.setTextColor(color)
@ -62,8 +65,7 @@ class CheckmarkDialog : AppCompatDialogFragment() {
setBackgroundDrawableResource(android.R.color.transparent) setBackgroundDrawableResource(android.R.color.transparent)
} }
fun onClick(v: Int) { fun onClick(v: Int) {
val notes = view.notes.text.toString().trim() save(v)
onToggle(v, notes)
requireDialog().dismiss() requireDialog().dismiss()
} }
view.yesBtn.setOnClickListener { onClick(YES_MANUAL) } view.yesBtn.setOnClickListener { onClick(YES_MANUAL) }
@ -77,4 +79,17 @@ class CheckmarkDialog : AppCompatDialogFragment() {
return dialog return dialog
} }
override fun onDestroyView() {
if (!wasSaved) {
save(requireArguments().getInt("value"))
}
super.onDestroyView()
}
fun save(v: Int) {
val notes = view.notes.text.toString().trim()
onToggle(v, notes)
wasSaved = true
}
} }

@ -30,6 +30,7 @@ class NumberDialog : AppCompatDialogFragment() {
private var originalNotes: String = "" private var originalNotes: String = ""
private var originalValue: Double = 0.0 private var originalValue: Double = 0.0
private var wasSaved = false
private lateinit var view: CheckmarkPopupBinding private lateinit var view: CheckmarkPopupBinding
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
@ -60,26 +61,26 @@ class NumberDialog : AppCompatDialogFragment() {
) )
view.value.setOnKeyListener { _, keyCode, event -> view.value.setOnKeyListener { _, keyCode, event ->
if (event.action == MotionEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) { if (event.action == MotionEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
save() saveAndDismiss()
return@setOnKeyListener true return@setOnKeyListener true
} }
return@setOnKeyListener false return@setOnKeyListener false
} }
view.saveBtn.setOnClickListener { view.saveBtn.setOnClickListener {
save() saveAndDismiss()
} }
view.skipBtnNumber.setOnClickListener { view.skipBtnNumber.setOnClickListener {
view.value.setText(DecimalFormat("#.###").format((Entry.SKIP.toDouble() / 1000))) view.value.setText(DecimalFormat("#.###").format((Entry.SKIP.toDouble() / 1000)))
save() saveAndDismiss()
} }
view.unknownBtnNumber.setOnClickListener { view.unknownBtnNumber.setOnClickListener {
view.value.setText(DecimalFormat("#.###").format((Entry.UNKNOWN.toDouble() / 1000))) view.value.setText(DecimalFormat("#.###").format((Entry.UNKNOWN.toDouble() / 1000)))
save() saveAndDismiss()
} }
view.notes.setOnEditorActionListener { v, actionId, event -> view.notes.setOnEditorActionListener { v, actionId, event ->
save() saveAndDismiss()
true true
} }
view.value.requestFocusWithKeyboard() view.value.requestFocusWithKeyboard()
@ -92,6 +93,13 @@ class NumberDialog : AppCompatDialogFragment() {
return dialog return dialog
} }
override fun onDestroyView() {
if (!wasSaved) {
save()
}
super.onDestroyView()
}
private fun fixDecimalSeparator(view: CheckmarkPopupBinding) { private fun fixDecimalSeparator(view: CheckmarkPopupBinding) {
// https://stackoverflow.com/a/34256139 // https://stackoverflow.com/a/34256139
val separator = DecimalFormatSymbols.getInstance().decimalSeparator val separator = DecimalFormatSymbols.getInstance().decimalSeparator
@ -107,6 +115,11 @@ class NumberDialog : AppCompatDialogFragment() {
} }
} }
fun saveAndDismiss() {
save()
requireDialog().dismiss()
}
fun save() { fun save() {
var value = originalValue var value = originalValue
try { try {
@ -123,6 +136,6 @@ class NumberDialog : AppCompatDialogFragment() {
val notes = view.notes.text.toString() val notes = view.notes.text.toString()
val location = view.saveBtn.getCenter() val location = view.saveBtn.getCenter()
onToggle(value, notes) onToggle(value, notes)
requireDialog().dismiss() wasSaved = true
} }
} }

Loading…
Cancel
Save