<commit_before>#include <pebble.h>

static Window *s_window;
static Layer *s_layer_time_date;
static TextLayer *s_text_hour, *s_text_minute, *s_text_top, *s_text_bottom;
static GFont s_font_time, s_font_date;
static BitmapLayer *s_layer_battery;
static GBitmap *s_bitmap_battery;
char date_current[5], month_current[16];

//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////// Methods /////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////

void getBatteryIcon(int colour_background, int image_number) {
	int BatteryBlack[] = {RESOURCE_ID_IMAGE_BATTERY_BLACK_1, RESOURCE_ID_IMAGE_BATTERY_BLACK_2, RESOURCE_ID_IMAGE_BATTERY_BLACK_3, RESOURCE_ID_IMAGE_BATTERY_BLACK_4};
	int BatteryWhite[] = {RESOURCE_ID_IMAGE_BATTERY_WHITE_1, RESOURCE_ID_IMAGE_BATTERY_WHITE_2, RESOURCE_ID_IMAGE_BATTERY_WHITE_3, RESOURCE_ID_IMAGE_BATTERY_WHITE_4};
	
	gbitmap_destroy(s_bitmap_battery);
 	if(colour_background) {
		if (gcolor_equal(gcolor_legible_over(GColorFromHEX(colour_background)), GColorBlack)) {
			s_bitmap_battery = gbitmap_create_with_resource(BatteryBlack[image_number]);
		} else {
			s_bitmap_battery = gbitmap_create_with_resource(BatteryWhite[image_number]);
		}
	} else {
		s_bitmap_battery = gbitmap_create_with_resource(BatteryWhite[image_number]);
	}
	bitmap_layer_set_bitmap(s_layer_battery, s_bitmap_battery);
}

void customText(char t_buffer[16], char b_buffer[16]) {
	int check_date_0 = persist_read_int(MESSAGE_KEY_CHECK_DATE);
	int check_date_1 = persist_read_int(MESSAGE_KEY_CHECK_DATE+1);
	int check_date_2 = persist_read_int(MESSAGE_KEY_CHECK_DATE+2);

	if(strcmp("0104", date_current) == 0) {		// If date is 1st April, Display "April Fools" on bottom
		strcpy(t_buffer, "\0");
		strcpy(b_buffer, "April  Fools");
	} else if(check_date_0 == 1 && strcmp("0101", date_current) == 0) {  // New Years
		strcpy(t_buffer, "Happy");
		strcpy(b_buffer, "New  Year");
	} else if(check_date_1 == 1 && strcmp("3110", date_current) == 0) {  // Halloween
		strcpy(t_buffer, "\0");
		strcpy(b_buffer, "Halloween");
	} else if(check_date_2 == 1 && strcmp("2512", date_current) == 0) {  // Christmas
		strcpy(t_buffer, "Merry");
		strcpy(b_buffer, "Christmas");
	}
	
	// Valentines				14th February
	// Earth Day				
	// Mother's Day US			May ish
	// Mother's Day UK			March ish
	// Father's Day US			
	// Father's Day UK			June ish
	// Independence Day (US)	4th July
	// Independence Day (MEX)
	// Canada Day				
	// Victoria Day (Canada)	
	// Thanksgiving				3rd thursday in november?
	// black friday??
	// Eid
	// Diwali
	// Boxing Day				26th December
	// Hannukah					december
	// Passover					
	// Chinese New Year
	// Kwanzaa
	// MLK Day
	
	
	// burns night				25h January
	// rememberence sunday could be from the 8th to the 14th november
	else {
		strcpy(t_buffer, "\0");
		strcpy(b_buffer, "\0");
	}
}

