-
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
// Copyright 2025 Shota FUJI
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0
using GLib;
namespace TimeTracker.Model {
public sealed class Model : Object {
public ListStore active_timers { get; construct; }
public ListStore stopped_timers { get; construct; }
public Storage.Storage? storage { get; construct; }
public Model(Storage.Storage? storage = null) {
Object(
active_timers: new ListStore(typeof (Timer)),
stopped_timers: new ListStore(typeof (Timer)),
storage: storage
);
}
construct {
if (storage != null) {
try {
var events = storage.load();
foreach (var event in events) {
update_internal(event);
}
} catch (Error error) {
log(null, LEVEL_ERROR, @"Loading of events failed: $(error.message)");
}
}
}
// Since Vala's type narrowing is half baked, cast inside branch may or may not be narrowed
// down. Explicit cast means compiler fails narrowing at that branch.
public void update(Event.Event event) {
if (storage != null) {
storage.save.begin(event, (_obj, res) => {
try {
storage.save.end(res);
} catch (Error error) {
critical(@"Save failed: $(error.message)");
}
});
}
update_internal(event);
}
internal void update_internal(Event.Event event) {
if (event is Event.TimerStarted) {
var timer = new Timer(event.timer_id, event.title, event.created_at);
active_timers.append(timer);
return;
}
if (event is Event.TimerTitleChanged) {
Event.TimerTitleChanged e = (Event.TimerTitleChanged) event;
for (uint i = 0; i < active_timers.n_items; i++) {
var timer = (Timer) active_timers.get_item(i);
if (timer.id == e.timer_id) {
timer.title = e.new_title;
return;
}
}
for (uint i = 0; i < stopped_timers.n_items; i++) {
var timer = (Timer) stopped_timers.get_item(i);
if (timer.id == e.timer_id) {
timer.title = e.new_title;
return;
}
}
return;
}
if (event is Event.TimerStopped) {
Event.TimerStopped e = (Event.TimerStopped) event;
for (uint i = 0; i < active_timers.n_items; i++) {
var timer = (Timer) active_timers.get_item(i);
if (timer.id == e.timer_id) {
active_timers.remove(i);
timer.stopped_at = e.created_at;
timer.is_stopped = true;
stopped_timers.insert(0, timer);
return;
}
}
return;
}
if (event is Event.TimerDeleted) {
Event.TimerDeleted e = (Event.TimerDeleted) event;
for (uint i = 0; i < active_timers.n_items; i++) {
var timer = (Timer) active_timers.get_item(i);
if (timer.id == e.timer_id) {
active_timers.remove(i);
return;
}
}
for (uint i = 0; i < stopped_timers.n_items; i++) {
var timer = (Timer) stopped_timers.get_item(i);
if (timer.id == e.timer_id) {
stopped_timers.remove(i);
return;
}
}
return;
}
info(@"Unknown event found: id=$(event.event_id)");
}
}
}