Basic login functionality
This commit is contained in:
parent
dca34c60a0
commit
3054e724fd
|
@ -7,5 +7,6 @@ edition = "2018"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[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" }
|
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 matrix;
|
||||||
pub mod ui;
|
pub mod ui;
|
||||||
|
|
||||||
fn main() {
|
#[tokio::main]
|
||||||
ui::Retrix::run(iced::Settings::default())
|
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,
|
username: &str,
|
||||||
password: &str,
|
password: &str,
|
||||||
server: &str,
|
server: &str,
|
||||||
) -> Result<Client, Box<dyn std::error::Error>> {
|
) -> Result<(Client, Session), Error> {
|
||||||
let url = Url::parse(server)?;
|
let url = Url::parse(server)?;
|
||||||
let client = Client::new(url)?;
|
let client = Client::new(url)?;
|
||||||
|
|
||||||
client
|
let response = client
|
||||||
.login(username, password, None, Some("retrix"))
|
.login(username, password, None, Some("retrix"))
|
||||||
.await?;
|
.await?;
|
||||||
|
let session = Session {
|
||||||
|
access_token: response.access_token,
|
||||||
|
user_id: response.user_id,
|
||||||
|
device_id: response.device_id,
|
||||||
|
};
|
||||||
client.sync(SyncSettings::new()).await;
|
client.sync(SyncSettings::new()).await;
|
||||||
|
|
||||||
Ok(client)
|
Ok((client, session))
|
||||||
}
|
}
|
||||||
|
|
54
src/ui.rs
54
src/ui.rs
|
@ -1,8 +1,10 @@
|
||||||
use iced::{
|
use iced::{
|
||||||
text_input::{self, TextInput},
|
text_input::{self, TextInput},
|
||||||
Button, Column, Container, Element, Sandbox, Text,
|
Application, Button, Column, Command, Container, Element, Text,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::matrix;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Retrix {
|
pub enum Retrix {
|
||||||
Prompt {
|
Prompt {
|
||||||
|
@ -14,8 +16,12 @@ pub enum Retrix {
|
||||||
user: String,
|
user: String,
|
||||||
password: String,
|
password: String,
|
||||||
server: String,
|
server: String,
|
||||||
|
error: Option<String>,
|
||||||
|
},
|
||||||
|
LoggedIn {
|
||||||
|
client: matrix_sdk::Client,
|
||||||
|
session: matrix_sdk::Session,
|
||||||
},
|
},
|
||||||
LoggedIn,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -24,13 +30,17 @@ pub enum Message {
|
||||||
SetPassword(String),
|
SetPassword(String),
|
||||||
SetServer(String),
|
SetServer(String),
|
||||||
Login,
|
Login,
|
||||||
|
LoggedIn(matrix_sdk::Client, matrix_sdk::Session),
|
||||||
|
SetError(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sandbox for Retrix {
|
impl Application for Retrix {
|
||||||
type Message = Message;
|
type Message = Message;
|
||||||
|
type Executor = iced::executor::Default;
|
||||||
|
type Flags = ();
|
||||||
|
|
||||||
fn new() -> Self {
|
fn new(_flags: ()) -> (Self, Command<Self::Message>) {
|
||||||
Retrix::Prompt {
|
let app = Retrix::Prompt {
|
||||||
user_input: text_input::State::new(),
|
user_input: text_input::State::new(),
|
||||||
password_input: text_input::State::new(),
|
password_input: text_input::State::new(),
|
||||||
server_input: text_input::State::new(),
|
server_input: text_input::State::new(),
|
||||||
|
@ -39,28 +49,45 @@ impl Sandbox for Retrix {
|
||||||
user: String::new(),
|
user: String::new(),
|
||||||
password: String::new(),
|
password: String::new(),
|
||||||
server: String::new(),
|
server: String::new(),
|
||||||
}
|
error: None,
|
||||||
|
};
|
||||||
|
(app, Command::none())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn title(&self) -> String {
|
fn title(&self) -> String {
|
||||||
String::from("Retrix matrix client")
|
String::from("Retrix matrix client")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, message: Self::Message) {
|
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
|
||||||
match *self {
|
match *self {
|
||||||
Retrix::Prompt {
|
Retrix::Prompt {
|
||||||
ref mut user,
|
ref mut user,
|
||||||
ref mut password,
|
ref mut password,
|
||||||
ref mut server,
|
ref mut server,
|
||||||
|
ref mut error,
|
||||||
..
|
..
|
||||||
} => match message {
|
} => match message {
|
||||||
Message::SetUser(u) => *user = u,
|
Message::SetUser(u) => *user = u,
|
||||||
Message::SetPassword(p) => *password = p,
|
Message::SetPassword(p) => *password = p,
|
||||||
Message::SetServer(s) => *server = s,
|
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> {
|
fn view(&mut self) -> Element<Self::Message> {
|
||||||
|
@ -73,8 +100,9 @@ impl Sandbox for Retrix {
|
||||||
ref user,
|
ref user,
|
||||||
ref password,
|
ref password,
|
||||||
ref server,
|
ref server,
|
||||||
|
ref error,
|
||||||
} => {
|
} => {
|
||||||
let content = Column::new()
|
let mut content = Column::new()
|
||||||
.width(500.into())
|
.width(500.into())
|
||||||
.push(Text::new("Username"))
|
.push(Text::new("Username"))
|
||||||
.push(
|
.push(
|
||||||
|
@ -97,6 +125,9 @@ impl Sandbox for Retrix {
|
||||||
.padding(5),
|
.padding(5),
|
||||||
)
|
)
|
||||||
.push(Button::new(login_button, Text::new("Login")).on_press(Message::Login));
|
.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)
|
Container::new(content)
|
||||||
.center_x()
|
.center_x()
|
||||||
|
@ -105,7 +136,10 @@ impl Sandbox for Retrix {
|
||||||
.height(iced::Length::Fill)
|
.height(iced::Length::Fill)
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
_ => Text::new("Beep").into(),
|
Retrix::LoggedIn {
|
||||||
|
ref client,
|
||||||
|
ref session,
|
||||||
|
} => Text::new(format!("Logged in to {}", session.user_id)).into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue