Simplify the logic for changing the instance
This commit is contained in:
parent
206bb2ab83
commit
7aa4936b0a
|
@ -2,6 +2,8 @@ use gtk::prelude::*;
|
||||||
use lemmy_api_common::lemmy_db_schema::source::instance::Instance;
|
use lemmy_api_common::lemmy_db_schema::source::instance::Instance;
|
||||||
use relm4::prelude::*;
|
use relm4::prelude::*;
|
||||||
|
|
||||||
|
use super::instances_page::InstancesPageInput;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct InstanceRow {
|
pub struct InstanceRow {
|
||||||
instance: Instance,
|
instance: Instance,
|
||||||
|
@ -16,10 +18,10 @@ pub enum InstanceRowMsg {
|
||||||
impl FactoryComponent for InstanceRow {
|
impl FactoryComponent for InstanceRow {
|
||||||
type Init = Instance;
|
type Init = Instance;
|
||||||
type Input = InstanceRowMsg;
|
type Input = InstanceRowMsg;
|
||||||
type Output = crate::AppMsg;
|
type Output = InstancesPageInput;
|
||||||
type CommandOutput = ();
|
type CommandOutput = ();
|
||||||
type Widgets = PostViewWidgets;
|
type Widgets = PostViewWidgets;
|
||||||
type ParentInput = crate::AppMsg;
|
type ParentInput = InstancesPageInput;
|
||||||
type ParentWidget = gtk::Box;
|
type ParentWidget = gtk::Box;
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
|
@ -78,9 +80,7 @@ impl FactoryComponent for InstanceRow {
|
||||||
match message {
|
match message {
|
||||||
InstanceRowMsg::OpenInstance => {
|
InstanceRowMsg::OpenInstance => {
|
||||||
let instance_address = format!("https://{}", self.instance.domain);
|
let instance_address = format!("https://{}", self.instance.domain);
|
||||||
sender.output(crate::AppMsg::DoneChoosingInstance(
|
sender.output(InstancesPageInput::SetInstance(instance_address.clone()))
|
||||||
instance_address.clone(),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use gtk::prelude::*;
|
||||||
use lemmy_api_common::lemmy_db_schema::source::instance::Instance;
|
use lemmy_api_common::lemmy_db_schema::source::instance::Instance;
|
||||||
use relm4::{factory::FactoryVecDeque, prelude::*};
|
use relm4::{factory::FactoryVecDeque, prelude::*};
|
||||||
|
|
||||||
use crate::api;
|
use crate::{api, settings};
|
||||||
|
|
||||||
use super::instance_row::InstanceRow;
|
use super::instance_row::InstanceRow;
|
||||||
|
|
||||||
|
@ -11,15 +11,16 @@ pub struct InstancesPage {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum InstancePageInput {
|
pub enum InstancesPageInput {
|
||||||
FetchInstances,
|
FetchInstances,
|
||||||
DoneFetchInstances(Vec<Instance>),
|
DoneFetchInstances(Vec<Instance>),
|
||||||
|
SetInstance(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[relm4::component(pub)]
|
#[relm4::component(pub)]
|
||||||
impl SimpleComponent for InstancesPage {
|
impl SimpleComponent for InstancesPage {
|
||||||
type Init = ();
|
type Init = ();
|
||||||
type Input = InstancePageInput;
|
type Input = InstancesPageInput;
|
||||||
type Output = crate::AppMsg;
|
type Output = crate::AppMsg;
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
|
@ -64,7 +65,7 @@ impl SimpleComponent for InstancesPage {
|
||||||
connect_clicked[sender, instance_url] => move |_| {
|
connect_clicked[sender, instance_url] => move |_| {
|
||||||
let text = instance_url.text().as_str().to_string();
|
let text = instance_url.text().as_str().to_string();
|
||||||
instance_url.set_text("");
|
instance_url.set_text("");
|
||||||
let _ = sender.output(crate::AppMsg::DoneChoosingInstance(text));
|
sender.input(InstancesPageInput::SetInstance(text));
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
} -> {
|
} -> {
|
||||||
|
@ -79,7 +80,7 @@ impl SimpleComponent for InstancesPage {
|
||||||
root: &Self::Root,
|
root: &Self::Root,
|
||||||
sender: ComponentSender<Self>,
|
sender: ComponentSender<Self>,
|
||||||
) -> ComponentParts<Self> {
|
) -> ComponentParts<Self> {
|
||||||
let instances = FactoryVecDeque::new(gtk::Box::default(), sender.output_sender());
|
let instances = FactoryVecDeque::new(gtk::Box::default(), sender.input_sender());
|
||||||
let model = Self { instances };
|
let model = Self { instances };
|
||||||
let instances = model.instances.widget();
|
let instances = model.instances.widget();
|
||||||
let widgets = view_output!();
|
let widgets = view_output!();
|
||||||
|
@ -88,10 +89,10 @@ 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 {
|
||||||
InstancePageInput::FetchInstances => {
|
InstancesPageInput::FetchInstances => {
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
let message = match api::instances::fetch_instances() {
|
let message = match api::instances::fetch_instances() {
|
||||||
Ok(instances) => Some(InstancePageInput::DoneFetchInstances(instances)),
|
Ok(instances) => Some(InstancesPageInput::DoneFetchInstances(instances)),
|
||||||
Err(_err) => None,
|
Err(_err) => None,
|
||||||
};
|
};
|
||||||
if let Some(message) = message {
|
if let Some(message) = message {
|
||||||
|
@ -99,12 +100,40 @@ impl SimpleComponent for InstancesPage {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
InstancePageInput::DoneFetchInstances(instances) => {
|
InstancesPageInput::DoneFetchInstances(instances) => {
|
||||||
self.instances.guard().clear();
|
self.instances.guard().clear();
|
||||||
for instance in instances {
|
for instance in instances {
|
||||||
self.instances.guard().push_back(instance);
|
self.instances.guard().push_back(instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
InstancesPageInput::SetInstance(instance_url) => {
|
||||||
|
if instance_url.trim().is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let url_with_scheme = if instance_url.starts_with("http") {
|
||||||
|
instance_url
|
||||||
|
} else {
|
||||||
|
format!("https://{}", instance_url)
|
||||||
|
};
|
||||||
|
let message = match reqwest::Url::parse(&url_with_scheme) {
|
||||||
|
Ok(url) => {
|
||||||
|
// clear the back queue to not mix up different instances
|
||||||
|
sender.output_sender().emit(crate::AppMsg::Logout);
|
||||||
|
sender
|
||||||
|
.output_sender()
|
||||||
|
.emit(crate::AppMsg::UpdateState(crate::AppState::Loading));
|
||||||
|
let mut current_account = settings::get_current_account();
|
||||||
|
let url = url.to_string();
|
||||||
|
// remove the "/" at the end of the url
|
||||||
|
current_account.instance_url = url[0..url.len() - 1].to_string();
|
||||||
|
current_account.jwt = None;
|
||||||
|
settings::update_current_account(current_account);
|
||||||
|
crate::AppMsg::StartFetchPosts(None, true)
|
||||||
|
}
|
||||||
|
Err(err) => crate::AppMsg::ShowMessage(err.to_string()),
|
||||||
|
};
|
||||||
|
sender.output_sender().emit(message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
32
src/main.rs
32
src/main.rs
|
@ -10,7 +10,7 @@ use components::{
|
||||||
communities_page::{CommunitiesPage, CommunitiesPageInput},
|
communities_page::{CommunitiesPage, CommunitiesPageInput},
|
||||||
community_page::{self, CommunityPage},
|
community_page::{self, CommunityPage},
|
||||||
inbox_page::{InboxInput, InboxPage},
|
inbox_page::{InboxInput, InboxPage},
|
||||||
instances_page::{InstancePageInput, InstancesPage},
|
instances_page::{InstancesPage, InstancesPageInput},
|
||||||
post_page::{self, PostPage},
|
post_page::{self, PostPage},
|
||||||
post_row::PostRow,
|
post_row::PostRow,
|
||||||
profile_page::{self, ProfilePage},
|
profile_page::{self, ProfilePage},
|
||||||
|
@ -75,7 +75,6 @@ pub enum AppMsg {
|
||||||
LoggedIn,
|
LoggedIn,
|
||||||
Logout,
|
Logout,
|
||||||
ShowMessage(String),
|
ShowMessage(String),
|
||||||
DoneChoosingInstance(String),
|
|
||||||
StartFetchPosts(Option<ListingType>, bool),
|
StartFetchPosts(Option<ListingType>, bool),
|
||||||
DoneFetchPosts(Vec<PostView>),
|
DoneFetchPosts(Vec<PostView>),
|
||||||
OpenCommunity(CommunityId),
|
OpenCommunity(CommunityId),
|
||||||
|
@ -363,38 +362,11 @@ impl SimpleComponent for App {
|
||||||
}
|
}
|
||||||
|
|
||||||
match msg {
|
match msg {
|
||||||
AppMsg::DoneChoosingInstance(instance_url) => {
|
|
||||||
if instance_url.trim().is_empty() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let url_with_scheme = if instance_url.starts_with("http") {
|
|
||||||
instance_url
|
|
||||||
} else {
|
|
||||||
format!("https://{}", instance_url)
|
|
||||||
};
|
|
||||||
let message = match reqwest::Url::parse(&url_with_scheme) {
|
|
||||||
Ok(url) => {
|
|
||||||
// clear the back queue to not mix up different instances
|
|
||||||
self.back_queue.clear();
|
|
||||||
let mut current_account = settings::get_current_account();
|
|
||||||
let url = url.to_string();
|
|
||||||
// remove the "/" at the end of the url
|
|
||||||
current_account.instance_url = url[0..url.len() - 1].to_string();
|
|
||||||
current_account.jwt = None;
|
|
||||||
settings::update_current_account(current_account);
|
|
||||||
self.state = AppState::Loading;
|
|
||||||
self.logged_in = false;
|
|
||||||
AppMsg::StartFetchPosts(None, true)
|
|
||||||
}
|
|
||||||
Err(err) => AppMsg::ShowMessage(err.to_string()),
|
|
||||||
};
|
|
||||||
sender.input(message);
|
|
||||||
}
|
|
||||||
AppMsg::ChooseInstance => {
|
AppMsg::ChooseInstance => {
|
||||||
self.state = AppState::ChooseInstance;
|
self.state = AppState::ChooseInstance;
|
||||||
self.instances_page
|
self.instances_page
|
||||||
.sender()
|
.sender()
|
||||||
.emit(InstancePageInput::FetchInstances);
|
.emit(InstancesPageInput::FetchInstances);
|
||||||
}
|
}
|
||||||
AppMsg::StartFetchPosts(type_, remove_previous) => {
|
AppMsg::StartFetchPosts(type_, remove_previous) => {
|
||||||
self.current_posts_type = type_;
|
self.current_posts_type = type_;
|
||||||
|
|
Loading…
Reference in New Issue