diff --git a/src/stages/network/mod.rs b/src/stages/network/mod.rs index 93d1e59..4fea9d0 100644 --- a/src/stages/network/mod.rs +++ b/src/stages/network/mod.rs @@ -71,7 +71,7 @@ pub enum UpdateResult { fn check_connection_blocking() -> CheckResult { use std::process::Command; - std::thread::sleep(std::time::Duration::from_secs(1)); + //std::thread::sleep(std::time::Duration::from_secs(1)); match Command::new("nm-online").arg("-q").status() { Ok(status) => { diff --git a/src/stages/timezone/mod.rs b/src/stages/timezone/mod.rs index 78a24d9..4bcac81 100644 --- a/src/stages/timezone/mod.rs +++ b/src/stages/timezone/mod.rs @@ -15,7 +15,7 @@ // along with this program. If not, see . /* - This is Welcome stage, used to greet used and allow user to choose program language + This is TimeZone stage, used to select timezone */ use crate::stage::{ConfigValue, StageAction, StageResult}; @@ -32,10 +32,6 @@ pub struct TimeZoneData { } impl TimeZoneData { - - fn to_full_code(&self) -> String { - format!("{}/{}", self.region, self.zone) - } fn from_str(s: &str) -> Option { let idx = s.find('/')?; let (region, zone) = s.split_at(idx); @@ -48,17 +44,19 @@ impl TimeZoneData { impl std::fmt::Display for TimeZoneData { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.zone) + write!(f, "{}/{}", self.region, self.zone) } } #[derive(Debug, Clone)] pub struct TimeZoneStage { - time_zones: Result>, String>, + time_zones: Result>, String>, regions: Vec, region_id: Option, - zones: Vec, + zones: Vec, zone_id: Option, + selected_zone: Option, + selected_zone_text: String, } #[derive(Debug, Clone)] @@ -103,22 +101,24 @@ fn get_timezones_blocking() -> Result, String> { impl TimeZoneStage { pub fn new() -> Self { match get_timezones_blocking() { - Ok(tzs) => { - let mut time_zones: HashMap> = HashMap::new(); - for tz in &tzs { - if let Some(z) = time_zones.get_mut(tz.region.as_str()) { - z.push(tz.clone()); + Ok(time_zones_data_list) => { + let mut time_zones_map: HashMap> = HashMap::new(); + for tz_data in &time_zones_data_list { + if let Some(zones) = time_zones_map.get_mut(&tz_data.region) { + zones.push(tz_data.zone.clone()); } else { - time_zones.insert(tz.region.clone(), vec![tz.clone()]); + time_zones_map.insert(tz_data.region.clone(), vec![tz_data.zone.clone()]); } } //let time_zones: HashMap = HashMap::from_iter(tzs.iter().map(|tz| (tz.region.clone(), tz.clone()))); Self { - regions: time_zones.keys().cloned().collect(), - zones: tzs.clone(), - time_zones: Ok(time_zones), + regions: time_zones_map.keys().cloned().collect(), + zones: Vec::new(), + time_zones: Ok(time_zones_map), region_id: None, zone_id: None, + selected_zone: None, + selected_zone_text: String::new(), } } Err(ex) => { @@ -132,20 +132,21 @@ impl TimeZoneStage { zones: Vec::new(), region_id: None, zone_id: None, + selected_zone: None, + selected_zone_text: String::new(), } } } } fn gen_result(&self) -> StageResult { - if let Some(tz_id) = self.zone_id { - let time_zone = &self.zones[tz_id]; + if let Some(time_zone) = &self.selected_zone { StageResult { name: "time_zone".to_string(), config: Some(HashMap::from([ ( "name".to_string(), - ConfigValue::String(time_zone.to_full_code()), + ConfigValue::String(time_zone.to_string()), ), ( "region".to_string(), @@ -181,10 +182,19 @@ impl TimeZoneStage { .unwrap() .clone(); self.region_id = Some(region_id); + self.zone_id = None; + self.selected_zone_text.clear(); StageAction::None } Message::SelectZone(scroll_list::ListViewMessage::Select(zone_id)) => { + let zone = TimeZoneData { region: self.regions[self.region_id.unwrap()].clone(), zone: self.zones[zone_id].clone() }; + self.selected_zone_text = format!( + "{} {}", + t!("timezone.selected_timezone"), + zone.to_string() + ); self.zone_id = Some(zone_id); + self.selected_zone = Some(zone); StageAction::None } Message::Next => StageAction::Next(self.gen_result()), @@ -199,8 +209,8 @@ impl TimeZoneStage { widget::button(widget::text(t!("button.next"))) }; let main_content = match &self.time_zones { - Ok(_) => { - let mut col = widget::column![ + Ok(_) => widget::container( + widget::column![ widget::text(t!("timezone.select_timezone")), widget::row![ scroll_list::list_view(&self.regions, self.region_id, Length::Shrink) @@ -210,21 +220,13 @@ impl TimeZoneStage { ] .padding(20) .spacing(10) - .height(Length::Shrink) + .height(Length::Shrink), + widget::text(self.selected_zone_text.clone()) ] .spacing(10) - .align_x(Alignment::Center); + .align_x(Alignment::Center), + ), - if let Some(tz_id) = self.zone_id { - col = col.push(widget::text(format!( - "{} {}", - t!("timezone.selected_timezone"), - self.zones[tz_id].to_full_code() - ))) - }; - - widget::container(col) - } Err(_) => widget::container(widget::text(t!("timezone.init_error"))), }; let back_button = widget::button(widget::text(t!("button.back"))).on_press(Message::Back);