From 2442843ce3fc3264574177772d7c5a3c46c8b6cc Mon Sep 17 00:00:00 2001 From: Lorenzo B Gomez Date: Sun, 9 Jul 2023 00:50:29 -0500 Subject: [PATCH] Possibility to search instances in public tab (#16) --- src/api/instances.rs | 5 +++-- src/components/instances_page.rs | 38 ++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/api/instances.rs b/src/api/instances.rs index 5718d81..15f8185 100644 --- a/src/api/instances.rs +++ b/src/api/instances.rs @@ -4,7 +4,7 @@ use lemmy_api_common::{ site::{GetFederatedInstances, GetFederatedInstancesResponse}, }; -pub fn fetch_instances() -> std::result::Result, reqwest::Error> { +pub fn fetch_instances(query_filter: &str) -> std::result::Result, reqwest::Error> { // TODO: Update code to use the Instance views from lemmy 0.18.0 let params = GetFederatedInstances { auth: settings::get_current_account().jwt, @@ -17,11 +17,12 @@ pub fn fetch_instances() -> std::result::Result, reqwest::Error> { .send()? .json::()?; + let lowercase_query_filter = query_filter.to_lowercase(); match instances.federated_instances { Some(instances) => Ok(instances .linked .iter() - .filter(|instance| instance.software == Some("lemmy".to_owned())) + .filter(|instance| instance.software == Some("lemmy".to_owned()) && instance.domain.clone().contains(&lowercase_query_filter)) .map(|instance| instance.clone()) .collect::>()), None => Ok(vec![]), diff --git a/src/components/instances_page.rs b/src/components/instances_page.rs index 6687a42..9f5ee3c 100644 --- a/src/components/instances_page.rs +++ b/src/components/instances_page.rs @@ -8,6 +8,7 @@ use super::instance_row::InstanceRow; pub struct InstancesPage { instances: FactoryVecDeque, + instances_search_buffer: gtk::EntryBuffer } #[derive(Debug)] @@ -40,12 +41,32 @@ impl SimpleComponent for InstancesPage { #[name(stack)] gtk::Stack { add_child = >k::ScrolledWindow { - #[local_ref] - instances -> gtk::Box { + set_vexpand: true, + set_hexpand: true, + gtk::Box { set_orientation: gtk::Orientation::Vertical, - set_spacing: 5, - set_vexpand: true, - }, + set_spacing: 10, + set_margin_all: 10, + + gtk::Box { + set_spacing: 10, + gtk::Entry { + set_hexpand: true, + set_tooltip_text: Some("Search"), + set_buffer: &model.instances_search_buffer, + }, + gtk::Button { + set_label: "Filter", + connect_clicked => InstancesPageInput::FetchInstances, + } + }, + #[local_ref] + instances -> gtk::Box { + set_orientation: gtk::Orientation::Vertical, + set_spacing: 5, + set_vexpand: true, + }, + } } -> { set_title: "Public", }, @@ -71,6 +92,7 @@ impl SimpleComponent for InstancesPage { } -> { set_title: "Custom", }, + } } } @@ -81,7 +103,8 @@ impl SimpleComponent for InstancesPage { sender: ComponentSender, ) -> ComponentParts { let instances = FactoryVecDeque::new(gtk::Box::default(), sender.input_sender()); - let model = Self { instances }; + let instances_search_buffer = gtk::EntryBuffer::builder().build(); + let model = Self { instances, instances_search_buffer }; let instances = model.instances.widget(); let widgets = view_output!(); ComponentParts { model, widgets } @@ -90,8 +113,9 @@ impl SimpleComponent for InstancesPage { fn update(&mut self, msg: Self::Input, sender: ComponentSender) { match msg { InstancesPageInput::FetchInstances => { + let filter = self.instances_search_buffer.text().as_str().to_owned(); std::thread::spawn(move || { - let message = match api::instances::fetch_instances() { + let message = match api::instances::fetch_instances(&filter) { Ok(instances) => Some(InstancesPageInput::DoneFetchInstances(instances)), Err(_err) => None, };