diff options
Diffstat (limited to 'src/viewmodel/address.rs')
-rw-r--r-- | src/viewmodel/address.rs | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/src/viewmodel/address.rs b/src/viewmodel/address.rs new file mode 100644 index 0000000..757236c --- /dev/null +++ b/src/viewmodel/address.rs @@ -0,0 +1,151 @@ +use vcard::properties; +use vcard::parameters; +use vcard::values::{self, text}; +use std::collections::HashSet; +use super::*; +use crate::view::address::*; + +#[derive(Clone)] +pub struct Address { + pub post_office_box: String, + pub extension: String, + pub street: String, + pub locality: String, + pub region: String, + pub code: String, + pub country: String, + pub work: bool, + pub home: bool, +} + +impl VCardPropertyInputObject<properties::Address, AddressView> for Address { + fn new() -> Self { + Self { + post_office_box: String::new(), + extension: String::new(), + street: String::new(), + locality: String::new(), + region: String::new(), + code: String::new(), + country: String::new(), + work: false, + home: false, + } + } + fn get_input_fields(&self, link: &ComponentLink<AddressView>) -> Vec<VCardPropertyInputField> { + vec![ + VCardPropertyInputField::Text{ + label: "Post Office Box".to_string(), + id: Some("post_office_box".to_string()), + placeholder: None, + oninput: link.callback(|e: InputData| Msg::UpdatePostOfficeBox(e.value)), + value: self.post_office_box.clone(), + }, + VCardPropertyInputField::Text{ + label: "Extension".to_string(), + id: Some("extension".to_string()), + placeholder: None, + oninput: link.callback(|e: InputData| Msg::UpdateExtension(e.value)), + value: self.extension.clone(), + }, + VCardPropertyInputField::Text{ + label: "Street".to_string(), + id: Some("street".to_string()), + placeholder: None, + oninput: link.callback(|e: InputData| Msg::UpdateStreet(e.value)), + value: self.street.clone(), + }, + VCardPropertyInputField::Text{ + label: "Locality".to_string(), + id: Some("locality".to_string()), + placeholder: None, + oninput: link.callback(|e: InputData| Msg::UpdateLocality(e.value)), + value: self.locality.clone(), + }, + VCardPropertyInputField::Text{ + label: "Region".to_string(), + id: Some("region".to_string()), + placeholder: None, + oninput: link.callback(|e: InputData| Msg::UpdateRegion(e.value)), + value: self.region.clone(), + }, + VCardPropertyInputField::Text{ + label: "Code".to_string(), + id: Some("code".to_string()), + placeholder: None, + oninput: link.callback(|e: InputData| Msg::UpdateCode(e.value)), + value: self.code.clone(), + }, + VCardPropertyInputField::Text{ + label: "Country".to_string(), + id: Some("country".to_string()), + placeholder: None, + oninput: link.callback(|e: InputData| Msg::UpdateCountry(e.value)), + value: self.country.clone(), + }, + VCardPropertyInputField::CheckBox{ + label: "Work".to_string(), + id: Some("work".to_string()), + onclick: link.callback(|_: MouseEvent| Msg::ToggleWork), + value: self.work, + }, + VCardPropertyInputField::CheckBox{ + label: "Home".to_string(), + id: Some("home".to_string()), + onclick: link.callback(|_: MouseEvent| Msg::ToggleHome), + value: self.home, + }, + ] + } + fn to_vcard_property(&self) -> Result<properties::Address, VCardPropertyInputError> { + let address_value = values::address_value::AddressValue::from_components( + match self.post_office_box.is_empty() { + true => None, + false => Some(text::Component::from_str(&self.post_office_box).unwrap()), + }, + match self.extension.is_empty() { + true => None, + false => Some(text::Component::from_str(&self.extension).unwrap()), + }, + match self.street.is_empty() { + true => None, + false => Some(text::Component::from_str(&self.street).unwrap()), + }, + match self.locality.is_empty() { + true => None, + false => Some(text::Component::from_str(&self.locality).unwrap()), + }, + match self.region.is_empty() { + true => None, + false => Some(text::Component::from_str(&self.region).unwrap()), + }, + match self.code.is_empty() { + true => None, + false => Some(text::Component::from_str(&self.code).unwrap()), + }, + match self.country.is_empty() { + true => None, + false => Some(text::Component::from_str(&self.country).unwrap()), + }, + ); + + let mut address = properties::Address::from_address_value(address_value); + + let type_values = { + let mut type_values = HashSet::new(); + + if self.work { + type_values.insert(values::type_value::TypeValue::Work); + } + if self.home { + type_values.insert(values::type_value::TypeValue::Home); + } + + vcard::Set::from_hash_set(type_values).unwrap() + }; + + address.typ = Some(parameters::typ::Type::from_type_values(type_values)); + + Ok(address) + } +}
\ No newline at end of file |