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 13, 2021
1 parent d85172e commit 023d655
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 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,12 @@ 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.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.time.temporal.ChronoUnit
import java.util.*
import kotlin.properties.Delegates.observable

Expand Down Expand Up @@ -93,12 +99,45 @@ class Lines {
filtered.clear()
}

private fun addDateLine(array: ArrayDeque<Line>, newTimestamp: Long) {
var oldDate = LocalDateTime.MIN
if (!array.isEmpty()) {
oldDate = Instant.ofEpochSecond(array.getLast().timestamp / 1000)
.atZone(ZoneId.systemDefault())
.toLocalDateTime()
}
val newDate = Instant.ofEpochSecond(newTimestamp / 1000)
.atZone(ZoneId.systemDefault())
.toLocalDateTime()
if (newDate.getDayOfYear() > oldDate.getDayOfYear() ||
newDate.getYear() > oldDate.getYear()) {
val midnight = newDate.truncatedTo(ChronoUnit.DAYS)
val tstamp = midnight.atZone(ZoneId.systemDefault()).toEpochSecond() * 1000
var msg = midnight.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)) + " --"
if (newDate.getDayOfYear() > oldDate.getDayOfYear() + 1 ||
newDate.getYear() > oldDate.getYear()) {
val oldMidnight = oldDate.truncatedTo(ChronoUnit.DAYS)
msg = midnight.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)) +
" (" +
oldMidnight.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)) +
") --"
}
val dateLine = Line(++fakePointerCounter, LineSpec.Type.Other, tstamp, "-- ", msg,
nick = null, isVisible = true, isHighlighted = false,
LineSpec.DisplayAs.Unspecified, LineSpec.NotifyLevel.Low)
array.addLast(dateLine)
}
}

fun addLast(line: Line) {
if (shouldAddSquiggleOnNewLine) {
shouldAddSquiggleOnNewLine = false
if (status == Status.Init && unfiltered.size > 0) addLast(SquiggleLine()) // invisible
}

addDateLine(unfiltered, line.timestamp)
if (line.isVisible) addDateLine(filtered, line.timestamp)

if (shouldAddSquiggleOnNewVisibleLine && line.isVisible) {
shouldAddSquiggleOnNewVisibleLine = false
if (status == Status.Init && filtered.size > 0) {
Expand Down

0 comments on commit 023d655

Please sign in to comment.