Skip to content

Commit

Permalink
feat: advanced boost dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
GeopJr committed Nov 6, 2023
1 parent 4703749 commit f953fc9
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 16 deletions.
3 changes: 3 additions & 0 deletions data/dev.geopjr.Tuba.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
<key name="group-push-notifications" type="b">
<default>false</default>
</key>
<key name="advanced-boost-dialog" type="b">
<default>false</default>
</key>

<key name="window-x" type="i">
<default>-1</default>
Expand Down
6 changes: 6 additions & 0 deletions data/ui/dialogs/preferences.ui
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@
<property name="subtitle" translatable="yes">It can lead to broken links</property>
</object>
</child>
<child>
<object class="AdwSwitchRow" id="advanced_boost_dialog">
<property name="title" translatable="no">Advanced boost dialog</property>
<property name="subtitle" translatable="yes">Change boost visibility, quote and confirm boosting</property>
</object>
</child>
</object>
</child>
<child>
Expand Down
13 changes: 13 additions & 0 deletions src/API/Status.vala
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,19 @@ public class Tuba.API.Status : Entity, Widgetizable {
return "";
}
}

public static ReblogVisibility? from_string (string id) {
switch (id) {
case "public":
return PUBLIC;
case "unlisted":
return UNLISTED;
case "private":
return PRIVATE;
default:
return null;
}
}
}

