Re-arrange main.c more logically
This commit is contained in:
+145
-139
@@ -41,9 +41,9 @@ static TextLayer *s_side_b_txt_layer;
|
||||
static TextLayer *s_side_c_txt_layer;
|
||||
|
||||
// fonts
|
||||
GFont s_font_lecoish_small;
|
||||
//// setter
|
||||
GFont s_font_lecoish_big;
|
||||
GFont s_font_lecoish_small;
|
||||
//// alarm
|
||||
GFont s_font_mansalva_big;
|
||||
GFont s_font_mansalva_small;
|
||||
@@ -57,6 +57,7 @@ static GBitmap *s_check_bmp;
|
||||
static GColor s_setter_highlight_color;
|
||||
static uint8_t s_highlight_pos = 0;
|
||||
static struct TheTime s_the_time;
|
||||
//// setter
|
||||
//// alarm
|
||||
static uint8_t s_solution_indices[3] = {8, 8, 8};
|
||||
static uint8_t s_selected_indices[3] = {9, 9, 9};
|
||||
@@ -130,128 +131,6 @@ static bool schedule_alarm(bool is_fallback) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static void setter_seconds_handler(struct tm *tick_time, TimeUnits units_changed) {
|
||||
struct tm now_tm = *tick_time;
|
||||
|
||||
// build the target time today
|
||||
struct tm target_tm = now_tm;
|
||||
target_tm.tm_hour = s_the_time.HourSet;
|
||||
target_tm.tm_min = s_the_time.MinuteSet;
|
||||
target_tm.tm_sec = 0;
|
||||
|
||||
time_t now = mktime(&now_tm);
|
||||
time_t target = mktime(&target_tm);
|
||||
|
||||
if (target <= now) {
|
||||
target += SECONDS_PER_DAY;
|
||||
}
|
||||
|
||||
int32_t diff = (int32_t)(target - now);
|
||||
|
||||
uint8_t hours = diff / 3600;
|
||||
uint8_t minutes = (diff % 3600) / 60;
|
||||
uint8_t seconds = diff % 60;
|
||||
|
||||
static char s_countdown_buffer[12];
|
||||
snprintf(s_countdown_buffer, sizeof(s_countdown_buffer), "%02u:%02u:%02u", hours, minutes, seconds);
|
||||
text_layer_set_text(s_counter_txt_layer, s_countdown_buffer);
|
||||
}
|
||||
|
||||
static void update_countdown_display() {
|
||||
time_t now = time(NULL);
|
||||
setter_seconds_handler(localtime(&now), SECOND_UNIT);
|
||||
}
|
||||
|
||||
static void update_setter_display() {
|
||||
static char s_setter_buffer[PBL_IF_ROUND_ELSE(10, 14)];
|
||||
snprintf(s_setter_buffer, sizeof(s_setter_buffer), PBL_IF_ROUND_ELSE("%02u : %02u", "%02u : %02u"), s_the_time.HourWIP, s_the_time.MinuteWIP);
|
||||
text_layer_set_text(s_setter_txt_layer, s_setter_buffer);
|
||||
}
|
||||
|
||||
static void setter_up_click_handler(void *recognizer, void *ctx) {
|
||||
switch (s_highlight_pos) {
|
||||
case 0:
|
||||
if (s_the_time.HourWIP == 23) {
|
||||
s_the_time.HourWIP = 0;
|
||||
} else {
|
||||
s_the_time.HourWIP++;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if (s_the_time.MinuteWIP == 59) {
|
||||
s_the_time.MinuteWIP = 0;
|
||||
} else {
|
||||
s_the_time.MinuteWIP++;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
update_setter_display();
|
||||
}
|
||||
|
||||
static void setter_down_click_handler(void *recognizer, void *ctx) {
|
||||
switch (s_highlight_pos) {
|
||||
case 0:
|
||||
if (s_the_time.HourWIP == 0) {
|
||||
s_the_time.HourWIP = 23;
|
||||
} else {
|
||||
s_the_time.HourWIP--;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if (s_the_time.MinuteWIP == 0) {
|
||||
s_the_time.MinuteWIP = 59;
|
||||
} else {
|
||||
s_the_time.MinuteWIP--;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
update_setter_display();
|
||||
}
|
||||
|
||||
static void setter_select_click_handler(void *recognizer, void *ctx) {
|
||||
if (s_highlight_pos == 2) {
|
||||
bool success = schedule_alarm(false);
|
||||
if (!success) {
|
||||
return; // early return to avoid indicating alarm was set when it was not
|
||||
}
|
||||
|
||||
// persist
|
||||
persist_write_int(STORAGE_KEY_ALARM_HOUR, s_the_time.HourSet);
|
||||
persist_write_int(STORAGE_KEY_ALARM_MINUTE, s_the_time.MinuteSet);
|
||||
|
||||
// update countdown immediately
|
||||
update_countdown_display();
|
||||
|
||||
// move selection back to start
|
||||
s_highlight_pos = 0;
|
||||
} else {
|
||||
s_highlight_pos++;
|
||||
}
|
||||
layer_mark_dirty(s_setter_highlight_layer);
|
||||
layer_mark_dirty(s_bottom_bar_layer);
|
||||
}
|
||||
|
||||
static void setter_back_click_handler(void *recognizer, void *ctx) {
|
||||
if (s_highlight_pos == 0) {
|
||||
window_stack_pop_all(true);
|
||||
} else {
|
||||
s_highlight_pos--;
|
||||
}
|
||||
layer_mark_dirty(s_setter_highlight_layer);
|
||||
layer_mark_dirty(s_bottom_bar_layer);
|
||||
}
|
||||
|
||||
static void setter_click_config_provider(void *ctx) {
|
||||
window_single_repeating_click_subscribe(BUTTON_ID_UP, 60, setter_up_click_handler);
|
||||
window_single_repeating_click_subscribe(BUTTON_ID_DOWN, 60, setter_down_click_handler);
|
||||
window_single_click_subscribe(BUTTON_ID_SELECT, setter_select_click_handler);
|
||||
window_single_click_subscribe(BUTTON_ID_BACK, setter_back_click_handler);
|
||||
}
|
||||
|
||||
static void top_bar_layer_update_proc(Layer *layer, GContext *ctx) {
|
||||
graphics_context_set_fill_color(ctx, s_primary_color);
|
||||
graphics_fill_rect(ctx, layer_get_bounds(layer), 0, GCornerNone);
|
||||
@@ -275,20 +154,7 @@ static void add_bar_layers_to_window(Layer *window_layer, bool small_top) {
|
||||
layer_add_child(window_layer, s_bottom_bar_layer);
|
||||
}
|
||||
|
||||
static void highlight_layer_update_proc(Layer *layer, GContext *ctx) {
|
||||
GRect fill_area = layer_get_bounds(layer);
|
||||
fill_area.size.w = PBL_DISPLAY_WIDTH / 2;
|
||||
switch (s_highlight_pos) {
|
||||
case 2:
|
||||
return; // do not draw highlight
|
||||
case 1:
|
||||
// draw highlight on minutes site
|
||||
fill_area.origin.x = fill_area.size.w;
|
||||
break;
|
||||
}
|
||||
graphics_context_set_fill_color(ctx, s_setter_highlight_color);
|
||||
graphics_fill_rect(ctx, fill_area, 0, GCornerNone);
|
||||
}
|
||||
////////////////////// ALARM //////////////////////
|
||||
|
||||
static void alarm_seconds_handler(struct tm *tick_time, TimeUnits units_changed) {
|
||||
// re-schedule fallback alarm every 15 seconds
|
||||
@@ -714,12 +580,153 @@ static void push_alarm_window() {
|
||||
window_stack_push(s_window, false);
|
||||
}
|
||||
|
||||
////////////////////// SETTER //////////////////////
|
||||
|
||||
static void setter_seconds_handler(struct tm *tick_time, TimeUnits units_changed) {
|
||||
struct tm now_tm = *tick_time;
|
||||
|
||||
// build the target time today
|
||||
struct tm target_tm = now_tm;
|
||||
target_tm.tm_hour = s_the_time.HourSet;
|
||||
target_tm.tm_min = s_the_time.MinuteSet;
|
||||
target_tm.tm_sec = 0;
|
||||
|
||||
time_t now = mktime(&now_tm);
|
||||
time_t target = mktime(&target_tm);
|
||||
|
||||
if (target <= now) {
|
||||
target += SECONDS_PER_DAY;
|
||||
}
|
||||
|
||||
int32_t diff = (int32_t)(target - now);
|
||||
|
||||
uint8_t hours = diff / 3600;
|
||||
uint8_t minutes = (diff % 3600) / 60;
|
||||
uint8_t seconds = diff % 60;
|
||||
|
||||
static char s_countdown_buffer[12];
|
||||
snprintf(s_countdown_buffer, sizeof(s_countdown_buffer), "%02u:%02u:%02u", hours, minutes, seconds);
|
||||
text_layer_set_text(s_counter_txt_layer, s_countdown_buffer);
|
||||
}
|
||||
|
||||
static void update_countdown_display() {
|
||||
time_t now = time(NULL);
|
||||
setter_seconds_handler(localtime(&now), SECOND_UNIT);
|
||||
}
|
||||
|
||||
static void update_setter_display() {
|
||||
static char s_setter_buffer[PBL_IF_ROUND_ELSE(10, 14)];
|
||||
snprintf(s_setter_buffer, sizeof(s_setter_buffer), PBL_IF_ROUND_ELSE("%02u : %02u", "%02u : %02u"), s_the_time.HourWIP, s_the_time.MinuteWIP);
|
||||
text_layer_set_text(s_setter_txt_layer, s_setter_buffer);
|
||||
}
|
||||
|
||||
static void setter_highlight_layer_update_proc(Layer *layer, GContext *ctx) {
|
||||
GRect fill_area = layer_get_bounds(layer);
|
||||
fill_area.size.w = PBL_DISPLAY_WIDTH / 2;
|
||||
switch (s_highlight_pos) {
|
||||
case 2:
|
||||
return; // do not draw highlight
|
||||
case 1:
|
||||
// draw highlight on minutes site
|
||||
fill_area.origin.x = fill_area.size.w;
|
||||
break;
|
||||
}
|
||||
graphics_context_set_fill_color(ctx, s_setter_highlight_color);
|
||||
graphics_fill_rect(ctx, fill_area, 0, GCornerNone);
|
||||
}
|
||||
|
||||
static void setter_up_click_handler(void *recognizer, void *ctx) {
|
||||
switch (s_highlight_pos) {
|
||||
case 0:
|
||||
if (s_the_time.HourWIP == 23) {
|
||||
s_the_time.HourWIP = 0;
|
||||
} else {
|
||||
s_the_time.HourWIP++;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if (s_the_time.MinuteWIP == 59) {
|
||||
s_the_time.MinuteWIP = 0;
|
||||
} else {
|
||||
s_the_time.MinuteWIP++;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
update_setter_display();
|
||||
}
|
||||
|
||||
static void setter_down_click_handler(void *recognizer, void *ctx) {
|
||||
switch (s_highlight_pos) {
|
||||
case 0:
|
||||
if (s_the_time.HourWIP == 0) {
|
||||
s_the_time.HourWIP = 23;
|
||||
} else {
|
||||
s_the_time.HourWIP--;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if (s_the_time.MinuteWIP == 0) {
|
||||
s_the_time.MinuteWIP = 59;
|
||||
} else {
|
||||
s_the_time.MinuteWIP--;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
update_setter_display();
|
||||
}
|
||||
|
||||
static void setter_select_click_handler(void *recognizer, void *ctx) {
|
||||
if (s_highlight_pos == 2) {
|
||||
bool success = schedule_alarm(false);
|
||||
if (!success) {
|
||||
return; // early return to avoid indicating alarm was set when it was not
|
||||
}
|
||||
|
||||
// persist
|
||||
persist_write_int(STORAGE_KEY_ALARM_HOUR, s_the_time.HourSet);
|
||||
persist_write_int(STORAGE_KEY_ALARM_MINUTE, s_the_time.MinuteSet);
|
||||
|
||||
// update countdown immediately
|
||||
update_countdown_display();
|
||||
|
||||
// move selection back to start
|
||||
s_highlight_pos = 0;
|
||||
} else {
|
||||
s_highlight_pos++;
|
||||
}
|
||||
layer_mark_dirty(s_setter_highlight_layer);
|
||||
layer_mark_dirty(s_bottom_bar_layer);
|
||||
}
|
||||
|
||||
static void setter_back_click_handler(void *recognizer, void *ctx) {
|
||||
if (s_highlight_pos == 0) {
|
||||
window_stack_pop_all(true);
|
||||
} else {
|
||||
s_highlight_pos--;
|
||||
}
|
||||
layer_mark_dirty(s_setter_highlight_layer);
|
||||
layer_mark_dirty(s_bottom_bar_layer);
|
||||
}
|
||||
|
||||
static void setter_click_config_provider(void *ctx) {
|
||||
window_single_repeating_click_subscribe(BUTTON_ID_UP, 60, setter_up_click_handler);
|
||||
window_single_repeating_click_subscribe(BUTTON_ID_DOWN, 60, setter_down_click_handler);
|
||||
window_single_click_subscribe(BUTTON_ID_SELECT, setter_select_click_handler);
|
||||
window_single_click_subscribe(BUTTON_ID_BACK, setter_back_click_handler);
|
||||
}
|
||||
|
||||
static void live_wakeup_handler_for_setter(WakeupId id, int32_t reason) {
|
||||
window_stack_pop_all(false);
|
||||
push_alarm_window();
|
||||
}
|
||||
|
||||
static void setter_window_load() {
|
||||
wakeup_service_subscribe(live_wakeup_handler_for_setter);
|
||||
|
||||
s_setter_highlight_color = GColorMediumAquamarine;
|
||||
Layer *window_layer = window_get_root_layer(s_window);
|
||||
|
||||
@@ -752,7 +759,7 @@ static void setter_window_load() {
|
||||
|
||||
// highlighter
|
||||
s_setter_highlight_layer = layer_create(GRect(0, s_top_bar_grect.size.h, PBL_DISPLAY_WIDTH, PBL_DISPLAY_HEIGHT - (2 * s_top_bar_grect.size.h)));
|
||||
layer_set_update_proc(s_setter_highlight_layer, highlight_layer_update_proc);
|
||||
layer_set_update_proc(s_setter_highlight_layer, setter_highlight_layer_update_proc);
|
||||
layer_add_child(window_layer, s_setter_highlight_layer);
|
||||
|
||||
// setter
|
||||
@@ -764,7 +771,6 @@ static void setter_window_load() {
|
||||
layer_add_child(window_layer, text_layer_get_layer(s_setter_txt_layer));
|
||||
update_setter_display();
|
||||
|
||||
wakeup_service_subscribe(live_wakeup_handler_for_setter);
|
||||
tick_timer_service_subscribe(SECOND_UNIT, setter_seconds_handler);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user