From 1fe18b924e32a0bb124565772211ad0dd7e5b9f9 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 | 48 +++++++++++++++++++ 1 file changed, 48 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..d9618ecb 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.MIN + private var filteredDate = LocalDate.MIN + + private var lastOldDate = LocalDate.MIN + private var lastNewDate = LocalDate.MIN + 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,53 @@ class Lines { if (status != Status.Fetching) return unfiltered.clear() filtered.clear() + unfilteredDate = LocalDate.MIN + filteredDate = LocalDate.MIN + for (line in lines) { addLast(line) } } + private fun getDateChangeLine(oldDate: LocalDate, newDate: LocalDate) : Line { + if (lastOldDate != oldDate || lastNewDate != newDate) { + val tstamp = newDate.atStartOfDay(ZoneId.systemDefault()).toEpochSecond() * 1000 + var msg = newDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)) + if (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 != newDate) { + unfiltered.addLast(getDateChangeLine(unfilteredDate, newDate)) + unfilteredDate = newDate + } + + if (line.isVisible && filteredDate != newDate) { + filtered.addLast(getDateChangeLine(filteredDate, newDate)) + filteredDate = newDate + } + if (shouldAddSquiggleOnNewVisibleLine && line.isVisible) { shouldAddSquiggleOnNewVisibleLine = false if (status == Status.Init && filtered.size > 0) {