aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: a9ea9b130de66f7b62c4605a279fa5cf80c2ea23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#![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};

mod views;
use crate::views::habitview::HabitView;

mod theme;

#[derive(PartialEq)]
enum ViewMode {
    Day,
    Month,
    Year,
}

struct App {
    habits: Vec<HabitView>,
    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() {
    let mut s = Cursive::default();

    let mut gymming = Habit::new("gym", HabitType::Count(5));
    gymming.insert_entry(NaiveDate::from_ymd(2020, 2, 11), HabitType::Count(7));
    gymming.insert_entry(NaiveDate::from_ymd(2020, 2, 12), HabitType::Count(8));
    gymming.insert_entry(NaiveDate::from_ymd(2020, 2, 13), HabitType::Count(9));
    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::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));
    reading.insert_entry(NaiveDate::from_ymd(2020, 2, 14), HabitType::Bit(false));
    reading.insert_entry(NaiveDate::from_ymd(2020, 2, 15), HabitType::Bit(true));

    let gym_title = gymming.get_name();
    let gym_view = HabitView::new(gymming);

    let read_title = reading.get_name();
    let read_view = HabitView::new(reading);

    s.add_global_callback('q', |a| a.quit());
    s.add_layer(
        LinearLayout::horizontal()
            .child(Dialog::around(gym_view).title(gym_title))
            .child(Dialog::around(read_view).title(read_title)),
    );

    s.set_theme(theme::theme_gen());
    s.run();
}