Skip to content

Commit

Permalink
version 1.2 with Config page
Browse files Browse the repository at this point in the history
  • Loading branch information
wildhart committed Jan 20, 2016
1 parent 32b3b07 commit 400df82
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 23 deletions.
5 changes: 4 additions & 1 deletion appinfo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"appKeys": {
"KEY_ALARM": 2,
"KEY_APP_VERSION": 5,
"KEY_CONFIG_DATA": 0,
"KEY_EXPORT": 6,
"KEY_MEDICATIONS": 100,
"KEY_MODE": 1,
"KEY_SORT": 3,
Expand Down Expand Up @@ -31,7 +34,7 @@
"shortName": "Meds Timer",
"uuid": "95d07e1a-2451-4ffa-aa44-836e523a7648",
"versionCode": 1,
"versionLabel": "1.1",
"versionLabel": "1.2",
"watchapp": {
"watchface": false
}
Expand Down
2 changes: 1 addition & 1 deletion src/job_adjust.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static void initialise_ui(void) {
layer_add_child(window_get_root_layer(s_window), (Layer *)s_actionbarlayer);

// s_textlayer_name
s_textlayer_name = text_layer_create(GRect(5, 7, 100, 24));
s_textlayer_name = text_layer_create(GRect(5, 7, 100, 30));
text_layer_set_font(s_textlayer_name, s_res_gothic_24_bold);
layer_add_child(window_get_root_layer(s_window), (Layer *)s_textlayer_name);

Expand Down
14 changes: 12 additions & 2 deletions src/jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ static void jobs_list_append_job(const char* name, time_t seconds, uint8_t repea
}
LOG("appended job: %s, seconds=%ld, repeat=%d, fixed=%d", new_job->Name, new_job->Seconds, new_job->Repeat_hrs, new_job->Fixed);
jobs_count++;
main_save_data();
}

void jobs_list_sort(void) {
Expand Down Expand Up @@ -176,8 +175,8 @@ static void callback(const char* result, size_t result_length, void* extra) {
jobs_list_append_job(result, time(NULL), 0, 0);
} else {
snprintf(jobs_list_get_index(index)->Name,JOB_NAME_LENGTH, result);
main_save_data();
}
main_save_data();
main_menu_update();
}

Expand All @@ -189,6 +188,17 @@ void jobs_rename_job(uint8_t index) {
tertiary_text_prompt(jobs_get_job_name(index), callback, (void*) (int) index);
}

void jobs_delete_all_jobs(void) {
Job_ptr* job_ptr = first_job_ptr;
while (first_job_ptr) {
Job_ptr * next_job=first_job_ptr->Next_ptr;
free(first_job_ptr->Job);
free(first_job_ptr);
first_job_ptr=next_job;
}
jobs_count=0;
}

void jobs_delete_job_and_save(uint8_t index) {
if (index>=jobs_count) return;

Expand Down
1 change: 1 addition & 0 deletions src/jobs.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ void jobs_list_load(uint8_t first_key, const uint8_t version);
void jobs_list_write_dict(DictionaryIterator *iter, uint8_t first_key);
void jobs_list_read_dict(DictionaryIterator *iter, uint8_t first_key, const uint8_t version);

void jobs_delete_all_jobs(void);
void jobs_delete_job_and_save(uint8_t index);
void jobs_add_job();
void jobs_rename_job(uint8_t index);
Expand Down
42 changes: 34 additions & 8 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#include "main.h"

#include "pebble_process_info.h"
extern const PebbleProcessInfo __pbl_app_info;
#define APP_VERSION_LENGTH 10
char app_version[APP_VERSION_LENGTH];

GBitmap *bitmap_matrix;
//GBitmap *bitmap_pause;
GBitmap *bitmap_play;
GBitmap *bitmap_add;
//GBitmap *bitmap_settings;
GBitmap *bitmap_settings;
GBitmap *bitmap_delete;
GBitmap *bitmap_edit;
GBitmap *bitmap_adjust;
Expand All @@ -16,36 +21,46 @@ Settings settings={MODE_COUNT_UP, false /*alarm*/, true /*sort*/};
static bool JS_ready = false;
static bool data_loaded_from_watch = false;
uint8_t stored_version=0;
bool export_after_save=false;

// *****************************************************************************************************
// MESSAGES
// *****************************************************************************************************

#define KEY_MEDICATIONS 100
#define KEY_CONFIG_DATA 0
#define KEY_MODE 1
#define KEY_ALARM 2
#define KEY_SORT 3
#define KEY_VERSION 4
#define KEY_APP_VERSION 5
#define KEY_EXPORT 6

static void inbox_received_handler(DictionaryIterator *iter, void *context) {
LOG("Inbox received...");
JS_ready = true;
Tuple *tuple_t;

bool new_data_from_config_page = dict_find(iter, KEY_CONFIG_DATA);

if (!data_loaded_from_watch) {
if (!data_loaded_from_watch || new_data_from_config_page) {
LOG("Loading settings from phone...");
tuple_t=dict_find(iter, KEY_VERSION); stored_version = (tuple_t) ? tuple_t->value->int32 : 1;
tuple_t=dict_find(iter, KEY_MODE); if (tuple_t) settings.Mode = tuple_t->value->int32;
tuple_t=dict_find(iter, KEY_ALARM); if (tuple_t) settings.Alarm = tuple_t->value->int8 > 0; // convert int to boolean
tuple_t=dict_find(iter, KEY_SORT); if (tuple_t) settings.Sort = tuple_t->value->int8 > 0; // convert int to boolean
jobs_delete_all_jobs();
jobs_list_read_dict(iter, KEY_MEDICATIONS, stored_version);

if (stored_version < CURRENT_STORAGE_VERSION) update_show(stored_version);

if (stored_version < CURRENT_STORAGE_VERSION) {
update_show(stored_version);
stored_version = CURRENT_STORAGE_VERSION;
}
main_save_data();
main_menu_highlight_top();
}

LOG("Inbox processed.");
main_save_data();
main_menu_highlight_top();
}

static void send_settings_to_phone() {
Expand All @@ -54,12 +69,19 @@ static void send_settings_to_phone() {
app_message_outbox_begin(&iter);
int dummy_int;

dict_write_cstring(iter, KEY_APP_VERSION, app_version);
dummy_int=CURRENT_STORAGE_VERSION; dict_write_int(iter, KEY_VERSION, &dummy_int, sizeof(int), true);
dummy_int=settings.Mode; dict_write_int(iter, KEY_MODE, &dummy_int, sizeof(int), true);
dummy_int=settings.Alarm; dict_write_int(iter, KEY_ALARM, &dummy_int, sizeof(int), true);
dummy_int=settings.Sort; dict_write_int(iter, KEY_SORT, &dummy_int, sizeof(int), true);
jobs_list_write_dict(iter, KEY_MEDICATIONS);

if (export_after_save) {
dummy_int=true;
dict_write_int(iter, KEY_EXPORT, &dummy_int, sizeof(int), true);
export_after_save=false;
}

dict_write_end(iter);
app_message_outbox_send();
}
Expand All @@ -85,6 +107,7 @@ void main_wakeup_set() {
// *****************************************************************************************************

void main_save_data(void) {
data_loaded_from_watch = true;
persist_write_int(STORAGE_KEY_VERSION, CURRENT_STORAGE_VERSION);
persist_write_data(STORAGE_KEY_SETTINGS, &settings, sizeof(Settings));
if (settings.Sort) jobs_list_sort();
Expand Down Expand Up @@ -113,12 +136,15 @@ static void main_load_data(void) {

void init(void) {
//WARN("Ignoring saved data!!");

snprintf(app_version,APP_VERSION_LENGTH,"%d.%d",__pbl_app_info.process_version.major, __pbl_app_info.process_version.minor);

main_load_data();
bitmap_matrix=gbitmap_create_with_resource(RESOURCE_ID_IMAGE_ICON_MATRIX);
//bitmap_pause=gbitmap_create_as_sub_bitmap(bitmap_matrix, ICON_RECT_PAUSE);
bitmap_play=gbitmap_create_as_sub_bitmap(bitmap_matrix, ICON_RECT_PLAY);
bitmap_add=gbitmap_create_as_sub_bitmap(bitmap_matrix, ICON_RECT_ADD);
//bitmap_settings=gbitmap_create_as_sub_bitmap(bitmap_matrix, ICON_RECT_SETTINGS);
bitmap_settings=gbitmap_create_as_sub_bitmap(bitmap_matrix, ICON_RECT_SETTINGS);
bitmap_delete=gbitmap_create_as_sub_bitmap(bitmap_matrix, ICON_RECT_DELETE);
bitmap_edit=gbitmap_create_as_sub_bitmap(bitmap_matrix, ICON_RECT_EDIT);
bitmap_adjust=gbitmap_create_as_sub_bitmap(bitmap_matrix, ICON_RECT_ADJUST);
Expand All @@ -127,7 +153,7 @@ void init(void) {
bitmap_tick=gbitmap_create_as_sub_bitmap(bitmap_matrix, ICON_RECT_TICK);
main_menu_show();
if (data_loaded_from_watch && stored_version < CURRENT_STORAGE_VERSION) update_show(stored_version);

app_message_register_inbox_received(inbox_received_handler);
app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum());
}
Expand Down
6 changes: 4 additions & 2 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ extern GBitmap *bitmap_matrix;
//extern GBitmap *bitmap_pause;
extern GBitmap *bitmap_play;
extern GBitmap *bitmap_add;
//extern GBitmap *bitmap_settings;
extern GBitmap *bitmap_settings;
extern GBitmap *bitmap_delete;
extern GBitmap *bitmap_edit;
extern GBitmap *bitmap_adjust;
Expand All @@ -75,8 +75,9 @@ extern GBitmap *bitmap_tick;
#define STORAGE_KEY_SETTINGS 2
#define STORAGE_KEY_FIRST_MED 100

#define CURRENT_STORAGE_VERSION 2
#define CURRENT_STORAGE_VERSION 3
//changes in storage version: 2 added bool Fixed to end of every Job struct
//changes in storage version: 3 configuration

typedef struct {
uint8_t Mode;
Expand All @@ -85,6 +86,7 @@ typedef struct {
} Settings;

extern Settings settings;
extern bool export_after_save;

enum {
MODE_COUNT_UP,
Expand Down
14 changes: 9 additions & 5 deletions src/main_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ enum { // main menu structure
MENU_SETTINGS_MODE=MENU_SECTION_SETTINGS*100,
MENU_SETTINGS_ALARM,
MENU_SETTINGS_SORT,
NUM_MENU_ITEMS_SETTINGS=3
MENU_SETTINGS_CONFIG,
NUM_MENU_ITEMS_SETTINGS=4
};

static uint16_t menu_get_num_sections_callback(MenuLayer *menu_layer, void *data) {
Expand Down Expand Up @@ -133,6 +134,7 @@ static void menu_draw_row_callback(GContext* ctx, const Layer *cell_layer, MenuI
break;
case MENU_SETTINGS_ALARM: menu_cell_draw_setting(ctx, cell_layer, "Alarm", settings.Alarm ? "YES" : "NO",NULL); break;
case MENU_SETTINGS_SORT: menu_cell_draw_setting(ctx, cell_layer, "Sort", settings.Sort ? "YES" : "NO",NULL); break;
case MENU_SETTINGS_CONFIG: menu_cell_draw_other(ctx, cell_layer, "Config/Donate", NULL , bitmap_settings); break;
}
}
}
Expand All @@ -141,27 +143,29 @@ static void menu_select_callback(MenuLayer *menu_layer, MenuIndex *cell_index, v
switch (cell_index->section) {
case MENU_SECTION_JOBS:
job_menu_show(cell_index->row);
//jobs_reset_and_save(cell_index->row);
//menu_layer_reload_data(s_menulayer);
break;
default:
switch (MENU_SECTION_CELL) {
case MENU_OTHER_ADD: jobs_add_job(); break;
case MENU_SETTINGS_MODE:
settings.Mode = (settings.Mode + 1) % 3;
main_save_data();
menu_layer_reload_data(s_menulayer);
main_save_data();
break;
case MENU_SETTINGS_ALARM:
settings.Alarm = !settings.Alarm;
main_save_data();
menu_layer_reload_data(s_menulayer);
main_save_data();
break;
case MENU_SETTINGS_SORT:
settings.Sort = !settings.Sort;
main_save_data();
menu_layer_reload_data(s_menulayer);
break;
case MENU_SETTINGS_CONFIG:
export_after_save=true;
main_save_data();
break;
}
}
}
Expand Down
82 changes: 80 additions & 2 deletions src/pebble-js-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Pebble.addEventListener('ready', function() {
console.log('PebbleKit JS ready!');
var settings=localStorage.getItem("settings");
//settings='{"101":"Paracetamol|1452934053|6|0","102":"Ibuprofin|1452934051|12|0","103":"Zopiclone|1452898015|24|1","104":"Omeprazole|1452931452|24|1","KEY_SORT":1,"KEY_MEDICATIONS":"Tremadol|1452913171|8|0","KEY_VERSION":2,"KEY_MODE":2,"KEY_ALARM":1}';
//settings='{"1":2,"2":1,"3":1,"4":3,"5":"1.2","100":"Ibuprofin|1453318208|12|1","101":"Omeprazole|1453275012|24|1","102":"Paracetamol|1453347465|6|0","103":"Tremadol|1453347472|8|0","104":"Zopiclone|1453330855|24|1","KEY_MODE":2,"KEY_ALARM":1,"KEY_SORT":1,"KEY_VERSION":3,"KEY_APP_VERSION":"1.2","KEY_MEDICATIONS":"Ibuprofin|1453318208|12|1"}';
var dict=settings ? JSON.parse(settings) : {};
Pebble.sendAppMessage(dict, function() {
console.log('Send successful: ' + JSON.stringify(dict));
Expand All @@ -14,5 +14,83 @@ Pebble.addEventListener('ready', function() {

Pebble.addEventListener("appmessage", function(e) {
console.log("Received message: " + JSON.stringify(e.payload));
localStorage.setItem("settings",JSON.stringify(e.payload));
if (e.payload.KEY_EXPORT) {
delete e.payload.KEY_EXPORT;
localStorage.setItem("settings",JSON.stringify(e.payload));
showConfiguration();
} else {
localStorage.setItem("settings",JSON.stringify(e.payload));
}
});

function padZero(i) {
return (i<10 ? "0":"")+i;
}

Pebble.addEventListener('showConfiguration', showConfiguration);

function showConfiguration() {
var url = 'https://wildhart.github.io/med-timer-config/index.html';
var settings=localStorage.getItem("settings");
var config="";
if (settings) {
settings=JSON.parse(settings);
config+="?app_version="+(settings.KEY_APP_VERSION ? settings.KEY_APP_VERSION : "1.2");
config+="&data_version="+settings.KEY_VERSION;
config+="&mode="+settings.KEY_MODE;
config+="&alarm="+settings.KEY_ALARM;
config+="&sort="+settings.KEY_SORT;

var med=0;
var setting;
while (setting=(med===0) ? settings.KEY_MEDICATIONS : settings[100+med]) {
setting=setting.split("|");
var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
d.setUTCSeconds(setting[1]*1.0 +setting[2]*3600);
var secs=d.getSeconds();
d.setUTCSeconds(d.getTimezoneOffset()*60); // this sets seconds to zero
setting[1]=padZero(d.getHours())+":"+padZero(d.getMinutes());
setting.push(secs);
config+="&med_"+med+"="+encodeURIComponent(setting.join("|"));
med++;
}
}
console.log('Showing configuration page: ' + url + config);

Pebble.openURL(url+config);
}

Pebble.addEventListener('webviewclosed', function(e) {
var configData = JSON.parse(decodeURIComponent(e.response));
console.log('Configuration page returned: ' + JSON.stringify(configData));

var dict = {};
dict.KEY_CONFIG_DATA = 1;
dict.KEY_MODE = configData.mode;
dict.KEY_ALARM = configData.alarm ? 1 : 0; // Send a boolean as an integer
dict.KEY_SORT = configData.sort ? 1 : 0; // Send a boolean as an integer
dict.KEY_VERSION = configData.data_version;

var med=0;
while (configData["med_"+med]) {
var data=decodeURIComponent(configData["med_"+med]).split("|");
var hhmm=data[1].split(":");
var d = new Date(); // The 0 there is the key, which sets the date to the epoch
d.setHours(hhmm[0]);
d.setMinutes(hhmm[1]);
d.setSeconds(data[4]);
d.setMilliseconds(0);
if (d < new Date()) d.setHours(hhmm[0]*1.0 + 24);
var secs=d.getTime()/1000;
data[1]=secs - d.getTimezoneOffset()*60 - data[2]*3600;
dict[100+med]=data.join("|");
med++;
}

// Send to watchapp
Pebble.sendAppMessage(dict, function() {
console.log('Send successful: ' + JSON.stringify(dict));
}, function() {
console.log('Send failed!');
});
});
4 changes: 2 additions & 2 deletions src/update.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static void initialise_ui(void) {
#endif

s_res_gothic_28_bold = fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD);
s_res_gothic_14 = fonts_get_system_font(FONT_KEY_GOTHIC_14);
s_res_gothic_14 = fonts_get_system_font(FONT_KEY_GOTHIC_18);
// s_textlayer_heading
s_textlayer_heading = text_layer_create(GRect(2, -1, 140, 28));
text_layer_set_text(s_textlayer_heading, "New Feature:");
Expand All @@ -24,7 +24,7 @@ static void initialise_ui(void) {

// s_textlayer_features
s_textlayer_features = text_layer_create(GRect(2, 27, 140, 125));
text_layer_set_text(s_textlayer_features, "* Medications can have FIXED time periods (timer starts from when pill SHOULD have been taken) or FLEXIBLE periods (timer starts from when pill was actually taken.");
text_layer_set_text(s_textlayer_features, "* Configuration page for easier medication entry.\n\n * DONATIONS can be accepted from the config page!");
text_layer_set_font(s_textlayer_features, s_res_gothic_14);
layer_add_child(window_get_root_layer(s_window), (Layer *)s_textlayer_features);
}
Expand Down

0 comments on commit 400df82

Please sign in to comment.