tootle-linux-client/src/Views/Notifications.vala

78 lines
2.2 KiB
Vala
Raw Normal View History

2018-04-17 14:01:55 +02:00
using Gtk;
using Gdk;
2021-02-20 13:46:57 +01:00
public class Tootle.Views.Notifications : Views.Timeline, IAccountHolder, IStreamListener {
2018-04-17 14:01:55 +02:00
2020-05-29 14:19:35 +02:00
protected int64 last_id = 0;
2018-10-29 16:43:34 +01:00
2019-03-11 15:14:37 +01:00
public Notifications () {
2020-05-31 12:28:35 +02:00
Object (
2020-06-03 14:41:21 +02:00
url: "/api/v1/notifications",
2020-05-31 12:28:35 +02:00
label: _("Notifications"),
icon: Desktop.fallback_icon ("notification-symbolic", "preferences-system-notifications-symbolic", "user-invisible-symbolic")
);
2020-06-03 17:06:11 +02:00
accepts = typeof (API.Notification);
on_notification.connect (add_notification);
2020-09-05 10:45:35 +02:00
disconnect (on_status_added_sigig);
2020-05-30 12:31:02 +02:00
}
2019-03-11 15:14:37 +01:00
public override string? get_stream_url () {
2020-06-20 12:04:58 +02:00
return account != null ? @"$(account.instance)/api/v1/streaming/?stream=user&access_token=$(account.access_token)" : null;
2018-05-24 21:08:28 +02:00
}
2019-03-11 15:14:37 +01:00
2020-05-31 12:28:35 +02:00
public override void on_shown () {
2018-10-29 16:43:34 +01:00
if (has_unread ()) {
2020-05-31 12:28:35 +02:00
needs_attention = false;
account.has_unread_notifications = false;
2018-10-29 16:43:34 +01:00
account.last_seen_notification = last_id;
2021-02-12 16:26:37 +01:00
accounts.safe_save ();
2018-10-28 13:54:09 +01:00
}
2018-04-17 14:01:55 +02:00
}
2019-03-11 15:14:37 +01:00
public override void append (Widget? w, bool reverse = false) {
base.append (w, reverse);
var nw = w as Widgets.Notification;
var notification = nw.notification;
2020-06-20 12:04:58 +02:00
if (int64.parse (notification.id) > last_id)
last_id = int64.parse (notification.id);
needs_attention = has_unread () && !current;
2020-05-31 12:28:35 +02:00
if (needs_attention)
2021-02-12 16:26:37 +01:00
accounts.safe_save ();
}
public override void on_account_changed (InstanceAccount? acc) {
base.on_account_changed (acc);
2020-05-29 14:19:35 +02:00
if (account == null) {
last_id = 0;
2020-05-31 12:28:35 +02:00
needs_attention = false;
2018-05-31 14:13:21 +02:00
}
2020-05-29 14:19:35 +02:00
else {
last_id = account.last_seen_notification;
2020-05-31 12:28:35 +02:00
needs_attention = account.has_unread_notifications;
2020-05-29 14:19:35 +02:00
}
2020-05-30 12:31:02 +02:00
}
public override bool request () {
2020-05-29 14:19:35 +02:00
if (account != null) {
account.cached_notifications.@foreach (n => {
2020-06-03 17:06:11 +02:00
append (n.to_widget ());
2020-05-29 14:19:35 +02:00
return true;
2019-03-14 12:55:27 +01:00
});
2020-05-29 14:19:35 +02:00
}
return base.request ();
}
2019-03-11 15:14:37 +01:00
2020-05-31 12:28:35 +02:00
bool has_unread () {
if (account == null)
return false;
return last_id > account.last_seen_notification || needs_attention;
}
void add_notification (API.Notification n) {
2020-06-03 17:06:11 +02:00
prepend (n.to_widget ());
2018-04-17 14:01:55 +02:00
}
}