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

102 lines
2.7 KiB
Vala
Raw Normal View History

2018-04-17 14:01:55 +02:00
using Gtk;
using Gdk;
public class Tootle.Views.Notifications : Views.Timeline, IAccountListener, 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 (
label: _("Notifications"),
icon: Desktop.fallback_icon ("notification-symbolic", "preferences-system-notifications-symbolic", "user-invisible-symbolic")
);
on_notification.connect (add_notification);
2020-05-30 12:31:02 +02:00
}
2019-03-11 15:14:37 +01:00
public override string? get_stream_url () {
return account != null ? @"$(account.instance)/api/v1/streaming/?stream=user&access_token=$(account.token)" : null;
2018-05-24 21:08:28 +02:00
}
2019-03-11 15:14:37 +01:00
public override string get_url () {
if (page_next != null)
return page_next;
2019-03-11 15:14:37 +01:00
return "/api/v1/notifications";
}
2020-05-29 14:19:35 +02: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;
2018-10-28 13:54:09 +01:00
accounts.save ();
}
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;
if (reverse && !current) {
2020-05-31 12:28:35 +02:00
needs_attention = accounts.active.has_unread_notifications = true;
}
if (notification.id > last_id)
last_id = notification.id;
2020-05-31 12:28:35 +02:00
needs_attention = has_unread ();
if (needs_attention)
accounts.save ();
}
public override GLib.Object? to_entity (Json.Object? json) {
if (json != null)
return new API.Notification (json);
else
return null;
2018-04-18 11:13:22 +02:00
}
2018-05-03 10:11:31 +02:00
public override Widget? widgetize (GLib.Object? entity) {
var n = entity as API.Notification;
if (n == null)
return null;
var w = new Widgets.Notification (n);
return w;
2018-05-03 10:11:31 +02:00
}
2019-03-11 15:14:37 +01:00
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 => {
append (widgetize (n));
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) {
prepend (widgetize (n));
2018-04-17 14:01:55 +02:00
}
}