Implement the Edit Cancel button
This commit is contained in:
parent
ce8bed13f9
commit
251077b0c1
|
@ -233,8 +233,19 @@ impl DayEdit {
|
||||||
|
|
||||||
*s.imp().on_finished.borrow_mut() = Box::new(on_finished);
|
*s.imp().on_finished.borrow_mut() = Box::new(on_finished);
|
||||||
|
|
||||||
s.append(
|
s.append(&control_buttons(&s, &view_model));
|
||||||
&ActionGroup::builder()
|
s.append(&weight_and_steps_row(&view_model));
|
||||||
|
|
||||||
|
s
|
||||||
|
}
|
||||||
|
|
||||||
|
fn finish(&self) {
|
||||||
|
(self.imp().on_finished.borrow())()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn control_buttons(s: &DayEdit, view_model: &DayDetailViewModel) -> ActionGroup {
|
||||||
|
ActionGroup::builder()
|
||||||
.primary_action("Save", {
|
.primary_action("Save", {
|
||||||
let s = s.clone();
|
let s = s.clone();
|
||||||
let view_model = view_model.clone();
|
let view_model = view_model.clone();
|
||||||
|
@ -251,13 +262,14 @@ impl DayEdit {
|
||||||
s.finish();
|
s.finish();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.build(),
|
.build()
|
||||||
);
|
}
|
||||||
|
|
||||||
let top_row = gtk::Box::builder()
|
fn weight_and_steps_row(view_model: &DayDetailViewModel) -> gtk::Box {
|
||||||
|
let row = gtk::Box::builder()
|
||||||
.orientation(gtk::Orientation::Horizontal)
|
.orientation(gtk::Orientation::Horizontal)
|
||||||
.build();
|
.build();
|
||||||
top_row.append(
|
row.append(
|
||||||
&weight_field(view_model.weight(), {
|
&weight_field(view_model.weight(), {
|
||||||
let view_model = view_model.clone();
|
let view_model = view_model.clone();
|
||||||
move |w| match w {
|
move |w| match w {
|
||||||
|
@ -268,7 +280,7 @@ impl DayEdit {
|
||||||
.widget(),
|
.widget(),
|
||||||
);
|
);
|
||||||
|
|
||||||
top_row.append(
|
row.append(
|
||||||
&steps_editor(view_model.steps(), {
|
&steps_editor(view_model.steps(), {
|
||||||
let view_model = view_model.clone();
|
let view_model = view_model.clone();
|
||||||
move |s| match s {
|
move |s| match s {
|
||||||
|
@ -278,12 +290,6 @@ impl DayEdit {
|
||||||
})
|
})
|
||||||
.widget(),
|
.widget(),
|
||||||
);
|
);
|
||||||
s.append(&top_row);
|
|
||||||
|
|
||||||
s
|
row
|
||||||
}
|
|
||||||
|
|
||||||
fn finish(&self) {
|
|
||||||
(self.imp().on_finished.borrow())()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -95,43 +95,24 @@ pub struct DayDetailViewModel {
|
||||||
weight: Arc<RwLock<Option<RecordState<ft_core::Weight>>>>,
|
weight: Arc<RwLock<Option<RecordState<ft_core::Weight>>>>,
|
||||||
steps: Arc<RwLock<Option<RecordState<ft_core::Steps>>>>,
|
steps: Arc<RwLock<Option<RecordState<ft_core::Steps>>>>,
|
||||||
records: Arc<RwLock<HashMap<RecordId, RecordState<TraxRecord>>>>,
|
records: Arc<RwLock<HashMap<RecordId, RecordState<TraxRecord>>>>,
|
||||||
|
|
||||||
|
original_records: Vec<Record<TraxRecord>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DayDetailViewModel {
|
impl DayDetailViewModel {
|
||||||
pub fn new(date: chrono::NaiveDate, records: Vec<Record<TraxRecord>>, app: App) -> Self {
|
pub fn new(date: chrono::NaiveDate, records: Vec<Record<TraxRecord>>, app: App) -> Self {
|
||||||
let (weight_records, records): (Vec<Record<TraxRecord>>, Vec<Record<TraxRecord>>) =
|
let s = Self {
|
||||||
records.into_iter().partition(|r| r.data.is_weight());
|
|
||||||
let (step_records, records): (Vec<Record<TraxRecord>>, Vec<Record<TraxRecord>>) =
|
|
||||||
records.into_iter().partition(|r| r.data.is_steps());
|
|
||||||
Self {
|
|
||||||
app: Some(app),
|
app: Some(app),
|
||||||
date,
|
date,
|
||||||
weight: Arc::new(RwLock::new(
|
|
||||||
weight_records
|
|
||||||
.first()
|
|
||||||
.and_then(|r| match r.data {
|
|
||||||
TraxRecord::Weight(ref w) => Some((r.id.clone(), w.clone())),
|
|
||||||
_ => None,
|
|
||||||
})
|
|
||||||
.map(|(id, w)| RecordState::Original(Record { id, data: w })),
|
|
||||||
)),
|
|
||||||
steps: Arc::new(RwLock::new(
|
|
||||||
step_records
|
|
||||||
.first()
|
|
||||||
.and_then(|r| match r.data {
|
|
||||||
TraxRecord::Steps(ref w) => Some((r.id.clone(), w.clone())),
|
|
||||||
_ => None,
|
|
||||||
})
|
|
||||||
.map(|(id, w)| RecordState::Original(Record { id, data: w })),
|
|
||||||
)),
|
|
||||||
|
|
||||||
records: Arc::new(RwLock::new(
|
weight: Arc::new(RwLock::new(None)),
|
||||||
records
|
steps: Arc::new(RwLock::new(None)),
|
||||||
.into_iter()
|
records: Arc::new(RwLock::new(HashMap::new())),
|
||||||
.map(|r| (r.id.clone(), RecordState::Original(r)))
|
|
||||||
.collect::<HashMap<RecordId, RecordState<TraxRecord>>>(),
|
original_records: records,
|
||||||
)),
|
};
|
||||||
}
|
s.populate_records();
|
||||||
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn weight(&self) -> Option<WeightFormatter> {
|
pub fn weight(&self) -> Option<WeightFormatter> {
|
||||||
|
@ -275,7 +256,37 @@ impl DayDetailViewModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn revert(&self) {
|
pub fn revert(&self) {
|
||||||
unimplemented!();
|
self.populate_records();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn populate_records(&self) {
|
||||||
|
let records = self.original_records.clone();
|
||||||
|
|
||||||
|
let (weight_records, records): (Vec<Record<TraxRecord>>, Vec<Record<TraxRecord>>) =
|
||||||
|
records.into_iter().partition(|r| r.data.is_weight());
|
||||||
|
let (step_records, records): (Vec<Record<TraxRecord>>, Vec<Record<TraxRecord>>) =
|
||||||
|
records.into_iter().partition(|r| r.data.is_steps());
|
||||||
|
|
||||||
|
*self.weight.write().unwrap() = weight_records
|
||||||
|
.first()
|
||||||
|
.and_then(|r| match r.data {
|
||||||
|
TraxRecord::Weight(ref w) => Some((r.id.clone(), w.clone())),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.map(|(id, w)| RecordState::Original(Record { id, data: w }));
|
||||||
|
|
||||||
|
*self.steps.write().unwrap() = step_records
|
||||||
|
.first()
|
||||||
|
.and_then(|r| match r.data {
|
||||||
|
TraxRecord::Steps(ref w) => Some((r.id.clone(), w.clone())),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.map(|(id, w)| RecordState::Original(Record { id, data: w }));
|
||||||
|
|
||||||
|
*self.records.write().unwrap() = records
|
||||||
|
.into_iter()
|
||||||
|
.map(|r| (r.id.clone(), RecordState::Original(r)))
|
||||||
|
.collect::<HashMap<RecordId, RecordState<TraxRecord>>>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue