use std::rc::Rc; use cursive::views::{Button, Dialog, DummyView, LinearLayout, SelectView, TextView}; use crate::screen::AddCredentialsScreen; use crate::screen::{EditCredentialsScreen, LoginScreen, Screen}; use crate::ClientBox; pub struct HomeScreen { client: ClientBox, } impl Screen for HomeScreen { const NAME: &'static str = "home"; fn construct(client: ClientBox) -> Self { Self { client } } fn render_dialog(&self) -> Dialog { let mut client = self.client.borrow_mut(); client.fetch_new_layers().unwrap(); let mut credentials_select = SelectView::new(); for credentials in client.vault().credentials().unwrap() { credentials_select.add_item(credentials.website.clone(), credentials.website.clone()); } let cl = Rc::clone(&self.client); credentials_select = credentials_select.on_submit(move |s, website: &String| { EditCredentialsScreen::new(&cl) .for_website(website.clone()) .print(s); }); let cl = Rc::clone(&self.client); let cl2 = Rc::clone(&self.client); let cl3 = Rc::clone(&self.client); let buttons = LinearLayout::vertical() .child(Button::new("Add credentials", move |s| { AddCredentialsScreen::new(&cl).print(s); })) .child(Button::new("Refresh", move |s| { HomeScreen::new(&cl2).replace(s); })) .child(Button::new("Logout", move |s| { let cl = cl3.clone(); s.add_layer( Dialog::text("Are you sure?") .button("Go back", |s| { s.pop_layer(); }) .button("Logout", move |s| { Rc::clone(&cl).borrow_mut().logout(); s.pop_layer(); LoginScreen::new(&cl).replace(s); }), ); })); Dialog::around( LinearLayout::vertical() .child(TextView::new(format!("Logged in as {}", client.username()))) .child(DummyView) .child( LinearLayout::horizontal() .child(credentials_select) .child(DummyView) .child(buttons), ), ) .title("Ovunto Security") } }