Use NumberPopup in HistoryCard

pull/1370/head
Alinson S. Xavier 3 years ago
parent 2a012619a7
commit 555873354c
No known key found for this signature in database
GPG Key ID: DCA0DAD4D2F58624

@ -23,6 +23,7 @@ import android.os.Bundle
import android.view.HapticFeedbackConstants import android.view.HapticFeedbackConstants
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers 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.ConfirmDeleteDialog
import org.isoron.uhabits.activities.common.dialogs.HistoryEditorDialog import org.isoron.uhabits.activities.common.dialogs.HistoryEditorDialog
import org.isoron.uhabits.activities.common.dialogs.NumberPickerFactory 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.activities.common.dialogs.POPUP_WIDTH
import org.isoron.uhabits.core.commands.Command import org.isoron.uhabits.core.commands.Command
import org.isoron.uhabits.core.commands.CommandRunner import org.isoron.uhabits.core.commands.CommandRunner
@ -187,6 +189,26 @@ class ShowHabitActivity : AppCompatActivity(), CommandRunner.Listener {
).show() ).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( override fun showCheckmarkPopup(
selectedValue: Int, selectedValue: Int,
notes: String, notes: String,
@ -195,11 +217,7 @@ class ShowHabitActivity : AppCompatActivity(), CommandRunner.Listener {
location: ScreenLocation, location: ScreenLocation,
callback: ListHabitsBehavior.CheckMarkDialogCallback callback: ListHabitsBehavior.CheckMarkDialogCallback
) { ) {
val dialog = val anchor = getPopupAnchor() ?: return
supportFragmentManager.findFragmentByTag("historyEditor") as HistoryEditorDialog?
?: return
val view = dialog.dataView
val corner = view.getTopLeftCorner()
CheckmarkPopup( CheckmarkPopup(
context = this@ShowHabitActivity, context = this@ShowHabitActivity,
prefs = preferences, prefs = preferences,
@ -209,15 +227,23 @@ class ShowHabitActivity : AppCompatActivity(), CommandRunner.Listener {
value = selectedValue, value = selectedValue,
).apply { ).apply {
onToggle = { v, n -> callback.onNotesSaved(v, n) } onToggle = { v, n -> callback.onNotesSaved(v, n) }
show( show(computePopupLocation(anchor, location))
ScreenLocation(
x = corner.x + location.x - POPUP_WIDTH / 2,
y = corner.y + location.y,
)
)
} }
} }
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) { override fun showEditHabitScreen(habit: Habit) {
startActivity(IntentFactory().startEditActivity(this@ShowHabitActivity, habit)) startActivity(IntentFactory().startEditActivity(this@ShowHabitActivity, habit))
} }

@ -70,7 +70,7 @@ class HistoryCardPresenter(
val timestamp = Timestamp.fromLocalDate(date) val timestamp = Timestamp.fromLocalDate(date)
screen.showFeedback() screen.showFeedback()
if (habit.isNumerical) { if (habit.isNumerical) {
showNumberPicker(timestamp) showNumberPopup(location, timestamp)
} else { } else {
if (preferences.isShortToggleEnabled) showCheckmarkPopup(location, timestamp) if (preferences.isShortToggleEnabled) showCheckmarkPopup(location, timestamp)
else toggle(timestamp) else toggle(timestamp)
@ -81,7 +81,7 @@ class HistoryCardPresenter(
val timestamp = Timestamp.fromLocalDate(date) val timestamp = Timestamp.fromLocalDate(date)
screen.showFeedback() screen.showFeedback()
if (habit.isNumerical) { if (habit.isNumerical) {
showNumberPicker(timestamp) showNumberPopup(location, timestamp)
} else { } else {
if (preferences.isShortToggleEnabled) toggle(timestamp) if (preferences.isShortToggleEnabled) toggle(timestamp)
else showCheckmarkPopup(location, 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 entry = habit.computedEntries.get(timestamp)
val oldValue = entry.value val oldValue = entry.value
screen.showNumberPicker( screen.showNumberPopup(
oldValue / 1000.0, value = oldValue / 1000.0,
habit.unit, notes = entry.notes,
entry.notes, preferences = preferences,
timestamp.toDialogDateString(), location = location,
frequency = habit.frequency
) { newValue: Double, newNotes: String -> ) { newValue: Double, newNotes: String ->
val thousands = (newValue * 1000).roundToInt() val thousands = (newValue * 1000).roundToInt()
commandRunner.run( commandRunner.run(
@ -213,7 +212,13 @@ class HistoryCardPresenter(
frequency: Frequency, frequency: Frequency,
callback: ListHabitsBehavior.NumberPickerCallback callback: ListHabitsBehavior.NumberPickerCallback
) )
fun showNumberPopup(
value: Double,
notes: String,
preferences: Preferences,
location: ScreenLocation,
callback: ListHabitsBehavior.NumberPickerCallback,
)
fun showCheckmarkPopup( fun showCheckmarkPopup(
selectedValue: Int, selectedValue: Int,
notes: String, notes: String,

Loading…
Cancel
Save