From a3db05c8bdf9b2e89d74db3574f7ce0aa1c1cf23 Mon Sep 17 00:00:00 2001
From: Donald Dugger <n0ano@n0ano.com>
Date: Tue, 30 Nov 2021 23:38:11 -0700
Subject: [PATCH 9/9] Convert to using dynamic include for the TZ data

This should reduce the heap space usage a lot.
---
 common/analog_face.js     |  7 +++--
 common/cfg_select.js      | 30 +++++++++++---------
 common/cfg_time.js        | 58 +++++++++++++++++++++++----------------
 common/constants.js       |  2 ++
 common/mk_table.py        |  0
 common/prefs.js           |  8 ++++++
 common/tz_data/data0.js   |  7 +++++
 common/tz_data/data1.js   |  6 ++++
 common/tz_data/data2.js   |  6 ++++
 common/tz_data/max.js     |  1 +
 common/utils.js           | 22 ---------------
 resources/cfg_select.view | 17 ++----------
 12 files changed, 89 insertions(+), 75 deletions(-)
 mode change 100755 => 100644 common/mk_table.py
 create mode 100644 common/tz_data/data0.js
 create mode 100644 common/tz_data/data1.js
 create mode 100644 common/tz_data/data2.js
 create mode 100644 common/tz_data/max.js

diff --git a/common/analog_face.js b/common/analog_face.js
index 5911c07..cf6fc51 100644
--- a/common/analog_face.js
+++ b/common/analog_face.js
@@ -5,6 +5,7 @@
 
 import clock from "clock";
 import display from "display";
+import {memory} from "system";
 import { HeartRateSensor } from "heart-rate";
 import { today } from 'user-activity';
 import { charger, battery } from "power";
@@ -114,6 +115,7 @@ export function init()
   flash.addEventListener("click", (evt) => {
     turn_off();
   })
+logger("mem: " + memory.js.used + "/" + memory.js.total);
   return 0;
 }
 
@@ -287,8 +289,8 @@ export function updateFace()
   let local = new Date();
   let tm1 = local;
   let tm2;
-  let tz1_idx = names.get_info(P.tz1).tz;
-  let tz2_idx = names.get_info(P.tz2).tz;
+  let tz1_idx = P.tz1_info.tz;
+  let tz2_idx = P.tz2_info.tz;
   if (tz1_idx > 0 || tz2_idx >= 0) {
     let off = utils.utc_off(tm1.toString());
     let utc_time = utils.utc(tm1, -off);
@@ -330,6 +332,7 @@ export function updateFace()
 
   // lower right corner
   sh_moon(local);
+//logger("mem: " + memory.js.used + "/" + memory.js.total);
 }
 
 export function updateClock()
diff --git a/common/cfg_select.js b/common/cfg_select.js
index 43f3329..4d527f2 100644
--- a/common/cfg_select.js
+++ b/common/cfg_select.js
@@ -24,6 +24,14 @@ export function init()
   utils.image_btn(screen, "ul_btn", "icons/left_btn.png", (btn) => {
     config1.sh_screen();
   });
+
+  utils.text_btn(screen, "cfg_time1", "Time1", "fb-yellow", "fb-red", (btn) => {
+    cfg_time.sh_screen(C.TZ_TILE1);
+  });
+
+  utils.text_btn(screen, "cfg_time2", "Time2", "fb-yellow", "fb-red", (btn) => {
+    cfg_time.sh_screen(C.TZ_TILE2);
+  });
   
   tz_idx = cfg_time.init();
   
@@ -32,21 +40,17 @@ export function init()
 
 export function sh_screen()
 {
-  let btn, txt;
+  let tz, txt;
 
-  txt = screen.getElementById("cfg_time1_text");
-  txt.text = "TZ1 - " + names.get_info(P.tz1).name; 
-  btn = screen.getElementById("cfg_time1");
-  btn.addEventListener("click", (evt) => {
-    cfg_time.sh_screen(C.TZ_TILE1);
-  })
+  tz = screen.getElementById("cfg_time1");
+  txt = tz.getElementById("b_text");
+  txt.text = "TZ1 - " + P.tz1_info.name;
+  txt.style.fill = "yellow";
 
-  txt = screen.getElementById("cfg_time2_text");
-  txt.text = "TZ2 - " + names.get_info(P.tz2).name;
-  btn = screen.getElementById("cfg_time2");
-  btn.addEventListener("click", (evt) => {
-    cfg_time.sh_screen(C.TZ_TILE2);
-  })
+  tz = screen.getElementById("cfg_time2");
+  txt = tz.getElementById("b_text");
+  txt.text = "TZ2 - " + P.tz2_info.name;
+  txt.style.fill = "yellow";
 
   utils.sh_screen(scr);
 }
