Validate instance urls for more user convenience

This commit is contained in:
Bnyro 2023-06-28 12:00:50 +02:00
parent 73666c8474
commit dcdcf1b0a1
1 changed files with 22 additions and 9 deletions

View File

@ -315,8 +315,8 @@ impl SimpleComponent for App {
set_text: &model.message.clone().unwrap_or("".to_string()), set_text: &model.message.clone().unwrap_or("".to_string()),
}, },
gtk::Button { gtk::Button {
set_label: "Go Home", set_label: "Go back",
connect_clicked => AppMsg::StartFetchPosts(None, true), connect_clicked => AppMsg::PopBackStack,
} }
} }
} }
@ -463,13 +463,26 @@ impl SimpleComponent for App {
if instance_url.trim().is_empty() { if instance_url.trim().is_empty() {
return; return;
} }
let mut current_account = settings::get_current_account(); let url_with_scheme = if instance_url.starts_with("http") {
current_account.instance_url = instance_url; instance_url
current_account.jwt = None; } else {
settings::update_current_account(current_account); format!("https://{}", instance_url)
self.state = AppState::Loading; };
self.logged_in = false; let message = match reqwest::Url::parse(&url_with_scheme) {
sender.input(AppMsg::StartFetchPosts(None, true)); Ok(url) => {
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;