application: Store pace in the model and show it in its own view.
[gps-watch.git] / src / application / model.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 pub enum Field {
25     UnixTime(u32),
26     Distance(u32),
27     Pace(u32)
28 }
29
30 pub struct Model {
31     pub unix_time: u32,
32     pub distance_cm: u32,
33     pub pace_s: u32,
34
35     is_dirty: u32,
36 }
37
38 impl Model {
39     pub fn new() -> Model {
40         Model {
41             unix_time: 0,
42             distance_cm: 0,
43             pace_s: 0,
44             is_dirty: 0,
45         }
46     }
47
48     pub fn reset(&mut self) {
49         if self.distance_cm != 0 {
50             self.distance_cm = 0;
51             self.is_dirty |= Model::dirty_mask(Field::Distance(0));
52         }
53
54         if self.pace_s != 0 {
55             self.pace_s = 0;
56
57             self.is_dirty |= Model::dirty_mask(Field::Pace(0));
58         }
59     }
60
61     pub fn update(&mut self, data: Field) {
62         match data {
63             Field::UnixTime(unix_time) => {
64                 if self.unix_time != unix_time {
65                     self.unix_time = unix_time;
66                     self.is_dirty |= Model::dirty_mask(data);
67                 }
68             },
69             Field::Distance(distance_cm) => {
70                 if self.distance_cm != distance_cm {
71                     self.distance_cm = distance_cm;
72                     self.is_dirty |= Model::dirty_mask(data);
73                 }
74             },
75             Field::Pace(pace_s) => {
76                 if self.pace_s != pace_s {
77                     self.pace_s = pace_s;
78                     self.is_dirty |= Model::dirty_mask(data);
79                 }
80             },
81         }
82     }
83
84     pub fn check_and_reset_is_dirty(&mut self, data: Field) -> bool {
85         let mask = Model::dirty_mask(data);
86
87         if (self.is_dirty & mask) == 0 {
88             false
89         } else {
90             self.is_dirty &= !mask;
91
92             true
93         }
94     }
95
96     fn dirty_mask(data: Field) -> u32 {
97         match data {
98             Field::UnixTime(_) => 1,
99             Field::Distance(_) => 2,
100             Field::Pace(_) => 4,
101         }
102     }
103 }