diff --git a/common/cfg_time.js b/common/cfg_time.js
index bc35b8c..900eced 100644
--- a/common/cfg_time.js
+++ b/common/cfg_time.js
@@ -7,14 +7,13 @@ import { logger } from "../common/utils";
 import * as utils from "../common/utils";
 import { P } from "../common/prefs";
 import * as prefs from "../common/prefs";
-import * as names from "../common/names";
+import { tz_max } from "../common/tz_data/max";
 import * as cfg_select from "../common/cfg_select";
 
 let scr;
 var table_no;
 
 var hi;
-var max;
 var lineno = 0;
 var line = [];
 
@@ -32,28 +31,34 @@ export function init()
   utils.image_btn(screen, "ul_btn", "icons/up.png", (btn) => {
     if (lineno >= 5)
       lineno -= 5;
-    sh_page();
+    get_page();
   });
 
   utils.image_btn(screen, "ur_btn", "icons/down.png", (btn) => {
-    if (lineno < (max - 5))
+    if (lineno < (tz_max - 5))
       lineno += 5;
-    sh_page();
+    get_page();
   });
 
   for (let i = 0; i < 5; i++) {
-    line[i] = { name: screen.getElementById("name" + i), bg: screen.getElementById("name" + i + "_bg")};
+    line[i] = {
+      name: screen.getElementById("name" + i),
+      bg: screen.getElementById("name" + i + "_bg")
+    };
     btn = screen.getElementById("name" + i + "_touch");
     btn.addEventListener("click", (evt) => {
-      let idx = tz_index(i + lineno);
-      let info = names.get_info(idx);
-      if (info.tz != C.TZ_HEADER) {
-        if (table_no == C.TZ_TILE1)
+      let tz = TZ.data[i].tz;
+      let idx = lineno + i;
+      if (tz != C.TZ_HEADER) {
+        if (table_no == C.TZ_TILE1) {
           P.tz1 = idx;
-        else
+          P.tz1_info = TZ.data[i];
+        } else {
           P.tz2 = idx;
+          P.tz2_info = TZ.data[i];
+        }
         prefs.save();
-        hi = idx;
+        hi = lineno + i;
         sh_page();
       }
     })
@@ -69,9 +74,8 @@ export function sh_screen(no)
   table_no = no;
   pre = 2;
   hi = (no == C.TZ_TILE1) ? P.tz1 : P.tz2;
-  max = pre + names.num_cities() + names.num_countries();
   lineno = (hi < 0) ? 0 : (parseInt((pre + hi)/5) * 5);
-  sh_page();
+  get_page();
   utils.sh_screen(scr);
 }
 
@@ -96,28 +100,36 @@ function tz_index(idx)
   return 0;
 }
 
