add a way to specify a default value for a setting

turns out more than one item needs to have a default that
doesn't match Rust default values for types. add _or_value()
method so we can specify a default on a blank DB that matches
the expected UX
This commit is contained in:
bunnie 2022-11-23 05:01:03 +08:00
parent c5d6bd655c
commit c48e29fb2c
4 changed files with 24 additions and 7 deletions

View file

@ -22,6 +22,7 @@ pub fn getter_setter(input: TokenStream) -> TokenStream {
let set_fn_name = format_ident!("set_{}", ident); let set_fn_name = format_ident!("set_{}", ident);
let fn_name = format_ident!("{}", ident); let fn_name = format_ident!("{}", ident);
let fn_name_or_default = format_ident!("{}_or_default", ident); let fn_name_or_default = format_ident!("{}_or_default", ident);
let fn_name_or_value = format_ident!("{}_or_value", ident);
let read = quote! { let read = quote! {
impl Manager { impl Manager {
@ -53,6 +54,16 @@ pub fn getter_setter(input: TokenStream) -> TokenStream {
Ok(ret) Ok(ret)
} }
pub fn #fn_name_or_value(&self, value: #typ) -> Result<#typ, Error> {
let bytes = self.pddb_get_key(#ident_str)?;
let ret: #typ = match bincode::decode_from_slice(&bytes, bincode::config::standard()) {
Ok((data, _)) => data,
Err(_) => value,
};
Ok(ret)
}
} }
}; };

View file

@ -515,6 +515,13 @@
"ja": "期間は正でなければなりません!", "ja": "期間は正でなければなりません!",
"zh": "持续时间必须为正数!" "zh": "持续时间必须为正数!"
}, },
"prefs.wifi_connect_auto": {
"en": "Connect to WiFi automatically",
"en-tts": "Connect to WiFi automatically",
"fr": "Se connecter au wifi automatiquement *MT*",
"ja": "自動でWiFiに接続",
"zh": "自动连接WiFi"
},
"prefs.yes": { "prefs.yes": {
"en": "Yes", "en": "Yes",
"en-tts": "Yes", "en-tts": "Yes",

View file

@ -401,11 +401,10 @@ fn wrapped_main() -> ! {
}.unwrap_or_else(|error| { }.unwrap_or_else(|error| {
log::error!("cannot start connection manager: {:?}", error) log::error!("cannot start connection manager: {:?}", error)
}); });
// note inversion of preferences to cause it to be on by default match prefs.autobacklight_on_boot_or_value(true).unwrap() {
match all_prefs.autobacklight_on_boot { true => send_message(status_cid, Message::new_scalar(
false => send_message(status_cid, Message::new_scalar(
StatusOpcode::EnableAutomaticBacklight.to_usize().unwrap(), 0,0,0,0)), StatusOpcode::EnableAutomaticBacklight.to_usize().unwrap(), 0,0,0,0)),
true => Ok(xous::Result::Ok), false => Ok(xous::Result::Ok),
}.unwrap_or_else(|error| { }.unwrap_or_else(|error| {
log::error!("cannot set autobacklight status: {:?}", error); log::error!("cannot set autobacklight status: {:?}", error);
xous::Result::Ok xous::Result::Ok

View file

@ -43,7 +43,7 @@ impl Display for DevicePrefsOp {
match self { match self {
Self::AutobacklightOnBoot => write!(f, "{}", t!("prefs.autobacklight_enable", xous::LANG)), Self::AutobacklightOnBoot => write!(f, "{}", t!("prefs.autobacklight_enable", xous::LANG)),
Self::AutobacklightTimeout => write!(f, "{}", t!("prefs.autobacklight_duration", xous::LANG)), Self::AutobacklightTimeout => write!(f, "{}", t!("prefs.autobacklight_duration", xous::LANG)),
Self::ConnectKnownNetworksOnBoot => write!(f, "Automatically connnect to networks"), Self::ConnectKnownNetworksOnBoot => write!(f, "{}", t!("prefs.wifi_connect_auto", xous::LANG)),
Self::RadioOnOnBoot => write!(f, "WiFi kill switch"), Self::RadioOnOnBoot => write!(f, "WiFi kill switch"),
Self::KeyboardLayout => write!(f, "Keyboard layout"), Self::KeyboardLayout => write!(f, "Keyboard layout"),
Self::WLANMenu => write!(f, "WiFi settings"), Self::WLANMenu => write!(f, "WiFi settings"),
@ -239,11 +239,11 @@ impl DevicePrefs {
impl DevicePrefs { impl DevicePrefs {
fn autobacklight_on_boot(&mut self) -> Result<(), DevicePrefsError> { fn autobacklight_on_boot(&mut self) -> Result<(), DevicePrefsError> {
let cv = !self.up.autobacklight_on_boot_or_default()?; // note inversion of storage sense to make it on when false let cv = self.up.autobacklight_on_boot_or_value(true)?; // note inversion of storage sense to make it on when false
self.modals.add_list(vec![t!("prefs.yes", xous::LANG), t!("prefs.no", xous::LANG)]).unwrap(); self.modals.add_list(vec![t!("prefs.yes", xous::LANG), t!("prefs.no", xous::LANG)]).unwrap();
let new_result = !yes_no_to_bool( // inversion of storage sense: false = on let new_result = yes_no_to_bool( // inversion of storage sense: false = on
self.modals self.modals
.get_radiobutton(&format!("Current status: {}", bool_to_yes_no(cv))) .get_radiobutton(&format!("Current status: {}", bool_to_yes_no(cv)))
.unwrap() .unwrap()