aboutsummaryrefslogtreecommitdiff
path: root/src/app.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/app.rs')
-rw-r--r--src/app.rs40
1 files changed, 27 insertions, 13 deletions
diff --git a/src/app.rs b/src/app.rs
index d3d36f9..710b25a 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -12,16 +12,22 @@ use chrono::NaiveDate;
12use crate::habit::{Bit, Count, Habit, HabitWrapper}; 12use crate::habit::{Bit, Count, Habit, HabitWrapper};
13use crate::CONFIGURATION; 13use crate::CONFIGURATION;
14 14
15use serde::Serialize; 15use serde::{Deserialize, Serialize};
16 16
17#[derive(PartialEq, Serialize)] 17#[derive(PartialEq, Serialize, Deserialize)]
18pub enum ViewMode { 18pub enum ViewMode {
19 Day, 19 Day,
20 Month, 20 Month,
21 Year, 21 Year,
22} 22}
23 23
24#[derive(Serialize)] 24impl std::default::Default for ViewMode {
25 fn default() -> Self {
26 ViewMode::Month
27 }
28}
29
30#[derive(Serialize, Deserialize)]
25pub struct App { 31pub 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
118impl View for App { 135impl 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),