From c0be0f5f85e9976400be5a6f7f3898366ecbbad4 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Sat, 21 Nov 2020 12:43:44 +0100 Subject: Update date separator at midnight fixes #868 --- .../date_separator_populator.vala | 66 ++++++++++++++++++---- 1 file changed, 54 insertions(+), 12 deletions(-) (limited to 'main/src/ui/conversation_content_view') diff --git a/main/src/ui/conversation_content_view/date_separator_populator.vala b/main/src/ui/conversation_content_view/date_separator_populator.vala index 91485f25..ff3b962d 100644 --- a/main/src/ui/conversation_content_view/date_separator_populator.vala +++ b/main/src/ui/conversation_content_view/date_separator_populator.vala @@ -62,30 +62,58 @@ public class MetaDateItem : Plugins.MetaConversationItem { } public override Object? get_widget(Plugins.WidgetType widget_type) { - Box box = new Box(Orientation.HORIZONTAL, 10) { width_request=300, halign=Align.CENTER, visible=true }; - box.add(new Separator(Orientation.HORIZONTAL) { valign=Align.CENTER, hexpand=true, visible=true }); - string date_str = get_relative_time(date); - Label label = new Label(@"$date_str") { 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; + return new DateSeparatorWidget(date); } public override Gee.List? 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 = @"$(get_relative_time(date))"; + 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) { DateTime time_local = time.to_local(); DateTime now_local = new DateTime.now_local(); if (time_local.get_year() == now_local.get_year() && - time_local.get_month() == now_local.get_month() && - time_local.get_day_of_month() == now_local.get_day_of_month()) { + time_local.get_month() == now_local.get_month() && + time_local.get_day_of_month() == now_local.get_day_of_month()) { return _("Today"); } DateTime now_local_minus = now_local.add_days(-1); if (time_local.get_year() == now_local_minus.get_year() && - 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_month() == now_local_minus.get_month() && + time_local.get_day_of_month() == now_local_minus.get_day_of_month()) { return _("Yesterday"); } if (time_local.get_year() != now_local.get_year()) { @@ -98,6 +126,20 @@ public class MetaDateItem : Plugins.MetaConversationItem { 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; + } + } } } -- cgit v1.2.3-54-g00ecf