TimeZone stage allmost done.
This commit is contained in:
+72
-61
@@ -19,10 +19,12 @@
|
||||
*/
|
||||
|
||||
use crate::stage::{ConfigValue, StageAction, StageResult};
|
||||
use iced::{Alignment, widget};
|
||||
use iced::{Alignment, Length, widget};
|
||||
use rust_i18n::t;
|
||||
use std::collections::HashMap;
|
||||
|
||||
mod scroll_list;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct TimeZoneData {
|
||||
region: String,
|
||||
@@ -30,7 +32,8 @@ pub struct TimeZoneData {
|
||||
}
|
||||
|
||||
impl TimeZoneData {
|
||||
fn to_string(&self) -> String {
|
||||
|
||||
fn to_full_code(&self) -> String {
|
||||
format!("{}/{}", self.region, self.zone)
|
||||
}
|
||||
fn from_str(s: &str) -> Option<Self> {
|
||||
@@ -38,30 +41,30 @@ impl TimeZoneData {
|
||||
let (region, zone) = s.split_at(idx);
|
||||
Some(Self {
|
||||
region: region.into(),
|
||||
zone: zone.into(),
|
||||
zone: zone.trim_start_matches('/').into(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for TimeZoneData {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}/{}", self.region, self.zone)
|
||||
write!(f, "{}", self.zone)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TimeZoneStage {
|
||||
time_zone: Option<TimeZoneData>,
|
||||
time_zone_region: Option<String>,
|
||||
time_zones: Result<HashMap<String, Vec<TimeZoneData>>, String>,
|
||||
regions: Vec<String>,
|
||||
region_id: Option<usize>,
|
||||
zones: Vec<TimeZoneData>,
|
||||
zone_id: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Message {
|
||||
SelectRegion(String),
|
||||
SelectZone(TimeZoneData),
|
||||
SelectRegion(scroll_list::ListViewMessage),
|
||||
SelectZone(scroll_list::ListViewMessage),
|
||||
Next,
|
||||
Back,
|
||||
}
|
||||
@@ -113,36 +116,45 @@ impl TimeZoneStage {
|
||||
Self {
|
||||
regions: time_zones.keys().cloned().collect(),
|
||||
zones: tzs.clone(),
|
||||
time_zone: None,
|
||||
time_zone_region: None,
|
||||
time_zones: Ok(time_zones),
|
||||
region_id: None,
|
||||
zone_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
Err(ex) => {
|
||||
println!(
|
||||
"Exception while trying to get time zones list from system {}",
|
||||
ex
|
||||
);
|
||||
Self {
|
||||
time_zone: None,
|
||||
time_zone_region: None,
|
||||
time_zones: Err(ex),
|
||||
regions: Vec::new(),
|
||||
zones: Vec::new(),
|
||||
region_id: None,
|
||||
zone_id: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn gen_result(&self) -> StageResult {
|
||||
if let Some(tz) = &self.time_zone {
|
||||
if let Some(tz_id) = self.zone_id {
|
||||
let time_zone = &self.zones[tz_id];
|
||||
StageResult {
|
||||
name: "time_zone".to_string(),
|
||||
config: Some(HashMap::from([
|
||||
("name".to_string(), ConfigValue::String(tz.to_string())),
|
||||
("region".to_string(), ConfigValue::String(tz.region.clone())),
|
||||
("zone".to_string(), ConfigValue::String(tz.zone.clone())),
|
||||
(
|
||||
"name".to_string(),
|
||||
ConfigValue::String(time_zone.to_full_code()),
|
||||
),
|
||||
(
|
||||
"region".to_string(),
|
||||
ConfigValue::String(time_zone.region.clone()),
|
||||
),
|
||||
(
|
||||
"zone".to_string(),
|
||||
ConfigValue::String(time_zone.zone.clone()),
|
||||
),
|
||||
])),
|
||||
resuts: None,
|
||||
error: None,
|
||||
@@ -159,12 +171,20 @@ impl TimeZoneStage {
|
||||
|
||||
pub fn update(&mut self, message: Message) -> StageAction {
|
||||
match message {
|
||||
Message::SelectRegion(region) => {
|
||||
self.time_zone_region = Some(region);
|
||||
Message::SelectRegion(scroll_list::ListViewMessage::Select(region_id)) => {
|
||||
let region = self.regions.get(region_id).unwrap();
|
||||
self.zones = self
|
||||
.time_zones
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.get(region)
|
||||
.unwrap()
|
||||
.clone();
|
||||
self.region_id = Some(region_id);
|
||||
StageAction::None
|
||||
}
|
||||
Message::SelectZone(zone) => {
|
||||
self.time_zone = Some(zone);
|
||||
Message::SelectZone(scroll_list::ListViewMessage::Select(zone_id)) => {
|
||||
self.zone_id = Some(zone_id);
|
||||
StageAction::None
|
||||
}
|
||||
Message::Next => StageAction::Next(self.gen_result()),
|
||||
@@ -173,48 +193,39 @@ impl TimeZoneStage {
|
||||
}
|
||||
|
||||
pub fn view(&self) -> iced::Element<'_, Message> {
|
||||
let (next_button, main_content) = match &self.time_zones {
|
||||
Ok(time_zones) => (
|
||||
widget::button(widget::text(t!("button.next"))).on_press(Message::Next),
|
||||
widget::container(
|
||||
widget::column![
|
||||
widget::text(t!("timezone.select_timezone")),
|
||||
widget::row![
|
||||
widget::text(t!("timezone.select_region")),
|
||||
widget::pick_list(
|
||||
self.regions.clone(),
|
||||
self.time_zone_region.clone(),
|
||||
Message::SelectRegion,
|
||||
)
|
||||
.placeholder("Select Region")
|
||||
],
|
||||
widget::row![
|
||||
widget::text(t!("timezone.select_timezone")),
|
||||
widget::pick_list(
|
||||
time_zones
|
||||
.get(
|
||||
self.time_zone_region
|
||||
.clone()
|
||||
.unwrap_or("Etc".into())
|
||||
.as_str()
|
||||
)
|
||||
.unwrap()
|
||||
.clone(),
|
||||
self.time_zone.clone(),
|
||||
Message::SelectZone,
|
||||
)
|
||||
.placeholder("Select Timezone")
|
||||
]
|
||||
let next_button = if self.zone_id.is_some() {
|
||||
widget::button(widget::text(t!("button.next"))).on_press(Message::Next)
|
||||
} else {
|
||||
widget::button(widget::text(t!("button.next")))
|
||||
};
|
||||
let main_content = match &self.time_zones {
|
||||
Ok(_) => {
|
||||
let mut col = widget::column![
|
||||
widget::text(t!("timezone.select_timezone")),
|
||||
widget::row![
|
||||
scroll_list::list_view(&self.regions, self.region_id, Length::Shrink)
|
||||
.map(Message::SelectRegion),
|
||||
scroll_list::list_view(&self.zones, self.zone_id, Length::FillPortion(1))
|
||||
.map(Message::SelectZone),
|
||||
]
|
||||
.padding(10)
|
||||
.padding(20)
|
||||
.spacing(10)
|
||||
.align_x(Alignment::Center),
|
||||
),
|
||||
),
|
||||
Err(_) => (
|
||||
widget::button(widget::text(t!("button.next"))),
|
||||
widget::container(widget::text(t!("timezone.init_error"))),
|
||||
),
|
||||
.height(Length::Shrink)
|
||||
]
|
||||
.spacing(10)
|
||||
.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);
|
||||
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use iced::widget::{Column, button, container, scrollable, text};
|
||||
use iced::{Alignment, Color, Element, Length, Theme};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ListViewMessage {
|
||||
Select(usize),
|
||||
}
|
||||
|
||||
fn unselected_button_style(theme: &Theme, status: button::Status) -> button::Style {
|
||||
//let palette = theme.extended_palette();
|
||||
use iced::{Border, border::Radius};
|
||||
|
||||
match status {
|
||||
button::Status::Hovered => button::Style {
|
||||
border: Border {
|
||||
color: Color::from_rgb(0.8, 0.3, 0.3),
|
||||
width: 2.0,
|
||||
radius: Radius::new(5.0),
|
||||
},
|
||||
..button::primary(theme, status)
|
||||
},
|
||||
button::Status::Active => button::Style {
|
||||
background: Some(iced::Background::Color(crate::theme::change_lightnes(
|
||||
&theme.palette().primary,
|
||||
-0.1,
|
||||
))),
|
||||
..button::primary(theme, status)
|
||||
},
|
||||
_ => button::primary(theme, status),
|
||||
}
|
||||
}
|
||||
|
||||
fn selected_button_style(theme: &Theme, status: button::Status) -> button::Style {
|
||||
//let palette = theme.extended_palette();
|
||||
|
||||
match status {
|
||||
button::Status::Active => button::Style {
|
||||
background: Some(iced::Background::Color(Color::from_rgb(0.8, 0.3, 0.3))),
|
||||
..button::primary(theme, status)
|
||||
},
|
||||
_ => button::Style {
|
||||
background: Some(iced::Background::Color(Color::from_rgb(0.8, 0.3, 0.3))),
|
||||
..button::primary(theme, status)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// fn view() -> container::Style {
|
||||
// container::Style {
|
||||
// text_color: None,
|
||||
// background: None,
|
||||
// border: Border {
|
||||
// color: Color::BLACK,
|
||||
// width: 2.0,
|
||||
// radius: Radius::new(5.0),
|
||||
// },
|
||||
// shadow: None,
|
||||
// snap: false,
|
||||
// }
|
||||
// }
|
||||
|
||||
// fn border_style(theme: &Theme) -> container::Style {
|
||||
// use iced::{Border, border::Radius};
|
||||
|
||||
// container::Style {
|
||||
// border: Border {
|
||||
// color: Color::from_rgb(0.8, 0.3, 0.3),
|
||||
// width: 2.0,
|
||||
// radius: Radius::new(5.0),
|
||||
// },
|
||||
// ..container::bordered_box(theme)
|
||||
// }
|
||||
// }
|
||||
|
||||
// Assuming your Item struct or type implements Display or has a text representation
|
||||
pub fn list_view<T: Display>(
|
||||
items: &Vec<T>,
|
||||
selected_id: Option<usize>,
|
||||
container_height: Length,
|
||||
) -> Element<'_, ListViewMessage> {
|
||||
let mut column = Column::new()
|
||||
.spacing(2)
|
||||
.align_x(Alignment::Center)
|
||||
.width(Length::Fill);
|
||||
|
||||
for (index, value) in items.iter().enumerate() {
|
||||
// Create a row for each item, possibly with buttons or other controls
|
||||
let list_item = if let Some(id) = selected_id
|
||||
&& id == index
|
||||
{
|
||||
button(text(value.to_string())).style(selected_button_style)
|
||||
} else {
|
||||
button(text(value.to_string())).style(unselected_button_style)
|
||||
};
|
||||
let list_item = list_item
|
||||
.width(Length::Fill)
|
||||
.on_press(ListViewMessage::Select(index));
|
||||
|
||||
column = column.push(list_item);
|
||||
}
|
||||
|
||||
// Wrap the column in a scrollable widget
|
||||
container(scrollable(column).width(Length::Fill))
|
||||
.style(container::bordered_box)
|
||||
.height(container_height)
|
||||
.padding(8)
|
||||
.into()
|
||||
}
|
||||
Reference in New Issue
Block a user