From 40329424b8d47c6261ce93d9b53af555c52a6db7 Mon Sep 17 00:00:00 2001 From: Jochen Sprickerhof Date: Sun, 13 Jun 2021 12:23:36 +0200 Subject: [PATCH] Display day change messages Similar to weechat.look.day_change. --- .../ubergeek42/WeechatAndroid/relay/Lines.kt | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/app/src/main/java/com/ubergeek42/WeechatAndroid/relay/Lines.kt b/app/src/main/java/com/ubergeek42/WeechatAndroid/relay/Lines.kt index 8b5ab65b..87bae21f 100644 --- a/app/src/main/java/com/ubergeek42/WeechatAndroid/relay/Lines.kt +++ b/app/src/main/java/com/ubergeek42/WeechatAndroid/relay/Lines.kt @@ -8,6 +8,11 @@ import com.ubergeek42.WeechatAndroid.utils.Linkify import com.ubergeek42.WeechatAndroid.utils.Utils import com.ubergeek42.WeechatAndroid.utils.invalidatableLazy import com.ubergeek42.weechat.Color +import java.time.Instant +import java.time.LocalDate +import java.time.ZoneId +import java.time.format.DateTimeFormatter +import java.time.format.FormatStyle import java.util.* import kotlin.properties.Delegates.observable @@ -46,6 +51,13 @@ class Lines { var maxUnfilteredSize = P.lineIncrement private set + private var unfilteredDate: LocalDate? = null + private var filteredDate: LocalDate? = null + + private var lastOldDate: LocalDate? = null + private lateinit var lastNewDate: LocalDate + private lateinit var lastDateLine: Line + // after reconnecting, in *full sync mode*, we are receiving and adding new lines to buffers. // there can be inconsistencies; if some lines were added while we were offline, // we can't display them, but can display what's on top and what's on bottom. @@ -91,17 +103,54 @@ class Lines { if (status != Status.Fetching) return unfiltered.clear() filtered.clear() + unfilteredDate = null + filteredDate = null + for (line in lines) { addLast(line) } } + private fun getDateChangeLine(oldDate: LocalDate?, newDate: LocalDate) : Line { + if (lastOldDate == null || oldDate == null || + lastOldDate != oldDate || lastNewDate != newDate) { + val tstamp = newDate.atStartOfDay(ZoneId.systemDefault()).toEpochSecond() * 1000 + var msg = newDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)) + if (oldDate != null && oldDate.plusDays(1) != newDate) { + msg += " (" + + oldDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)) + + ")" + } + msg += " --" + lastDateLine = Line(++fakePointerCounter, LineSpec.Type.Other, tstamp, "--", msg, + nick = null, isVisible = true, isHighlighted = false, + LineSpec.DisplayAs.Unspecified, LineSpec.NotifyLevel.Low) + lastOldDate = oldDate + lastNewDate = newDate + } + return lastDateLine + } + fun addLast(line: Line) { if (shouldAddSquiggleOnNewLine) { shouldAddSquiggleOnNewLine = false if (status == Status.Init && unfiltered.size > 0) addLast(SquiggleLine()) // invisible } + val newDate = Instant.ofEpochSecond(line.timestamp / 1000) + .atZone(ZoneId.systemDefault()) + .toLocalDate() + + if (unfilteredDate == null || unfilteredDate != newDate) { + unfiltered.addLast(getDateChangeLine(unfilteredDate, newDate)) + unfilteredDate = newDate + } + + if (line.isVisible && (filteredDate == null || filteredDate != newDate)) { + filtered.addLast(getDateChangeLine(filteredDate, newDate)) + filteredDate = newDate + } + if (shouldAddSquiggleOnNewVisibleLine && line.isVisible) { shouldAddSquiggleOnNewVisibleLine = false if (status == Status.Init && filtered.size > 0) {