diff options
author | Akshay <[email protected]> | 2020-03-11 12:50:54 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-03-11 12:50:54 +0000 |
commit | 076e09627e3579332954e9a4a74a275dab1feefd (patch) | |
tree | 3b50527734a92960dfe3648e8b21b188b295ab26 /src | |
parent | 05137cef78bd4342d0ae101c24baa03e4a843a64 (diff) |
switch to Trait implementation of Habit
Diffstat (limited to 'src')
-rw-r--r-- | src/app.rs | 44 |
1 files changed, 21 insertions, 23 deletions
@@ -5,7 +5,8 @@ use cursive::event::{Event, EventResult, Key}; | |||
5 | use cursive::view::View; | 5 | use cursive::view::View; |
6 | use cursive::{Printer, Vec2}; | 6 | use cursive::{Printer, Vec2}; |
7 | 7 | ||
8 | use crate::views::habitview::HabitView; | 8 | use crate::habit::{Bit, Count, Habit, HabitWrapper}; |
9 | use crate::CONFIGURATION; | ||
9 | 10 | ||
10 | #[derive(PartialEq)] | 11 | #[derive(PartialEq)] |
11 | pub enum ViewMode { | 12 | pub enum ViewMode { |
@@ -15,12 +16,9 @@ pub enum ViewMode { | |||
15 | } | 16 | } |
16 | 17 | ||
17 | pub struct App { | 18 | pub struct App { |
18 | habits: Vec<HabitView>, | 19 | habits: Vec<Box<dyn HabitWrapper>>, |
19 | view_mode: ViewMode, | 20 | view_mode: ViewMode, |
20 | focus: usize, | 21 | focus: usize, |
21 | grid_width: usize, | ||
22 | |||
23 | padding: usize, | ||
24 | } | 22 | } |
25 | 23 | ||
26 | impl App { | 24 | impl App { |
@@ -29,12 +27,10 @@ impl App { | |||
29 | habits: vec![], | 27 | habits: vec![], |
30 | view_mode: ViewMode::Day, | 28 | view_mode: ViewMode::Day, |
31 | focus: 0, | 29 | focus: 0, |
32 | grid_width: 3, | ||
33 | padding: 12, | ||
34 | }; | 30 | }; |
35 | } | 31 | } |
36 | 32 | ||
37 | pub fn add_habit(mut self, h: HabitView) -> Self { | 33 | pub fn add_habit(mut self, h: Box<dyn HabitWrapper>) -> Self { |
38 | self.habits.push(h); | 34 | self.habits.push(h); |
39 | return self; | 35 | return self; |
40 | } | 36 | } |
@@ -47,6 +43,7 @@ impl App { | |||
47 | } | 43 | } |
48 | 44 | ||
49 | fn set_focus(&mut self, d: Absolute) { | 45 | fn set_focus(&mut self, d: Absolute) { |
46 | let grid_width = CONFIGURATION.grid_width; | ||
50 | match d { | 47 | match d { |
51 | Absolute::Right => { | 48 | Absolute::Right => { |
52 | if self.focus != self.habits.len() - 1 { | 49 | if self.focus != self.habits.len() - 1 { |
@@ -59,15 +56,15 @@ impl App { | |||
59 | } | 56 | } |
60 | } | 57 | } |
61 | Absolute::Down => { | 58 | Absolute::Down => { |
62 | if self.focus + self.grid_width < self.habits.len() - 1 { | 59 | if self.focus + grid_width < self.habits.len() - 1 { |
63 | self.focus += self.grid_width; | 60 | self.focus += grid_width; |
64 | } else { | 61 | } else { |
65 | self.focus = self.habits.len() - 1; | 62 | self.focus = self.habits.len() - 1; |
66 | } | 63 | } |
67 | } | 64 | } |
68 | Absolute::Up => { | 65 | Absolute::Up => { |
69 | if self.focus as isize - self.grid_width as isize >= 0 { | 66 | if self.focus as isize - grid_width as isize >= 0 { |
70 | self.focus -= self.grid_width; | 67 | self.focus -= grid_width; |
71 | } else { | 68 | } else { |
72 | self.focus = 0; | 69 | self.focus = 0; |
73 | } | 70 | } |
@@ -77,24 +74,25 @@ impl App { | |||
77 | } | 74 | } |
78 | 75 | ||
79 | fn status(&self) -> String { | 76 | fn status(&self) -> String { |
80 | let remaining = self.habits.iter().map(|h| h.remaining()).sum::<u32>(); | 77 | let today = chrono::Local::now().naive_utc().date(); |
78 | let remaining = self.habits.iter().map(|h| h.remaining(today)).sum::<u32>(); | ||
81 | let total = self.habits.iter().map(|h| h.total()).sum::<u32>(); | 79 | let total = self.habits.iter().map(|h| h.total()).sum::<u32>(); |
82 | let completed = total - remaining; | 80 | let completed = total - remaining; |
83 | 81 | ||
84 | return format!("{}/{} ", completed, total); | 82 | return format!("{} completed, {} remaining", completed, remaining); |
85 | } | 83 | } |
86 | fn max_size(&self) -> Vec2 { | 84 | fn max_size(&self) -> Vec2 { |
87 | let grid_width = self.grid_width; | 85 | let grid_width = CONFIGURATION.grid_width; |
88 | let width = { | 86 | let width = { |
89 | if self.habits.len() > 0 { | 87 | if self.habits.len() > 0 { |
90 | grid_width * self.habits[0].get_size().x | 88 | grid_width * CONFIGURATION.view_width |
91 | } else { | 89 | } else { |
92 | 0 | 90 | 0 |
93 | } | 91 | } |
94 | }; | 92 | }; |
95 | let height = { | 93 | let height = { |
96 | if self.habits.len() > 0 { | 94 | if self.habits.len() > 0 { |
97 | (self.habits[0].get_size().y as f64 | 95 | (CONFIGURATION.view_height as f64 |
98 | * (self.habits.len() as f64 / grid_width as f64).ceil()) | 96 | * (self.habits.len() as f64 / grid_width as f64).ceil()) |
99 | as usize | 97 | as usize |
100 | } else { | 98 | } else { |
@@ -107,31 +105,31 @@ impl App { | |||
107 | 105 | ||
108 | impl View for App { | 106 | impl View for App { |
109 | fn draw(&self, printer: &Printer) { | 107 | fn draw(&self, printer: &Printer) { |
110 | let grid_width = self.grid_width; | 108 | let grid_width = CONFIGURATION.grid_width; |
111 | let mut offset = Vec2::zero(); | 109 | let mut offset = Vec2::zero(); |
112 | for (idx, i) in self.habits.iter().enumerate() { | 110 | for (idx, i) in self.habits.iter().enumerate() { |
113 | if idx >= grid_width && idx % grid_width == 0 { | 111 | if idx >= grid_width && idx % grid_width == 0 { |
114 | offset = offset.map_y(|y| y + i.get_size().y).map_x(|_| 0); | 112 | offset = offset.map_y(|y| y + CONFIGURATION.view_height).map_x(|_| 0); |
115 | } | 113 | } |
116 | i.draw(&printer.offset(offset).focused(self.focus == idx)); | 114 | i.draw(&printer.offset(offset).focused(self.focus == idx)); |
117 | offset = offset.map_x(|x| x + i.get_size().x); | 115 | offset = offset.map_x(|x| x + CONFIGURATION.view_width); |
118 | } | 116 | } |
119 | offset = offset.map_x(|_| 0).map_y(|_| self.max_size().y - 2); | 117 | offset = offset.map_x(|_| 0).map_y(|_| self.max_size().y - 2); |
120 | printer.print(offset, &self.status()); | 118 | printer.print(offset, &self.status()); |
121 | } | 119 | } |
122 | 120 | ||
123 | fn required_size(&mut self, _: Vec2) -> Vec2 { | 121 | fn required_size(&mut self, _: Vec2) -> Vec2 { |
124 | let grid_width = self.grid_width; | 122 | let grid_width = CONFIGURATION.grid_width; |
125 | let width = { | 123 | let width = { |
126 | if self.habits.len() > 0 { | 124 | if self.habits.len() > 0 { |
127 | grid_width * self.habits[0].get_size().x | 125 | grid_width * CONFIGURATION.view_width |
128 | } else { | 126 | } else { |
129 | 0 | 127 | 0 |
130 | } | 128 | } |
131 | }; | 129 | }; |
132 | let height = { | 130 | let height = { |
133 | if self.habits.len() > 0 { | 131 | if self.habits.len() > 0 { |
134 | (self.habits[0].get_size().y as f64 | 132 | (CONFIGURATION.view_height as f64 |
135 | * (self.habits.len() as f64 / grid_width as f64).ceil()) | 133 | * (self.habits.len() as f64 / grid_width as f64).ceil()) |
136 | as usize | 134 | as usize |
137 | } else { | 135 | } else { |