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

148 lines
4.1 KiB
Vala
Raw Normal View History

2018-04-17 14:01:55 +02:00
using Gtk;
using Gdk;
2020-05-30 12:31:02 +02:00
public class Tootle.Views.Notifications : Views.Base, IAccountListener, IStreamListener { //TODO: make this a timeline
2018-04-17 14:01:55 +02:00
2020-05-29 14:19:35 +02:00
protected InstanceAccount? account = null;
protected int64 last_id = 0;
protected bool force_dot = false;
2018-10-29 16:43:34 +01:00
2020-05-30 12:31:02 +02:00
protected string? stream;
2019-03-11 15:14:37 +01:00
public Notifications () {
2018-10-23 12:05:24 +02:00
app.refresh.connect (on_refresh);
2020-05-29 14:19:35 +02:00
status_button.clicked.connect (on_refresh);
connect_account ();
2018-04-17 14:01:55 +02:00
}
2020-05-30 12:31:02 +02:00
~Notifications () {
streams.unsubscribe (stream, this);
}
2019-03-11 15:14:37 +01:00
2018-10-29 16:43:34 +01:00
private bool has_unread () {
2019-03-11 15:14:37 +01:00
if (account == null)
2018-10-29 16:43:34 +01:00
return false;
2019-03-11 15:14:37 +01:00
return last_id > account.last_seen_notification || force_dot;
2018-10-29 16:43:34 +01:00
}
2019-03-11 15:14:37 +01:00
2018-10-29 16:43:34 +01:00
public override string get_icon () {
if (has_unread ())
2018-10-28 13:54:09 +01:00
return Desktop.fallback_icon ("notification-new-symbolic", "user-available-symbolic");
2018-10-29 16:43:34 +01:00
else
return Desktop.fallback_icon ("notification-symbolic", "user-invisible-symbolic");
2018-04-17 14:01:55 +02:00
}
2019-03-11 15:14:37 +01:00
2018-04-17 14:01:55 +02:00
public override string get_name () {
2018-05-09 23:46:24 +02:00
return _("Notifications");
2018-04-17 14:01:55 +02:00
}
2019-03-11 15:14:37 +01:00
public void prepend (API.Notification notification) {
2018-10-23 12:05:24 +02:00
append (notification, true);
2018-05-24 21:08:28 +02:00
}
2019-03-11 15:14:37 +01:00
public void append (API.Notification notification, bool reverse = false) {
2020-05-29 14:19:35 +02:00
GLib.Idle.add (() => {
var widget = new Widgets.Notification (notification);
content.pack_start (widget, false, false, 0);
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
if (reverse) {
content.reorder_child (widget, 0);
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
if (!current) {
force_dot = true;
accounts.active.has_unread_notifications = force_dot;
}
2018-10-30 16:57:37 +01:00
}
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
on_content_changed ();
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
if (notification.id > last_id)
last_id = notification.id;
if (has_unread ()) {
accounts.save ();
image.icon_name = get_icon ();
}
return GLib.Source.REMOVE;
});
2018-10-28 13:54:09 +01:00
}
2019-03-11 15:14:37 +01:00
2018-10-28 13:54:09 +01:00
public override void on_set_current () {
2018-10-29 16:43:34 +01:00
if (has_unread ()) {
force_dot = false;
account.has_unread_notifications = force_dot;
account.last_seen_notification = last_id;
2018-10-28 13:54:09 +01:00
accounts.save ();
2018-10-29 16:43:34 +01:00
image.icon_name = get_icon ();
2018-10-28 13:54:09 +01:00
}
2018-04-17 14:01:55 +02:00
}
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
public override void on_content_changed () {
base.on_content_changed ();
if (image != null && empty)
image.icon_name = get_icon ();
2018-04-18 11:13:22 +02:00
}
2018-05-03 10:11:31 +02:00
2018-05-16 13:23:48 +02:00
public virtual void on_refresh () {
2018-05-03 10:11:31 +02:00
clear ();
2020-05-29 14:19:35 +02:00
GLib.Idle.add (request);
2018-05-03 10:11:31 +02:00
}
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
public virtual void on_account_changed (InstanceAccount? acc) {
account = acc;
2020-05-30 12:31:02 +02:00
streams.unsubscribe (stream, this);
2020-05-29 14:19:35 +02:00
if (account == null) {
last_id = 0;
force_dot = false;
2018-05-31 14:13:21 +02:00
}
2020-05-29 14:19:35 +02:00
else {
last_id = account.last_seen_notification;
force_dot = account.has_unread_notifications;
2020-05-30 12:31:02 +02:00
streams.subscribe (get_stream_url (), this, out stream);
2020-05-29 14:19:35 +02:00
}
on_refresh ();
}
2019-03-11 15:14:37 +01:00
2020-05-30 12:31:02 +02:00
public virtual string? get_stream_url () {
return account != null ? @"$(account.instance)/api/v1/streaming/?stream=user&access_token=$(account.token)" : null;
}
public override bool accepts (ref string event) {
return true;
}
public override void on_notification (API.Notification n) {
prepend (n);
}
2020-05-29 14:19:35 +02:00
public bool request () {
if (account != null) {
account.cached_notifications.@foreach (notification => {
append (notification);
return true;
2019-03-14 12:55:27 +01:00
});
2020-05-29 14:19:35 +02:00
}
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
// new Request.GET ("/api/v1/follow_requests") //TODO: this
// .with_account ()
// .then_parse_array (node => {
// var notification = API.Notification.parse_follow_request (node.get_object ());
// append (notification);
// })
// .on_error (on_error)
// .exec ();
new Request.GET ("/api/v1/notifications")
.with_account (account)
.with_param ("limit", "30")
.then_parse_array (node => {
var notification = new API.Notification (node.get_object ());
append (notification);
})
.on_error (on_error)
.exec ();
return GLib.Source.REMOVE;
2018-04-17 14:01:55 +02:00
}
}