Possibility to search instances in public tab (#16)
This commit is contained in:
parent
5e0c84aec7
commit
2442843ce3
|
@ -4,7 +4,7 @@ use lemmy_api_common::{
|
||||||
site::{GetFederatedInstances, GetFederatedInstancesResponse},
|
site::{GetFederatedInstances, GetFederatedInstancesResponse},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn fetch_instances() -> std::result::Result<Vec<Instance>, reqwest::Error> {
|
pub fn fetch_instances(query_filter: &str) -> std::result::Result<Vec<Instance>, reqwest::Error> {
|
||||||
// TODO: Update code to use the Instance views from lemmy 0.18.0
|
// TODO: Update code to use the Instance views from lemmy 0.18.0
|
||||||
let params = GetFederatedInstances {
|
let params = GetFederatedInstances {
|
||||||
auth: settings::get_current_account().jwt,
|
auth: settings::get_current_account().jwt,
|
||||||
|
@ -17,11 +17,12 @@ pub fn fetch_instances() -> std::result::Result<Vec<Instance>, reqwest::Error> {
|
||||||
.send()?
|
.send()?
|
||||||
.json::<GetFederatedInstancesResponse>()?;
|
.json::<GetFederatedInstancesResponse>()?;
|
||||||
|
|
||||||
|
let lowercase_query_filter = query_filter.to_lowercase();
|
||||||
match instances.federated_instances {
|
match instances.federated_instances {
|
||||||
Some(instances) => Ok(instances
|
Some(instances) => Ok(instances
|
||||||
.linked
|
.linked
|
||||||
.iter()
|
.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())
|
.map(|instance| instance.clone())
|
||||||
.collect::<Vec<Instance>>()),
|
.collect::<Vec<Instance>>()),
|
||||||
None => Ok(vec![]),
|
None => Ok(vec![]),
|
||||||
|
|
|
@ -8,6 +8,7 @@ use super::instance_row::InstanceRow;
|
||||||
|
|
||||||
pub struct InstancesPage {
|
pub struct InstancesPage {
|
||||||
instances: FactoryVecDeque<InstanceRow>,
|
instances: FactoryVecDeque<InstanceRow>,
|
||||||
|
instances_search_buffer: gtk::EntryBuffer
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -40,12 +41,32 @@ impl SimpleComponent for InstancesPage {
|
||||||
#[name(stack)]
|
#[name(stack)]
|
||||||
gtk::Stack {
|
gtk::Stack {
|
||||||
add_child = >k::ScrolledWindow {
|
add_child = >k::ScrolledWindow {
|
||||||
#[local_ref]
|
set_vexpand: true,
|
||||||
instances -> gtk::Box {
|
set_hexpand: true,
|
||||||
|
gtk::Box {
|
||||||
set_orientation: gtk::Orientation::Vertical,
|
set_orientation: gtk::Orientation::Vertical,
|
||||||
set_spacing: 5,
|
set_spacing: 10,
|
||||||
set_vexpand: true,
|
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",
|
set_title: "Public",
|
||||||
},
|
},
|
||||||
|
@ -71,6 +92,7 @@ impl SimpleComponent for InstancesPage {
|
||||||
} -> {
|
} -> {
|
||||||
set_title: "Custom",
|
set_title: "Custom",
|
||||||
},
|
},
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,7 +103,8 @@ impl SimpleComponent for InstancesPage {
|
||||||
sender: ComponentSender<Self>,
|
sender: ComponentSender<Self>,
|
||||||
) -> ComponentParts<Self> {
|
) -> ComponentParts<Self> {
|
||||||
let instances = FactoryVecDeque::new(gtk::Box::default(), sender.input_sender());
|
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 instances = model.instances.widget();
|
||||||
let widgets = view_output!();
|
let widgets = view_output!();
|
||||||
ComponentParts { model, widgets }
|
ComponentParts { model, widgets }
|
||||||
|
@ -90,8 +113,9 @@ impl SimpleComponent for InstancesPage {
|
||||||
fn update(&mut self, msg: Self::Input, sender: ComponentSender<Self>) {
|
fn update(&mut self, msg: Self::Input, sender: ComponentSender<Self>) {
|
||||||
match msg {
|
match msg {
|
||||||
InstancesPageInput::FetchInstances => {
|
InstancesPageInput::FetchInstances => {
|
||||||
|
let filter = self.instances_search_buffer.text().as_str().to_owned();
|
||||||
std::thread::spawn(move || {
|
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)),
|
Ok(instances) => Some(InstancesPageInput::DoneFetchInstances(instances)),
|
||||||
Err(_err) => None,
|
Err(_err) => None,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue