diff options
author | Akshay <[email protected]> | 2021-02-06 13:30:40 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2021-02-06 13:30:40 +0000 |
commit | 77368eed146ec8278609e04e395c7ef04c386313 (patch) | |
tree | 440eed647438cd8de03e91609e1daa43caf04bca /src/habit | |
parent | 9cdef4e296c77fb94d99553de05ba1aaa6c81ed8 (diff) |
deprecate view_month_offset in favor of cursor
Diffstat (limited to 'src/habit')
-rw-r--r-- | src/habit/bit.rs | 37 | ||||
-rw-r--r-- | src/habit/count.rs | 37 | ||||
-rw-r--r-- | src/habit/mod.rs | 27 | ||||
-rw-r--r-- | src/habit/traits.rs | 61 |
4 files changed, 61 insertions, 101 deletions
diff --git a/src/habit/bit.rs b/src/habit/bit.rs index 7fe6fd9..da64ece 100644 --- a/src/habit/bit.rs +++ b/src/habit/bit.rs | |||
@@ -1,13 +1,12 @@ | |||
1 | use std::collections::HashMap; | 1 | use std::collections::HashMap; |
2 | use std::default::Default; | ||
2 | 3 | ||
3 | use chrono::NaiveDate; | 4 | use chrono::NaiveDate; |
4 | use cursive::direction::Absolute; | ||
5 | use serde::{Deserialize, Serialize}; | 5 | use serde::{Deserialize, Serialize}; |
6 | 6 | ||
7 | use crate::app::Cursor; | ||
8 | use crate::habit::prelude::default_auto; | 7 | use crate::habit::prelude::default_auto; |
9 | use crate::habit::traits::Habit; | 8 | use crate::habit::traits::Habit; |
10 | use crate::habit::{TrackEvent, ViewMode}; | 9 | use crate::habit::{InnerData, TrackEvent}; |
11 | use crate::CONFIGURATION; | 10 | use crate::CONFIGURATION; |
12 | 11 | ||
13 | #[derive(Copy, Clone, Debug, Serialize, Deserialize)] | 12 | #[derive(Copy, Clone, Debug, Serialize, Deserialize)] |
@@ -44,13 +43,7 @@ pub struct Bit { | |||
44 | auto: bool, | 43 | auto: bool, |
45 | 44 | ||
46 | #[serde(skip)] | 45 | #[serde(skip)] |
47 | view_month_offset: u32, | 46 | inner_data: InnerData, |
48 | |||
49 | #[serde(skip)] | ||
50 | cursor: Cursor, | ||
51 | |||
52 | #[serde(skip)] | ||
53 | view_mode: ViewMode, | ||
54 | } | 47 | } |
55 | 48 | ||
56 | impl Bit { | 49 | impl Bit { |
@@ -60,9 +53,7 @@ impl Bit { | |||
60 | stats: HashMap::new(), | 53 | stats: HashMap::new(), |
61 | goal: CustomBool(true), | 54 | goal: CustomBool(true), |
62 | auto, | 55 | auto, |
63 | view_month_offset: 0, | 56 | inner_data: Default::default(), |
64 | cursor: Cursor::new(), | ||
65 | view_mode: ViewMode::Day, | ||
66 | }; | 57 | }; |
67 | } | 58 | } |
68 | } | 59 | } |
@@ -124,23 +115,11 @@ impl Habit for Bit { | |||
124 | } | 115 | } |
125 | } | 116 | } |
126 | } | 117 | } |
127 | fn set_view_month_offset(&mut self, offset: u32) { | 118 | fn inner_data_ref(&self) -> &InnerData { |
128 | self.view_month_offset = offset; | 119 | &self.inner_data |
129 | } | ||
130 | fn view_month_offset(&self) -> u32 { | ||
131 | self.view_month_offset | ||
132 | } | ||
133 | fn move_cursor(&mut self, d: Absolute) { | ||
134 | self.cursor.do_move(d); | ||
135 | } | ||
136 | fn cursor(&self) -> Cursor { | ||
137 | self.cursor | ||
138 | } | ||
139 | fn set_view_mode(&mut self, mode: ViewMode) { | ||
140 | self.view_mode = mode; | ||
141 | } | 120 | } |
142 | fn view_mode(&self) -> ViewMode { | 121 | fn inner_data_mut_ref(&mut self) -> &mut InnerData { |
143 | self.view_mode | 122 | &mut self.inner_data |
144 | } | 123 | } |
145 | fn is_auto(&self) -> bool { | 124 | fn is_auto(&self) -> bool { |
146 | self.auto | 125 | self.auto |
diff --git a/src/habit/count.rs b/src/habit/count.rs index b14354c..09fd399 100644 --- a/src/habit/count.rs +++ b/src/habit/count.rs | |||
@@ -1,13 +1,12 @@ | |||
1 | use std::collections::HashMap; | 1 | use std::collections::HashMap; |
2 | use std::default::Default; | ||
2 | 3 | ||
3 | use chrono::NaiveDate; | 4 | use chrono::NaiveDate; |
4 | use cursive::direction::Absolute; | ||
5 | use serde::{Deserialize, Serialize}; | 5 | use serde::{Deserialize, Serialize}; |
6 | 6 | ||
7 | use crate::app::Cursor; | ||
8 | use crate::habit::prelude::default_auto; | 7 | use crate::habit::prelude::default_auto; |
9 | use crate::habit::traits::Habit; | 8 | use crate::habit::traits::Habit; |
10 | use crate::habit::{TrackEvent, ViewMode}; | 9 | use crate::habit::{InnerData, TrackEvent}; |
11 | 10 | ||
12 | #[derive(Debug, Serialize, Deserialize)] | 11 | #[derive(Debug, Serialize, Deserialize)] |
13 | pub struct Count { | 12 | pub struct Count { |
@@ -19,13 +18,7 @@ pub struct Count { | |||
19 | auto: bool, | 18 | auto: bool, |
20 | 19 | ||
21 | #[serde(skip)] | 20 | #[serde(skip)] |
22 | view_month_offset: u32, | 21 | inner_data: InnerData, |
23 | |||
24 | #[serde(skip)] | ||
25 | cursor: Cursor, | ||
26 | |||
27 | #[serde(skip)] | ||
28 | view_mode: ViewMode, | ||
29 | } | 22 | } |
30 | 23 | ||
31 | impl Count { | 24 | impl Count { |
@@ -35,9 +28,7 @@ impl Count { | |||
35 | stats: HashMap::new(), | 28 | stats: HashMap::new(), |
36 | goal, | 29 | goal, |
37 | auto, | 30 | auto, |
38 | view_month_offset: 0, | 31 | inner_data: Default::default(), |
39 | cursor: Cursor::new(), | ||
40 | view_mode: ViewMode::Day, | ||
41 | }; | 32 | }; |
42 | } | 33 | } |
43 | } | 34 | } |
@@ -101,23 +92,11 @@ impl Habit for Count { | |||
101 | }; | 92 | }; |
102 | } | 93 | } |
103 | } | 94 | } |
104 | fn set_view_month_offset(&mut self, offset: u32) { | 95 | fn inner_data_ref(&self) -> &InnerData { |
105 | self.view_month_offset = offset; | 96 | &self.inner_data |
106 | } | ||
107 | fn view_month_offset(&self) -> u32 { | ||
108 | self.view_month_offset | ||
109 | } | ||
110 | fn move_cursor(&mut self, d: Absolute) { | ||
111 | self.cursor.do_move(d); | ||
112 | } | ||
113 | fn cursor(&self) -> Cursor { | ||
114 | self.cursor | ||
115 | } | ||
116 | fn set_view_mode(&mut self, mode: ViewMode) { | ||
117 | self.view_mode = mode; | ||
118 | } | 97 | } |
119 | fn view_mode(&self) -> ViewMode { | 98 | fn inner_data_mut_ref(&mut self) -> &mut InnerData { |
120 | self.view_mode | 99 | &mut self.inner_data |
121 | } | 100 | } |
122 | fn is_auto(&self) -> bool { | 101 | fn is_auto(&self) -> bool { |
123 | self.auto | 102 | self.auto |
diff --git a/src/habit/mod.rs b/src/habit/mod.rs index 75e734a..d51abe5 100644 --- a/src/habit/mod.rs +++ b/src/habit/mod.rs | |||
@@ -1,3 +1,5 @@ | |||
1 | use std::default::Default; | ||
2 | |||
1 | mod traits; | 3 | mod traits; |
2 | pub use traits::{Habit, HabitWrapper}; | 4 | pub use traits::{Habit, HabitWrapper}; |
3 | 5 | ||
@@ -9,3 +11,28 @@ pub use bit::Bit; | |||
9 | 11 | ||
10 | mod prelude; | 12 | mod prelude; |
11 | pub use prelude::{TrackEvent, ViewMode}; | 13 | pub use prelude::{TrackEvent, ViewMode}; |
14 | |||
15 | use crate::app::Cursor; | ||
16 | |||
17 | use cursive::direction::Absolute; | ||
18 | |||
19 | #[derive(Debug, Default)] | ||
20 | pub struct InnerData { | ||
21 | pub cursor: Cursor, | ||
22 | pub view_mode: ViewMode, | ||
23 | } | ||
24 | |||
25 | impl InnerData { | ||
26 | pub fn move_cursor(&mut self, d: Absolute) { | ||
27 | self.cursor.small_seek(d); | ||
28 | } | ||
29 | pub fn cursor(&self) -> Cursor { | ||
30 | self.cursor | ||
31 | } | ||
32 | pub fn set_view_mode(&mut self, mode: ViewMode) { | ||
33 | self.view_mode = mode; | ||
34 | } | ||
35 | pub fn view_mode(&self) -> ViewMode { | ||
36 | self.view_mode | ||
37 | } | ||
38 | } | ||
diff --git a/src/habit/traits.rs b/src/habit/traits.rs index 289fd95..24d941d 100644 --- a/src/habit/traits.rs +++ b/src/habit/traits.rs | |||
@@ -1,58 +1,45 @@ | |||
1 | use chrono::NaiveDate; | 1 | use chrono::NaiveDate; |
2 | use cursive::direction::{Absolute, Direction}; | 2 | use cursive::direction::Direction; |
3 | use cursive::event::{Event, EventResult}; | 3 | use cursive::event::{Event, EventResult}; |
4 | use cursive::{Printer, Vec2}; | 4 | use cursive::{Printer, Vec2}; |
5 | 5 | ||
6 | use typetag; | 6 | use typetag; |
7 | 7 | ||
8 | use crate::app::Cursor; | 8 | use crate::habit::{Bit, Count, InnerData, TrackEvent}; |
9 | use crate::habit::{Bit, Count, TrackEvent, ViewMode}; | ||
10 | use crate::views::ShadowView; | 9 | use crate::views::ShadowView; |
11 | 10 | ||
12 | pub trait Habit { | 11 | pub trait Habit { |
13 | type HabitType; | 12 | type HabitType; |
14 | 13 | ||
15 | fn set_name(&mut self, name: impl AsRef<str>); | ||
16 | fn set_goal(&mut self, goal: Self::HabitType); | ||
17 | fn name(&self) -> String; | ||
18 | fn get_by_date(&self, date: NaiveDate) -> Option<&Self::HabitType>; | 14 | fn get_by_date(&self, date: NaiveDate) -> Option<&Self::HabitType>; |
15 | fn goal(&self) -> u32; | ||
19 | fn insert_entry(&mut self, date: NaiveDate, val: Self::HabitType); | 16 | fn insert_entry(&mut self, date: NaiveDate, val: Self::HabitType); |
17 | fn modify(&mut self, date: NaiveDate, event: TrackEvent); | ||
18 | fn name(&self) -> String; | ||
20 | fn reached_goal(&self, date: NaiveDate) -> bool; | 19 | fn reached_goal(&self, date: NaiveDate) -> bool; |
21 | fn remaining(&self, date: NaiveDate) -> u32; | 20 | fn remaining(&self, date: NaiveDate) -> u32; |
22 | fn goal(&self) -> u32; | 21 | fn set_goal(&mut self, goal: Self::HabitType); |
23 | fn modify(&mut self, date: NaiveDate, event: TrackEvent); | 22 | fn set_name(&mut self, name: impl AsRef<str>); |
24 | |||
25 | fn set_view_month_offset(&mut self, offset: u32); | ||
26 | fn view_month_offset(&self) -> u32; | ||
27 | |||
28 | fn move_cursor(&mut self, d: Absolute); | ||
29 | fn cursor(&self) -> Cursor; | ||
30 | 23 | ||
31 | fn set_view_mode(&mut self, mode: ViewMode); | 24 | fn inner_data_ref(&self) -> &InnerData; |
32 | fn view_mode(&self) -> ViewMode; | 25 | fn inner_data_mut_ref(&mut self) -> &mut InnerData; |
33 | 26 | ||
34 | fn is_auto(&self) -> bool; | 27 | fn is_auto(&self) -> bool; |
35 | } | 28 | } |
36 | 29 | ||
37 | #[typetag::serde(tag = "type")] | 30 | #[typetag::serde(tag = "type")] |
38 | pub trait HabitWrapper: erased_serde::Serialize { | 31 | pub trait HabitWrapper: erased_serde::Serialize { |
39 | fn remaining(&self, date: NaiveDate) -> u32; | 32 | fn draw(&self, printer: &Printer); |
40 | fn goal(&self) -> u32; | 33 | fn goal(&self) -> u32; |
41 | fn modify(&mut self, date: NaiveDate, event: TrackEvent); | 34 | fn modify(&mut self, date: NaiveDate, event: TrackEvent); |
42 | fn draw(&self, printer: &Printer); | 35 | fn name(&self) -> String; |
43 | fn on_event(&mut self, event: Event) -> EventResult; | 36 | fn on_event(&mut self, event: Event) -> EventResult; |
37 | fn remaining(&self, date: NaiveDate) -> u32; | ||
44 | fn required_size(&mut self, _: Vec2) -> Vec2; | 38 | fn required_size(&mut self, _: Vec2) -> Vec2; |
45 | fn take_focus(&mut self, _: Direction) -> bool; | 39 | fn take_focus(&mut self, _: Direction) -> bool; |
46 | fn name(&self) -> String; | ||
47 | 40 | ||
48 | fn set_view_month_offset(&mut self, offset: u32); | 41 | fn inner_data_ref(&self) -> &InnerData; |
49 | fn view_month_offset(&self) -> u32; | 42 | fn inner_data_mut_ref(&mut self) -> &mut InnerData; |
50 | |||
51 | fn move_cursor(&mut self, d: Absolute); | ||
52 | fn cursor(&self) -> Cursor; | ||
53 | |||
54 | fn set_view_mode(&mut self, mode: ViewMode); | ||
55 | fn view_mode(&self) -> ViewMode; | ||
56 | 43 | ||
57 | fn is_auto(&self) -> bool; | 44 | fn is_auto(&self) -> bool; |
58 | } | 45 | } |
@@ -88,23 +75,11 @@ macro_rules! auto_habit_impl { | |||
88 | fn name(&self) -> String { | 75 | fn name(&self) -> String { |
89 | Habit::name(self) | 76 | Habit::name(self) |
90 | } | 77 | } |
91 | fn set_view_month_offset(&mut self, offset: u32) { | 78 | fn inner_data_ref(&self) -> &InnerData { |
92 | Habit::set_view_month_offset(self, offset) | 79 | Habit::inner_data_ref(self) |
93 | } | ||
94 | fn view_month_offset(&self) -> u32 { | ||
95 | Habit::view_month_offset(self) | ||
96 | } | ||
97 | fn move_cursor(&mut self, d: Absolute) { | ||
98 | Habit::move_cursor(self, d) | ||
99 | } | ||
100 | fn cursor(&self) -> Cursor { | ||
101 | Habit::cursor(self) | ||
102 | } | ||
103 | fn set_view_mode(&mut self, mode: ViewMode) { | ||
104 | Habit::set_view_mode(self, mode) | ||
105 | } | 80 | } |
106 | fn view_mode(&self) -> ViewMode { | 81 | fn inner_data_mut_ref(&mut self) -> &mut InnerData { |
107 | Habit::view_mode(self) | 82 | Habit::inner_data_mut_ref(self) |
108 | } | 83 | } |
109 | fn is_auto(&self) -> bool { | 84 | fn is_auto(&self) -> bool { |
110 | Habit::is_auto(self) | 85 | Habit::is_auto(self) |