Reduce code duplication

This commit is contained in:
2026-05-16 20:50:29 -04:00
parent 2821446fa4
commit 61219458ab
+29 -58
View File
@@ -44,45 +44,29 @@ static void main_window_load() {
}
if (persist_exists(storage_key_mg_color)) {
s_time_mg_layers[0] = bitmap_layer_create(s_time_grects[0]);
s_time_mg_layers[1] = bitmap_layer_create(s_time_grects[1]);
s_time_mg_layers[2] = bitmap_layer_create(s_time_grects[2]);
s_time_mg_layers[3] = bitmap_layer_create(s_time_grects[3]);
bitmap_layer_set_compositing_mode(s_time_mg_layers[0], GCompOpSet);
bitmap_layer_set_compositing_mode(s_time_mg_layers[1], GCompOpSet);
bitmap_layer_set_compositing_mode(s_time_mg_layers[2], GCompOpSet);
bitmap_layer_set_compositing_mode(s_time_mg_layers[3], GCompOpSet);
layer_add_child(window_layer, bitmap_layer_get_layer(s_time_mg_layers[0]));
layer_add_child(window_layer, bitmap_layer_get_layer(s_time_mg_layers[1]));
layer_add_child(window_layer, bitmap_layer_get_layer(s_time_mg_layers[2]));
layer_add_child(window_layer, bitmap_layer_get_layer(s_time_mg_layers[3]));
for (int i = 0; i < 4; ++i) {
s_time_mg_layers[i] = bitmap_layer_create(s_time_grects[i]);
bitmap_layer_set_compositing_mode(s_time_mg_layers[i], GCompOpSet);
layer_add_child(window_layer, bitmap_layer_get_layer(s_time_mg_layers[i]));
}
}
if (persist_exists(storage_key_fg_color)) {
replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[0], NULL);
replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[1], NULL);
replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[2], NULL);
replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[3], NULL);
replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[4], NULL);
replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[5], NULL);
replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[6], NULL);
replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[7], NULL);
replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[8], NULL);
replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[9], NULL);
for (int i = 0; i < 10; ++i) {
replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[i], NULL);
}
}
layer_add_child(window_layer, bitmap_layer_get_layer(s_time_fg_layers[0]));
layer_add_child(window_layer, bitmap_layer_get_layer(s_time_fg_layers[1]));
layer_add_child(window_layer, bitmap_layer_get_layer(s_time_fg_layers[2]));
layer_add_child(window_layer, bitmap_layer_get_layer(s_time_fg_layers[3]));
for (int i = 0; i < 4; ++i) {
layer_add_child(window_layer, bitmap_layer_get_layer(s_time_fg_layers[i]));
}
}
static void main_window_unload() {
if (s_time_mg_layers[0]) {
bitmap_layer_destroy(s_time_mg_layers[0]);
bitmap_layer_destroy(s_time_mg_layers[1]);
bitmap_layer_destroy(s_time_mg_layers[2]);
bitmap_layer_destroy(s_time_mg_layers[3]);
for (int i = 0; i < 4; ++i) {
if (s_time_mg_layers[i]) {
bitmap_layer_destroy(s_time_mg_layers[i]);
}
}
}
@@ -105,10 +89,9 @@ static void update_minute_1() {
s_time_digits[2] = minute / 10;
s_time_digits[3] = minute % 10;
bitmap_layer_set_bitmap(s_time_fg_layers[0], s_font_bitmaps[s_time_digits[0]]);
bitmap_layer_set_bitmap(s_time_fg_layers[1], s_font_bitmaps[s_time_digits[1]]);
bitmap_layer_set_bitmap(s_time_fg_layers[2], s_font_bitmaps[s_time_digits[2]]);
bitmap_layer_set_bitmap(s_time_fg_layers[3], s_font_bitmaps[s_time_digits[3]]);
for (int i = 0; i < 4; ++i) {
bitmap_layer_set_bitmap(s_time_fg_layers[i], s_font_bitmaps[s_time_digits[i]]);
}
}
static void minute_handler(struct tm *tick_time, TimeUnits units_changed) {
@@ -140,15 +123,11 @@ static void init() {
s_font_bitmaps[8] = gbitmap_create_with_resource(RESOURCE_ID_8);
s_font_bitmaps[9] = gbitmap_create_with_resource(RESOURCE_ID_9);
// do everything else
s_time_fg_layers[0] = bitmap_layer_create(s_time_grects[0]);
s_time_fg_layers[1] = bitmap_layer_create(s_time_grects[1]);
s_time_fg_layers[2] = bitmap_layer_create(s_time_grects[2]);
s_time_fg_layers[3] = bitmap_layer_create(s_time_grects[3]);
bitmap_layer_set_compositing_mode(s_time_fg_layers[0], GCompOpSet);
bitmap_layer_set_compositing_mode(s_time_fg_layers[1], GCompOpSet);
bitmap_layer_set_compositing_mode(s_time_fg_layers[2], GCompOpSet);
bitmap_layer_set_compositing_mode(s_time_fg_layers[3], GCompOpSet);
for (int i = 0; i < 4; ++i) {
s_time_fg_layers[i] = bitmap_layer_create(s_time_grects[i]);
bitmap_layer_set_compositing_mode(s_time_fg_layers[i], GCompOpSet);
}
update_minute_1();
soft_reload(true);
tick_timer_service_subscribe(MINUTE_UNIT, minute_handler);
@@ -156,20 +135,12 @@ static void init() {
static void deinit() {
tick_timer_service_unsubscribe();
bitmap_layer_destroy(s_time_fg_layers[0]);
bitmap_layer_destroy(s_time_fg_layers[1]);
bitmap_layer_destroy(s_time_fg_layers[2]);
bitmap_layer_destroy(s_time_fg_layers[3]);
gbitmap_destroy(s_font_bitmaps[0]);
gbitmap_destroy(s_font_bitmaps[1]);
gbitmap_destroy(s_font_bitmaps[2]);
gbitmap_destroy(s_font_bitmaps[3]);
gbitmap_destroy(s_font_bitmaps[4]);
gbitmap_destroy(s_font_bitmaps[5]);
gbitmap_destroy(s_font_bitmaps[6]);
gbitmap_destroy(s_font_bitmaps[7]);
gbitmap_destroy(s_font_bitmaps[8]);
gbitmap_destroy(s_font_bitmaps[9]);
for (int i = 0; i < 4; ++i) {
bitmap_layer_destroy(s_time_fg_layers[i]);
}
for (int i = 0; i < 10; ++i) {
gbitmap_destroy(s_font_bitmaps[i]);
}
window_destroy(s_main_window);
}