aboutsummaryrefslogblamecommitdiffstats
path: root/src/login_modal.rs
blob: f61c204fb0f3a6a85566c8398ef3475935fc5ba6 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                             





                                                                    
                   
                                    























                                           
               




                                        
                                       

 
                                        




                         


















                                                          
                                                           
                                                                    










































                                                                                                 
use filamento::presence::{Offline, Presence};
use iced::{
    futures::StreamExt,
    widget::{button, checkbox, column, container, text, text_input},
    Element, Task,
};
use jid::JID;
use keyring::Entry;
use serde::{Deserialize, Serialize};
use tokio_stream::wrappers::ReceiverStream;
use tracing::info;

use crate::Client;

#[derive(Default)]
pub struct LoginModal {
    jid: String,
    password: String,
    remember_me: bool,
    error: Option<Error>,
}

#[derive(Debug, Clone)]
pub enum Message {
    JID(String),
    Password(String),
    RememberMe,
    Submit,
    Error(Error),
}

#[derive(Debug, Clone)]
pub enum Error {
    InvalidJID,
}

pub enum Action {
    None,
    ClientCreated(Task<crate::Message>),
    CreateClient(String, String, bool),
}

#[derive(Serialize, Deserialize, Clone)]
pub struct Creds {
    pub jid: String,
    pub password: String,
}

impl LoginModal {
    pub fn update(&mut self, message: Message) -> Action {
        match message {
            Message::JID(j) => {
                self.jid = j;
                Action::None
            }
            Message::Password(p) => {
                self.password = p;
                Action::None
            }
            Message::RememberMe => {
                self.remember_me = !self.remember_me;
                Action::None
            }
            Message::Submit => {
                info!("submitting login");
                let jid_str = self.jid.clone();
                let password = self.password.clone();
                let remember_me = self.remember_me.clone();
                Action::CreateClient(jid_str, password, remember_me)
            }
            Message::Error(error) => {
                self.error = Some(error);
                Action::None
            }
        }
    }

    pub fn view(&self) -> Element<Message> {
        container(
            column![
                text("Log In").size(24),
                column![
                    column![
                        text("JID").size(12),
                        text_input("berry@macaw.chat", &self.jid)
                            .on_input(|j| Message::JID(j))
                            .on_submit(Message::Submit)
                            .padding(5),
                    ]
                    .spacing(5),
                    column![
                        text("Password").size(12),
                        text_input("", &self.password)
                            .on_input(|p| Message::Password(p))
                            .on_submit(Message::Submit)
                            .secure(true)
                            .padding(5),
                    ]
                    .spacing(5),
                    checkbox("remember me", self.remember_me).on_toggle(|_| Message::RememberMe),
                    button(text("Submit")).on_press(Message::Submit),
                ]
                .spacing(10)
            ]
            .spacing(20),
        )
        .width(300)
        .padding(10)
        .style(container::rounded_box)
        .into()
    }
}