public Request reblog_req (ReblogVisibility? visibility = null) {
Expand Down
2 changes: 2 additions & 0 deletions src/Dialogs/Preferences.vala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class Tuba.Dialogs.Preferences : Adw.PreferencesWindow {
[GtkChild] unowned Adw.SwitchRow enlarge_custom_emojis;
[GtkChild] unowned Adw.SwitchRow use_blurhash;
[GtkChild] unowned Adw.SwitchRow group_push_notifications;
[GtkChild] unowned Adw.SwitchRow advanced_boost_dialog;

[GtkChild] unowned Adw.SwitchRow new_followers_notifications_switch;
[GtkChild] unowned Adw.SwitchRow new_follower_requests_notifications_switch;
Expand Down Expand Up @@ -125,6 +126,7 @@ public class Tuba.Dialogs.Preferences : Adw.PreferencesWindow {
settings.bind ("enlarge-custom-emojis", enlarge_custom_emojis, "active", SettingsBindFlags.DEFAULT);
settings.bind ("use-blurhash", use_blurhash, "active", SettingsBindFlags.DEFAULT);
settings.bind ("group-push-notifications", group_push_notifications, "active", SettingsBindFlags.DEFAULT);
settings.bind ("advanced-boost-dialog", advanced_boost_dialog, "active", SettingsBindFlags.DEFAULT);

post_visibility_combo_row.notify["selected-item"].connect (on_post_visibility_changed);

Expand Down
4 changes: 3 additions & 1 deletion src/Services/Settings.vala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Tuba.Settings : GLib.Settings {
public string default_content_type { get; set; default = "text/plain"; }
public bool use_blurhash { get; set; }
public bool group_push_notifications { get; set; }
public bool advanced_boost_dialog { get; set; }

public string[] muted_notification_types { get; set; default = {}; }
private static string[] keys_to_init = {
Expand All @@ -43,7 +44,8 @@ public class Tuba.Settings : GLib.Settings {
"muted-notification-types",
"default-content-type",
"use-blurhash",
"group-push-notifications"
"group-push-notifications",
"advanced-boost-dialog"
};

public Settings () {
Expand Down
118 changes: 103 additions & 15 deletions src/Widgets/Status/ActionsRow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public class Tuba.Widgets.ActionsRow : Gtk.Box {
bindings = {};
}

construct {
this.add_css_class ("ttl-post-actions");
this.spacing = 6;
construct {
this.add_css_class ("ttl-post-actions");
this.spacing = 6;

reply_button = new StatusActionButton.with_icon_name ("tuba-reply-sender-symbolic") {
active = false,
Expand Down Expand Up @@ -100,7 +100,7 @@ public class Tuba.Widgets.ActionsRow : Gtk.Box {
};
bookmark_button.clicked.connect (on_bookmark_button_clicked);
this.append (bookmark_button);
}
}

private void on_reply_button_clicked (Gtk.Button btn) {
reply (btn);
Expand Down Expand Up @@ -154,21 +154,109 @@ public class Tuba.Widgets.ActionsRow : Gtk.Box {
if (status_btn.working) return;

status_btn.block_clicked ();
status_btn.active = !status_btn.active;

string action;
Request req;
if (status_btn.active) {
action = "reblog";
req = this.status.reblog_req ();
if (!status_btn.active && settings.advanced_boost_dialog) {
Gtk.ListBox visibility_box = new Gtk.ListBox () {
css_classes = {"content"},
selection_mode = Gtk.SelectionMode.NONE
};

Gtk.CheckButton? group = null; // hashmap is not ordered
Gee.HashMap<API.Status.ReblogVisibility, Gtk.CheckButton> check_buttons = new Gee.HashMap<API.Status.ReblogVisibility, Gtk.CheckButton> ();
for (int i = 0; i < accounts.active.visibility_list.n_items; i++) {
var visibility = (InstanceAccount.Visibility) accounts.active.visibility_list.get_item (i);
var reblog_visibility = API.Status.ReblogVisibility.from_string (visibility.id);
if (reblog_visibility == null) continue;

var checkbutton = new Gtk.CheckButton () {
css_classes = {"selection-mode"},
active = settings.default_post_visibility == visibility.id
};
check_buttons.set (reblog_visibility, checkbutton);

if (group != null) {
checkbutton.group = group;
} else {
group = checkbutton;
}

var visibility_row = new Adw.ActionRow () {
title = visibility.name,
subtitle = visibility.description,
activatable_widget = checkbutton
};
visibility_row.add_prefix (new Gtk.Image.from_icon_name (visibility.icon_name));
visibility_row.add_prefix (checkbutton);

visibility_box.append (visibility_row);
}

var dlg = new Adw.MessageDialog (
app.main_window,
_("Boost with Visibility"),
null
) {
extra_child = visibility_box
};
dlg.add_responses (
"no", _("Cancel"),
// "quote", _("Quote"),
"yes", _("Boost")
);
dlg.set_response_appearance ("yes", Adw.ResponseAppearance.SUGGESTED);
dlg.transient_for = app.main_window;

dlg.response.connect (res => {
dlg.destroy ();

switch (res) {
case "yes":
API.Status.ReblogVisibility? reblog_visibility = null;
check_buttons.foreach (e => {
if (((Gtk.CheckButton) e.value).active) {
reblog_visibility = (API.Status.ReblogVisibility) e.key;
return false;
}

return true;
});

commit_boost (status_btn, reblog_visibility);
break;
// case "quote":
// break;
default:
status_btn.unblock_clicked ();
break;
}

group = null;
check_buttons.clear ();
});

dlg.present ();
} else {
action = "unreblog";
req = this.status.unreblog_req ();
commit_boost (status_btn);
}
status_btn.amount += status_btn.active ? 1 : -1;
}

debug (@"Performing status action '$action'…");
mastodon_action (status_btn, req, action, "reblogs-count");
private void commit_boost (StatusActionButton status_btn, API.Status.ReblogVisibility? visibility = null) {
status_btn.active = !status_btn.active;

string action;
Request req;
if (status_btn.active) {
action = "reblog";
req = this.status.reblog_req (visibility);
} else {
action = "unreblog";
req = this.status.unreblog_req ();
}

status_btn.amount += status_btn.active ? 1 : -1;
debug (@"Performing status action '$action'…");
status_btn.unblock_clicked ();
mastodon_action (status_btn, req, action, "reblogs-count");
}

private void mastodon_action (StatusActionButton status_btn, Request req, string action, string? count_property = null) {
Expand Down

0 comments on commit f953fc9

Please sign in to comment.