meli/ui/src/components/contacts.rs

199 lines
5.8 KiB
Rust
Raw Normal View History

2019-02-15 09:06:42 +02:00
/*
* meli - contacts module
*
* Copyright 2019 Manos Pitsidianakis
*
* This file is part of meli.
*
* meli is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* meli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with meli. If not, see <http://www.gnu.org/licenses/>.
*/
use super::*;
2019-03-01 14:01:06 +02:00
use fnv::FnvHashMap;
2019-02-15 09:06:42 +02:00
2019-02-21 15:31:01 +02:00
mod contact_list;
pub use self::contact_list::*;
#[derive(Debug)]
enum ViewMode {
2019-03-03 22:11:15 +02:00
//ReadOnly,
2019-02-21 15:31:01 +02:00
Read,
2019-03-03 22:11:15 +02:00
//Edit,
//New,
2019-02-21 15:31:01 +02:00
}
2019-02-15 09:06:42 +02:00
#[derive(Debug)]
pub struct ContactManager {
2019-04-10 22:01:02 +03:00
id: ComponentId,
pub card: Card,
2019-02-21 15:31:01 +02:00
mode: ViewMode,
form: FormWidget,
2019-02-25 12:00:17 +02:00
account_pos: usize,
2019-02-15 09:06:42 +02:00
content: CellBuffer,
dirty: bool,
initialized: bool,
}
impl Default for ContactManager {
fn default() -> Self {
ContactManager {
2019-02-21 15:31:01 +02:00
id: Uuid::nil(),
card: Card::new(),
2019-02-21 15:31:01 +02:00
mode: ViewMode::Read,
form: FormWidget::default(),
2019-02-25 12:00:17 +02:00
account_pos: 0,
content: CellBuffer::new(200, 100, Cell::with_char(' ')),
2019-02-15 09:06:42 +02:00
dirty: true,
initialized: false,
}
}
}
impl fmt::Display for ContactManager {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "contacts")
}
}
impl ContactManager {
fn initialize(&mut self) {
2019-03-03 22:11:15 +02:00
let (width, _) = self.content.size();
2019-03-03 22:11:15 +02:00
let (x, _) = write_string_to_grid(
"Last edited: ",
&mut self.content,
Color::Byte(250),
Color::Default,
2019-07-06 20:36:59 +03:00
((0, 0), (width, 0)),
false,
2019-03-14 12:19:25 +02:00
);
2019-03-03 22:11:15 +02:00
write_string_to_grid(
&self.card.last_edited(),
&mut self.content,
Color::Byte(250),
Color::Default,
((x, 0), (width, 0)),
false,
2019-03-14 12:19:25 +02:00
);
self.form = FormWidget::new("Save".into());
self.form.add_button(("Cancel".into(), false));
2019-03-14 12:19:25 +02:00
self.form
2019-07-06 20:36:59 +03:00
.push(("NAME".into(), self.card.name().to_string()));
2019-03-14 12:19:25 +02:00
self.form.push((
2019-07-06 20:36:59 +03:00
"ADDITIONAL NAME".into(),
2019-03-14 12:19:25 +02:00
self.card.additionalname().to_string(),
));
self.form
2019-07-06 20:36:59 +03:00
.push(("NAME PREFIX".into(), self.card.name_prefix().to_string()));
2019-03-14 12:19:25 +02:00
self.form
2019-07-06 20:36:59 +03:00
.push(("NAME SUFFIX".into(), self.card.name_suffix().to_string()));
2019-03-14 12:19:25 +02:00
self.form
2019-07-06 20:36:59 +03:00
.push(("E-MAIL".into(), self.card.email().to_string()));
self.form.push(("URL".into(), self.card.url().to_string()));
self.form.push(("KEY".into(), self.card.key().to_string()));
}
2019-02-15 09:06:42 +02:00
}
impl Component for ContactManager {
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
if !self.initialized {
self.initialize();
2019-02-15 09:06:42 +02:00
self.initialized = true;
}
clear_area(grid, area);
2019-03-03 22:11:15 +02:00
let (width, _height) = self.content.size();
copy_area(grid, &self.content, area, ((0, 0), (width - 1, 0)));
let upper_left = upper_left!(area);
let bottom_right = bottom_right!(area);
2019-03-14 12:19:25 +02:00
self.form.draw(
grid,
(set_y(upper_left, get_y(upper_left) + 1), bottom_right),
context,
);
2019-02-15 09:06:42 +02:00
context.dirty_areas.push_back(area);
}
2019-02-26 17:50:47 +02:00
fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
if self.form.process_event(event, context) {
match self.form.buttons_result() {
2019-03-14 12:19:25 +02:00
None => {}
Some(true) => {
2019-06-18 21:13:58 +03:00
let fields = std::mem::replace(&mut self.form, FormWidget::default())
2019-03-14 12:19:25 +02:00
.collect()
.unwrap();
let fields: FnvHashMap<String, String> = fields
.into_iter()
.map(|(s, v)| {
(
s,
match v {
Field::Text(v, _) => v.as_str().to_string(),
2019-03-14 12:19:25 +02:00
Field::Choice(mut v, c) => v.remove(c),
},
)
})
.collect();
2019-03-01 14:01:06 +02:00
let mut new_card = Card::from(fields);
2019-02-25 12:00:17 +02:00
new_card.set_id(*self.card.id());
2019-03-14 12:19:25 +02:00
context.accounts[self.account_pos]
.address_book
.add_card(new_card);
2019-04-10 23:37:20 +03:00
context
.replies
.push_back(UIEvent::StatusEvent(StatusEvent::DisplayMessage(
2019-03-14 12:19:25 +02:00
"Saved.".into(),
2019-04-10 23:37:20 +03:00
)));
context.replies.push_back(UIEvent::ComponentKill(self.id));
2019-03-14 12:19:25 +02:00
}
Some(false) => {
2019-04-10 23:37:20 +03:00
context.replies.push_back(UIEvent::ComponentKill(self.id));
2019-03-14 12:19:25 +02:00
}
}
return true;
2019-02-21 15:31:01 +02:00
}
/*
2019-04-10 23:37:20 +03:00
match *event {
UIEvent::Input(Key::Char('\n')) => {
2019-03-14 12:19:25 +02:00
context.replies.push_back(UIEvent {
id: 0,
2019-04-10 23:37:20 +03:00
event_type: UIEvent::ComponentKill(self.id),
2019-03-14 12:19:25 +02:00
});
return true;
},
_ => {},
}
*/
2019-02-15 09:06:42 +02:00
false
}
fn is_dirty(&self) -> bool {
self.dirty | self.form.is_dirty()
2019-02-15 09:06:42 +02:00
}
fn set_dirty(&mut self) {
self.dirty = true;
self.initialized = false;
self.form.set_dirty();
2019-02-15 09:06:42 +02:00
}
2019-04-10 22:01:02 +03:00
fn id(&self) -> ComponentId {
self.id
}
fn set_id(&mut self, id: ComponentId) {
self.id = id;
2019-02-15 09:06:42 +02:00
}
}