@@ -8,6 +8,11 @@ import com.ubergeek42.WeechatAndroid.utils.Linkify
88import com.ubergeek42.WeechatAndroid.utils.Utils
99import com.ubergeek42.WeechatAndroid.utils.invalidatableLazy
1010import com.ubergeek42.weechat.Color
11+ import java.time.Instant
12+ import java.time.LocalDate
13+ import java.time.ZoneId
14+ import java.time.format.DateTimeFormatter
15+ import java.time.format.FormatStyle
1116import java.util.*
1217import kotlin.properties.Delegates.observable
1318
@@ -46,6 +51,13 @@ class Lines {
4651 var maxUnfilteredSize = P .lineIncrement
4752 private set
4853
54+ private var unfilteredDate = LocalDate .MIN
55+ private var filteredDate = LocalDate .MIN
56+
57+ private var lastOldDate = LocalDate .MIN
58+ private var lastNewDate = LocalDate .MIN
59+ private lateinit var lastDateLine: Line
60+
4961 // after reconnecting, in *full sync mode*, we are receiving and adding new lines to buffers.
5062 // there can be inconsistencies; if some lines were added while we were offline,
5163 // we can't display them, but can display what's on top and what's on bottom.
@@ -91,17 +103,53 @@ class Lines {
91103 if (status != Status .Fetching ) return
92104 unfiltered.clear()
93105 filtered.clear()
106+ unfilteredDate = LocalDate .MIN
107+ filteredDate = LocalDate .MIN
108+
94109 for (line in lines) {
95110 addLast(line)
96111 }
97112 }
98113
114+ private fun getDateChangeLine (oldDate : LocalDate , newDate : LocalDate ) : Line {
115+ if (lastOldDate != oldDate || lastNewDate != newDate) {
116+ val tstamp = newDate.atStartOfDay(ZoneId .systemDefault()).toEpochSecond() * 1000
117+ var msg = newDate.format(DateTimeFormatter .ofLocalizedDate(FormatStyle .MEDIUM ))
118+ if (oldDate.plusDays(1 ) != newDate) {
119+ msg + = " (" +
120+ oldDate.format(DateTimeFormatter .ofLocalizedDate(FormatStyle .MEDIUM )) +
121+ " )"
122+ }
123+ msg + = " --"
124+ lastDateLine = Line (++ fakePointerCounter, LineSpec .Type .Other , tstamp, " --" , msg,
125+ nick = null , isVisible = true , isHighlighted = false ,
126+ LineSpec .DisplayAs .Unspecified , LineSpec .NotifyLevel .Low )
127+ lastOldDate = oldDate
128+ lastNewDate = newDate
129+ }
130+ return lastDateLine
131+ }
132+
99133 fun addLast (line : Line ) {
100134 if (shouldAddSquiggleOnNewLine) {
101135 shouldAddSquiggleOnNewLine = false
102136 if (status == Status .Init && unfiltered.size > 0 ) addLast(SquiggleLine ()) // invisible
103137 }
104138
139+ val newDate = Instant .ofEpochSecond(line.timestamp / 1000 )
140+ .atZone(ZoneId .systemDefault())
141+ .toLocalDate()
142+
143+ if (unfilteredDate != newDate) {
144+ unfiltered.addLast(getDateChangeLine(unfilteredDate, newDate))
145+ unfilteredDate = newDate
146+ }
147+
148+ if (line.isVisible && filteredDate != newDate) {
149+ filtered.addLast(getDateChangeLine(filteredDate, newDate))
150+ filteredDate = newDate
151+ }
152+
105153 if (shouldAddSquiggleOnNewVisibleLine && line.isVisible) {
106154 shouldAddSquiggleOnNewVisibleLine = false
107155 if (status == Status .Init && filtered.size > 0 ) {
0 commit comments