aboutsummaryrefslogtreecommitdiff
path: root/src/habit
diff options
context:
space:
mode:
Diffstat (limited to 'src/habit')
-rw-r--r--src/habit/bit.rs25
-rw-r--r--src/habit/count.rs25
-rw-r--r--src/habit/mod.rs27
-rw-r--r--src/habit/traits.rs46
4 files changed, 60 insertions, 63 deletions
diff --git a/src/habit/bit.rs b/src/habit/bit.rs
index 2bbb0ac..da64ece 100644
--- a/src/habit/bit.rs
+++ b/src/habit/bit.rs
@@ -1,11 +1,12 @@
1use std::collections::HashMap; 1use std::collections::HashMap;
2use std::default::Default;
2 3
3use chrono::NaiveDate; 4use chrono::NaiveDate;
4use serde::{Deserialize, Serialize}; 5use serde::{Deserialize, Serialize};
5 6
6use crate::habit::prelude::default_auto; 7use crate::habit::prelude::default_auto;
7use crate::habit::traits::Habit; 8use crate::habit::traits::Habit;
8use crate::habit::{TrackEvent, ViewMode}; 9use crate::habit::{InnerData, TrackEvent};
9use crate::CONFIGURATION; 10use crate::CONFIGURATION;
10 11
11#[derive(Copy, Clone, Debug, Serialize, Deserialize)] 12#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
@@ -42,10 +43,7 @@ pub struct Bit {
42 auto: bool, 43 auto: bool,
43 44
44 #[serde(skip)] 45 #[serde(skip)]
45 view_month_offset: u32, 46 inner_data: InnerData,
46
47 #[serde(skip)]
48 view_mode: ViewMode,
49} 47}
50 48
51impl Bit { 49impl Bit {
@@ -55,8 +53,7 @@ impl Bit {
55 stats: HashMap::new(), 53 stats: HashMap::new(),
56 goal: CustomBool(true), 54 goal: CustomBool(true),
57 auto, 55 auto,
58 view_month_offset: 0, 56 inner_data: Default::default(),
59 view_mode: ViewMode::Day,
60 }; 57 };
61 } 58 }
62} 59}
@@ -118,17 +115,11 @@ impl Habit for Bit {
118 } 115 }
119 } 116 }
120 } 117 }
121 fn set_view_month_offset(&mut self, offset: u32) { 118 fn inner_data_ref(&self) -> &InnerData {
122 self.view_month_offset = offset; 119 &self.inner_data
123 }
124 fn view_month_offset(&self) -> u32 {
125 self.view_month_offset
126 }
127 fn set_view_mode(&mut self, mode: ViewMode) {
128 self.view_mode = mode;
129 } 120 }
130 fn view_mode(&self) -> ViewMode { 121 fn inner_data_mut_ref(&mut self) -> &mut InnerData {
131 self.view_mode 122 &mut self.inner_data
132 } 123 }
133 fn is_auto(&self) -> bool { 124 fn is_auto(&self) -> bool {
134 self.auto 125 self.auto
diff --git a/src/habit/count.rs b/src/habit/count.rs
index d351758..09fd399 100644
--- a/src/habit/count.rs
+++ b/src/habit/count.rs
@@ -1,11 +1,12 @@
1use std::collections::HashMap; 1use std::collections::HashMap;
2use std::default::Default;
2 3
3use chrono::NaiveDate; 4use chrono::NaiveDate;
4use serde::{Deserialize, Serialize}; 5use serde::{Deserialize, Serialize};
5 6
6use crate::habit::prelude::default_auto; 7use crate::habit::prelude::default_auto;
7use crate::habit::traits::Habit; 8use crate::habit::traits::Habit;
8use crate::habit::{TrackEvent, ViewMode}; 9use crate::habit::{InnerData, TrackEvent};
9 10
10#[derive(Debug, Serialize, Deserialize)] 11#[derive(Debug, Serialize, Deserialize)]
11pub struct Count { 12pub struct Count {
@@ -17,10 +18,7 @@ pub struct Count {
17 auto: bool, 18 auto: bool,
18 19
19 #[serde(skip)] 20 #[serde(skip)]
20 view_month_offset: u32, 21 inner_data: InnerData,
21
22 #[serde(skip)]
23 view_mode: ViewMode,
24} 22}
25 23
26impl Count { 24impl Count {
@@ -30,8 +28,7 @@ impl Count {
30 stats: HashMap::new(), 28 stats: HashMap::new(),
31 goal, 29 goal,
32 auto, 30 auto,
33 view_month_offset: 0, 31 inner_data: Default::default(),
34 view_mode: ViewMode::Day,
35 }; 32 };
36 } 33 }
37} 34}
@@ -95,17 +92,11 @@ impl Habit for Count {
95 }; 92 };
96 } 93 }
97 } 94 }
98 fn set_view_month_offset(&mut self, offset: u32) { 95 fn inner_data_ref(&self) -> &InnerData {
99 self.view_month_offset = offset; 96 &self.inner_data
100 }
101 fn view_month_offset(&self) -> u32 {
102 self.view_month_offset
103 }
104 fn set_view_mode(&mut self, mode: ViewMode) {
105 self.view_mode = mode;
106 } 97 }
107 fn view_mode(&self) -> ViewMode { 98 fn inner_data_mut_ref(&mut self) -> &mut InnerData {
108 self.view_mode 99 &mut self.inner_data
109 } 100 }
110 fn is_auto(&self) -> bool { 101 fn is_auto(&self) -> bool {
111 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 @@
1use std::default::Default;
2
1mod traits; 3mod traits;
2pub use traits::{Habit, HabitWrapper}; 4pub use traits::{Habit, HabitWrapper};
3 5
@@ -9,3 +11,28 @@ pub use bit::Bit;
9 11
10mod prelude; 12mod prelude;
11pub use prelude::{TrackEvent, ViewMode}; 13pub use prelude::{TrackEvent, ViewMode};
14
15use crate::app::Cursor;
16
17use cursive::direction::Absolute;
18
19#[derive(Debug, Default)]
20pub struct InnerData {
21 pub cursor: Cursor,
22 pub view_mode: ViewMode,
23}
24
25impl 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 74fd00b..24d941d 100644
--- a/src/habit/traits.rs
+++ b/src/habit/traits.rs
@@ -5,47 +5,41 @@ use cursive::{Printer, Vec2};
5 5
6use typetag; 6use typetag;
7 7
8use crate::habit::{Bit, Count, TrackEvent, ViewMode}; 8use crate::habit::{Bit, Count, InnerData, TrackEvent};
9use crate::views::ShadowView; 9use crate::views::ShadowView;
10 10
11pub trait Habit { 11pub trait Habit {
12 type HabitType; 12 type HabitType;
13 13
14 fn set_name(&mut self, name: impl AsRef<str>);
15 fn set_goal(&mut self, goal: Self::HabitType);
16 fn name(&self) -> String;
17 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;
18 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;
19 fn reached_goal(&self, date: NaiveDate) -> bool; 19 fn reached_goal(&self, date: NaiveDate) -> bool;
20 fn remaining(&self, date: NaiveDate) -> u32; 20 fn remaining(&self, date: NaiveDate) -> u32;
21 fn goal(&self) -> u32; 21 fn set_goal(&mut self, goal: Self::HabitType);
22 fn modify(&mut self, date: NaiveDate, event: TrackEvent); 22 fn set_name(&mut self, name: impl AsRef<str>);
23
24 fn set_view_month_offset(&mut self, offset: u32);
25 fn view_month_offset(&self) -> u32;
26 23
27 fn set_view_mode(&mut self, mode: ViewMode); 24 fn inner_data_ref(&self) -> &InnerData;
28 fn view_mode(&self) -> ViewMode; 25 fn inner_data_mut_ref(&mut self) -> &mut InnerData;
29 26
30 fn is_auto(&self) -> bool; 27 fn is_auto(&self) -> bool;
31} 28}
32 29
33#[typetag::serde(tag = "type")] 30#[typetag::serde(tag = "type")]
34pub trait HabitWrapper: erased_serde::Serialize { 31pub trait HabitWrapper: erased_serde::Serialize {
35 fn remaining(&self, date: NaiveDate) -> u32; 32 fn draw(&self, printer: &Printer);
36 fn goal(&self) -> u32; 33 fn goal(&self) -> u32;
37 fn modify(&mut self, date: NaiveDate, event: TrackEvent); 34 fn modify(&mut self, date: NaiveDate, event: TrackEvent);
38 fn draw(&self, printer: &Printer); 35 fn name(&self) -> String;
39 fn on_event(&mut self, event: Event) -> EventResult; 36 fn on_event(&mut self, event: Event) -> EventResult;
37 fn remaining(&self, date: NaiveDate) -> u32;
40 fn required_size(&mut self, _: Vec2) -> Vec2; 38 fn required_size(&mut self, _: Vec2) -> Vec2;
41 fn take_focus(&mut self, _: Direction) -> bool; 39 fn take_focus(&mut self, _: Direction) -> bool;
42 fn name(&self) -> String;
43
44 fn set_view_month_offset(&mut self, offset: u32);
45 fn view_month_offset(&self) -> u32;
46 40
47 fn set_view_mode(&mut self, mode: ViewMode); 41 fn inner_data_ref(&self) -> &InnerData;
48 fn view_mode(&self) -> ViewMode; 42 fn inner_data_mut_ref(&mut self) -> &mut InnerData;
49 43
50 fn is_auto(&self) -> bool; 44 fn is_auto(&self) -> bool;
51} 45}
@@ -81,17 +75,11 @@ macro_rules! auto_habit_impl {
81 fn name(&self) -> String { 75 fn name(&self) -> String {
82 Habit::name(self) 76 Habit::name(self)
83 } 77 }
84 fn set_view_month_offset(&mut self, offset: u32) { 78 fn inner_data_ref(&self) -> &InnerData {
85 Habit::set_view_month_offset(self, offset) 79 Habit::inner_data_ref(self)
86 }
87 fn view_month_offset(&self) -> u32 {
88 Habit::view_month_offset(self)
89 }
90 fn set_view_mode(&mut self, mode: ViewMode) {
91 Habit::set_view_mode(self, mode)
92 } 80 }
93 fn view_mode(&self) -> ViewMode { 81 fn inner_data_mut_ref(&mut self) -> &mut InnerData {
94 Habit::view_mode(self) 82 Habit::inner_data_mut_ref(self)
95 } 83 }
96 fn is_auto(&self) -> bool { 84 fn is_auto(&self) -> bool {
97 Habit::is_auto(self) 85 Habit::is_auto(self)