status: use new multi-text modals to take time

This commit is contained in:
Gianguido Sorà 2022-04-13 15:50:07 +02:00
parent 8375fdf801
commit c2e619949c
2 changed files with 137 additions and 102 deletions

View file

@ -180,38 +180,41 @@
"zh": "NTP 查询失败,请手动输入时间。", "zh": "NTP 查询失败,请手动输入时间。",
"en-tts": "NTP query failed, please enter time manually." "en-tts": "NTP query failed, please enter time manually."
}, },
"rtc.set_time_modal": {
"en": "Enter time"
},
"rtc.month": { "rtc.month": {
"en": "Enter month (1-12)", "en": "Month (1-12)",
"ja": "月(1-12)を入力してください。", "ja": "月(1-12)を入力してください。",
"zh": "输入月份 (1-12)", "zh": "输入月份 (1-12)",
"en-tts": "Enter month one through twelve" "en-tts": "Enter month one through twelve"
}, },
"rtc.day": { "rtc.day": {
"en": "Enter day (1-31)", "en": "Day (1-31)",
"ja": "日数 (1-31) を入力してください。", "ja": "日数 (1-31) を入力してください。",
"zh": "输入日期 (1-31)", "zh": "输入日期 (1-31)",
"en-tts": "Enter day one through 31" "en-tts": "Enter day one through 31"
}, },
"rtc.year": { "rtc.year": {
"en": "Enter last two digits of year", "en": "Last two digits of year",
"ja": "西暦の下2桁を入力してください。", "ja": "西暦の下2桁を入力してください。",
"zh": "输入年份 (最后两位数)", "zh": "输入年份 (最后两位数)",
"en-tts": "Enter last two digits of year" "en-tts": "Enter last two digits of year"
}, },
"rtc.hour": { "rtc.hour": {
"en": "Enter hours in local timezone (0-23)", "en": "Hours in local timezone (0-23)",
"ja": "時間(0-23)を入力してください。", "ja": "時間(0-23)を入力してください。",
"zh": "输入小时 (0-23)", "zh": "输入小时 (0-23)",
"en-tts": "Enter hours for local timezone as 24 hour format" "en-tts": "Enter hours for local timezone as 24 hour format"
}, },
"rtc.minute": { "rtc.minute": {
"en": "Enter minutes (0-59)", "en": "Minutes (0-59)",
"ja": "分(0-59)を入力してください。", "ja": "分(0-59)を入力してください。",
"zh": "输入分钟 (0-59)", "zh": "输入分钟 (0-59)",
"en-tts": "Enter minutes" "en-tts": "Enter minutes"
}, },
"rtc.seconds": { "rtc.seconds": {
"en": "Enter seconds (0-59)", "en": "Seconds (0-59)",
"ja": "秒 (0-59) を入力してください。", "ja": "秒 (0-59) を入力してください。",
"zh": "输入秒数 (0-59)", "zh": "输入秒数 (0-59)",
"en-tts": "Enter seconds" "en-tts": "Enter seconds"

View file

@ -516,11 +516,13 @@ pub fn start_time_ux(sid: xous::SID) {
// note that we don't do an "else" here because we also want to catch the case of // note that we don't do an "else" here because we also want to catch the case of
// a key exists, but nothing was written to it (length of key was 0 or inappropriate) // a key exists, but nothing was written to it (length of key was 0 or inappropriate)
if !tz_set { if !tz_set {
let tz = modals.get_text( let tz = modals.alert_builder(t!("rtc.timezone", xous::LANG))
t!("rtc.timezone", xous::LANG), .field(None, Some(tz_ux_validator))
Some(tz_ux_validator), None, None .build()
).expect("couldn't get timezone").as_str() .expect("couldn't get timezone")
.parse::<f32>().expect("pre-validated input failed to re-parse!"); .first()
.as_str()
.parse::<f32>().expect("pre-validated input failed to re-parse!");
log::info!("got tz offset {}", tz); log::info!("got tz offset {}", tz);
tz_offset_ms = (tz * 3600.0 * 1000.0) as i64; tz_offset_ms = (tz * 3600.0 * 1000.0) as i64;
xous::send_message(timeserver_cid, xous::send_message(timeserver_cid,
@ -578,54 +580,35 @@ pub fn start_time_ux(sid: xous::SID) {
} }
} }
let secs: u8; let mut secs: u8 = 0;
let mins: u8; let mut mins: u8 = 0;
let hours: u8; let mut hours: u8 = 0;
let months: u8; let mut months: u8 = 0;
let days: u8; let mut days: u8 = 0;
let years: u8; let mut years: u8 = 0;
months = modals.get_text( let date = modals.alert_builder(t!("rtc.set_time_modal", xous::LANG))
t!("rtc.month", xous::LANG), .field(Some(String::from(t!("rtc.month", xous::LANG))), Some(rtc_ux_validate_month))
Some(rtc_ux_validator), Some(ValidatorOp::UxMonth.to_u32().unwrap()), None, .field(Some(String::from(t!("rtc.day", xous::LANG))), Some(rtc_ux_validate_day))
).expect("couldn't get month").as_str() .field(Some(String::from(t!("rtc.year", xous::LANG))), Some(rtc_ux_validate_year))
.parse::<u8>().expect("pre-validated input failed to re-parse!"); .field(Some(String::from(t!("rtc.hour", xous::LANG))), Some(rtc_ux_validate_hour))
log::debug!("got months {}", months); .field(Some(String::from(t!("rtc.minute", xous::LANG))), Some(rtc_ux_validate_minute))
.field(Some(String::from(t!("rtc.seconds", xous::LANG))), Some(rtc_ux_validate_seconds))
.build()
.expect("cannot get date from user");
days = modals.get_text( for (index, elem) in date.content().iter().enumerate() {
t!("rtc.day", xous::LANG), let elem = elem.as_str().parse::<u8>().expect("pre-validated input failed to re-parse!");
Some(rtc_ux_validator), Some(ValidatorOp::UxDay.to_u32().unwrap()), None match index {
).expect("couldn't get month").as_str() 0 => months = elem,
.parse::<u8>().expect("pre-validated input failed to re-parse!"); 1 => days = elem,
log::debug!("got days {}", days); 2 => years = elem,
3 => hours = elem,
years = modals.get_text( 4 => mins = elem,
t!("rtc.year", xous::LANG), 5 => secs = elem,
Some(rtc_ux_validator), Some(ValidatorOp::UxYear.to_u32().unwrap()), None _ => {},
).expect("couldn't get month").as_str() }
.parse::<u8>().expect("pre-validated input failed to re-parse!"); }
log::debug!("got years {}", years);
hours = modals.get_text(
t!("rtc.hour", xous::LANG),
Some(rtc_ux_validator), Some(ValidatorOp::UxHour.to_u32().unwrap()), None
).expect("couldn't get hour").as_str()
.parse::<u8>().expect("pre-validated input failed to re-parse!");
log::debug!("got hours {}", hours);
mins = modals.get_text(
t!("rtc.minute", xous::LANG),
Some(rtc_ux_validator), Some(ValidatorOp::UxMinute.to_u32().unwrap()), None
).expect("couldn't get minutes").as_str()
.parse::<u8>().expect("pre-validated input failed to re-parse!");
log::debug!("got minutes {}", mins);
secs = modals.get_text(
t!("rtc.seconds", xous::LANG),
Some(rtc_ux_validator), Some(ValidatorOp::UxSeconds.to_u32().unwrap()), None
).expect("couldn't get seconds").as_str()
.parse::<u8>().expect("pre-validated input failed to re-parse!");
log::debug!("got seconds {}", secs);
log::info!("Setting time: {}/{}/{} {}:{}:{}", months, days, years, hours, mins, secs); log::info!("Setting time: {}/{}/{} {}:{}:{}", months, days, years, hours, mins, secs);
let new_dt = chrono::FixedOffset::east((tz_offset_ms / 1000) as i32).ymd(years as i32 + 2000, months as u32, days as u32) let new_dt = chrono::FixedOffset::east((tz_offset_ms / 1000) as i32).ymd(years as i32 + 2000, months as u32, days as u32)
@ -644,11 +627,14 @@ pub fn start_time_ux(sid: xous::SID) {
modals.show_notification(t!("stats.please_mount", xous::LANG)).expect("couldn't show notification"); modals.show_notification(t!("stats.please_mount", xous::LANG)).expect("couldn't show notification");
continue; continue;
} }
let tz = modals.get_text(
t!("rtc.timezone", xous::LANG), let tz = modals.alert_builder(t!("rtc.timezone", xous::LANG))
Some(tz_ux_validator), None, None .field(None, Some(tz_ux_validator))
).expect("couldn't get timezone").as_str() .build()
.parse::<f32>().expect("pre-validated input failed to re-parse!"); .expect("couldn't get timezone")
.first()
.as_str()
.parse::<f32>().expect("pre-validated input failed to re-parse!");
log::info!("got tz offset {}", tz); log::info!("got tz offset {}", tz);
let tzoff_ms = (tz * 3600.0 * 1000.0) as i64; let tzoff_ms = (tz * 3600.0 * 1000.0) as i64;
xous::send_message(timeserver_cid, xous::send_message(timeserver_cid,
@ -685,8 +671,9 @@ pub(crate) enum ValidatorOp {
UxSeconds, UxSeconds,
} }
fn tz_ux_validator(input: TextEntryPayload, _opcode: u32) -> Option<ValidatorErr> { fn tz_ux_validator(input: TextEntryPayload) -> Option<ValidatorErr> {
let text_str = input.as_str(); let text_str = input.as_str();
match text_str.parse::<f32>() { match text_str.parse::<f32>() {
Ok(input) => if input < -12.0 || input > 14.0 { Ok(input) => if input < -12.0 || input > 14.0 {
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG))); return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)));
@ -696,48 +683,93 @@ fn tz_ux_validator(input: TextEntryPayload, _opcode: u32) -> Option<ValidatorErr
None None
} }
fn rtc_ux_validator(input: TextEntryPayload, opcode: u32) -> Option<ValidatorErr> { fn rtc_ux_validate_month(input: TextEntryPayload) -> Option<ValidatorErr> {
let text_str = input.as_str(); let text_str = input.as_str();
let input_int = match text_str.parse::<u32>() {
let input = match text_str.parse::<u32>() {
Ok(input_int) => input_int, Ok(input_int) => input_int,
_ => return Some(ValidatorErr::from_str(t!("rtc.integer_err", xous::LANG))), _ => return Some(ValidatorErr::from_str(t!("rtc.integer_err", xous::LANG))),
}; };
log::trace!("validating input {}, parsed as {} for opcode {}", text_str, input_int, opcode);
match FromPrimitive::from_u32(opcode) { if input < 1 || input > 12 {
Some(ValidatorOp::UxMonth) => { return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
if input_int < 1 || input_int > 12 {
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
}
}
Some(ValidatorOp::UxDay) => {
if input_int < 1 || input_int > 31 {
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
}
}
Some(ValidatorOp::UxYear) => {
if input_int > 99 {
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
}
}
Some(ValidatorOp::UxHour) => {
if input_int > 23 {
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
}
}
Some(ValidatorOp::UxMinute) => {
if input_int > 59 {
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
}
}
Some(ValidatorOp::UxSeconds) => {
if input_int > 59 {
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
}
}
_ => {
log::error!("internal error: invalid opcode was sent to validator: {:?}", opcode);
panic!("internal error: invalid opcode was sent to validator");
}
} }
None None
} }
fn rtc_ux_validate_day(input: TextEntryPayload) -> Option<ValidatorErr> {
let text_str = input.as_str();
let input = match text_str.parse::<u32>() {
Ok(input_int) => input_int,
_ => return Some(ValidatorErr::from_str(t!("rtc.integer_err", xous::LANG))),
};
if input < 1 || input > 31 {
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
}
None
}
fn rtc_ux_validate_year(input: TextEntryPayload) -> Option<ValidatorErr> {
let text_str = input.as_str();
let input = match text_str.parse::<u32>() {
Ok(input_int) => input_int,
_ => return Some(ValidatorErr::from_str(t!("rtc.integer_err", xous::LANG))),
};
if input > 99 {
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
}
None
}
fn rtc_ux_validate_hour(input: TextEntryPayload) -> Option<ValidatorErr> {
let text_str = input.as_str();
let input = match text_str.parse::<u32>() {
Ok(input_int) => input_int,
_ => return Some(ValidatorErr::from_str(t!("rtc.integer_err", xous::LANG))),
};
if input > 23 {
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
}
None
}
fn rtc_ux_validate_minute(input: TextEntryPayload) -> Option<ValidatorErr> {
let text_str = input.as_str();
let input = match text_str.parse::<u32>() {
Ok(input_int) => input_int,
_ => return Some(ValidatorErr::from_str(t!("rtc.integer_err", xous::LANG))),
};
if input > 59 {
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
}
None
}
fn rtc_ux_validate_seconds(input: TextEntryPayload) -> Option<ValidatorErr> {
let text_str = input.as_str();
let input = match text_str.parse::<u32>() {
Ok(input_int) => input_int,
_ => return Some(ValidatorErr::from_str(t!("rtc.integer_err", xous::LANG))),
};
if input > 59 {
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
}
None
}