mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Add dialog and notes indicator for HistoryChart
This commit is contained in:
@@ -23,6 +23,8 @@ interface View {
|
||||
fun draw(canvas: Canvas)
|
||||
fun onClick(x: Double, y: Double) {
|
||||
}
|
||||
fun onLongClick(x: Double, y: Double) {
|
||||
}
|
||||
}
|
||||
|
||||
interface DataView : View {
|
||||
|
||||
@@ -46,6 +46,7 @@ data class HistoryCardState(
|
||||
val firstWeekday: DayOfWeek,
|
||||
val series: List<HistoryChart.Square>,
|
||||
val defaultSquare: HistoryChart.Square,
|
||||
val hasNotes: List<Boolean>,
|
||||
val theme: Theme,
|
||||
val today: LocalDate,
|
||||
)
|
||||
@@ -58,7 +59,7 @@ class HistoryCardPresenter(
|
||||
val screen: Screen,
|
||||
) : OnDateClickedListener {
|
||||
|
||||
override fun onDateClicked(date: LocalDate) {
|
||||
override fun onDateClicked(date: LocalDate, isLongClick: Boolean) {
|
||||
val timestamp = Timestamp.fromLocalDate(date)
|
||||
screen.showFeedback()
|
||||
if (habit.isNumerical) {
|
||||
@@ -81,20 +82,34 @@ class HistoryCardPresenter(
|
||||
val entry = habit.computedEntries.get(timestamp)
|
||||
val currentValue = entry.value
|
||||
val notes = entry.notes
|
||||
val nextValue = Entry.nextToggleValue(
|
||||
value = currentValue,
|
||||
isSkipEnabled = preferences.isSkipEnabled,
|
||||
areQuestionMarksEnabled = preferences.areQuestionMarksEnabled
|
||||
)
|
||||
commandRunner.run(
|
||||
CreateRepetitionCommand(
|
||||
habitList,
|
||||
habit,
|
||||
timestamp,
|
||||
nextValue,
|
||||
notes,
|
||||
),
|
||||
)
|
||||
if (!isLongClick) {
|
||||
val nextValue = Entry.nextToggleValue(
|
||||
value = currentValue,
|
||||
isSkipEnabled = preferences.isSkipEnabled,
|
||||
areQuestionMarksEnabled = preferences.areQuestionMarksEnabled
|
||||
)
|
||||
commandRunner.run(
|
||||
CreateRepetitionCommand(
|
||||
habitList,
|
||||
habit,
|
||||
timestamp,
|
||||
nextValue,
|
||||
notes,
|
||||
),
|
||||
)
|
||||
return
|
||||
}
|
||||
screen.showCheckmarkDialog(notes) { newNotes ->
|
||||
commandRunner.run(
|
||||
CreateRepetitionCommand(
|
||||
habitList,
|
||||
habit,
|
||||
timestamp,
|
||||
currentValue,
|
||||
newNotes,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,6 +157,13 @@ class HistoryCardPresenter(
|
||||
else
|
||||
HistoryChart.Square.OFF
|
||||
|
||||
val hasNotes = entries.map {
|
||||
when (it.notes) {
|
||||
"" -> false
|
||||
else -> true
|
||||
}
|
||||
}
|
||||
|
||||
return HistoryCardState(
|
||||
color = habit.color,
|
||||
firstWeekday = firstWeekday,
|
||||
@@ -149,6 +171,7 @@ class HistoryCardPresenter(
|
||||
theme = theme,
|
||||
series = series,
|
||||
defaultSquare = defaultSquare
|
||||
hasNotes = hasNotes,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ import kotlin.math.min
|
||||
import kotlin.math.round
|
||||
|
||||
fun interface OnDateClickedListener {
|
||||
fun onDateClicked(date: LocalDate)
|
||||
fun onDateClicked(date: LocalDate, isLongClick: Boolean)
|
||||
}
|
||||
|
||||
class HistoryChart(
|
||||
@@ -42,9 +42,10 @@ class HistoryChart(
|
||||
var paletteColor: PaletteColor,
|
||||
var series: List<Square>,
|
||||
var defaultSquare: Square,
|
||||
var hasNotes: List<Boolean>,
|
||||
var theme: Theme,
|
||||
var today: LocalDate,
|
||||
var onDateClickedListener: OnDateClickedListener = OnDateClickedListener { },
|
||||
var onDateClickedListener: OnDateClickedListener = OnDateClickedListener { _, _ -> },
|
||||
var padding: Double = 0.0,
|
||||
) : DataView {
|
||||
|
||||
@@ -72,6 +73,14 @@ class HistoryChart(
|
||||
get() = squareSpacing + squareSize
|
||||
|
||||
override fun onClick(x: Double, y: Double) {
|
||||
onDateClicked(x, y, false)
|
||||
}
|
||||
|
||||
override fun onLongClick(x: Double, y: Double) {
|
||||
onDateClicked(x, y, true)
|
||||
}
|
||||
|
||||
private fun onDateClicked(x: Double, y: Double, isLongClick: Boolean) {
|
||||
if (width <= 0.0) throw IllegalStateException("onClick must be called after draw(canvas)")
|
||||
val col = ((x - padding) / squareSize).toInt()
|
||||
val row = ((y - padding) / squareSize).toInt()
|
||||
@@ -79,7 +88,7 @@ class HistoryChart(
|
||||
if (row == 0 || col == nColumns) return
|
||||
val clickedDate = topLeftDate.plus(offset)
|
||||
if (clickedDate.isNewerThan(today)) return
|
||||
onDateClickedListener.onDateClicked(clickedDate)
|
||||
onDateClickedListener.onDateClicked(clickedDate, isLongClick)
|
||||
}
|
||||
|
||||
override fun draw(canvas: Canvas) {
|
||||
@@ -191,7 +200,9 @@ class HistoryChart(
|
||||
) {
|
||||
|
||||
val value = if (offset >= series.size) defaultSquare else series[offset]
|
||||
val notes = if (offset >= hasNotes.size) false else hasNotes[offset]
|
||||
val squareColor: Color
|
||||
val circleColor: Color
|
||||
val color = theme.color(paletteColor.paletteIndex)
|
||||
squareColor = when (value) {
|
||||
Square.ON -> {
|
||||
@@ -235,5 +246,14 @@ class HistoryChart(
|
||||
canvas.setColor(textColor)
|
||||
canvas.setTextAlign(TextAlign.CENTER)
|
||||
canvas.drawText(date.day.toString(), x + width / 2, y + width / 2)
|
||||
|
||||
if (notes) {
|
||||
circleColor = when (value) {
|
||||
Square.ON -> theme.lowContrastTextColor
|
||||
else -> color
|
||||
}
|
||||
canvas.setColor(circleColor)
|
||||
canvas.fillCircle(x + width - width / 5, y + width / 5, width / 12)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user