Tooth/src/Views/Search.vala

87 lines
1.9 KiB
Vala
Raw Permalink Normal View History

2018-05-14 14:43:10 +00:00
using Gtk;
2022-11-13 20:57:43 +00:00
public class Tooth.Views.Search : Views.TabbedBase {
2018-05-14 14:43:10 +00:00
public string query { get; set; default = ""; }
2021-07-25 18:15:59 +00:00
protected SearchBar bar;
protected Adw.Clamp bar_clamp;
protected SearchEntry entry;
2020-08-01 15:40:56 +00:00
2021-07-25 18:15:59 +00:00
Views.ContentBase accounts_tab;
Views.ContentBase statuses_tab;
Views.ContentBase hashtags_tab;
2020-08-01 15:40:56 +00:00
public Search () {
Object (label: _("Search"));
2021-07-25 18:15:59 +00:00
bar = new SearchBar () {
search_mode_enabled = true
};
prepend (bar);
reorder_child_after (bar, header);
2020-08-01 15:40:56 +00:00
2021-07-25 18:15:59 +00:00
entry = new SearchEntry () {
width_chars = 25,
text = query
};
2021-07-25 18:15:59 +00:00
bar_clamp = new Adw.Clamp () {
child = entry
};
2021-07-25 18:15:59 +00:00
bar.child = bar_clamp;
bar.connect_entry (entry);
2020-08-01 15:40:56 +00:00
entry.activate.connect (() => request ());
status_button.clicked.connect (request);
accounts_tab = add_list_tab (_("Accounts"), "tooth-people-symbolic");
statuses_tab = add_list_tab (_("Statuses"), "tooth-chat-symbolic");
hashtags_tab = add_list_tab (_("Hashtags"), "tooth-hashtag-symbolic");
2020-08-01 15:40:56 +00:00
request ();
}
2021-07-25 18:15:59 +00:00
bool append_entity (Views.ContentBase tab, owned Entity entity) {
tab.model.append (entity);
return true;
}
2020-08-01 15:40:56 +00:00
void request () {
query = entry.text.chug ().chomp ();
if (query == "") {
clear ();
state = "status";
status_title = _("Enter Query");
2020-08-01 15:40:56 +00:00
return;
}
clear ();
state = "status";
status_loading = true;
2020-08-01 15:40:56 +00:00
API.SearchResults.request.begin (query, accounts.active, (obj, res) => {
try {
var results = API.SearchResults.request.end (res);
2021-07-25 18:15:59 +00:00
if (!results.accounts.is_empty) {
results.accounts.@foreach (e => append_entity (accounts_tab, e));
}
if (!results.statuses.is_empty) {
results.statuses.@foreach (e => append_entity (statuses_tab, e));
}
if (!results.hashtags.is_empty) {
results.hashtags.@foreach (e => append_entity (hashtags_tab, e));
}
2020-08-01 15:40:56 +00:00
status_title = STATUS_EMPTY;
2020-08-01 15:40:56 +00:00
on_content_changed ();
}
catch (Error e) {
on_error (-1, e.message);
}
});
}
2018-05-14 14:43:10 +00:00
}