application: Store pace in the model and show it in its own view.
[gps-watch.git] / src / application / views.rs
1 /*
2  * Copyright (c) 2020 Tilman Sauerbeck (tilman at code-monkey de)
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23
24 use common::time::Time;
25 use common::screen;
26 use model;
27
28 pub struct TimeView {
29     is_valid: bool,
30 }
31
32 impl TimeView {
33     pub fn new() -> TimeView {
34         TimeView {
35             is_valid: false,
36         }
37     }
38
39     pub fn invalidate(&mut self) {
40         self.is_valid = false;
41     }
42
43     pub fn draw(&mut self, screen: &mut screen::Screen,
44                 model: &mut model::Model) -> bool {
45         if self.is_valid &&
46            !model.check_and_reset_is_dirty(model::Field::UnixTime(0)) {
47             return false;
48         }
49
50         if let Some(tm) = Time::from_unix_time(model.unix_time) {
51             let mut time_s = [b' '; 8];
52             tm.fmt_time(&mut time_s);
53
54             screen.clear();
55             screen.draw_text(&time_s);
56         }
57
58         self.is_valid = true;
59
60         true
61     }
62 }
63
64 pub struct DistanceView {
65     is_valid: bool,
66 }
67
68 impl DistanceView {
69     pub fn new() -> DistanceView {
70         DistanceView {
71             is_valid: false,
72         }
73     }
74
75     pub fn invalidate(&mut self) {
76         self.is_valid = false;
77     }
78
79     pub fn draw(&mut self, screen: &mut screen::Screen,
80                 model: &mut model::Model) -> bool {
81         if self.is_valid &&
82            !model.check_and_reset_is_dirty(model::Field::Distance(0)) {
83             return false;
84         }
85
86         let mut distance_m_s = [b' '; 8];
87
88         common::fmt::fmt_u32(&mut distance_m_s, model.distance_cm / 100);
89
90         screen.clear();
91         screen.draw_text(&distance_m_s);
92
93         self.is_valid = true;
94
95         true
96     }
97 }
98
99 pub struct PaceView {
100     is_valid: bool,
101 }
102
103 impl PaceView {
104     pub fn new() -> PaceView {
105         PaceView {
106             is_valid: false,
107         }
108     }
109
110     pub fn invalidate(&mut self) {
111         self.is_valid = false;
112     }
113
114     pub fn draw(&mut self, screen: &mut screen::Screen,
115                 model: &mut model::Model) -> bool {
116         if self.is_valid &&
117            !model.check_and_reset_is_dirty(model::Field::Pace(0)) {
118             return false;
119         }
120
121         let mut min_and_s = [b':'; 6];
122
123         common::fmt::fmt_u32_pad(&mut min_and_s[0..3], model.pace_s / 60,
124                                  3, b' ');
125
126         common::fmt::fmt_u32_pad(&mut min_and_s[4..6], model.pace_s % 60,
127                                  2, b'0');
128
129         screen.clear();
130         screen.draw_text(&min_and_s);
131
132         self.is_valid = true;
133
134         true
135     }
136 }