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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
|
#include <pebble.h>
#include "fastforge.h"
FastEntry history[MAX_FASTS];
int history_count = 0;
FastEntry current_fast = {0};
uint16_t global_target_minutes = DEFAULT_TARGET_MINUTES;
StreakData streak_data = {0};
AppTimer *alarm_timer = NULL;
time_t target_time = 0;
static Window *s_main_window;
static Window *s_goal_window;
static Layer *s_goal_background_layer;
static Layer *s_progress_layer;
static TextLayer *s_title_layer;
static TextLayer *s_timer_layer;
static TextLayer *s_detail_layer;
static TextLayer *s_stage_layer;
static TextLayer *s_hint_layer;
static TextLayer *s_goal_title_layer;
static TextLayer *s_goal_time_layer;
static TextLayer *s_goal_stage_layer;
static TextLayer *s_goal_hint_layer;
static char s_title_text[24];
static char s_timer_text[16];
static char s_detail_text[48];
static char s_stage_text[32];
static char s_goal_time_text[24];
static char s_goal_stage_text[24];
static const uint32_t s_alarm_vibe[] = {200, 100, 200, 100, 400, 100, 200};
static VibePattern s_alarm_pattern = {
.durations = s_alarm_vibe,
.num_segments = ARRAY_LENGTH(s_alarm_vibe),
};
static void refresh_timer_view(void);
static void refresh_goal_window_content(void);
static int progress_width_for_elapsed(time_t elapsed_seconds, uint32_t total_seconds, int width) {
if (width <= 0 || total_seconds == 0) {
return 0;
}
if (elapsed_seconds <= 0) {
return 0;
}
if ((uint32_t)elapsed_seconds >= total_seconds) {
return width;
}
return (int)((elapsed_seconds * width) / (time_t)total_seconds);
}
static int tick_x_for_seconds(uint32_t total_seconds, uint32_t tick_seconds, int width) {
if (width <= 0 || total_seconds == 0 || tick_seconds > total_seconds) {
return -1;
}
int tick_x = (int)((tick_seconds * (uint32_t)width) / total_seconds);
if (tick_x >= width) {
tick_x = width - 1;
}
return tick_x;
}
static void timer_progress_update_proc(Layer *layer, GContext *ctx) {
GRect bounds = layer_get_bounds(layer);
graphics_context_set_fill_color(ctx, GColorLightGray);
graphics_fill_rect(ctx, bounds, 2, GCornersAll);
if (!fast_is_running()) {
return;
}
time_t elapsed = time(NULL) - current_fast.start_time;
if (elapsed < 0) {
elapsed = 0;
}
uint32_t total_seconds = (uint32_t)current_fast.target_minutes * 60;
int fill_width = progress_width_for_elapsed(elapsed, total_seconds, bounds.size.w);
if (fill_width > 0) {
graphics_context_set_fill_color(ctx, GColorGreen);
graphics_fill_rect(ctx, GRect(0, 0, fill_width, bounds.size.h), 2, GCornersAll);
}
graphics_context_set_stroke_color(ctx, GColorBlack);
int tick_12h_x = tick_x_for_seconds(total_seconds, 12 * 3600, bounds.size.w);
int tick_24h_x = tick_x_for_seconds(total_seconds, 24 * 3600, bounds.size.w);
if (tick_12h_x >= 0) {
graphics_draw_line(ctx, GPoint(tick_12h_x, 0), GPoint(tick_12h_x, bounds.size.h - 1));
}
if (tick_24h_x >= 0) {
graphics_draw_line(ctx, GPoint(tick_24h_x, 0), GPoint(tick_24h_x, bounds.size.h - 1));
}
}
static void goal_background_update_proc(Layer *layer, GContext *ctx) {
graphics_context_set_fill_color(ctx, GColorBlack);
graphics_fill_rect(ctx, layer_get_bounds(layer), 0, GCornerNone);
}
static void show_goal_reached_window(void) {
if (!s_goal_window) {
return;
}
refresh_goal_window_content();
if (!window_stack_contains_window(s_goal_window)) {
window_stack_push(s_goal_window, true);
}
}
static void alarm_callback(void *data) {
(void)data;
alarm_timer = NULL;
target_time = 0;
vibes_enqueue_custom_pattern(s_alarm_pattern);
light_enable_interaction();
show_goal_reached_window();
refresh_timer_view();
}
static void schedule_alarm_if_needed(void) {
if (alarm_timer) {
app_timer_cancel(alarm_timer);
alarm_timer = NULL;
}
if (!fast_is_running() || current_fast.target_minutes == 0) {
target_time = 0;
return;
}
target_time = current_fast.start_time + (time_t)current_fast.target_minutes * 60;
time_t now = time(NULL);
if (target_time <= now) {
alarm_callback(NULL);
return;
}
uint32_t ms_until_alarm = (uint32_t)(target_time - now) * 1000;
alarm_timer = app_timer_register(ms_until_alarm, alarm_callback, NULL);
}
bool fast_is_running(void) {
return current_fast.start_time != 0;
}
static int clamp_history_count(int value) {
if (value < 0) {
return 0;
}
if (value > MAX_FASTS) {
return MAX_FASTS;
}
return value;
}
static void normalize_loaded_data(void) {
if (global_target_minutes == 0) {
global_target_minutes = DEFAULT_TARGET_MINUTES;
}
if (current_fast.start_time != 0 && current_fast.target_minutes == 0) {
current_fast.target_minutes = global_target_minutes;
}
if (current_fast.end_time != 0 && current_fast.end_time < current_fast.start_time) {
current_fast.end_time = 0;
}
}
void save_all_data(void) {
persist_write_int(KEY_HISTORY_COUNT, history_count);
persist_write_data(KEY_HISTORY_DATA, history, sizeof(FastEntry) * history_count);
persist_write_data(KEY_CURRENT_FAST, ¤t_fast, sizeof(FastEntry));
persist_write_int(KEY_TARGET_MIN, global_target_minutes);
persist_write_data(KEY_STREAK_DATA, &streak_data, sizeof(StreakData));
}
void load_all_data(void) {
history_count = 0;
memset(history, 0, sizeof(history));
memset(¤t_fast, 0, sizeof(current_fast));
global_target_minutes = DEFAULT_TARGET_MINUTES;
memset(&streak_data, 0, sizeof(streak_data));
if (persist_exists(KEY_HISTORY_COUNT)) {
history_count = clamp_history_count(persist_read_int(KEY_HISTORY_COUNT));
}
if (history_count > 0 && persist_exists(KEY_HISTORY_DATA)) {
const int expected_size = sizeof(FastEntry) * history_count;
const int read_size = persist_read_data(KEY_HISTORY_DATA, history, expected_size);
if (read_size != expected_size) {
history_count = 0;
memset(history, 0, sizeof(history));
}
}
if (persist_exists(KEY_CURRENT_FAST)) {
const int read_size = persist_read_data(KEY_CURRENT_FAST, ¤t_fast, sizeof(FastEntry));
if (read_size != (int)sizeof(FastEntry)) {
memset(¤t_fast, 0, sizeof(current_fast));
}
}
if (persist_exists(KEY_TARGET_MIN)) {
const int read_target = persist_read_int(KEY_TARGET_MIN);
if (read_target > 0 && read_target <= UINT16_MAX) {
global_target_minutes = (uint16_t)read_target;
}
}
if (persist_exists(KEY_STREAK_DATA)) {
const int read_size = persist_read_data(KEY_STREAK_DATA, &streak_data, sizeof(StreakData));
if (read_size != (int)sizeof(StreakData)) {
memset(&streak_data, 0, sizeof(streak_data));
}
}
normalize_loaded_data();
}
static void format_hhmmss(time_t seconds, char *buffer, size_t size) {
if (seconds < 0) {
seconds = 0;
}
int hours = (int)(seconds / 3600);
int minutes = (int)((seconds % 3600) / 60);
int secs = (int)(seconds % 60);
snprintf(buffer, size, "%02d:%02d:%02d", hours, minutes, secs);
}
static uint8_t stage_level_for_elapsed(time_t elapsed_seconds) {
if (elapsed_seconds >= 24 * 3600) {
return 3;
}
if (elapsed_seconds >= 18 * 3600) {
return 2;
}
if (elapsed_seconds >= 12 * 3600) {
return 1;
}
return 0;
}
static const char *stage_text_for_elapsed(time_t elapsed_seconds) {
if (elapsed_seconds >= 24 * 3600) {
return "DEEP KETOSIS";
}
if (elapsed_seconds >= 18 * 3600) {
return "EARLY KETOSIS";
}
if (elapsed_seconds >= 12 * 3600) {
return "FAT BURN";
}
return "GLYCOGEN";
}
static void update_max_stage_if_needed(time_t elapsed_seconds) {
if (!fast_is_running()) {
return;
}
uint8_t stage_level = stage_level_for_elapsed(elapsed_seconds);
if (stage_level > current_fast.max_stage_reached) {
current_fast.max_stage_reached = stage_level;
save_all_data();
}
}
static void refresh_goal_window_content(void) {
if (!s_goal_time_layer || !s_goal_stage_layer) {
return;
}
time_t elapsed = 0;
if (fast_is_running()) {
elapsed = time(NULL) - current_fast.start_time;
if (elapsed < 0) {
elapsed = 0;
}
update_max_stage_if_needed(elapsed);
}
char elapsed_text[16];
format_hhmmss(elapsed, elapsed_text, sizeof(elapsed_text));
snprintf(s_goal_time_text, sizeof(s_goal_time_text), "Elapsed %s", elapsed_text);
snprintf(s_goal_stage_text, sizeof(s_goal_stage_text), "Stage: %s", stage_text_for_elapsed(elapsed));
text_layer_set_text(s_goal_time_layer, s_goal_time_text);
text_layer_set_text(s_goal_stage_layer, s_goal_stage_text);
}
static void refresh_timer_view(void) {
if (!fast_is_running()) {
snprintf(s_title_text, sizeof(s_title_text), "NO FAST RUNNING");
format_hhmmss(0, s_timer_text, sizeof(s_timer_text));
snprintf(s_detail_text, sizeof(s_detail_text), "Target: %um", global_target_minutes);
snprintf(s_stage_text, sizeof(s_stage_text), "Stage: --");
text_layer_set_text(s_hint_layer, "SELECT Start DOWN Quit");
} else {
time_t now = time(NULL);
time_t elapsed = now - current_fast.start_time;
if (elapsed < 0) {
elapsed = 0;
}
update_max_stage_if_needed(elapsed);
uint32_t target_seconds = current_fast.target_minutes * 60;
if (target_seconds > 0) {
time_t remaining = (time_t)target_seconds - elapsed;
if (remaining > 0) {
snprintf(s_title_text, sizeof(s_title_text), "COUNTDOWN");
format_hhmmss(remaining, s_timer_text, sizeof(s_timer_text));
} else {
snprintf(s_title_text, sizeof(s_title_text), "GOAL REACHED");
format_hhmmss(0, s_timer_text, sizeof(s_timer_text));
}
char elapsed_text[16];
format_hhmmss(elapsed, elapsed_text, sizeof(elapsed_text));
snprintf(s_detail_text, sizeof(s_detail_text), "Elapsed %s", elapsed_text);
} else {
snprintf(s_title_text, sizeof(s_title_text), "ELAPSED");
format_hhmmss(elapsed, s_timer_text, sizeof(s_timer_text));
snprintf(s_detail_text, sizeof(s_detail_text), "No target set");
}
snprintf(s_stage_text, sizeof(s_stage_text), "Stage: %s", stage_text_for_elapsed(elapsed));
text_layer_set_text(s_hint_layer, "SELECT Stop DOWN Quit");
}
text_layer_set_text(s_title_layer, s_title_text);
text_layer_set_text(s_timer_layer, s_timer_text);
text_layer_set_text(s_detail_layer, s_detail_text);
text_layer_set_text(s_stage_layer, s_stage_text);
if (s_progress_layer) {
layer_mark_dirty(s_progress_layer);
}
}
static void append_history_entry(const FastEntry *entry) {
if (!entry) {
return;
}
if (history_count < MAX_FASTS) {
history[history_count++] = *entry;
return;
}
memmove(&history[0], &history[1], sizeof(FastEntry) * (MAX_FASTS - 1));
history[MAX_FASTS - 1] = *entry;
}
static void mark_fast_completed(time_t end_time) {
streak_data.current_streak++;
if (streak_data.current_streak > streak_data.longest_streak) {
streak_data.longest_streak = streak_data.current_streak;
}
streak_data.last_completed_fast_end = end_time;
}
static uint16_t resolve_target_minutes(uint16_t preset_target_minutes) {
if (preset_target_minutes > 0) {
global_target_minutes = preset_target_minutes;
}
if (global_target_minutes == 0) {
global_target_minutes = DEFAULT_TARGET_MINUTES;
}
return global_target_minutes;
}
bool fast_start(uint16_t preset_target_minutes) {
if (fast_is_running()) {
return false;
}
current_fast.start_time = time(NULL);
current_fast.end_time = 0;
current_fast.target_minutes = resolve_target_minutes(preset_target_minutes);
memset(current_fast.note, 0, sizeof(current_fast.note));
current_fast.max_stage_reached = 0;
save_all_data();
schedule_alarm_if_needed();
APP_LOG(APP_LOG_LEVEL_INFO, "Started fast at %ld (target=%u)",
(long)current_fast.start_time, current_fast.target_minutes);
return true;
}
bool fast_stop(void) {
if (!fast_is_running()) {
return false;
}
FastEntry completed = current_fast;
completed.end_time = time(NULL);
if (completed.end_time < completed.start_time) {
completed.end_time = completed.start_time;
}
append_history_entry(&completed);
mark_fast_completed(completed.end_time);
memset(¤t_fast, 0, sizeof(current_fast));
if (alarm_timer) {
app_timer_cancel(alarm_timer);
alarm_timer = NULL;
}
target_time = 0;
save_all_data();
APP_LOG(APP_LOG_LEVEL_INFO, "Stopped fast and saved to history (count=%d)", history_count);
return true;
}
// ==================== BUTTON HANDLERS ====================
static void select_click_handler(ClickRecognizerRef recognizer, void *context) {
(void)recognizer;
(void)context;
if (fast_is_running()) {
fast_stop();
} else {
fast_start(0);
}
refresh_timer_view();
}
static void down_click_handler(ClickRecognizerRef recognizer, void *context) {
(void)recognizer;
(void)context;
APP_LOG(APP_LOG_LEVEL_INFO, "DOWN pressed - Quitting app...");
window_stack_pop_all(false); // Cleanly quit the app
}
static void goal_window_stop_handler(ClickRecognizerRef recognizer, void *context) {
(void)recognizer;
(void)context;
fast_stop();
window_stack_remove(s_goal_window, true);
refresh_timer_view();
}
static void goal_window_continue_handler(ClickRecognizerRef recognizer, void *context) {
(void)recognizer;
(void)context;
window_stack_remove(s_goal_window, true);
refresh_timer_view();
}
// ==================== CLICK CONFIG ====================
static void click_config_provider(void *context) {
window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler);
window_single_click_subscribe(BUTTON_ID_DOWN, down_click_handler);
}
static void goal_click_config_provider(void *context) {
(void)context;
window_single_click_subscribe(BUTTON_ID_SELECT, goal_window_stop_handler);
window_single_click_subscribe(BUTTON_ID_DOWN, goal_window_continue_handler);
window_single_click_subscribe(BUTTON_ID_BACK, goal_window_continue_handler);
window_single_click_subscribe(BUTTON_ID_UP, goal_window_continue_handler);
}
static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
(void)tick_time;
(void)units_changed;
refresh_timer_view();
}
// ==================== WINDOW LOAD / UNLOAD ====================
static void window_load(Window *window) {
Layer *window_layer = window_get_root_layer(window);
GRect bounds = layer_get_bounds(window_layer);
s_title_layer = text_layer_create(GRect(0, 4, bounds.size.w, 24));
text_layer_set_background_color(s_title_layer, GColorClear);
text_layer_set_text_color(s_title_layer, GColorBlack);
text_layer_set_text_alignment(s_title_layer, GTextAlignmentCenter);
text_layer_set_font(s_title_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD));
s_timer_layer = text_layer_create(GRect(0, 28, bounds.size.w, 42));
text_layer_set_background_color(s_timer_layer, GColorClear);
text_layer_set_text_color(s_timer_layer, GColorBlack);
text_layer_set_text_alignment(s_timer_layer, GTextAlignmentCenter);
text_layer_set_font(s_timer_layer, fonts_get_system_font(FONT_KEY_BITHAM_34_MEDIUM_NUMBERS));
s_detail_layer = text_layer_create(GRect(0, 76, bounds.size.w, 24));
text_layer_set_background_color(s_detail_layer, GColorClear);
text_layer_set_text_color(s_detail_layer, GColorBlack);
text_layer_set_text_alignment(s_detail_layer, GTextAlignmentCenter);
text_layer_set_font(s_detail_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18));
s_progress_layer = layer_create(GRect(10, 104, bounds.size.w - 20, 12));
layer_set_update_proc(s_progress_layer, timer_progress_update_proc);
s_stage_layer = text_layer_create(GRect(0, 118, bounds.size.w, 20));
text_layer_set_background_color(s_stage_layer, GColorClear);
text_layer_set_text_color(s_stage_layer, GColorBlack);
text_layer_set_text_alignment(s_stage_layer, GTextAlignmentCenter);
text_layer_set_font(s_stage_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD));
s_hint_layer = text_layer_create(GRect(0, 138, bounds.size.w, 28));
text_layer_set_background_color(s_hint_layer, GColorClear);
text_layer_set_text_color(s_hint_layer, GColorBlack);
text_layer_set_text_alignment(s_hint_layer, GTextAlignmentCenter);
text_layer_set_font(s_hint_layer, fonts_get_system_font(FONT_KEY_GOTHIC_14));
layer_add_child(window_layer, text_layer_get_layer(s_title_layer));
layer_add_child(window_layer, text_layer_get_layer(s_timer_layer));
layer_add_child(window_layer, text_layer_get_layer(s_detail_layer));
layer_add_child(window_layer, s_progress_layer);
layer_add_child(window_layer, text_layer_get_layer(s_stage_layer));
layer_add_child(window_layer, text_layer_get_layer(s_hint_layer));
refresh_timer_view();
}
static void window_unload(Window *window) {
(void)window;
save_all_data();
layer_destroy(s_progress_layer);
s_progress_layer = NULL;
text_layer_destroy(s_title_layer);
text_layer_destroy(s_timer_layer);
text_layer_destroy(s_detail_layer);
text_layer_destroy(s_stage_layer);
text_layer_destroy(s_hint_layer);
}
static void goal_window_load(Window *window) {
Layer *window_layer = window_get_root_layer(window);
GRect bounds = layer_get_bounds(window_layer);
s_goal_background_layer = layer_create(bounds);
layer_set_update_proc(s_goal_background_layer, goal_background_update_proc);
layer_add_child(window_layer, s_goal_background_layer);
s_goal_title_layer = text_layer_create(GRect(0, 20, bounds.size.w, 30));
text_layer_set_background_color(s_goal_title_layer, GColorBlack);
text_layer_set_text_color(s_goal_title_layer, GColorWhite);
text_layer_set_text_alignment(s_goal_title_layer, GTextAlignmentCenter);
text_layer_set_font(s_goal_title_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD));
text_layer_set_text(s_goal_title_layer, "GOAL HIT");
s_goal_time_layer = text_layer_create(GRect(0, 56, bounds.size.w, 26));
text_layer_set_background_color(s_goal_time_layer, GColorBlack);
text_layer_set_text_color(s_goal_time_layer, GColorWhite);
text_layer_set_text_alignment(s_goal_time_layer, GTextAlignmentCenter);
text_layer_set_font(s_goal_time_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD));
text_layer_set_text(s_goal_time_layer, "Elapsed 00:00:00");
s_goal_stage_layer = text_layer_create(GRect(0, 84, bounds.size.w, 24));
text_layer_set_background_color(s_goal_stage_layer, GColorBlack);
text_layer_set_text_color(s_goal_stage_layer, GColorWhite);
text_layer_set_text_alignment(s_goal_stage_layer, GTextAlignmentCenter);
text_layer_set_font(s_goal_stage_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD));
text_layer_set_text(s_goal_stage_layer, "Stage: --");
s_goal_hint_layer = text_layer_create(GRect(0, 120, bounds.size.w, 42));
text_layer_set_background_color(s_goal_hint_layer, GColorBlack);
text_layer_set_text_color(s_goal_hint_layer, GColorWhite);
text_layer_set_text_alignment(s_goal_hint_layer, GTextAlignmentCenter);
text_layer_set_font(s_goal_hint_layer, fonts_get_system_font(FONT_KEY_GOTHIC_14_BOLD));
text_layer_set_text(s_goal_hint_layer, "SELECT Stop\nDOWN Continue");
layer_add_child(window_layer, text_layer_get_layer(s_goal_title_layer));
layer_add_child(window_layer, text_layer_get_layer(s_goal_time_layer));
layer_add_child(window_layer, text_layer_get_layer(s_goal_stage_layer));
layer_add_child(window_layer, text_layer_get_layer(s_goal_hint_layer));
refresh_goal_window_content();
}
static void goal_window_unload(Window *window) {
(void)window;
layer_destroy(s_goal_background_layer);
text_layer_destroy(s_goal_title_layer);
text_layer_destroy(s_goal_time_layer);
text_layer_destroy(s_goal_stage_layer);
text_layer_destroy(s_goal_hint_layer);
}
// ==================== APP INIT / DEINIT ====================
static void init(void) {
load_all_data();
tick_timer_service_subscribe(SECOND_UNIT, tick_handler);
s_main_window = window_create();
window_set_click_config_provider(s_main_window, click_config_provider);
window_set_window_handlers(s_main_window, (WindowHandlers) {
.load = window_load,
.unload = window_unload,
});
window_stack_push(s_main_window, true);
s_goal_window = window_create();
window_set_background_color(s_goal_window, GColorBlack);
window_set_click_config_provider(s_goal_window, goal_click_config_provider);
window_set_window_handlers(s_goal_window, (WindowHandlers) {
.load = goal_window_load,
.unload = goal_window_unload,
});
schedule_alarm_if_needed();
}
static void deinit(void) {
if (alarm_timer) {
app_timer_cancel(alarm_timer);
alarm_timer = NULL;
}
target_time = 0;
save_all_data();
tick_timer_service_unsubscribe();
window_destroy(s_goal_window);
window_destroy(s_main_window);
}
int main(void) {
init();
app_event_loop();
deinit();
}
|