diff options
author | Akshay <[email protected]> | 2020-05-02 11:12:14 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-05-02 11:12:14 +0100 |
commit | b1581c83ff6157041b040cf7b05799dfb4f4a50b (patch) | |
tree | 94d12cbc2e2fdd0047a40fb78dbaea21d1cea5bb /src | |
parent | 87d24adff9019b40714c9482ad24be518676cbae (diff) |
wow its been a month huh
Diffstat (limited to 'src')
-rw-r--r-- | src/app.rs | 40 |
1 files changed, 27 insertions, 13 deletions
@@ -12,16 +12,22 @@ use chrono::NaiveDate; | |||
12 | use crate::habit::{Bit, Count, Habit, HabitWrapper}; | 12 | use crate::habit::{Bit, Count, Habit, HabitWrapper}; |
13 | use crate::CONFIGURATION; | 13 | use crate::CONFIGURATION; |
14 | 14 | ||
15 | use serde::Serialize; | 15 | use serde::{Deserialize, Serialize}; |
16 | 16 | ||
17 | #[derive(PartialEq, Serialize)] | 17 | #[derive(PartialEq, Serialize, Deserialize)] |
18 | pub enum ViewMode { | 18 | pub enum ViewMode { |
19 | Day, | 19 | Day, |
20 | Month, | 20 | Month, |
21 | Year, | 21 | Year, |
22 | } | 22 | } |
23 | 23 | ||
24 | #[derive(Serialize)] | 24 | impl std::default::Default for ViewMode { |
25 | fn default() -> Self { | ||
26 | ViewMode::Month | ||
27 | } | ||
28 | } | ||
29 | |||
30 | #[derive(Serialize, Deserialize)] | ||
25 | pub struct App { | 31 | pub struct App { |
26 | habits: Vec<Box<dyn HabitWrapper>>, | 32 | habits: Vec<Box<dyn HabitWrapper>>, |
27 | focus: usize, | 33 | focus: usize, |
@@ -110,9 +116,20 @@ impl App { | |||
110 | Vec2::new(width, height) | 116 | Vec2::new(width, height) |
111 | } | 117 | } |
112 | 118 | ||
119 | fn load_state() -> Self { | ||
120 | let mut file = File::open("foo.txt").unwrap(); | ||
121 | let mut j = String::new(); | ||
122 | file.read_to_string(&mut j); | ||
123 | return serde_json::from_str(&j).unwrap(); | ||
124 | } | ||
125 | |||
113 | // this function does IO | 126 | // this function does IO |
114 | // TODO: convert this into non-blocking async function | 127 | // TODO: convert this into non-blocking async function |
115 | fn save_state(&self) {} | 128 | fn save_state(&self) { |
129 | let j = serde_json::to_string_pretty(&self).unwrap(); | ||
130 | let mut file = File::create("foo.txt").unwrap(); | ||
131 | file.write_all(j.as_bytes()).unwrap(); | ||
132 | } | ||
116 | } | 133 | } |
117 | 134 | ||
118 | impl View for App { | 135 | impl View for App { |
@@ -175,11 +192,11 @@ impl View for App { | |||
175 | } | 192 | } |
176 | Event::Char('a') => { | 193 | Event::Char('a') => { |
177 | let mut gymming = Count::new("gym", 5); | 194 | let mut gymming = Count::new("gym", 5); |
178 | gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 11), 7); | 195 | gymming.insert_entry(NaiveDate::from_ymd(2020, 4, 11), 7); |
179 | gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 12), 8); | 196 | gymming.insert_entry(NaiveDate::from_ymd(2020, 4, 12), 8); |
180 | gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 13), 9); | 197 | gymming.insert_entry(NaiveDate::from_ymd(2020, 4, 13), 9); |
181 | gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 14), 10); | 198 | gymming.insert_entry(NaiveDate::from_ymd(2020, 4, 14), 10); |
182 | gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 15), 11); | 199 | gymming.insert_entry(NaiveDate::from_ymd(2020, 4, 15), 11); |
183 | self.add_habit(Box::new(gymming)); | 200 | self.add_habit(Box::new(gymming)); |
184 | return EventResult::Consumed(None); | 201 | return EventResult::Consumed(None); |
185 | } | 202 | } |
@@ -194,10 +211,7 @@ impl View for App { | |||
194 | return EventResult::Consumed(None); | 211 | return EventResult::Consumed(None); |
195 | } | 212 | } |
196 | Event::Char('q') => { | 213 | Event::Char('q') => { |
197 | let j = serde_json::to_string_pretty(&self).unwrap(); | 214 | self.save_state(); |
198 | let mut file = File::create("foo.txt").unwrap(); | ||
199 | file.write_all(j.as_bytes()).unwrap(); | ||
200 | |||
201 | return EventResult::with_cb(|s| s.quit()); | 215 | return EventResult::with_cb(|s| s.quit()); |
202 | } | 216 | } |
203 | _ => self.habits[self.focus].on_event(e), | 217 | _ => self.habits[self.focus].on_event(e), |