Update date separator at midnight

fixes #868
This commit is contained in:
fiaxh 2020-11-21 12:43:44 +01:00
parent 4f4a1036e1
commit c0be0f5f85

View file

@ -62,30 +62,58 @@ public class MetaDateItem : Plugins.MetaConversationItem {
} }
public override Object? get_widget(Plugins.WidgetType widget_type) { public override Object? get_widget(Plugins.WidgetType widget_type) {
Box box = new Box(Orientation.HORIZONTAL, 10) { width_request=300, halign=Align.CENTER, visible=true }; return new DateSeparatorWidget(date);
box.add(new Separator(Orientation.HORIZONTAL) { valign=Align.CENTER, hexpand=true, visible=true });
string date_str = get_relative_time(date);
Label label = new Label(@"<span size='small'>$date_str</span>") { use_markup=true, halign=Align.CENTER, hexpand=false, visible=true };
label.get_style_context().add_class("dim-label");
box.add(label);
box.add(new Separator(Orientation.HORIZONTAL) { valign=Align.CENTER, hexpand=true, visible=true });
return box;
} }
public override Gee.List<Plugins.MessageAction>? get_item_actions(Plugins.WidgetType type) { return null; } public override Gee.List<Plugins.MessageAction>? get_item_actions(Plugins.WidgetType type) { return null; }
}
public class DateSeparatorWidget : Box {
private DateTime date;
private Label label;
private uint time_update_timeout = 0;
public DateSeparatorWidget(DateTime date) {
Object(orientation:Orientation.HORIZONTAL, spacing:10);
width_request = 300;
halign = Align.CENTER;
visible = true;
this.date = date;
label = new Label("") { use_markup=true, halign=Align.CENTER, hexpand=false, visible=true };
label.get_style_context().add_class("dim-label");
this.add(new Separator(Orientation.HORIZONTAL) { valign=Align.CENTER, hexpand=true, visible=true });
this.add(label);
this.add(new Separator(Orientation.HORIZONTAL) { valign=Align.CENTER, hexpand=true, visible=true });
update_time();
}
private void update_time() {
label.label = @"<span size='small'>$(get_relative_time(date))</span>";
time_update_timeout = Timeout.add_seconds((int) get_next_time_change(), () => {
if (this.parent == null) return false;
update_time();
return false;
});
}
private static string get_relative_time(DateTime time) { private static string get_relative_time(DateTime time) {
DateTime time_local = time.to_local(); DateTime time_local = time.to_local();
DateTime now_local = new DateTime.now_local(); DateTime now_local = new DateTime.now_local();
if (time_local.get_year() == now_local.get_year() && if (time_local.get_year() == now_local.get_year() &&
time_local.get_month() == now_local.get_month() && time_local.get_month() == now_local.get_month() &&
time_local.get_day_of_month() == now_local.get_day_of_month()) { time_local.get_day_of_month() == now_local.get_day_of_month()) {
return _("Today"); return _("Today");
} }
DateTime now_local_minus = now_local.add_days(-1); DateTime now_local_minus = now_local.add_days(-1);
if (time_local.get_year() == now_local_minus.get_year() && if (time_local.get_year() == now_local_minus.get_year() &&
time_local.get_month() == now_local_minus.get_month() && time_local.get_month() == now_local_minus.get_month() &&
time_local.get_day_of_month() == now_local_minus.get_day_of_month()) { time_local.get_day_of_month() == now_local_minus.get_day_of_month()) {
return _("Yesterday"); return _("Yesterday");
} }
if (time_local.get_year() != now_local.get_year()) { if (time_local.get_year() != now_local.get_year()) {
@ -98,6 +126,20 @@ public class MetaDateItem : Plugins.MetaConversationItem {
return time_local.format(_("%b %d")); return time_local.format(_("%b %d"));
} }
} }
private int get_next_time_change() {
DateTime now = new DateTime.now_local();
return (23 - now.get_hour()) * 3600 + (59 - now.get_minute()) * 60 + (59 - now.get_second()) + 1;
}
public override void dispose() {
base.dispose();
if (time_update_timeout != 0) {
Source.remove(time_update_timeout);
time_update_timeout = 0;
}
}
} }
} }