Skip to content

Commit

Permalink
feat: refactor question api (#672)
Browse files Browse the repository at this point in the history
feat: use pango markup for the forget dialog
  • Loading branch information
GeopJr authored Nov 30, 2023
1 parent d3adbe9 commit a0870cf
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 213 deletions.
130 changes: 62 additions & 68 deletions src/API/Status/PreviewCard.vala
Original file line number Diff line number Diff line change
Expand Up @@ -157,78 +157,72 @@ public class Tuba.API.PreviewCard : Entity, Widgetizable {
}

public static void open_special_card (CardSpecialType card_special_type, string card_url) {
var privacy_dialog = app.question (
card_special_type.to_dialog_title (),
card_special_type.to_dialog_body (card_url),
app.question.begin (
{card_special_type.to_dialog_title (), false},
{card_special_type.to_dialog_body (card_url), false},
app.main_window,
_("Proceed"),
Adw.ResponseAppearance.DESTRUCTIVE
);

privacy_dialog.response.connect (res => {
if (res == "yes") {
if (card_special_type.open_special_card (card_url)) {
privacy_dialog.destroy ();
return;
};
string special_api_url = "";
string special_host = "";
try {
card_special_type.parse_url (card_url, out special_host, out special_api_url);
} catch {
Host.open_uri (card_url);
privacy_dialog.destroy ();
return;
}

{ { _("Proceed"), Adw.ResponseAppearance.DESTRUCTIVE}, { _("Cancel"), Adw.ResponseAppearance.DEFAULT } },
false,
(obj, res) => {
if (app.question.end (res)) {
if (card_special_type.open_special_card (card_url)) {
return;
};
string special_api_url = "";
string special_host = "";
try {
card_special_type.parse_url (card_url, out special_host, out special_api_url);
} catch {
Host.open_uri (card_url);
return;
}


new Request.GET (special_api_url)
.then ((in_stream) => {
bool failed = true;
var parser = Network.get_parser_from_inputstream (in_stream);
var node = network.parse_node (parser);
string res_url = "";
API.BookWyrm? bookwyrm_obj = null;

switch (card_special_type) {
case API.PreviewCard.CardSpecialType.PEERTUBE:
var peertube_obj = API.PeerTube.from (node);

peertube_obj.get_video (card_url, out res_url, out failed);
break;
case API.PreviewCard.CardSpecialType.FUNKWHALE:
var funkwhale_obj = API.Funkwhale.from (node);

funkwhale_obj.get_track (special_host, out res_url, out failed);
break;
case API.PreviewCard.CardSpecialType.BOOKWYRM:
bookwyrm_obj = API.BookWyrm.from (node);
res_url = bookwyrm_obj.id;

if (bookwyrm_obj.title != null && bookwyrm_obj.title != "") failed = false;
break;
default:
assert_not_reached ();
}

new Request.GET (special_api_url)
.then ((in_stream) => {
bool failed = true;
var parser = Network.get_parser_from_inputstream (in_stream);
var node = network.parse_node (parser);
string res_url = "";
API.BookWyrm? bookwyrm_obj = null;

switch (card_special_type) {
case API.PreviewCard.CardSpecialType.PEERTUBE:
var peertube_obj = API.PeerTube.from (node);

peertube_obj.get_video (card_url, out res_url, out failed);
break;
case API.PreviewCard.CardSpecialType.FUNKWHALE:
var funkwhale_obj = API.Funkwhale.from (node);

funkwhale_obj.get_track (special_host, out res_url, out failed);
break;
case API.PreviewCard.CardSpecialType.BOOKWYRM:
bookwyrm_obj = API.BookWyrm.from (node);
res_url = bookwyrm_obj.id;

if (bookwyrm_obj.title != null && bookwyrm_obj.title != "") failed = false;
break;
default:
assert_not_reached ();
}

if (failed || res_url == "") {
Host.open_uri (card_url);
} else {
if (bookwyrm_obj == null) {
app.main_window.show_media_viewer (res_url, Tuba.Attachment.MediaType.VIDEO, null, 0, null, false, null, card_url, true);
if (failed || res_url == "") {
Host.open_uri (card_url);
} else {
app.main_window.show_book (bookwyrm_obj, card_url);
if (bookwyrm_obj == null) {
app.main_window.show_media_viewer (res_url, Tuba.Attachment.MediaType.VIDEO, null, 0, null, false, null, card_url, true);
} else {
app.main_window.show_book (bookwyrm_obj, card_url);
}
}
}
})
.on_error (() => {
Host.open_uri (card_url);
})
.exec ();
})
.on_error (() => {
Host.open_uri (card_url);
})
.exec ();
}
}
privacy_dialog.destroy ();
});

privacy_dialog.present ();
);
}
}
49 changes: 35 additions & 14 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -479,30 +479,51 @@ namespace Tuba {
return dlg;
}

public Adw.MessageDialog question (
string text,
string? msg = null,
public struct QuestionButton {
public string label;
public Adw.ResponseAppearance appearance;
}

public struct QuestionButtons {
public QuestionButton yes;
public QuestionButton no;
}

public struct QuestionText {
public string text;
public bool use_markup;
}

public async bool question (
QuestionText title,
QuestionText? msg = null,
Gtk.Window? win = app.main_window,
string yes_label = _("Yes"),
Adw.ResponseAppearance yes_appearance = Adw.ResponseAppearance.DEFAULT,
string no_label = _("Cancel"),
Adw.ResponseAppearance no_appearance = Adw.ResponseAppearance.DEFAULT
QuestionButtons buttons = {
{ _("Yes"), Adw.ResponseAppearance.DEFAULT },
{ _("Cancel"), Adw.ResponseAppearance.DEFAULT }
},
bool skip = false // skip the dialog, used for preferences to avoid duplicate code
) {
if (skip) return true;

var dlg = new Adw.MessageDialog (
win,
text,
msg
title.text,
msg == null ? null : msg.text
);

dlg.add_response ("no", no_label);
dlg.set_response_appearance ("no", no_appearance);
dlg.heading_use_markup = title.use_markup;
if (msg != null) dlg.body_use_markup = msg.use_markup;

dlg.add_response ("yes", yes_label);
dlg.set_response_appearance ("yes", yes_appearance);
dlg.add_response ("no", buttons.no.label);
dlg.set_response_appearance ("no", buttons.no.appearance);

dlg.add_response ("yes", buttons.yes.label);
dlg.set_response_appearance ("yes", buttons.yes.appearance);

if (win != null)
dlg.transient_for = win;
return dlg;
return (yield dlg.choose (null)) == "yes";
}

}
Expand Down
25 changes: 8 additions & 17 deletions src/Dialogs/Composer/Dialog.vala
Original file line number Diff line number Diff line change
Expand Up @@ -195,34 +195,25 @@ public class Tuba.Dialogs.Compose : Adw.Window {
on_paste_activated (this.title);
}

Adw.MessageDialog? dlg;
void on_exit () {
push_all ();

if (status.equal (original_status)) {
on_close ();
} else {
dlg = app.question (
_("Are you sure you want to exit?"),
_("Your progress will be lost."),
app.question.begin (
{_("Are you sure you want to exit?"), false},
{_("Your progress will be lost."), false},
this,
_("Discard"),
Adw.ResponseAppearance.DESTRUCTIVE,
_("Cancel")
{ { _("Discard"), Adw.ResponseAppearance.DESTRUCTIVE }, { _("Cancel"), Adw.ResponseAppearance.DEFAULT } },
false,
(obj, res) => {
if (app.question.end (res)) on_close ();
}
);
dlg.response.connect (on_dlg_response);
dlg.present ();
}
}

void on_dlg_response (string res) {
if (dlg == null) return;
dlg.dispose ();
dlg = null;

if (res == "yes") on_close ();
}

private ComposerPage[] t_pages = {};
protected virtual signal void build () {
var p_edit = new EditorPage () {
Expand Down
25 changes: 9 additions & 16 deletions src/Services/Accounts/SecretAccountStore.vala
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,17 @@ public class Tuba.SecretAccountStore : AccountStore {
warning (@"$help_msg\nread more: $wiki_page");

new Dialogs.NewAccount ();
var dlg = app.question (
"Error while searching for user accounts",
@"$help_msg.",
app.question.begin (
{"Error while searching for user accounts", false},
{@"$help_msg.", false},
app.add_account_window,
"Read More",
Adw.ResponseAppearance.SUGGESTED,
"Close"
);

dlg.response.connect (res => {
if (res == "yes") {
Host.open_uri (wiki_page);
{ {"Read More", Adw.ResponseAppearance.SUGGESTED }, { "Close", Adw.ResponseAppearance.DEFAULT } },
false,
(obj, res) => {
if (app.question.end (res)) Host.open_uri (wiki_page);
Process.exit (1);
}
dlg.destroy ();
Process.exit (1);
});

dlg.present ();
);
}

secrets.foreach (item => {
Expand Down
36 changes: 16 additions & 20 deletions src/Views/Lists.vala
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,24 @@ public class Tuba.Views.Lists : Views.Timeline {
public virtual signal void remove_from_model (API.List? t_list);

void on_remove_clicked () {
var remove = app.question (
_("Delete \"%s\"?").printf (this.list.title),
_("This action cannot be reverted."),
app.question.begin (
{_("Delete \"%s\"?").printf (this.list.title), false},
{_("This action cannot be reverted."), false},
app.main_window,
_("Delete"),
Adw.ResponseAppearance.DESTRUCTIVE
);

remove.response.connect (res => {
if (res == "yes") {
new Request.DELETE (@"/api/v1/lists/$(list.id)")
.with_account (accounts.active)
.then (() => {
remove_from_model (this.list);
this.destroy ();
})
.exec ();
{ { _("Delete"), Adw.ResponseAppearance.DESTRUCTIVE }, { _("Cancel"), Adw.ResponseAppearance.DEFAULT } },
false,
(obj, res) => {
if (app.question.end (res)) {
new Request.DELETE (@"/api/v1/lists/$(list.id)")
.with_account (accounts.active)
.then (() => {
remove_from_model (this.list);
this.destroy ();
})
.exec ();
}
}
remove.destroy ();
});

remove.present ();
);
}

public Adw.PreferencesWindow create_edit_preferences_window (API.List t_list) {
Expand Down
Loading

0 comments on commit a0870cf

Please sign in to comment.