From ce5f500998f05fac13c6472681eedb7702055a7f Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 26 Feb 2020 12:26:25 +0530 Subject: add App struct --- src/main.rs | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 2b70033..a9ea9b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,14 @@ #![allow(unused_must_use)] use chrono::NaiveDate; +use std::f64; +use cursive::direction::Direction; +use cursive::event::{Event, EventResult, Key}; +use cursive::view::View; use cursive::views::{Dialog, EditView, LinearLayout, ListView, SelectView}; use cursive::Cursive; +use cursive::{Printer, Vec2}; mod habit; use crate::habit::{Habit, HabitTrait, HabitType}; @@ -13,9 +18,78 @@ use crate::views::habitview::HabitView; mod theme; +#[derive(PartialEq)] +enum ViewMode { + Day, + Month, + Year, +} + struct App { - habits: Vec, + habits: Vec, status: String, + view_mode: ViewMode, + focus: usize, + + padding: usize, +} + +impl App { + fn new() -> Self { + return App { + habits: vec![], + status: "".to_string(), + view_mode: ViewMode::Day, + focus: 0, + padding: 2, + }; + } + fn add_habit(&mut self, h: HabitView) -> &mut Self { + self.habits.push(h); + return self; + } + fn set_mode(&mut self, set_mode: ViewMode) -> &mut Self { + if set_mode != self.view_mode { + self.view_mode = set_mode + } + return self; + } +} + +impl View for App { + fn draw(&self, printer: &Printer) { + let grid_width = 3; + let width = { + if self.habits.len() > 0 { + grid_width * self.habits[0].get_size().x + } else { + 0 + } + }; + let height = { + if self.habits.len() > 0 { + (self.habits[0].get_size().y as f64 / grid_width as f64).ceil() as usize + } else { + 0 + } + }; + let mut offset = Vec2::new(width + self.padding, height + self.padding); + for (idx, i) in self.habits.iter().enumerate() { + i.draw(&printer.offset(offset).focused(self.focus == idx)); + offset = offset.saturating_add(i.get_size()); + } + } + fn required_size(&mut self, _: Vec2) -> Vec2 { + todo!() + } + + fn take_focus(&mut self, _: Direction) -> bool { + todo!() + } + + fn on_event(&mut self, e: Event) -> EventResult { + todo!() + } } fn main() { @@ -28,7 +102,7 @@ fn main() { gymming.insert_entry(NaiveDate::from_ymd(2020, 2, 14), HabitType::Count(10)); gymming.insert_entry(NaiveDate::from_ymd(2020, 2, 15), HabitType::Count(11)); - let mut reading = Habit::new("read", HabitType::Count(5)); + let mut reading = Habit::new("read", HabitType::Bit(true)); reading.insert_entry(NaiveDate::from_ymd(2020, 2, 11), HabitType::Bit(true)); reading.insert_entry(NaiveDate::from_ymd(2020, 2, 12), HabitType::Bit(false)); reading.insert_entry(NaiveDate::from_ymd(2020, 2, 13), HabitType::Bit(true)); -- cgit v1.2.3