Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(View): Notifications #286

Merged
merged 2 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 2 additions & 13 deletions src/Services/Accounts/InstanceAccount.vala
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,8 @@ public class Tuba.InstanceAccount : API.Account, Streamable {
// Notifications

public int unread_count { get; set; default = 0; }
public bool has_unread { get; set; default = false; }
public int last_read_id { get; set; default = 0; }
public int last_received_id { get; set; default = 0; }
// public HashMap<int,GLib.Notification> unread_toasts { get; set; default = new HashMap<int,GLib.Notification> (); }
// public ArrayList<string> sent_notification_ids { get; set; default = new ArrayList<string> (); }
public ArrayList<Object> notification_inhibitors { get; set; default = new ArrayList<Object> (); }
private bool passed_init_notifications = false;

public void gather_instance_info () {
Expand Down Expand Up @@ -231,7 +227,6 @@ public class Tuba.InstanceAccount : API.Account, Streamable {
if (unread_count > 0) {
last_received_id = int.parse (array.get_object_element(0).get_string_member_with_default ("id", "-1"));
}
has_unread = unread_count > 0;
}
passed_init_notifications = true;
})
Expand Down Expand Up @@ -278,7 +273,6 @@ public class Tuba.InstanceAccount : API.Account, Streamable {
}

unread_count = 0;
has_unread = false;

// if (sent_notification_ids.size > 0) {
// sent_notification_ids.@foreach (entry => {
Expand Down Expand Up @@ -330,13 +324,8 @@ public class Tuba.InstanceAccount : API.Account, Streamable {
if (id > last_received_id) {
last_received_id = id;

if (notification_inhibitors.is_empty) {
unread_count++;
has_unread = true;
send_toast (entity);
} else {
read_notifications (last_received_id);
}
unread_count++;
send_toast (entity);
}
} catch (Error e) {
warning (@"on_notification_event: $(e.message)");
Expand Down
44 changes: 23 additions & 21 deletions src/Views/Notifications.vala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class Tuba.Views.Notifications : Views.Timeline, AccountHolder, Streamabl

protected InstanceAccount? last_account = null;

private Binding badge_number_binding;
public Notifications () {
Object (
url: "/api/v1/notifications",
Expand All @@ -14,45 +15,46 @@ public class Tuba.Views.Notifications : Views.Timeline, AccountHolder, Streamabl
needs_attention: false
);
accepts = typeof (API.Notification);

stream_event[InstanceAccount.EVENT_NOTIFICATION].connect (on_new_post);
}

~Notifications () {
warning ("Destroying Notifications");
stream_event[InstanceAccount.EVENT_NOTIFICATION].disconnect (on_new_post);
badge_number_binding.unbind ();
}

public override void on_account_changed (InstanceAccount? acc) {
base.on_account_changed (acc);

if (last_account != null) {
last_account.notification_inhibitors.remove (this);
acc.stream_event[InstanceAccount.EVENT_NOTIFICATION].disconnect (on_new_post);
// acc.stream_event[InstanceAccount.EVENT_NOTIFICATION].disconnect (on_new_post_badge);
}
if (badge_number_binding != null)
badge_number_binding.unbind ();

last_account = acc;
acc.stream_event[InstanceAccount.EVENT_NOTIFICATION].connect (on_new_post);
// acc.stream_event[InstanceAccount.EVENT_NOTIFICATION].connect (on_new_post_badge);
acc.bind_property ("unread_count", this, "badge_number", BindingFlags.SYNC_CREATE);
acc.bind_property ("has_unread", this, "needs_attention", BindingFlags.SYNC_CREATE);
// acc.check_notifications ();
// acc.init_notifications();
}
badge_number_binding = accounts.active.bind_property ("unread-count", this, "badge-number", BindingFlags.SYNC_CREATE, (b, src, ref target) => {
var unread_count = src.get_int ();
target.set_int (unread_count);
this.needs_attention = unread_count > 0;

// public virtual void on_new_post_badge (Streamable.Event ev) {
// needs_attention = true;
// }
return true;
});
}

public override void on_shown () {
base.on_shown ();
if (account != null) {
if (!account.notification_inhibitors.contains (this))
account.notification_inhibitors.add (this);

account.read_notifications (account.last_received_id > account.last_read_id ? account.last_received_id : account.last_read_id);
}
}

public override void on_hidden () {
base.on_hidden ();
if (account != null) {
if (account.notification_inhibitors.contains (this))
account.notification_inhibitors.remove (this);
account.unread_count = 0;
}
}

public override string? get_stream_url () {
return account != null ? @"$(account.instance)/api/v1/streaming/?stream=user:notification&access_token=$(account.access_token)" : null;
}
}