use std::rc::Rc; use cursive::view::{Nameable, Resizable}; use cursive::views::{Dialog, EditView, LinearLayout, TextView}; use cursive::Cursive; use ovunto_security::Credentials; use crate::screen::{HomeScreen, Screen}; use crate::{get_content, ClientBox}; pub struct AddCredentialsScreen { client: ClientBox, } impl Screen for AddCredentialsScreen { const NAME: &'static str = "add_credentials"; fn construct(client: ClientBox) -> Self { Self { client } } fn render_dialog(&self) -> Dialog { fn add(s: &mut Cursive, cl: &ClientBox, website: &str, login: &str, password: &str) { let credentials = Credentials { website: website.to_string(), login: login.to_string(), password: password.to_string(), }; s.pop_layer(); Rc::clone(cl) .borrow_mut() .vault_mut() .save_credentials(credentials) .unwrap(); HomeScreen::new(cl).replace(s); } let website_input = EditView::new() .on_submit(|s, _| { s.focus_name("login").unwrap(); }) .with_name("website") .fixed_width(20); let login_input = EditView::new() .on_submit(|s, _| { s.focus_name("password").unwrap(); }) .with_name("login") .fixed_width(20); let cl = Rc::clone(&self.client); let password_input = EditView::new() .on_submit(move |s, password| { let website = &s.call_on_name("website", get_content).unwrap(); let login = &s.call_on_name("login", get_content).unwrap(); add(s, &cl, &website, &login, password); }) .with_name("password") .fixed_width(20); let cl = Rc::clone(&self.client); Dialog::around( LinearLayout::vertical() .child(TextView::new("website:")) .child(website_input) .child(TextView::new("login:")) .child(login_input) .child(TextView::new("password:")) .child(password_input), ) .title("Add Credentials") .button("Cancel", |s| { s.pop_layer(); }) .button("Add", move |s| { let website = &s.call_on_name("website", get_content).unwrap(); let login = &s.call_on_name("login", get_content).unwrap(); let password = &s.call_on_name("password", get_content).unwrap(); add(s, &cl, &website, &login, &password); }) } }