-function get_bg(idx, info)
+function get_bg(idx, tz)
 {
 
-  if (info.tz == -2)
+  if (tz == C.TZ_HEADER)
     return "blue";
   return (idx == hi) ? "grey" : "black";
 }
 
 function sh_page()
 {
-  var info;
-  var idx;
 
   for (let i = 0; i < 5; i++) {
-    idx = tz_index(i + lineno);
-    info = names.get_info(idx);
-    if (info) {
-      line[i].name.text = info.name;
-      line[i].bg.style.fill = get_bg(idx, info);
+    let idx = lineno + i;
+    if (idx < tz_max) {
+      line[i].name.text = TZ.data[i];
+      line[i].bg.style.fill = get_bg(idx);
     } else {
       line[i].name.text = "";
       line[i].bg.style.fill = "black";
     }
   }
 }
+
+function get_page()
+{
+  
+  let incl = "../common/tz_data/data" + (lineno / 5);
+logger("import:" + incl);
+  import(incl).then((TZ) => {
+logger("loading page " + pageno + " => " + TZ.data[0].name)
+    sh_page();
+  });
+}
diff --git a/common/constants.js b/common/constants.js
index 0e61a5a..999003a 100644
--- a/common/constants.js
+++ b/common/constants.js
@@ -14,6 +14,8 @@ export const TZ_CUSTOM2   = -1;
 export const TZ_HEADER  = -2;
 export const TZ_NONE    = -1;
 
+export const TZ_COMPAT1 = 200;
+
 export const TZ_TILE1   = 1;
 export const TZ_TILE2   = 2;
 
diff --git a/common/mk_table.py b/common/mk_table.py
old mode 100755
new mode 100644
diff --git a/common/prefs.js b/common/prefs.js
index 25fe837..4e1ebbb 100644
--- a/common/prefs.js
+++ b/common/prefs.js
@@ -20,6 +20,14 @@ export let P;
 let def_prefs = {
   tz1: -4,
   tz2: -2,
+  tz1_info: {
+              name: "Local",
+              tz: 0
+            },
+  tz2_info: {
+              name: "None",
+              tz: C.TZ_NONE
+            },
   rotation: 1,
   custom_tz1: 0,
   custom_tz2: 0,
diff --git a/common/tz_data/data0.js b/common/tz_data/data0.js
new file mode 100644
index 0000000..93749e3
--- /dev/null
+++ b/common/tz_data/data0.js
@@ -0,0 +1,7 @@
+export const data = [
+	{ name: "Local", tz: -1 },
+  { name: "Custom1", tz: -1 },
+  { name: " Cities", tz: -2 },
+  { name: "Anchorage", tz: 4 },
+  { name: "Atlantic", tz: 9 }
+];
diff --git a/common/tz_data/data1.js b/common/tz_data/data1.js
new file mode 100644
index 0000000..c1dd2a0
--- /dev/null
+++ b/common/tz_data/data1.js
@@ -0,0 +1,6 @@
+export let data = [
+	{ name: "Azores", tz: 12 },
+  { name: "Baker I.", tz: 1 },
+  { name: "Bangkok", tz: 20 },
+  { name: "Beijing", tz: 21 }
+];
diff --git a/common/tz_data/data2.js b/common/tz_data/data2.js
new file mode 100644
index 0000000..c1dd2a0
--- /dev/null
+++ b/common/tz_data/data2.js
@@ -0,0 +1,6 @@
+export let data = [
+	{ name: "Azores", tz: 12 },
+  { name: "Baker I.", tz: 1 },
+  { name: "Bangkok", tz: 20 },
+  { name: "Beijing", tz: 21 }
+];
diff --git a/common/tz_data/max.js b/common/tz_data/max.js
new file mode 100644
index 0000000..ab34d51
--- /dev/null
+++ b/common/tz_data/max.js
@@ -0,0 +1 @@
+export const tz_max = 9;
diff --git a/common/utils.js b/common/utils.js
index 7a1961a..4ab7338 100644
--- a/common/utils.js
+++ b/common/utils.js
@@ -5,7 +5,6 @@
 import document from "document";
 import { tz_data } from "../common/tz.js";
 import * as C from "../common/constants.js";
-import * as names from "../common/names";
 import { P } from "../common/prefs";
 import * as prefs from "../common/prefs";
 import * as face from "../common/analog_face";
@@ -352,13 +351,6 @@ export function moon_phase(d)
   return "icons/moon-" + moon_icon(ndays) + ".png";
 }
 
-export function tz_split(name)
-{
-
-  let str = name.split(";");
-  return { name: str[0], tz: parseInt(str[1]) };
-}
-
 export function comma(n)
 {
 
@@ -396,17 +388,3 @@ export function text_bck(txt, e)
   b.x = bbox.x;
   b.width = bbox.width;
 }
-
-export function mk_ascii(idx, a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16)
-{
-
-  var r = "";
-  let l = [ a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16 ];
-  for (let i = 0; i < 16; i++) {
-    if (!l[i])
-      return r;
-    r = r + l[i];
-  }
-  r = r + ";" + idx;
-  return r;
-}
diff --git a/resources/cfg_select.view b/resources/cfg_select.view
index c310923..1084f39 100644
--- a/resources/cfg_select.view
+++ b/resources/cfg_select.view
@@ -1,20 +1,7 @@
 <svg id="cfg_select" x="0" y="0" width="100%" height="100%" display="none">
 
-  <section x="0" y="120" width="100%" height="60">
-    <rect fill="red" x="10" y="0" width="100%-20" height="100%-10"/>
-    <rect fill="black" x="11" y="1" width="100%-22" height="100%-12"/>
-    <text id="cfg_time1_text" fill="yellow" x="12" y="40" text-length="16">TZ1</text>
-    <rect id="cfg_time1" fill="black" x="0" y="0" width="100%" height="100%" pointer-events="visible" opacity="0.0"/>
-  </section>
-
-  <rect fill="black" x="0" y="$" width="100%" height="10"/>
-  
-  <section x="0" y="$" width="100%" height="60">
-    <rect fill="red" x="10" y="0" width="100%-20" height="100%-10"/>
-    <rect fill="black" x="11" y="1" width="100%-22" height="100%-12"/>
-    <text id="cfg_time2_text" fill="yellow" x="12" y="40" text-length="16">TZ2</text>
-    <rect id="cfg_time2" fill="black" x="0" y="0" width="100%" height="100%" pointer-events="visible" opacity="0.0"/>
-  </section>
+  <use id="cfg_time1" href="#red-box" x="4" y="120" height="60" width="100%-8"/>
+  <use id="cfg_time2" href="#red-box" x="4" y="$+10" height="60" width="100%-8"/>
 
   <svg x="0" y="0" width="100%" height="60">
     <use id="ul_btn" href="#image-btn" x="14" y="20"/>
-- 
2.30.0

