From 2a3be003015bac9c6a13549029b9fb4595e88384 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 20 Jul 2020 21:13:45 +0530 Subject: allow "untracking" of habits, closes #18 --- src/habit/bit.rs | 17 ++++++++++++++--- src/habit/count.rs | 7 +++++-- src/habit/prelude.rs | 1 + 3 files changed, 20 insertions(+), 5 deletions(-) (limited to 'src/habit') diff --git a/src/habit/bit.rs b/src/habit/bit.rs index 3386182..8fa14c2 100644 --- a/src/habit/bit.rs +++ b/src/habit/bit.rs @@ -100,11 +100,22 @@ impl Habit for Bit { fn goal(&self) -> u32 { return 1; } - fn modify(&mut self, date: NaiveDate, _: TrackEvent) { + fn modify(&mut self, date: NaiveDate, event: TrackEvent) { if let Some(val) = self.stats.get_mut(&date) { - *val = (val.0 ^ true).into(); + match event { + TrackEvent::Increment => *val = (val.0 ^ true).into(), + TrackEvent::Decrement => { + if val.0 { + *val = false.into(); + } else { + self.stats.remove(&date); + } + } + } } else { - self.insert_entry(date, CustomBool(true)); + if event == TrackEvent::Increment { + self.insert_entry(date, CustomBool(true)); + } } } fn set_view_month_offset(&mut self, offset: u32) { diff --git a/src/habit/count.rs b/src/habit/count.rs index 1bdf920..d351758 100644 --- a/src/habit/count.rs +++ b/src/habit/count.rs @@ -84,12 +84,15 @@ impl Habit for Count { if *val > 0 { *val -= 1 } else { - *val = 0 + self.stats.remove(&date); }; } } } else { - self.insert_entry(date, 1); + match event { + TrackEvent::Increment => self.insert_entry(date, 1), + _ => {} + }; } } fn set_view_month_offset(&mut self, offset: u32) { diff --git a/src/habit/prelude.rs b/src/habit/prelude.rs index 19b00a7..8335d6c 100644 --- a/src/habit/prelude.rs +++ b/src/habit/prelude.rs @@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize}; use std::default; use std::fmt; +#[derive(Debug, PartialEq)] pub enum TrackEvent { Increment, Decrement, -- cgit v1.2.3