tmollov

Untitled

Jul 25th, 2025
1,667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.41 KB | Source Code | 0 0
  1. fn add_edit_contact(app: &mut Cursive, contact_to_edit: Option<Contact>) {
  2.     app.pop_layer();
  3.     let contact_clone = contact_to_edit.clone(); // make a local owned copy
  4.     let mut first_name_view = LinearLayout::horizontal();
  5.     first_name_view.add_child(TextView::new("First name: "));
  6.     first_name_view.add_child(EditView::new().with_name("first-name-edit").fixed_width(30));
  7.  
  8.     let mut last_name_view = LinearLayout::horizontal();
  9.     last_name_view.add_child(TextView::new("Last name: "));
  10.     last_name_view.add_child(EditView::new().with_name("last-name-edit").fixed_width(30));
  11.  
  12.     let mut email_view = LinearLayout::horizontal();
  13.     email_view.add_child(TextView::new("Email: "));
  14.     email_view.add_child(EditView::new().with_name("email-edit").fixed_width(30));
  15.  
  16.     let mut phone_view = LinearLayout::horizontal();
  17.     phone_view.add_child(TextView::new("Phone: "));
  18.     phone_view.add_child(EditView::new().with_name("phone-edit").fixed_width(30));
  19.  
  20.     let mut first_name_edit = first_name_view
  21.         .find_name::<EditView>("first-name-edit")
  22.         .unwrap();
  23.     let mut last_name_edit = last_name_view
  24.         .find_name::<EditView>("last-name-edit")
  25.         .unwrap();
  26.  
  27.     let mut email_edit = email_view.find_name::<EditView>("email-edit").unwrap();
  28.     let mut phone_edit = phone_view.find_name::<EditView>("phone-edit").unwrap();
  29.  
  30.     if let Some(c) = contact_to_edit {
  31.         first_name_edit.set_content(c.first_name);
  32.         last_name_edit.set_content(c.last_name);
  33.         email_edit.set_content(c.email);
  34.         phone_edit.set_content(c.phone);
  35.     }
  36.  
  37.     let mut layout = LinearLayout::vertical();
  38.     layout.add_child(first_name_view);
  39.     layout.add_child(last_name_view);
  40.     layout.add_child(phone_view);
  41.     layout.add_child(email_view);
  42.     app.add_layer(
  43.         Dialog::new()
  44.             .title("Add Contact")
  45.             .padding_lrtb(1, 1, 1, 0)
  46.             .content(layout)
  47.             .button("Ok", move |a| {
  48.                 let mut res = Contact {
  49.                     id: 0,
  50.                     first_name: a
  51.                         .find_name::<EditView>("first-name-edit")
  52.                         .unwrap()
  53.                         .get_content()
  54.                         .to_string(),
  55.                     last_name: a
  56.                         .find_name::<EditView>("last-name-edit")
  57.                         .unwrap()
  58.                         .get_content()
  59.                         .to_string(),
  60.                     phone: a
  61.                         .find_name::<EditView>("phone-edit")
  62.                         .unwrap()
  63.                         .get_content()
  64.                         .to_string(),
  65.                     email: a
  66.                         .find_name::<EditView>("email-edit")
  67.                         .unwrap()
  68.                         .get_content()
  69.                         .to_string(),
  70.                 };
  71.                 if let Some(c) = contact_clone.clone() {
  72.                     let res = Contact {
  73.                         id: c.id,
  74.                         first_name: res.first_name,
  75.                         last_name: res.last_name,
  76.                         phone: res.phone,
  77.                         email: res.email,
  78.                     };
  79.                     edit_product_from_file(res);
  80.                 } else {
  81.                     add_product_to_file(res);
  82.                 }
  83.  
  84.                 show_menu(a);
  85.             }),
  86.     );
  87. }
Advertisement
Add Comment
Please, Sign In to add comment