Update date separator at midnight

fixes #868
This commit is contained in:
fiaxh 2020-11-21 12:43:44 +01:00
parent 2a775bcfb9
commit 9a3b13a95a

View file

@ -62,18 +62,46 @@ 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();
@ -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;
}
}
} }
} }