TimerSetScreen.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import {t, extendLocale} from "../lib/i18n";
  2. import {TouchEventManager} from "../lib/TouchEventManager";
  3. extendLocale({
  4. "timer_start": {
  5. "en-US": "Begin",
  6. "zh-CN": "开始",
  7. "zh-TW": "開始",
  8. "ru-RU": "Начать",
  9. "de-DE": "Start"
  10. },
  11. "timer_stop": {
  12. "en-US": "Cancel",
  13. "zh-CN": "取消",
  14. "zh-TW": "取消",
  15. "ru-RU": "Отмена",
  16. "de-DE": "Stop"
  17. },
  18. })
  19. class TimerSetScreen {
  20. STYLE_DISPLAY = {
  21. x: 0,
  22. text_size: 80,
  23. align_h: hmUI.align.CENTER_H,
  24. align_v: hmUI.align.CENTER_V,
  25. w: 192,
  26. h: 96,
  27. color: 0xffffff
  28. };
  29. STYLE_EDIT_BTN = {
  30. text_size: 48,
  31. align_h: hmUI.align.CENTER_H,
  32. align_v: hmUI.align.CENTER_V,
  33. h: 96,
  34. w: 50,
  35. color: 0xAAAAAA
  36. };
  37. STYLE_EDIT_INC = {
  38. text: "+",
  39. x: 142
  40. };
  41. STYLE_EDIT_DEG = {
  42. text: "−",
  43. x: 0
  44. }
  45. hour = 0;
  46. minute = 1;
  47. second = 0;
  48. editButtons = [];
  49. localTimer = null;
  50. timerID = null;
  51. startedTime = 0;
  52. endTime = 0;
  53. formatDisplay(v) {
  54. return v.toString().padStart(2, "0");
  55. }
  56. start() {
  57. // Load all
  58. let cfg = hmFS.SysProGetBool("mmk_tb_cfg_timer_keep");
  59. if(cfg === undefined) cfg = true;
  60. const lastDX = hmFS.SysProGetInt("mmk_tb_timer_last");
  61. if(lastDX && cfg) {
  62. this.hour = Math.floor(lastDX / 3600);
  63. this.minute = Math.floor((lastDX % 3600) / 60);
  64. this.second = lastDX % 60;
  65. console.log("load last", this.hour, this.minute, this.second);
  66. }
  67. const state = hmFS.SysProGetChars("mmk_tb_timer_state");
  68. if(state) {
  69. const [id, startedTime, endTime] = state.split(":");
  70. if(Date.now() < endTime) {
  71. this.timerID = parseInt(id);
  72. this.startedTime = parseInt(startedTime);
  73. this.endTime = parseInt(endTime);
  74. }
  75. }
  76. this.initView();
  77. this.updateLayout();
  78. }
  79. initView() {
  80. this.viewHour = hmUI.createWidget(hmUI.widget.TEXT, {
  81. y: 72,
  82. text: this.formatDisplay(this.hour),
  83. ...this.STYLE_DISPLAY
  84. });
  85. this.viewMinute = hmUI.createWidget(hmUI.widget.TEXT, {
  86. y: 72 + 96,
  87. text: this.formatDisplay(this.minute),
  88. ...this.STYLE_DISPLAY
  89. });
  90. this.viewSecond = hmUI.createWidget(hmUI.widget.TEXT, {
  91. y: 72 + 96*2,
  92. text: this.formatDisplay(this.second),
  93. ...this.STYLE_DISPLAY
  94. });
  95. ["hour", "minute", "second"].map((key, i) => {
  96. [-1, 1].map((dir) => {
  97. const widget = hmUI.createWidget(hmUI.widget.TEXT, {
  98. ...this.STYLE_EDIT_BTN,
  99. ...(dir > 0 ? this.STYLE_EDIT_INC : this.STYLE_EDIT_DEG),
  100. y: 72 + 96*i
  101. });
  102. const events = new TouchEventManager(widget);
  103. const edit = () => {
  104. let val = this[key] + dir;
  105. val = Math.min(Math.max(0, val), 59);
  106. this[key] = val;
  107. this.refresh();
  108. };
  109. events.ontouch = edit;
  110. events.onlongtouchrepeatly = edit;
  111. this.editButtons.push(widget);
  112. })
  113. });
  114. this.actionButton = hmUI.createWidget(hmUI.widget.BUTTON, {
  115. x: 0,
  116. y: 400,
  117. w: 192,
  118. h: 90,
  119. text: "Start",
  120. normal_color: 0x222222,
  121. press_color: 0x333333,
  122. color: 0xFFFFFF,
  123. click_func: () => {
  124. this.timerID !== null ? this.stopTimer() : this.runTimer();
  125. }
  126. })
  127. }
  128. runTimer() {
  129. const dx = this.hour * 3600 + this.minute * 60 + this.second;
  130. if(dx === 0) return;
  131. this.startedTime = Date.now();
  132. this.endTime = this.startedTime + dx * 1000;
  133. this.timerID = 1;
  134. try {
  135. this.timerID = hmApp.alarmNew({
  136. url: "page/TimerOutScreen",
  137. appid: 33904,
  138. delay: dx
  139. })
  140. } catch(e) {
  141. console.log(e);
  142. hmUI.showToast({text: "Can't start OS app alarm"});
  143. }
  144. // Bundle data for persistant
  145. const bundle = this.timerID + ":" + this.startedTime + ":" + this.endTime;
  146. hmFS.SysProSetChars("mmk_tb_timer_state", bundle);
  147. hmFS.SysProSetInt("mmk_tb_timer_last", dx);
  148. this.updateLayout();
  149. }
  150. stopTimer() {
  151. hmFS.SysProSetChars("mmk_tb_timer_state", "");
  152. try {
  153. hmApp.alarmCancel(this.timerID);
  154. } catch(e) {
  155. console.log(e);
  156. hmUI.showToast({text: "Can't cancel OS app alarm"});
  157. }
  158. let delay = Math.floor((this.endTime - Date.now()) / 1000);
  159. if(delay > 0) {
  160. this.hour = Math.floor(delay / 3600);
  161. this.minute = Math.floor((delay % 3600) / 60);
  162. this.second = delay % 60;
  163. }
  164. this.timerID = null;
  165. this.updateLayout();
  166. }
  167. refresh() {
  168. let hour = this.hour,
  169. minute = this.minute,
  170. second = this.second;
  171. if(this.timerID) {
  172. let delay = Math.floor((this.endTime - Date.now()) / 1000);
  173. if(delay < 0) delay = 0;
  174. hour = Math.floor(delay / 3600);
  175. minute = Math.floor((delay % 3600) / 60);
  176. second = delay % 60;
  177. }
  178. this.viewHour.setProperty(hmUI.prop.TEXT, this.formatDisplay(hour));
  179. this.viewMinute.setProperty(hmUI.prop.TEXT, this.formatDisplay(minute));
  180. this.viewSecond.setProperty(hmUI.prop.TEXT, this.formatDisplay(second));
  181. }
  182. updateLayout() {
  183. // Hide edit buttons if timer started
  184. this.editButtons.forEach((v) => {
  185. v.setProperty(hmUI.prop.VISIBLE, this.timerID === null);
  186. });
  187. // Set button text
  188. const buttonText = this.timerID === null ? t("timer_start") : t("timer_stop");
  189. this.actionButton.setProperty(hmUI.prop.TEXT, buttonText);
  190. // UI update timer
  191. if(this.timerID && !this.localTimer) {
  192. this.localTimer = timer.createTimer(0, 500, () => this.refresh());
  193. } else if(this.timerID === null && this.localTimer) {
  194. timer.stopTimer(this.localTimer);
  195. this.localTimer = null;
  196. }
  197. this.refresh();
  198. }
  199. }
  200. let __$$app$$__ = __$$hmAppManager$$__.currentApp;
  201. let __$$module$$__ = __$$app$$__.current;
  202. __$$module$$__.module = DeviceRuntimeCore.Page({
  203. onInit(p) {
  204. new TimerSetScreen().start();
  205. }
  206. });