Browse code

Implement the new auto-close feature

Natasha Kerensikova authored on23/04/2016 23:55:15
Showing3 changed files
... ...
@@ -17,7 +17,8 @@
17 17
     "uploadDone": 130,
18 18
     "uploadStart": 140,
19 19
     "dataKey": 210,
20
-    "dataLine": 220
20
+    "dataLine": 220,
21
+    "cfgAutoClose": 310
21 22
   },
22 23
   "resources": {
23 24
     "media": []
... ...
@@ -209,9 +209,10 @@ Pebble.addEventListener("webviewclosed", function(e) {
209 209
       localStorage.setItem("cfgSignKeyFormat", cfg_sign_key_format);
210 210
    }
211 211
 
212
-   if (configData.autoClose) {
212
+   if (configData.autoClose !== null) {
213 213
       cfg_auto_close = configData.autoClose;
214 214
       localStorage.setItem("cfgAutoClose", cfg_auto_close);
215
+      Pebble.sendAppMessage({ "cfgAutoClose": cfg_auto_close ? 1 : 0 });
215 216
    }
216 217
 
217 218
    console.log(cfg_sign_field ? "Signature enabled" : "Signature disabled");
... ...
@@ -26,6 +26,7 @@
26 26
 #define MSG_KEY_UPLOAD_START	140
27 27
 #define MSG_KEY_DATA_KEY	210
28 28
 #define MSG_KEY_DATA_LINE	220
29
+#define MSG_KEY_CFG_AUTO_CLOSE	310
29 30
 
30 31
 static Window *window;
31 32
 static TextLayer *modal_text_layer;
... ...
@@ -39,6 +40,7 @@ static bool modal_displayed = false;
39 40
 static bool display_dirty = false;
40 41
 static char global_buffer[1024];
41 42
 static bool sending_data = false;
43
+static bool auto_close = false;
42 44
 
43 45
 static struct widget {
44 46
 	char		label[64];
... ...
@@ -51,6 +53,11 @@ static struct widget {
51 53
 	time_t		start_time;
52 54
 } phone, web;
53 55
 
56
+static void
57
+close_app(void) {
58
+	window_stack_pop_all(true);
59
+}
60
+
54 61
 static void
55 62
 set_modal_mode(bool is_modal) {
56 63
 	if (is_modal == modal_displayed) return;
... ...
@@ -376,6 +383,8 @@ send_next_line(void) {
376 383
 	if (minute_index >= minute_data_size
377 384
 	    && !load_minute_data_page(minute_last)) {
378 385
 		sending_data = false;
386
+		if (auto_close && web.current_key >= phone.current_key)
387
+			close_app();
379 388
 		return;
380 389
 	}
381 390
 
... ...
@@ -441,6 +450,9 @@ inbox_received_handler(DictionaryIterator *iterator, void *context) {
441 450
 		web.current_key = tuple_uint(tuple);
442 451
 		if (!web.first_key) web.first_key = web.current_key;
443 452
 		display_dirty = true;
453
+		if (auto_close && !sending_data
454
+		    && web.current_key >= phone.current_key)
455
+			close_app();
444 456
 	}
445 457
 
446 458
 	tuple = dict_find(iterator, MSG_KEY_UPLOAD_START);
... ...
@@ -448,6 +460,15 @@ inbox_received_handler(DictionaryIterator *iterator, void *context) {
448 460
 		web.first_key = tuple_uint(tuple);
449 461
 		web.start_time = time(0);
450 462
 	}
463
+
464
+	tuple = dict_find(iterator, MSG_KEY_CFG_AUTO_CLOSE);
465
+	if (tuple) {
466
+		auto_close = (tuple_uint(tuple) != 0);
467
+		persist_write_bool(MSG_KEY_CFG_AUTO_CLOSE, auto_close);
468
+		if (auto_close && !sending_data
469
+		    && web.current_key >= phone.current_key)
470
+			close_app();
471
+	}
451 472
 }
452 473
 
453 474
 static void
... ...
@@ -474,6 +495,10 @@ tick_handler(struct tm *tick_time, TimeUnits units_changed) {
474 495
 
475 496
 static void
476 497
 init(void) {
498
+	auto_close = persist_read_bool(MSG_KEY_CFG_AUTO_CLOSE);
499
+	APP_LOG(APP_LOG_LEVEL_INFO, "auto_close initialized to %s",
500
+	    auto_close ? "true" : "false");
501
+
477 502
 	app_message_register_inbox_received(inbox_received_handler);
478 503
 	app_message_register_outbox_failed(outbox_failed_handler);
479 504
 	app_message_register_outbox_sent(outbox_sent_handler);