Skip to content

Commit

Permalink
Merge pull request #7 from manexim/open-corresponding-tab-when-clicki…
Browse files Browse the repository at this point in the history
…ng-a-notification

Open corresponding tab when clicking a notification
  • Loading branch information
meisenzahl authored Feb 14, 2020
2 parents 7703880 + b0e7f70 commit 47dfbc9
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 27 deletions.
12 changes: 12 additions & 0 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ public class Application : Granite.Application {
}
}

construct {
var action = new SimpleAction ("show-messenger", VariantType.STRING);
action.activate.connect ((parameter) => {
this.hold ();
string id = parameter.get_string ();
Services.Messengers.get_default ().visible = id;
this.release ();
});

add_action (action);
}

private Application () {
Object (
application_id: Config.APP_ID,
Expand Down
74 changes: 51 additions & 23 deletions src/Services/Messengers.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
* Authored by: Marius Meisenzahl <[email protected]>
*/

public class Services.Messengers {
public class Services.Messengers : Object {
private static Messengers instance;
private Models.Messenger[] _data;
private Array<Models.Messenger> _data;
private string _visible;

public static Messengers get_default () {
if (instance == null) {
Expand All @@ -31,32 +32,59 @@ public class Services.Messengers {
return instance;
}

public Models.Messenger[] data {
public Array<Models.Messenger> data {
get {
return _data;
}
}

public string visible {
get {
return _visible;
}
set {
foreach (var m in _data.data) {
if (m.id == value) {
_visible = value;
break;
}
}
}
}

private Messengers () {
_data = new Models.Messenger[4];
_data[0] = new Models.Messenger ();
_data[0].id = "com.messenger";
_data[0].name = "Messenger";
_data[0].url = "https://www.messenger.com/";

_data[1] = new Models.Messenger ();
_data[1].id = "com.slack";
_data[1].name = "Slack";
_data[1].url = "https://slack.com/signin/";

_data[2] = new Models.Messenger ();
_data[2].id = "org.telegram.web";
_data[2].name = "Telegram";
_data[2].url = "https://web.telegram.org/";

_data[3] = new Models.Messenger ();
_data[3].id = "com.whatsapp.web";
_data[3].name = "WhatsApp";
_data[3].url = "https://web.whatsapp.com/";
_data = new Array<Models.Messenger> ();

{
var messenger = new Models.Messenger ();
messenger.id = "com.messenger";
messenger.name = "Messenger";
messenger.url = "https://www.messenger.com/";
_data.append_val (messenger);
}

{
var messenger = new Models.Messenger ();
messenger.id = "com.slack";
messenger.name = "Slack";
messenger.url = "https://slack.com/signin/";
_data.append_val (messenger);
}

{
var messenger = new Models.Messenger ();
messenger.id = "org.telegram.web";
messenger.name = "Telegram";
messenger.url = "https://web.telegram.org/";
_data.append_val (messenger);
}

{
var messenger = new Models.Messenger ();
messenger.id = "com.whatsapp.web";
messenger.name = "WhatsApp";
messenger.url = "https://web.whatsapp.com/";
_data.append_val (messenger);
}
}
}
28 changes: 24 additions & 4 deletions src/Views/MainView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public class Views.MainView : Gtk.Paned {

var messengers = Services.Messengers.get_default ().data;
for (var i = 0; i < messengers.length; i++) {
var messenger = messengers[i];
var messenger = messengers.index (i);

var view = new Widgets.MessengerView (messenger);
stack.add_named (view, "%d".printf (i));
stack.add_named (view, messenger.id);

var menu_item = new Gtk.MenuItem ();

Expand All @@ -57,7 +57,7 @@ public class Views.MainView : Gtk.Paned {
}

uint unread_notifications_sum = 0;
foreach (var m in messengers) {
foreach (var m in messengers.data) {
unread_notifications_sum += m.unread_notifications;
}

Expand Down Expand Up @@ -89,7 +89,27 @@ public class Views.MainView : Gtk.Paned {
}

list_box.row_activated.connect ((row) => {
stack.set_visible_child_name ("%d".printf (row.get_index ()));
for (var i = 0; i < messengers.length; i++) {
if (row.get_index () == i) {
Services.Messengers.get_default ().visible = messengers.index (i).id;
break;
}
}
});

Services.Messengers.get_default ().notify["visible"].connect (() => {
var visible = Services.Messengers.get_default ().visible;
int index = 0;
for (var i = 0; i < messengers.length; i++) {
if (visible == messengers.index (i).id) {
index = i;
break;
}
}

list_box.select_row (list_box.get_row_at_index (index));

stack.set_visible_child_name (visible);
var view = stack.get_visible_child ();
(view as Widgets.MessengerView).model.unread_notifications = 0;
});
Expand Down
2 changes: 2 additions & 0 deletions src/Widgets/MessengerView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class Widgets.MessengerView : WebKit.WebView {
var native_notification = new GLib.Notification (notification.title);
native_notification.set_body (notification.body);
native_notification.set_icon (this.icon);
Variant target = new Variant.string (this.messenger.id);
native_notification.set_default_action_and_target_value ("app.show-messenger", target);
Application.instance.send_notification (this.messenger.id, native_notification);

this.messenger.unread_notifications += 1;
Expand Down

0 comments on commit 47dfbc9

Please sign in to comment.