Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

Add an undo #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/MainView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

public class Wallet.MainView : Granite.SimpleSettingsPage {
private Gtk.Overlay overlay;
private Gtk.ListBox listbox;
private Secret.Collection collection;

Expand Down Expand Up @@ -64,7 +65,10 @@ public class Wallet.MainView : Granite.SimpleSettingsPage {
var frame = new Gtk.Frame (null);
frame.add (grid);

content_area.add (frame);
overlay = new Gtk.Overlay ();
overlay.add (frame);

content_area.add (overlay);
show_all ();

init_collection.begin ();
Expand Down Expand Up @@ -107,13 +111,46 @@ public class Wallet.MainView : Granite.SimpleSettingsPage {
}

var secret_item_row = new SecretItemRow (secret_item);
secret_item_row.delete_request.connect ((secret_item) => {
var toast = new Granite.Widgets.Toast (_("'%s' Removed").printf (secret_item.get_label ()));
toast.set_default_action (_("Undo"));
toast.show_all ();

overlay.add_overlay (toast);
toast.send_notification ();

var timeout = GLib.Timeout.add (4000, () => {
delete_secret (secret_item_row, secret_item);
toast.destroy ();
return GLib.Source.REMOVE;
});

toast.closed.connect (() => {
delete_secret (secret_item_row, secret_item);
toast.destroy ();
});

toast.default_action.connect (() => {
Source.remove (timeout);
secret_item_row.revealer.reveal_child = true;
});
});

listbox.add (secret_item_row);
}

listbox.show_all ();
}

private async void delete_secret (SecretItemRow row, Secret.Item secret_item) {
try {
yield secret_item.delete (null);
row.destroy ();
} catch (Error error) {
critical (error.message);
}
}

[CCode (instance_pos = -1)]
private int sort_func (SecretItemRow row1, SecretItemRow row2) {
return row1.secret_item.get_label ().collate (row2.secret_item.get_label ());
Expand Down
20 changes: 5 additions & 15 deletions src/Widgets/SecretItemRow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/

public class Wallet.SecretItemRow : Gtk.ListBoxRow {
public signal void delete_request (Secret.Item secret_item);

public Gtk.Revealer revealer { get; private set; }
public Gtk.Revealer close_revealer { get; private set; }
public Secret.Item secret_item { get; construct; }

Expand Down Expand Up @@ -68,7 +71,7 @@ public class Wallet.SecretItemRow : Gtk.ListBoxRow {
grid.attach (title_label, 2, 0);
grid.attach (description, 2, 1);

var revealer = new Gtk.Revealer ();
revealer = new Gtk.Revealer ();
revealer.reveal_child = true;
revealer.transition_type = Gtk.RevealerTransitionType.SLIDE_UP;
revealer.add (grid);
Expand All @@ -94,22 +97,9 @@ public class Wallet.SecretItemRow : Gtk.ListBoxRow {
revealer.reveal_child = false;

GLib.Timeout.add (revealer.transition_duration, () => {
delete_secret ();
delete_request (secret_item);
return false;
});
});

focus_out_event.connect (() => {
close_revealer.reveal_child = false;
});
}

private async void delete_secret () {
try {
yield secret_item.delete (null);
destroy ();
} catch (Error error) {
critical (error.message);
}
}
}