diff --git a/package.json b/package.json index 4b390ba..055d1a8 100644 --- a/package.json +++ b/package.json @@ -29,8 +29,8 @@ "messageKeys": [ "BackgroundColor", "ForegroundColor", - "SecondTick", - "Animations" + "FastBegin", + "FastEnd" ], "resources": { "media": [] diff --git a/src/c/main.c b/src/c/main.c new file mode 100644 index 0000000..3581947 --- /dev/null +++ b/src/c/main.c @@ -0,0 +1,147 @@ +#include "main.h" +#include "message_keys.auto.h" +#include + +static Window *s_main_window; +static TextLayer *s_time_layer; +static TextLayer *s_date_layer; + +// A struct for our specific settings (see main.h) +static ClaySettings settings; + +// Initialize the default settings +static void prv_default_settings() { + settings.BackgroundColor = GColorBlack; + settings.ForegroundColor = GColorWhite; + settings.FastBegin = "12:00"; + settings.FastEnd = "20:00"; +} + +// Read settings from persistent storage +static void prv_load_settings() { + // Load the default settings + prv_default_settings(); + // Read settings from persistent storage, if they exist + persist_read_data(SETTINGS_KEY, &settings, sizeof(settings)); +} + +// Save the settings to persistent storage +static void prv_save_settings() { + persist_write_data(SETTINGS_KEY, &settings, sizeof(settings)); + // Update the display based on new settings + update_time(); +} +// Handle the response from AppMessage +static void inbox_recieved_callback(DictionaryIterator *iter, void *context) { + // Background Color + Tuple *bg_color_t = dict_find(iter, MESSAGE_KEY_BackgroundColor); + if (bg_color_t) { + settings.BackgroundColor = GColorFromHEX(bg_color_t->value->int32); + } + + // Foreground Color + Tuple *fg_color_t = dict_find(iter, MESSAGE_KEY_ForegroundColor); + if (fg_color_t) { + settings.ForegroundColor = GColorFromHEX(fg_color_t->value->int32); + } + + // Second Tick + Tuple *fast_begin_t = dict_find(iter, MESSAGE_KEY_FastBegin); + if (fast_begin_t) { + settings.FastBegin = fast_begin_t->value->cstring; + } + + // Animations + Tuple *fast_end_t = dict_find(iter, MESSAGE_KEY_FastEnd); + if (fast_end_t) { + settings.FastEnd = fast_end_t->value->cstring; + + // Save the new settings to persistent storage + prv_save_settings(); + } +} + +static void update_time() { + // Get a tm structure + time_t temp = time(NULL); + struct tm *tick_time = localtime(&temp); + + // Write the current hours and minutes into a buffer + static char s_time_buffer[8]; + strftime(s_time_buffer, sizeof(s_time_buffer), + clock_is_24h_style() ? "%H:%M" : "%I:%M", tick_time); + + // Display this time on the TextLayer + text_layer_set_text(s_time_layer, s_time_buffer); + + // Write the current date into a buffer + static char s_date_buffer[16]; + strftime(s_date_buffer, sizeof(s_date_buffer), "%a %b %d", tick_time); + + // Display the date + text_layer_set_text(s_date_layer, s_date_buffer); +} + +static void tick_handler(struct tm *tick_time, TimeUnits units_changed) { + update_time(); +} + +static void main_window_load(Window *window) { + Layer *window_layer = window_get_root_layer(window); + GRect bounds = layer_get_bounds(window_layer); + + // Create the time TextLayer + s_time_layer = + text_layer_create(GRect(0, PBL_IF_ROUND_ELSE(58, 52), bounds.size.w, 50)); + text_layer_set_background_color(s_time_layer, settings.BackgroundColor); + text_layer_set_text_color(s_time_layer, settings.ForegroundColor); + text_layer_set_font(s_time_layer, + fonts_get_system_font(FONT_KEY_BITHAM_42_BOLD)); + text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter); + + // Add it as a child layer to the Window's root layer + layer_add_child(window_layer, text_layer_get_layer(s_time_layer)); + + // Create the date TextLayer + s_date_layer = text_layer_create( + GRect(0, PBL_IF_ROUND_ELSE(110, 104), bounds.size.w, 30)); + text_layer_set_background_color(s_date_layer, settings.BackgroundColor); + text_layer_set_text_color(s_date_layer, settings.ForegroundColor); + text_layer_set_font(s_date_layer, + fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD)); + text_layer_set_text_alignment(s_date_layer, GTextAlignmentCenter); + + // Add to Window + layer_add_child(window_layer, text_layer_get_layer(s_date_layer)); +} + +static void main_window_unload(Window *window) { + text_layer_destroy(s_date_layer); + text_layer_destroy(s_time_layer); +} + +static void init() { + prv_load_settings(); + app_message_register_inbox_received(inbox_recieved_callback); + app_message_open(128, 128); + + s_main_window = window_create(); + window_set_background_color(s_main_window, GColorBlack); + + window_set_window_handlers( + s_main_window, + (WindowHandlers){.load = main_window_load, .unload = main_window_unload}); + + window_stack_push(s_main_window, true); + + update_time(); + tick_timer_service_subscribe(MINUTE_UNIT, tick_handler); +} + +static void deinit() { window_destroy(s_main_window); } + +int main(void) { + init(); + app_event_loop(); + deinit(); +} diff --git a/src/c/main.h b/src/c/main.h new file mode 100644 index 0000000..c28adbb --- /dev/null +++ b/src/c/main.h @@ -0,0 +1,25 @@ +#pragma once +#include + +#define SETTINGS_KEY 1 + +// A structure containing our settings +typedef struct ClaySettings { + GColor BackgroundColor; + GColor ForegroundColor; + char *FastBegin; + char *FastEnd; +} __attribute__((__packed__)) ClaySettings; + +static void prv_default_settings(); +static void prv_load_settings(); +static void prv_save_settings(); + +int main(void); +static void inbox_recieved_callback(DictionaryIterator *iter, void *context); +static void update_time(); +static void tick_handler(struct tm *tick_time, TimeUnits units_changed); +static void main_window_load(Window *window); +static void main_window_unload(Window *window); +static void init(); +static void deinit(); diff --git a/src/pkjs/config.js b/src/pkjs/config.js index 261c390..9968d08 100644 --- a/src/pkjs/config.js +++ b/src/pkjs/config.js @@ -33,20 +33,28 @@ module.exports = [ "items": [ { "type": "heading", - "defaultValue": "More Settings" + "defaultValue": "Fasting Period (24hr format)" }, { - "type": "toggle", - "messageKey": "SecondTick", - "label": "Enable Seconds", - "defaultValue": false + "type": "input", + "messageKey": "FastBegin", + "label": "Time your fast begins", + "attributes": { + "placeholder": "12:00", + "limit": 5, + "type": "time", + } }, { - "type": "toggle", - "messageKey": "Animations", - "label": "Enable Animations", - "defaultValue": false - } + "type": "input", + "messageKey": "FastEnd", + "label": "Time your fast ends", + "attributes": { + "placeholder": "20:00", + "limit": 5, + "type": "time", + } + }, ] }, {