diff options
author | Akshay <[email protected]> | 2020-07-20 16:43:45 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-07-20 16:43:45 +0100 |
commit | 2a3be003015bac9c6a13549029b9fb4595e88384 (patch) | |
tree | 6d613a1b59ca277a82c8a688ec029774d10f31d5 /src | |
parent | 8b91a7c0b3d9bd4fac3b140f5bc8a40ac324289a (diff) |
allow "untracking" of habits, closes #18
Diffstat (limited to 'src')
-rw-r--r-- | src/habit/bit.rs | 17 | ||||
-rw-r--r-- | src/habit/count.rs | 7 | ||||
-rw-r--r-- | src/habit/prelude.rs | 1 |
3 files changed, 20 insertions, 5 deletions
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 { | |||
100 | fn goal(&self) -> u32 { | 100 | fn goal(&self) -> u32 { |
101 | return 1; | 101 | return 1; |
102 | } | 102 | } |
103 | fn modify(&mut self, date: NaiveDate, _: TrackEvent) { | 103 | fn modify(&mut self, date: NaiveDate, event: TrackEvent) { |
104 | if let Some(val) = self.stats.get_mut(&date) { | 104 | if let Some(val) = self.stats.get_mut(&date) { |
105 | *val = (val.0 ^ true).into(); | 105 | match event { |
106 | TrackEvent::Increment => *val = (val.0 ^ true).into(), | ||
107 | TrackEvent::Decrement => { | ||
108 | if val.0 { | ||
109 | *val = false.into(); | ||
110 | } else { | ||
111 | self.stats.remove(&date); | ||
112 | } | ||
113 | } | ||
114 | } | ||
106 | } else { | 115 | } else { |
107 | self.insert_entry(date, CustomBool(true)); | 116 | if event == TrackEvent::Increment { |
117 | self.insert_entry(date, CustomBool(true)); | ||
118 | } | ||
108 | } | 119 | } |
109 | } | 120 | } |
110 | fn set_view_month_offset(&mut self, offset: u32) { | 121 | 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 { | |||
84 | if *val > 0 { | 84 | if *val > 0 { |
85 | *val -= 1 | 85 | *val -= 1 |
86 | } else { | 86 | } else { |
87 | *val = 0 | 87 | self.stats.remove(&date); |
88 | }; | 88 | }; |
89 | } | 89 | } |
90 | } | 90 | } |
91 | } else { | 91 | } else { |
92 | self.insert_entry(date, 1); | 92 | match event { |
93 | TrackEvent::Increment => self.insert_entry(date, 1), | ||
94 | _ => {} | ||
95 | }; | ||
93 | } | 96 | } |
94 | } | 97 | } |
95 | fn set_view_month_offset(&mut self, offset: u32) { | 98 | 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}; | |||
2 | use std::default; | 2 | use std::default; |
3 | use std::fmt; | 3 | use std::fmt; |
4 | 4 | ||
5 | #[derive(Debug, PartialEq)] | ||
5 | pub enum TrackEvent { | 6 | pub enum TrackEvent { |
6 | Increment, | 7 | Increment, |
7 | Decrement, | 8 | Decrement, |