Print a 'date divider' when the day changes

I.E., if one message is sent on 2021-05-02 and the next on 2021-05-03, a
divider displaying the new date is printed before the second message.
This commit is contained in:
Jaidyn Ann 2021-07-25 11:28:07 -05:00
parent fff36dff6f
commit 5966df1a34
3 changed files with 26 additions and 5 deletions

View File

@ -352,7 +352,7 @@ ConversationView::_AppendMessage(BMessage* msg)
} }
if (sender_name.IsEmpty() == true) { if (sender_name.IsEmpty() == true) {
fReceiveView->AppendGenericMessage(body.String()); fReceiveView->AppendGeneric(body.String());
continue; continue;
} }

View File

@ -10,7 +10,9 @@
RenderView::RenderView(const char* name) RenderView::RenderView(const char* name)
: :
RunView(name) RunView(name),
fLastDay(364),
fLastYear(64)
{ {
} }
@ -32,7 +34,7 @@ RenderView::AppendMessage(const char* nick, const char* message,
void void
RenderView::AppendGenericMessage(const char* message) RenderView::AppendGeneric(const char* message)
{ {
if (BString(message).IsEmpty() == true) return; if (BString(message).IsEmpty() == true) return;
AppendTimestamp(time(NULL)); AppendTimestamp(time(NULL));
@ -44,11 +46,26 @@ RenderView::AppendGenericMessage(const char* message)
void void
RenderView::AppendTimestamp(time_t time) RenderView::AppendTimestamp(time_t time)
{ {
tm* tm = localtime(&time);
// If day changed, print date divider
if (fLastDay < tm->tm_yday || fLastYear < tm->tm_year) {
char datestamp[11] = { '\0' };
strftime(datestamp, 10, "%Y-%m-%d", tm);
BString stamp("――― %date% ―――\n");
stamp.ReplaceAll("%date%", datestamp);
Append(stamp.String(), ui_color(B_PANEL_TEXT_COLOR), B_ITALIC_FACE);
fLastDay = tm->tm_yday;
fLastYear = tm->tm_year;
}
if (time == 0) { if (time == 0) {
Append("[xx:xx] ", ui_color(B_LINK_HOVER_COLOR)); Append("[xx:xx] ", ui_color(B_LINK_HOVER_COLOR));
return; return;
} }
char timestamp[9] = { '\0' }; char timestamp[9] = { '\0' };
strftime(timestamp, 8, "[%H:%M] ", localtime(&time)); strftime(timestamp, 8, "[%H:%M] ", tm);
Append(timestamp, ui_color(B_LINK_HOVER_COLOR)); Append(timestamp, ui_color(B_LINK_HOVER_COLOR));
} }

View File

@ -14,8 +14,12 @@ public:
void AppendMessage(const char* nick, const char* message, void AppendMessage(const char* nick, const char* message,
rgb_color nameColor, time_t time = 0); rgb_color nameColor, time_t time = 0);
void AppendGenericMessage(const char* message); void AppendGeneric(const char* message);
void AppendTimestamp(time_t time = 0); void AppendTimestamp(time_t time = 0);
private:
int fLastDay;
int fLastYear;
}; };
#endif // _RENDER_VIEW_H #endif // _RENDER_VIEW_H