Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn add_edit_contact(app: &mut Cursive, contact_to_edit: Option<Contact>) {
- app.pop_layer();
- let contact_clone = contact_to_edit.clone(); // make a local owned copy
- let mut first_name_view = LinearLayout::horizontal();
- first_name_view.add_child(TextView::new("First name: "));
- first_name_view.add_child(EditView::new().with_name("first-name-edit").fixed_width(30));
- let mut last_name_view = LinearLayout::horizontal();
- last_name_view.add_child(TextView::new("Last name: "));
- last_name_view.add_child(EditView::new().with_name("last-name-edit").fixed_width(30));
- let mut email_view = LinearLayout::horizontal();
- email_view.add_child(TextView::new("Email: "));
- email_view.add_child(EditView::new().with_name("email-edit").fixed_width(30));
- let mut phone_view = LinearLayout::horizontal();
- phone_view.add_child(TextView::new("Phone: "));
- phone_view.add_child(EditView::new().with_name("phone-edit").fixed_width(30));
- let mut first_name_edit = first_name_view
- .find_name::<EditView>("first-name-edit")
- .unwrap();
- let mut last_name_edit = last_name_view
- .find_name::<EditView>("last-name-edit")
- .unwrap();
- let mut email_edit = email_view.find_name::<EditView>("email-edit").unwrap();
- let mut phone_edit = phone_view.find_name::<EditView>("phone-edit").unwrap();
- if let Some(c) = contact_to_edit {
- first_name_edit.set_content(c.first_name);
- last_name_edit.set_content(c.last_name);
- email_edit.set_content(c.email);
- phone_edit.set_content(c.phone);
- }
- let mut layout = LinearLayout::vertical();
- layout.add_child(first_name_view);
- layout.add_child(last_name_view);
- layout.add_child(phone_view);
- layout.add_child(email_view);
- app.add_layer(
- Dialog::new()
- .title("Add Contact")
- .padding_lrtb(1, 1, 1, 0)
- .content(layout)
- .button("Ok", move |a| {
- let mut res = Contact {
- id: 0,
- first_name: a
- .find_name::<EditView>("first-name-edit")
- .unwrap()
- .get_content()
- .to_string(),
- last_name: a
- .find_name::<EditView>("last-name-edit")
- .unwrap()
- .get_content()
- .to_string(),
- phone: a
- .find_name::<EditView>("phone-edit")
- .unwrap()
- .get_content()
- .to_string(),
- email: a
- .find_name::<EditView>("email-edit")
- .unwrap()
- .get_content()
- .to_string(),
- };
- if let Some(c) = contact_clone.clone() {
- let res = Contact {
- id: c.id,
- first_name: res.first_name,
- last_name: res.last_name,
- phone: res.phone,
- email: res.email,
- };
- edit_product_from_file(res);
- } else {
- add_product_to_file(res);
- }
- show_menu(a);
- }),
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment