Skip to content

Commit

Permalink
Display day change messages
Browse files Browse the repository at this point in the history
Similar to weechat.look.day_change.
  • Loading branch information
jspricke committed Jun 17, 2021
1 parent 6ee35b2 commit 1fe18b9
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions app/src/main/java/com/ubergeek42/WeechatAndroid/relay/Lines.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1fe18b9

Please sign in to comment.