Basic login functionality
This commit is contained in:
		
							parent
							
								
									dca34c60a0
								
							
						
					
					
						commit
						3054e724fd
					
				
					 4 changed files with 62 additions and 19 deletions
				
			
		|  | @ -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"] } | ||||
|  |  | |||
|  | @ -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()); | ||||
| } | ||||
|  |  | |||
|  | @ -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<dyn std::error::Error>; | ||||
| 
 | ||||
| pub async fn login( | ||||
|     username: &str, | ||||
|     password: &str, | ||||
|     server: &str, | ||||
| ) -> Result<Client, Box<dyn std::error::Error>> { | ||||
| ) -> 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)) | ||||
| } | ||||
|  |  | |||
							
								
								
									
										54
									
								
								src/ui.rs
									
										
									
									
									
								
							
							
						
						
									
										54
									
								
								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<String>, | ||||
|     }, | ||||
|     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<Self::Message>) { | ||||
|         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<Self::Message> { | ||||
|         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<Self::Message> { | ||||
|  | @ -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(), | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue