From 71a05d598a483c1c28c2947e26d4b87d421df65a Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Mon, 30 Jan 2023 05:59:42 -0600 Subject: [PATCH] CheckmarkDialog: Switch to AppCompatDialogFragment Fixes issues with the soft keyboard covering the popup. --- .../common/dialogs/CheckmarkDialog.kt | 79 +++++++++++++++++++ .../habits/list/ListHabitsScreen.kt | 23 +++--- .../habits/show/ShowHabitActivity.kt | 21 +++-- .../org/isoron/uhabits/utils/DialogUtils.kt | 10 ++- .../src/main/res/layout/checkmark_popup.xml | 8 +- 5 files changed, 111 insertions(+), 30 deletions(-) create mode 100644 uhabits-android/src/main/java/org/isoron/uhabits/activities/common/dialogs/CheckmarkDialog.kt diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/activities/common/dialogs/CheckmarkDialog.kt b/uhabits-android/src/main/java/org/isoron/uhabits/activities/common/dialogs/CheckmarkDialog.kt new file mode 100644 index 000000000..5df8ffd7b --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/activities/common/dialogs/CheckmarkDialog.kt @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2016-2021 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.activities.common.dialogs + +import android.app.Dialog +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View.GONE +import android.view.View.VISIBLE +import androidx.appcompat.app.AppCompatDialogFragment +import org.isoron.uhabits.HabitsApplication +import org.isoron.uhabits.R +import org.isoron.uhabits.core.models.Entry.Companion.NO +import org.isoron.uhabits.core.models.Entry.Companion.SKIP +import org.isoron.uhabits.core.models.Entry.Companion.UNKNOWN +import org.isoron.uhabits.core.models.Entry.Companion.YES_MANUAL +import org.isoron.uhabits.databinding.CheckmarkPopupBinding +import org.isoron.uhabits.utils.InterfaceUtils.getFontAwesome +import org.isoron.uhabits.utils.sres + +class CheckmarkDialog : AppCompatDialogFragment() { + var onToggle: (Int, String) -> Unit = { _, _ -> } + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val appComponent = (requireActivity().application as HabitsApplication).component + val prefs = appComponent.preferences + val view = CheckmarkPopupBinding.inflate(LayoutInflater.from(context)) + arrayOf(view.yesBtn, view.skipBtn).forEach { + it.setTextColor(requireArguments().getInt("color")) + } + arrayOf(view.noBtn, view.unknownBtn).forEach { + it.setTextColor(view.root.sres.getColor(R.attr.contrast60)) + } + arrayOf(view.yesBtn, view.noBtn, view.skipBtn, view.unknownBtn).forEach { + it.typeface = getFontAwesome(requireContext()) + } + view.notes.setText(requireArguments().getString("notes")!!) + if (!prefs.isSkipEnabled) view.skipBtn.visibility = GONE + if (!prefs.areQuestionMarksEnabled) view.unknownBtn.visibility = GONE + view.booleanButtons.visibility = VISIBLE + val dialog = Dialog(requireContext()) + dialog.setContentView(view.root) + dialog.window?.apply { + setBackgroundDrawableResource(android.R.color.transparent) + } + fun onClick(v: Int) { + val notes = view.notes.text.toString().trim() + onToggle(v, notes) + requireDialog().dismiss() + } + view.yesBtn.setOnClickListener { onClick(YES_MANUAL) } + view.noBtn.setOnClickListener { onClick(NO) } + view.skipBtn.setOnClickListener { onClick(SKIP) } + view.unknownBtn.setOnClickListener { onClick(UNKNOWN) } + view.notes.setOnEditorActionListener { v, actionId, event -> + onClick(requireArguments().getInt("value")) + true + } + + return dialog + } +} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsScreen.kt b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsScreen.kt index 731342974..8dda56a3d 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsScreen.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsScreen.kt @@ -22,11 +22,12 @@ package org.isoron.uhabits.activities.habits.list import android.app.Activity import android.content.Context import android.content.Intent +import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import dagger.Lazy import org.isoron.platform.gui.toInt import org.isoron.uhabits.R -import org.isoron.uhabits.activities.common.dialogs.CheckmarkPopup +import org.isoron.uhabits.activities.common.dialogs.CheckmarkDialog import org.isoron.uhabits.activities.common.dialogs.ColorPickerDialogFactory import org.isoron.uhabits.activities.common.dialogs.ConfirmDeleteDialog import org.isoron.uhabits.activities.common.dialogs.NumberPopup @@ -252,18 +253,16 @@ class ListHabitsScreen color: PaletteColor, callback: ListHabitsBehavior.CheckMarkDialogCallback ) { - val view = rootView.get() - CheckmarkPopup( - context = context, - prefs = preferences, - anchor = view, - color = view.currentTheme().color(color).toInt(), - notes = notes, - value = selectedValue, - ).apply { - onToggle = { value, notes -> callback.onNotesSaved(value, notes) } - show() + val theme = rootView.get().currentTheme() + val fm = (context as AppCompatActivity).supportFragmentManager + val dialog = CheckmarkDialog() + dialog.arguments = Bundle().apply { + putInt("color", theme.color(color).toInt()) + putInt("value", selectedValue) + putString("notes", notes) } + dialog.onToggle = { v, n -> callback.onNotesSaved(v, n) } + dialog.dismissCurrentAndShow(fm, "checkmarkDialog") } private fun getExecuteString(command: Command): String? { diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitActivity.kt b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitActivity.kt index b68dc1f36..b92f3a5b1 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitActivity.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitActivity.kt @@ -34,7 +34,7 @@ import org.isoron.uhabits.HabitsApplication import org.isoron.uhabits.R import org.isoron.uhabits.activities.AndroidThemeSwitcher import org.isoron.uhabits.activities.HabitsDirFinder -import org.isoron.uhabits.activities.common.dialogs.CheckmarkPopup +import org.isoron.uhabits.activities.common.dialogs.CheckmarkDialog import org.isoron.uhabits.activities.common.dialogs.ConfirmDeleteDialog import org.isoron.uhabits.activities.common.dialogs.HistoryEditorDialog import org.isoron.uhabits.activities.common.dialogs.NumberPopup @@ -195,18 +195,15 @@ class ShowHabitActivity : AppCompatActivity(), CommandRunner.Listener { color: PaletteColor, callback: ListHabitsBehavior.CheckMarkDialogCallback ) { - val anchor = getPopupAnchor() ?: return - CheckmarkPopup( - context = this@ShowHabitActivity, - prefs = preferences, - notes = notes, - color = view.currentTheme().color(color).toInt(), - anchor = anchor, - value = selectedValue, - ).apply { - onToggle = { v, n -> callback.onNotesSaved(v, n) } - show() + val theme = view.currentTheme() + val dialog = CheckmarkDialog() + dialog.arguments = Bundle().apply { + putInt("color", theme.color(color).toInt()) + putInt("value", selectedValue) + putString("notes", notes) } + dialog.onToggle = { v, n -> callback.onNotesSaved(v, n) } + dialog.dismissCurrentAndShow(supportFragmentManager, "checkmarkDialog") } private fun getPopupAnchor(): View? { diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/utils/DialogUtils.kt b/uhabits-android/src/main/java/org/isoron/uhabits/utils/DialogUtils.kt index 00a17aa9a..a1aefe16f 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/utils/DialogUtils.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/utils/DialogUtils.kt @@ -6,20 +6,24 @@ import androidx.fragment.app.FragmentManager import java.lang.ref.WeakReference var currentDialog: WeakReference = WeakReference(null) +var currentDialogFragment: WeakReference = WeakReference(null) fun dismissCurrentDialog() { currentDialog.get()?.dismiss() + currentDialog = WeakReference(null) + currentDialogFragment.get()?.dismiss() + currentDialogFragment = WeakReference(null) } fun Dialog.dismissCurrentAndShow() { - currentDialog.get()?.dismiss() + dismissCurrentDialog() currentDialog = WeakReference(this) show() } fun DialogFragment.dismissCurrentAndShow(fragmentManager: FragmentManager, tag: String) { - currentDialog.get()?.dismiss() + dismissCurrentDialog() + currentDialogFragment = WeakReference(this) show(fragmentManager, tag) fragmentManager.executePendingTransactions() - currentDialog = WeakReference(this.dialog) } diff --git a/uhabits-android/src/main/res/layout/checkmark_popup.xml b/uhabits-android/src/main/res/layout/checkmark_popup.xml index 72dc79786..e21c2fb54 100644 --- a/uhabits-android/src/main/res/layout/checkmark_popup.xml +++ b/uhabits-android/src/main/res/layout/checkmark_popup.xml @@ -21,8 +21,10 @@