From ce6db76dc3d8f2f8b43a2e8769cf17416e57a065 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 11 Jul 2020 22:17:45 +0530 Subject: begin work on month mode --- src/app.rs | 29 ++++++----------------------- src/habit.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++--- src/views.rs | 51 +++++++++++++++++++++++++++++++-------------------- 3 files changed, 87 insertions(+), 46 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index 82096e1..f39ae3c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -9,26 +9,13 @@ use cursive::{Printer, Vec2}; use chrono::{Local, NaiveDate}; -use crate::habit::{Bit, Count, Habit, HabitWrapper}; +use crate::habit::{Bit, Count, Habit, HabitWrapper, ViewMode}; use crate::utils; use crate::Command; use crate::CONFIGURATION; use serde::{Deserialize, Serialize}; -#[derive(PartialEq, Serialize, Deserialize)] -pub enum ViewMode { - Day, - Month, - Year, -} - -impl std::default::Default for ViewMode { - fn default() -> Self { - ViewMode::Month - } -} - struct StatusLine(String, String); #[derive(Serialize, Deserialize)] @@ -40,9 +27,6 @@ pub struct App { #[serde(skip)] focus: usize, - #[serde(skip)] - view_mode: ViewMode, - #[serde(skip)] view_month_offset: u32, } @@ -51,7 +35,6 @@ impl App { pub fn new() -> Self { return App { habits: vec![], - view_mode: ViewMode::Day, focus: 0, view_month_offset: 0, }; @@ -65,9 +48,9 @@ impl App { self.habits.retain(|h| h.get_name() != name); } - pub fn set_mode(&mut self, set_mode: ViewMode) { - if set_mode != self.view_mode { - self.view_mode = set_mode; + pub fn set_mode(&mut self, mode: ViewMode) { + if !self.habits.is_empty() { + self.habits[self.focus].set_view_mode(mode); } } @@ -233,8 +216,8 @@ impl View for App { } offset = offset.map_x(|_| 0).map_y(|_| self.max_size().y - 2); - printer.print(offset, &self.status().1); // right - printer.print(offset, &self.status().0); // left + printer.print(offset, &self.status().1); // right status + printer.print(offset, &self.status().0); // left status } fn required_size(&mut self, _: Vec2) -> Vec2 { diff --git a/src/habit.rs b/src/habit.rs index 48dd363..92e0b9f 100644 --- a/src/habit.rs +++ b/src/habit.rs @@ -15,6 +15,19 @@ pub enum TrackEvent { Decrement, } +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub enum ViewMode { + Day, + Month, + Year, +} + +impl std::default::Default for ViewMode { + fn default() -> Self { + ViewMode::Day + } +} + #[derive(Copy, Clone, Debug, Serialize, Deserialize)] pub struct CustomBool(bool); @@ -51,8 +64,12 @@ pub trait Habit { fn remaining(&self, date: NaiveDate) -> u32; fn total(&self) -> u32; fn modify(&mut self, date: NaiveDate, event: TrackEvent); + fn set_view_month_offset(&mut self, offset: u32); fn view_month_offset(&self) -> u32; + + fn set_view_mode(&mut self, mode: ViewMode); + fn view_mode(&self) -> ViewMode; } #[typetag::serde(tag = "type")] @@ -64,9 +81,13 @@ pub trait HabitWrapper: erased_serde::Serialize { fn on_event(&mut self, event: Event) -> EventResult; fn required_size(&mut self, _: Vec2) -> Vec2; fn take_focus(&mut self, _: Direction) -> bool; + fn get_name(&self) -> String; + fn set_view_month_offset(&mut self, offset: u32); fn view_month_offset(&self) -> u32; - fn get_name(&self) -> String; + + fn set_view_mode(&mut self, mode: ViewMode); + fn view_mode(&self) -> ViewMode; } macro_rules! auto_habit_impl { @@ -97,11 +118,17 @@ macro_rules! auto_habit_impl { fn set_view_month_offset(&mut self, offset: u32) { Habit::set_view_month_offset(self, offset) } + fn get_name(&self) -> String { + Habit::name(self) + } fn view_month_offset(&self) -> u32 { Habit::view_month_offset(self) } - fn get_name(&self) -> String { - Habit::name(self) + fn set_view_mode(&mut self, mode: ViewMode) { + Habit::set_view_mode(&mut self, mode: ViewMode) + } + fn view_mode(&self) -> ViewMode { + Habit::view_mode(self) } } }; @@ -118,6 +145,9 @@ pub struct Count { #[serde(skip)] view_month_offset: u32, + + #[serde(skip)] + view_mode: ViewMode, } impl Count { @@ -127,6 +157,7 @@ impl Count { stats: HashMap::new(), goal, view_month_offset: 0, + view_mode: ViewMode::Day, }; } } @@ -193,6 +224,12 @@ impl Habit for Count { fn view_month_offset(&self) -> u32 { self.view_month_offset } + fn set_view_mode(&mut self, mode: ViewMode) { + self.view_mode = mode; + } + fn view_mode(&self) -> ViewMode { + self.view_mode + } } #[derive(Debug, Serialize, Deserialize)] @@ -203,6 +240,9 @@ pub struct Bit { #[serde(skip)] view_month_offset: u32, + + #[serde(skip)] + view_mode: ViewMode, } impl Bit { @@ -212,6 +252,7 @@ impl Bit { stats: HashMap::new(), goal: CustomBool(true), view_month_offset: 0, + view_mode: ViewMode::Day, }; } } @@ -268,4 +309,10 @@ impl Habit for Bit { fn view_month_offset(&self) -> u32 { self.view_month_offset } + fn set_view_mode(&mut self, mode: ViewMode) { + self.view_mode = mode; + } + fn view_mode(&self) -> ViewMode { + self.view_mode + } } diff --git a/src/views.rs b/src/views.rs index d25e59b..facbd55 100644 --- a/src/views.rs +++ b/src/views.rs @@ -7,7 +7,7 @@ use cursive::{Printer, Vec2}; use chrono::prelude::*; use chrono::{Duration, Local, NaiveDate}; -use crate::habit::{Bit, Count, Habit, TrackEvent}; +use crate::habit::{Bit, Count, Habit, TrackEvent, ViewMode}; use crate::CONFIGURATION; pub trait ShadowView { @@ -62,26 +62,37 @@ where }, ); - let mut i = 1; - while let Some(d) = NaiveDate::from_ymd_opt(year, month, i) { - let day_style; - if self.reached_goal(d) { - day_style = goal_reached_style; - } else { - day_style = todo_style; - } - let coords: Vec2 = ((i % 7) * 3, i / 7 + 2).into(); - if let Some(c) = self.get_by_date(d) { - printer.with_style(day_style, |p| { - p.print(coords, &format!("{:^3}", c)); - }); - } else { - printer.with_style(future_style, |p| { - p.print(coords, &format!("{:^3}", CONFIGURATION.future_chr)); - }); + // fn draw_day(&self, p: &Printer) { }; + // fn draw_month(&self, p: &Printer) {}; + + // match self.view_mode() { + // ViewMode::Day => draw_day(self, p), + // ViewMode::Month => + // } + + let draw_day = |p: &Printer| { + let mut i = 1; + while let Some(d) = NaiveDate::from_ymd_opt(year, month, i) { + let day_style; + if self.reached_goal(d) { + day_style = goal_reached_style; + } else { + day_style = todo_style; + } + let coords: Vec2 = ((i % 7) * 3, i / 7 + 2).into(); + if let Some(c) = self.get_by_date(d) { + printer.with_style(day_style, |p| { + p.print(coords, &format!("{:^3}", c)); + }); + } else { + printer.with_style(future_style, |p| { + p.print(coords, &format!("{:^3}", CONFIGURATION.future_chr)); + }); + } + i += 1; } - i += 1; - } + }; + draw_day(printer); } fn required_size(&mut self, _: Vec2) -> Vec2 { -- cgit v1.2.3