aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-03-18 06:03:39 +0000
committerAkshay <[email protected]>2020-03-18 06:03:39 +0000
commit0ed2a357ec1446dd03eba963d0e144ef3ebba25a (patch)
tree87c7ad368d374a72ed2e0f4e73ab026b6573b2b5 /src
parent49057cfdc6ff69114dbc5190d3ad70e26d74143c (diff)
serialize App into json with erased_serde
Diffstat (limited to 'src')
-rw-r--r--src/app.rs53
1 files changed, 46 insertions, 7 deletions
diff --git a/src/app.rs b/src/app.rs
index b7bae72..d3d36f9 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -1,24 +1,33 @@
1use std::f64; 1use std::f64;
2use std::fs::File;
3use std::io::prelude::*;
2 4
3use cursive::direction::{Absolute, Direction}; 5use cursive::direction::{Absolute, Direction};
4use cursive::event::{Event, EventResult, Key}; 6use cursive::event::{Event, EventResult, Key};
5use cursive::view::View; 7use cursive::view::View;
6use cursive::{Printer, Vec2}; 8use cursive::{Printer, Vec2};
7 9
10use chrono::NaiveDate;
11
8use crate::habit::{Bit, Count, Habit, HabitWrapper}; 12use crate::habit::{Bit, Count, Habit, HabitWrapper};
9use crate::CONFIGURATION; 13use crate::CONFIGURATION;
10 14
11#[derive(PartialEq)] 15use serde::Serialize;
16
17#[derive(PartialEq, Serialize)]
12pub enum ViewMode { 18pub enum ViewMode {
13 Day, 19 Day,
14 Month, 20 Month,
15 Year, 21 Year,
16} 22}
17 23
24#[derive(Serialize)]
18pub struct App { 25pub struct App {
19 habits: Vec<Box<dyn HabitWrapper>>, 26 habits: Vec<Box<dyn HabitWrapper>>,
20 view_mode: ViewMode,
21 focus: usize, 27 focus: usize,
28
29 #[serde(skip)]
30 view_mode: ViewMode,
22} 31}
23 32
24impl App { 33impl App {
@@ -30,16 +39,14 @@ impl App {
30 }; 39 };
31 } 40 }
32 41
33 pub fn add_habit(mut self, h: Box<dyn HabitWrapper>) -> Self { 42 pub fn add_habit(&mut self, h: Box<dyn HabitWrapper>) {
34 self.habits.push(h); 43 self.habits.push(h);
35 return self;
36 } 44 }
37 45
38 pub fn set_mode(mut self, set_mode: ViewMode) -> Self { 46 pub fn set_mode(&mut self, set_mode: ViewMode) {
39 if set_mode != self.view_mode { 47 if set_mode != self.view_mode {
40 self.view_mode = set_mode 48 self.view_mode = set_mode;
41 } 49 }
42 return self;
43 } 50 }
44 51
45 fn set_focus(&mut self, d: Absolute) { 52 fn set_focus(&mut self, d: Absolute) {
@@ -81,6 +88,7 @@ impl App {
81 88
82 return format!("{} completed, {} remaining", completed, remaining); 89 return format!("{} completed, {} remaining", completed, remaining);
83 } 90 }
91
84 fn max_size(&self) -> Vec2 { 92 fn max_size(&self) -> Vec2 {
85 let grid_width = CONFIGURATION.grid_width; 93 let grid_width = CONFIGURATION.grid_width;
86 let width = { 94 let width = {
@@ -101,6 +109,10 @@ impl App {
101 }; 109 };
102 Vec2::new(width, height) 110 Vec2::new(width, height)
103 } 111 }
112
113 // this function does IO
114 // TODO: convert this into non-blocking async function
115 fn save_state(&self) {}
104} 116}
105 117
106impl View for App { 118impl View for App {
@@ -161,6 +173,33 @@ impl View for App {
161 self.set_focus(Absolute::Down); 173 self.set_focus(Absolute::Down);
162 return EventResult::Consumed(None); 174 return EventResult::Consumed(None);
163 } 175 }
176 Event::Char('a') => {
177 let mut gymming = Count::new("gym", 5);
178 gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 11), 7);
179 gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 12), 8);
180 gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 13), 9);
181 gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 14), 10);
182 gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 15), 11);
183 self.add_habit(Box::new(gymming));
184 return EventResult::Consumed(None);
185 }
186 Event::Char('d') => {
187 self.habits.remove(self.focus);
188 return EventResult::Consumed(None);
189 }
190 Event::Char('w') => {
191 let j = serde_json::to_string_pretty(&self).unwrap();
192 let mut file = File::create("foo.txt").unwrap();
193 file.write_all(j.as_bytes()).unwrap();
194 return EventResult::Consumed(None);
195 }
196 Event::Char('q') => {
197 let j = serde_json::to_string_pretty(&self).unwrap();
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());
202 }
164 _ => self.habits[self.focus].on_event(e), 203 _ => self.habits[self.focus].on_event(e),
165 } 204 }
166 } 205 }