void setColours(int colour_background, int colour_hour, int colour_minute) {
	if(colour_background || colour_hour) {
		GColor bg_colour = GColorFromHEX(colour_background);
		window_set_background_color(s_window, bg_colour);			// Set Background Colour
		#if defined(PBL_COLOR)
			text_layer_set_text_color(s_text_hour, GColorFromHEX(colour_hour));		// Set Hour Colour
			text_layer_set_text_color(s_text_minute, GColorFromHEX(colour_minute));	// Set Minute Colour
		#elif defined(PBL_BW)	
			text_layer_set_text_color(s_text_hour, gcolor_legible_over(bg_colour));	// Set Hour Colour
			text_layer_set_text_color(s_text_minute, gcolor_legible_over(bg_colour));	// Set Minute Colour
		#endif
	} else {
		window_set_background_color(s_window, GColorBlack);			// Set Background Colour
		text_layer_set_text_color(s_text_hour, GColorWhite);		// Set Hour Colour
		text_layer_set_text_color(s_text_minute, GColorWhite);		// Set Minute Colour
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////// Callbacks ///////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////

void unobstructed_change(AnimationProgress progress, void* data) {
	GRect bounds = layer_get_unobstructed_bounds(window_get_root_layer(s_window));

	layer_set_frame(text_layer_get_layer(s_text_hour),GRect(				 0, bounds.size.h / 2 - 47, bounds.size.w/2, 75));
	layer_set_frame(text_layer_get_layer(s_text_minute),GRect(bounds.size.w/2, bounds.size.h / 2 - 47, bounds.size.w/2, 75));
	layer_set_frame(text_layer_get_layer(s_text_top),GRect(				 0, bounds.size.h / 4 - 31, bounds.size.w,   30));
	layer_set_frame(text_layer_get_layer(s_text_bottom),GRect(				 0, bounds.size.h * 3/4 -5, bounds.size.w,   30));
}

static void battery_callback(BatteryChargeState state) {
	char *select_battery_percent = "";
 	persist_read_string(MESSAGE_KEY_SELECT_BATTERY_PERCENT,select_battery_percent,5);
	int select_battery_percent_int = atoi(select_battery_percent);
	int colour_background = persist_read_int(MESSAGE_KEY_COLOUR_BACKGROUND);
	
	if(!select_battery_percent_int == 0) { // Show
		if(select_battery_percent_int == 100) { // Always show battery
			if(75 < state.charge_percent && state.charge_percent <= 100) {
				getBatteryIcon(colour_background, 3);
			} else if(50 < state.charge_percent && state.charge_percent <= 75) {
				getBatteryIcon(colour_background, 2);
			} else if(25 < state.charge_percent && state.charge_percent <= 50) {
				getBatteryIcon(colour_background, 1);
			} else if( 0 < state.charge_percent && state.charge_percent <= 25) {
				getBatteryIcon(colour_background, 0);
			}
			layer_set_hidden(bitmap_layer_get_layer(s_layer_battery), false);	// Visible
		} else {
			if(state.charge_percent < select_battery_percent_int) {
				getBatteryIcon(colour_background, 0);
				layer_set_hidden(bitmap_layer_get_layer(s_layer_battery), false);	// Visible
			} else {
				layer_set_hidden(bitmap_layer_get_layer(s_layer_battery), true);	// Hidden
			}
		}
	} else {
		layer_set_hidden(bitmap_layer_get_layer(s_layer_battery), true);	// Hidden
	}
}

static void bluetooth_callback(bool connected) {
	char *select_bluetooth_disconnect = "";
 	persist_read_string(MESSAGE_KEY_SELECT_BLUETOOTH_DISCONNECT,select_bluetooth_disconnect,5);
	int select_bluetooth_disconnect_int = atoi(select_bluetooth_disconnect);
	int colour_background = persist_read_int(MESSAGE_KEY_COLOUR_BACKGROUND);
	int colour_date = persist_read_int(MESSAGE_KEY_COLOUR_DATE);
	int colour_bluetooth = persist_read_int(MESSAGE_KEY_COLOUR_BLUETOOTH);
	
	if(!connected) {												
		if(colour_background || colour_bluetooth) {	// Disconected with config
			text_layer_set_text_color(s_text_top, PBL_IF_BW_ELSE(gcolor_legible_over(GColorFromHEX(colour_background)), GColorFromHEX(colour_bluetooth)));		// Set Top Colour
			text_layer_set_text_color(s_text_bottom, PBL_IF_BW_ELSE(gcolor_legible_over(GColorFromHEX(colour_background)), GColorFromHEX(colour_bluetooth)));	// Set Bottom Colour
		} else { 									// Disconnected and no config
			text_layer_set_text_color(s_text_top, PBL_IF_BW_ELSE(GColorBlack, GColorRed));		// Set Top Colour
			text_layer_set_text_color(s_text_bottom, PBL_IF_BW_ELSE(GColorBlack, GColorRed));	// Set Bottom Colour
		}
		if(select_bluetooth_disconnect_int == 0) { }		// No vibration 
		else if(select_bluetooth_disconnect_int == 1) { vibes_short_pulse(); }		// Short vibration
		else if(select_bluetooth_disconnect_int == 2) { vibes_long_pulse(); }	// Long vibration
		else if(select_bluetooth_disconnect_int == 3) { vibes_double_pulse(); }	// Double vibration
		else { vibes_long_pulse(); }	// Default
	} else {														// Connected
		if(colour_background || colour_date) {
			text_layer_set_text_color(s_text_top, PBL_IF_BW_ELSE(gcolor_legible_over(GColorFromHEX(colour_background)), GColorFromHEX(colour_date)));		// Set Top Colour
			text_layer_set_text_color(s_text_bottom, PBL_IF_BW_ELSE(gcolor_legible_over(GColorFromHEX(colour_background)), GColorFromHEX(colour_date)));	// Set Bottom Colour
		} else {
			text_layer_set_text_color(s_text_top, GColorWhite);		// Set Top Colour
			text_layer_set_text_color(s_text_bottom, GColorWhite);		// Set Bottom Colour
		}
	}
}

static void inbox_received_handler(DictionaryIterator *iter, void *context) {
// Colours
	Tuple *colour_background_t = dict_find(iter, MESSAGE_KEY_COLOUR_BACKGROUND);
	int colour_background = colour_background_t->value->int32;
	persist_write_int(MESSAGE_KEY_COLOUR_BACKGROUND, colour_background);
	int colour_hour = 0, colour_minute = 0;
	#if defined(PBL_COLOR)
		Tuple *colour_hour_t = dict_find(iter, MESSAGE_KEY_COLOUR_HOUR);
		Tuple *colour_minute_t = dict_find(iter, MESSAGE_KEY_COLOUR_MINUTE);
		Tuple *colour_date_t = dict_find(iter, MESSAGE_KEY_COLOUR_DATE);
			colour_hour = colour_hour_t->value->int32;
			colour_minute = colour_minute_t->value->int32;
		int colour_date = colour_date_t->value->int32;
		persist_write_int(MESSAGE_KEY_COLOUR_HOUR, colour_hour);
		persist_write_int(MESSAGE_KEY_COLOUR_MINUTE, colour_minute);
		persist_write_int(MESSAGE_KEY_COLOUR_DATE, colour_date);
// Bluetooth
		Tuple *colour_bluetooth_t = dict_find(iter, MESSAGE_KEY_COLOUR_BLUETOOTH);
		int colour_bluetooth = colour_bluetooth_t->value->int32;
		persist_write_int(MESSAGE_KEY_COLOUR_BLUETOOTH, colour_bluetooth);
	#endif
	Tuple *select_bluetooth_diconnect_t = dict_find(iter, MESSAGE_KEY_SELECT_BLUETOOTH_DISCONNECT);
	char *select_bluetooth_disconnect = select_bluetooth_diconnect_t->value->cstring;<commit_msg>Improved how config settings are used<commit_after>#include <pebble.h>

uint32_t key = 0;	// Used to determine whether AppMessages have been recieved

static Window *s_window;
static Layer *s_layer_time_date;
static TextLayer *s_text_hour, *s_text_minute, *s_text_top, *s_text_bottom;
static GFont s_font_time, s_font_date;
static BitmapLayer *s_layer_battery;
static GBitmap *s_bitmap_battery;
char date_current[5], month_current[16];

//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////// Methods /////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////

void getBatteryIcon(int colour_background, int image_number) {
	int BatteryBlack[] = {RESOURCE_ID_IMAGE_BATTERY_BLACK_1, RESOURCE_ID_IMAGE_BATTERY_BLACK_2, RESOURCE_ID_IMAGE_BATTERY_BLACK_3, RESOURCE_ID_IMAGE_BATTERY_BLACK_4};
	int BatteryWhite[] = {RESOURCE_ID_IMAGE_BATTERY_WHITE_1, RESOURCE_ID_IMAGE_BATTERY_WHITE_2, RESOURCE_ID_IMAGE_BATTERY_WHITE_3, RESOURCE_ID_IMAGE_BATTERY_WHITE_4};
	
	gbitmap_destroy(s_bitmap_battery);
	if(persist_read_bool(key)) {
		if (gcolor_equal(gcolor_legible_over(GColorFromHEX(colour_background)), GColorBlack)) {
			s_bitmap_battery = gbitmap_create_with_resource(BatteryBlack[image_number]);
		} else {
			s_bitmap_battery = gbitmap_create_with_resource(BatteryWhite[image_number]);
		}
	} else {
		s_bitmap_battery = gbitmap_create_with_resource(BatteryWhite[image_number]);
	}
	bitmap_layer_set_bitmap(s_layer_battery, s_bitmap_battery);
}

void customText(char t_buffer[16], char b_buffer[16]) {
	int check_date_0 = persist_read_int(MESSAGE_KEY_CHECK_DATE);
	int check_date_1 = persist_read_int(MESSAGE_KEY_CHECK_DATE+1);
	int check_date_2 = persist_read_int(MESSAGE_KEY_CHECK_DATE+2);

	if(strcmp("0104", date_current) == 0) {		// If date is 1st April, Display "April Fools" on bottom
		strcpy(t_buffer, "\0");
		strcpy(b_buffer, "April  Fools");
	} else if(check_date_0 == 1 && strcmp("0101", date_current) == 0) {  // New Years
		strcpy(t_buffer, "Happy");
		strcpy(b_buffer, "New  Year");
	} else if(check_date_1 == 1 && strcmp("3110", date_current) == 0) {  // Halloween
		strcpy(t_buffer, "\0");
		strcpy(b_buffer, "Halloween");
	} else if(check_date_2 == 1 && strcmp("2512", date_current) == 0) {  // Christmas
		strcpy(t_buffer, "Merry");
		strcpy(b_buffer, "Christmas");
	}
	
	// Valentines				14th February
	// Earth Day				
	// Mother's Day US			May ish
	// Mother's Day UK			March ish
	// Father's Day US			
	// Father's Day UK			June ish
	// Independence Day (US)	4th July
	// Independence Day (MEX)
	// Canada Day				
	// Victoria Day (Canada)	
	// Thanksgiving				3rd thursday in november?
	// black friday??
	// Eid
	// Diwali
	// Boxing Day				26th December
	// Hannukah					december
	// Passover					
	// Chinese New Year
	// Kwanzaa
	// MLK Day
	
	
	// burns night				25h January
	// rememberence sunday could be from the 8th to the 14th november
	else {
		strcpy(t_buffer, "\0");
		strcpy(b_buffer, "\0");
	}
}

void setColours(int colour_background, int colour_hour, int colour_minute) {
	if(persist_read_bool(key)) {
		GColor bg_colour = GColorFromHEX(colour_background);
		window_set_background_color(s_window, bg_colour);			// Set Background Colour
		#if defined(PBL_COLOR)
			text_layer_set_text_color(s_text_hour, GColorFromHEX(colour_hour));		// Set Hour Colour
			text_layer_set_text_color(s_text_minute, GColorFromHEX(colour_minute));	// Set Minute Colour
		#elif defined(PBL_BW)	
			text_layer_set_text_color(s_text_hour, gcolor_legible_over(bg_colour));	// Set Hour Colour
			text_layer_set_text_color(s_text_minute, gcolor_legible_over(bg_colour));	// Set Minute Colour
		#endif
	} else {
		window_set_background_color(s_window, GColorBlack);			// Set Background Colour
		text_layer_set_text_color(s_text_hour, GColorWhite);		// Set Hour Colour
		text_layer_set_text_color(s_text_minute, GColorWhite);		// Set Minute Colour
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////// Callbacks ///////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////

void unobstructed_change(AnimationProgress progress, void* data) {
	GRect bounds = layer_get_unobstructed_bounds(window_get_root_layer(s_window));

	layer_set_frame(text_layer_get_layer(s_text_hour),GRect(				 0, bounds.size.h / 2 - 47, bounds.size.w/2, 75));
	layer_set_frame(text_layer_get_layer(s_text_minute),GRect(bounds.size.w/2, bounds.size.h / 2 - 47, bounds.size.w/2, 75));
	layer_set_frame(text_layer_get_layer(s_text_top),GRect(				 0, bounds.size.h / 4 - 31, bounds.size.w,   30));
	layer_set_frame(text_layer_get_layer(s_text_bottom),GRect(				 0, bounds.size.h * 3/4 -5, bounds.size.w,   30));
}

static void battery_callback(BatteryChargeState state) {
	char *select_battery_percent = "";
 	persist_read_string(MESSAGE_KEY_SELECT_BATTERY_PERCENT,select_battery_percent,5);
	int select_battery_percent_int = atoi(select_battery_percent);
	int colour_background = persist_read_int(MESSAGE_KEY_COLOUR_BACKGROUND);
	
	if(!select_battery_percent_int == 0) { // Show
		if(select_battery_percent_int == 100) { // Always show battery
			if(75 < state.charge_percent && state.charge_percent <= 100) {
				getBatteryIcon(colour_background, 3);
			} else if(50 < state.charge_percent && state.charge_percent <= 75) {
				getBatteryIcon(colour_background, 2);
			} else if(25 < state.charge_percent && state.charge_percent <= 50) {
				getBatteryIcon(colour_background, 1);
			} else if( 0 < state.charge_percent && state.charge_percent <= 25) {
				getBatteryIcon(colour_background, 0);
			}
			layer_set_hidden(bitmap_layer_get_layer(s_layer_battery), false);	// Visible
		} else {
			if(state.charge_percent < select_battery_percent_int) {
				getBatteryIcon(colour_background, 0);
				layer_set_hidden(bitmap_layer_get_layer(s_layer_battery), false);	// Visible
			} else {
				layer_set_hidden(bitmap_layer_get_layer(s_layer_battery), true);	// Hidden
			}
		}
	} else {
		layer_set_hidden(bitmap_layer_get_layer(s_layer_battery), true);	// Hidden
	}
}

static void bluetooth_callback(bool connected) {													  	
	if(!connected) {
		char *select_bluetooth_disconnect = "";
 		persist_read_string(MESSAGE_KEY_SELECT_BLUETOOTH_DISCONNECT,select_bluetooth_disconnect,5);
		int select_bluetooth_disconnect_int = atoi(select_bluetooth_disconnect);
		
	if(persist_read_bool(key)) {	// Disconected with config
			GColor bg_colour = GColorFromHEX(persist_read_int(MESSAGE_KEY_COLOUR_BACKGROUND));
			GColor bt_colour = GColorFromHEX(persist_read_int(MESSAGE_KEY_COLOUR_BLUETOOTH));
			text_layer_set_text_color(s_text_top, PBL_IF_BW_ELSE(gcolor_legible_over(bg_colour), bt_colour));		// Set Top Colour
			text_layer_set_text_color(s_text_bottom, PBL_IF_BW_ELSE(gcolor_legible_over(bg_colour), bt_colour));	// Set Bottom Colour
		} else { 									// Disconnected and no config
			text_layer_set_text_color(s_text_top, PBL_IF_BW_ELSE(GColorBlack, GColorRed));		// Set Top Colour
			text_layer_set_text_color(s_text_bottom, PBL_IF_BW_ELSE(GColorBlack, GColorRed));	// Set Bottom Colour
		}
		if(select_bluetooth_disconnect_int == 0) { }		// No vibration 
		else if(select_bluetooth_disconnect_int == 1) { vibes_short_pulse(); }		// Short vibration
		else if(select_bluetooth_disconnect_int == 2) { vibes_long_pulse(); }	// Long vibration
		else if(select_bluetooth_disconnect_int == 3) { vibes_double_pulse(); }	// Double vibration
		else { vibes_long_pulse(); }	// Default
	} else {														// Connected
	if(persist_read_bool(key)) {
			GColor bg_colour = GColorFromHEX(persist_read_int(MESSAGE_KEY_COLOUR_BACKGROUND));
			GColor dt_colour = GColorFromHEX(persist_read_int(MESSAGE_KEY_COLOUR_DATE));
			text_layer_set_text_color(s_text_top, PBL_IF_BW_ELSE(gcolor_legible_over(bg_colour), dt_colour));		// Set Top Colour
			text_layer_set_text_color(s_text_bottom, PBL_IF_BW_ELSE(gcolor_legible_over(bg_colour), dt_colour));	// Set Bottom Colour
		} else {
			text_layer_set_text_color(s_text_top, GColorWhite);		// Set Top Colour
			text_layer_set_text_color(s_text_bottom, GColorWhite);	// Set Bottom Colour
		}
	}
}

static void inbox_received_handler(DictionaryIterator *iter, void *context) {
	persist_write_bool(key, true);
// Colours
	Tuple *colour_background_t = dict_find(iter, MESSAGE_KEY_COLOUR_BACKGROUND);
	int colour_background = colour_background_t->value->int32;
	persist_write_int(MESSAGE_KEY_COLOUR_BACKGROUND, colour_background);
	int colour_hour = 0, colour_minute = 0;
	#if defined(PBL_COLOR)
		Tuple *colour_hour_t = dict_find(iter, MESSAGE_KEY_COLOUR_HOUR);
		Tuple *colour_minute_t = dict_find(iter, MESSAGE_KEY_COLOUR_MINUTE);
		Tuple *colour_date_t = dict_find(iter, MESSAGE_KEY_COLOUR_DATE);
			colour_hour = colour_hour_t->value->int32;
			colour_minute = colour_minute_t->value->int32;
		int colour_date = colour_date_t->value->int32;
		persist_write_int(MESSAGE_KEY_COLOUR_HOUR, colour_hour);
		persist_write_int(MESSAGE_KEY_COLOUR_MINUTE, colour_minute);
		persist_write_int(MESSAGE_KEY_COLOUR_DATE, colour_date);
// Bluetooth
		Tuple *colour_bluetooth_t = dict_find(iter, MESSAGE_KEY_COLOUR_BLUETOOTH);
		int colour_bluetooth = colour_bluetooth_t->value->int32;
		persist_write_int(MESSAGE_KEY_COLOUR_BLUETOOTH, colour_bluetooth);
	#endif
	Tuple *select_bluetooth_diconnect_t = dict_find(iter, MESSAGE_KEY_SELECT_BLUETOOTH_DISCONNECT);
	char *select_bluetooth_disconnect = select_bluetooth_diconnect_t->value->cstring;
{'fraction_non_alphanumeric': 0.157786038826931, 'fraction_numerical': 0.013682362660057828, 'mean_word_length': 3.989438433797012, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2520550', 'n_tokens_mistral': 7555, 'n_tokens_neox': 6793, 'n_words': 1095}