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 397c6f4ff..6d8528b76 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 @@ -23,6 +23,7 @@ import android.os.Bundle import android.view.HapticFeedbackConstants import android.view.Menu import android.view.MenuItem +import android.view.View import androidx.appcompat.app.AppCompatActivity import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -38,6 +39,7 @@ import org.isoron.uhabits.activities.common.dialogs.CheckmarkPopup import org.isoron.uhabits.activities.common.dialogs.ConfirmDeleteDialog import org.isoron.uhabits.activities.common.dialogs.HistoryEditorDialog import org.isoron.uhabits.activities.common.dialogs.NumberPickerFactory +import org.isoron.uhabits.activities.common.dialogs.NumberPopup import org.isoron.uhabits.activities.common.dialogs.POPUP_WIDTH import org.isoron.uhabits.core.commands.Command import org.isoron.uhabits.core.commands.CommandRunner @@ -187,6 +189,26 @@ class ShowHabitActivity : AppCompatActivity(), CommandRunner.Listener { ).show() } + override fun showNumberPopup( + value: Double, + notes: String, + preferences: Preferences, + location: ScreenLocation, + callback: ListHabitsBehavior.NumberPickerCallback + ) { + val anchor = getPopupAnchor() ?: return + NumberPopup( + context = this@ShowHabitActivity, + prefs = preferences, + notes = notes, + anchor = anchor, + value = value, + ).apply { + onToggle = { v, n -> callback.onNumberPicked(v, n) } + show(computePopupLocation(anchor, location)) + } + } + override fun showCheckmarkPopup( selectedValue: Int, notes: String, @@ -195,11 +217,7 @@ class ShowHabitActivity : AppCompatActivity(), CommandRunner.Listener { location: ScreenLocation, callback: ListHabitsBehavior.CheckMarkDialogCallback ) { - val dialog = - supportFragmentManager.findFragmentByTag("historyEditor") as HistoryEditorDialog? - ?: return - val view = dialog.dataView - val corner = view.getTopLeftCorner() + val anchor = getPopupAnchor() ?: return CheckmarkPopup( context = this@ShowHabitActivity, prefs = preferences, @@ -209,15 +227,23 @@ class ShowHabitActivity : AppCompatActivity(), CommandRunner.Listener { value = selectedValue, ).apply { onToggle = { v, n -> callback.onNotesSaved(v, n) } - show( - ScreenLocation( - x = corner.x + location.x - POPUP_WIDTH / 2, - y = corner.y + location.y, - ) - ) + show(computePopupLocation(anchor, location)) } } + private fun getPopupAnchor(): View? { + val dialog = supportFragmentManager.findFragmentByTag("historyEditor") as HistoryEditorDialog? + return dialog?.dataView + } + + private fun computePopupLocation(anchor: View, clickLocation: ScreenLocation): ScreenLocation { + val corner = anchor.getTopLeftCorner() + return ScreenLocation( + x = corner.x + clickLocation.x - POPUP_WIDTH / 2, + y = corner.y + clickLocation.y, + ) + } + override fun showEditHabitScreen(habit: Habit) { startActivity(IntentFactory().startEditActivity(this@ShowHabitActivity, habit)) } diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/screens/habits/show/views/HistoryCard.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/screens/habits/show/views/HistoryCard.kt index a6f9fd98b..b2ec71f99 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/screens/habits/show/views/HistoryCard.kt +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/screens/habits/show/views/HistoryCard.kt @@ -70,7 +70,7 @@ class HistoryCardPresenter( val timestamp = Timestamp.fromLocalDate(date) screen.showFeedback() if (habit.isNumerical) { - showNumberPicker(timestamp) + showNumberPopup(location, timestamp) } else { if (preferences.isShortToggleEnabled) showCheckmarkPopup(location, timestamp) else toggle(timestamp) @@ -81,7 +81,7 @@ class HistoryCardPresenter( val timestamp = Timestamp.fromLocalDate(date) screen.showFeedback() if (habit.isNumerical) { - showNumberPicker(timestamp) + showNumberPopup(location, timestamp) } else { if (preferences.isShortToggleEnabled) toggle(timestamp) else showCheckmarkPopup(location, timestamp) @@ -127,15 +127,14 @@ class HistoryCardPresenter( ) } - private fun showNumberPicker(timestamp: Timestamp) { + private fun showNumberPopup(location: ScreenLocation, timestamp: Timestamp) { val entry = habit.computedEntries.get(timestamp) val oldValue = entry.value - screen.showNumberPicker( - oldValue / 1000.0, - habit.unit, - entry.notes, - timestamp.toDialogDateString(), - frequency = habit.frequency + screen.showNumberPopup( + value = oldValue / 1000.0, + notes = entry.notes, + preferences = preferences, + location = location, ) { newValue: Double, newNotes: String -> val thousands = (newValue * 1000).roundToInt() commandRunner.run( @@ -213,7 +212,13 @@ class HistoryCardPresenter( frequency: Frequency, callback: ListHabitsBehavior.NumberPickerCallback ) - + fun showNumberPopup( + value: Double, + notes: String, + preferences: Preferences, + location: ScreenLocation, + callback: ListHabitsBehavior.NumberPickerCallback, + ) fun showCheckmarkPopup( selectedValue: Int, notes: String,