From 3054e724fddeda7fe4418d5822e3f79f2c7bd54a Mon Sep 17 00:00:00 2001 From: Amanda Graven Date: Tue, 24 Nov 2020 09:12:24 +0100 Subject: [PATCH] Basic login functionality --- Cargo.toml | 3 ++- src/main.rs | 7 ++++--- src/matrix.rs | 17 +++++++++++----- src/ui.rs | 54 +++++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 62 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ceb3ef8..4ab4805 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,6 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -iced = { version = "0.1", features = ["debug"] } +iced = { git = "https://github.com/hecrj/iced", rev = "fc4270f", features = ["debug", "tokio"] } matrix-sdk = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "27c6f30" } +tokio = { version = "0.2", features = ["macros"] } diff --git a/src/main.rs b/src/main.rs index ea9c0ba..0810076 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,9 @@ -use iced::Sandbox; +use iced::Application; pub mod matrix; pub mod ui; -fn main() { - ui::Retrix::run(iced::Settings::default()) +#[tokio::main] +async fn main() { + ui::Retrix::run(iced::Settings::default()); } diff --git a/src/matrix.rs b/src/matrix.rs index 83943d9..95d3e26 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -1,17 +1,24 @@ -use matrix_sdk::{reqwest::Url, Client, SyncSettings}; +use matrix_sdk::{reqwest::Url, Client, Session, SyncSettings}; -async fn login( +pub type Error = Box; + +pub async fn login( username: &str, password: &str, server: &str, -) -> Result> { +) -> Result<(Client, Session), Error> { let url = Url::parse(server)?; let client = Client::new(url)?; - client + let response = client .login(username, password, None, Some("retrix")) .await?; + let session = Session { + access_token: response.access_token, + user_id: response.user_id, + device_id: response.device_id, + }; client.sync(SyncSettings::new()).await; - Ok(client) + Ok((client, session)) } diff --git a/src/ui.rs b/src/ui.rs index 44039d2..29a03aa 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,8 +1,10 @@ use iced::{ text_input::{self, TextInput}, - Button, Column, Container, Element, Sandbox, Text, + Application, Button, Column, Command, Container, Element, Text, }; +use crate::matrix; + #[derive(Debug, Clone)] pub enum Retrix { Prompt { @@ -14,8 +16,12 @@ pub enum Retrix { user: String, password: String, server: String, + error: Option, + }, + LoggedIn { + client: matrix_sdk::Client, + session: matrix_sdk::Session, }, - LoggedIn, } #[derive(Debug, Clone)] @@ -24,13 +30,17 @@ pub enum Message { SetPassword(String), SetServer(String), Login, + LoggedIn(matrix_sdk::Client, matrix_sdk::Session), + SetError(String), } -impl Sandbox for Retrix { +impl Application for Retrix { type Message = Message; + type Executor = iced::executor::Default; + type Flags = (); - fn new() -> Self { - Retrix::Prompt { + fn new(_flags: ()) -> (Self, Command) { + let app = Retrix::Prompt { user_input: text_input::State::new(), password_input: text_input::State::new(), server_input: text_input::State::new(), @@ -39,28 +49,45 @@ impl Sandbox for Retrix { user: String::new(), password: String::new(), server: String::new(), - } + error: None, + }; + (app, Command::none()) } fn title(&self) -> String { String::from("Retrix matrix client") } - fn update(&mut self, message: Self::Message) { + fn update(&mut self, message: Self::Message) -> Command { match *self { Retrix::Prompt { ref mut user, ref mut password, ref mut server, + ref mut error, .. } => match message { Message::SetUser(u) => *user = u, Message::SetPassword(p) => *password = p, Message::SetServer(s) => *server = s, - Message::Login => (), + Message::SetError(e) => *error = Some(e), + Message::Login => { + let user = user.clone(); + let password = password.clone(); + let server = server.clone(); + return Command::perform( + async move { matrix::login(&user, &password, &server).await }, + |result| match result { + Ok((c, r)) => Message::LoggedIn(c, r), + Err(e) => Message::SetError(e.to_string()), + }, + ); + } + Message::LoggedIn(client, session) => *self = Retrix::LoggedIn { client, session }, }, _ => (), } + Command::none() } fn view(&mut self) -> Element { @@ -73,8 +100,9 @@ impl Sandbox for Retrix { ref user, ref password, ref server, + ref error, } => { - let content = Column::new() + let mut content = Column::new() .width(500.into()) .push(Text::new("Username")) .push( @@ -97,6 +125,9 @@ impl Sandbox for Retrix { .padding(5), ) .push(Button::new(login_button, Text::new("Login")).on_press(Message::Login)); + if let Some(ref error) = error { + content = content.push(Text::new(error).color([1.0, 0.0, 0.0])); + } Container::new(content) .center_x() @@ -105,7 +136,10 @@ impl Sandbox for Retrix { .height(iced::Length::Fill) .into() } - _ => Text::new("Beep").into(), + Retrix::LoggedIn { + ref client, + ref session, + } => Text::new(format!("Logged in to {}", session.user_id)).into(), } } }