|
1 | | -import time |
2 | | - |
3 | 1 | from mpos.apps import Activity |
4 | 2 | import mpos.config |
5 | 3 | import mpos.ui |
6 | 4 |
|
7 | 5 | from wallet import LNBitsWallet, NWCWallet |
8 | 6 |
|
| 7 | + |
| 8 | +class MainActivity(Activity): |
| 9 | + |
| 10 | + def __init__(self): |
| 11 | + self.wallet = None |
| 12 | + self.receive_qr_data = None |
| 13 | + # widgets |
| 14 | + self.balance_label = None |
| 15 | + self.receive_qr = None |
| 16 | + self.payments_label = None |
| 17 | + |
| 18 | + def settings_button_tap(self, event): |
| 19 | + settings_activity = SettingsActivity() |
| 20 | + settings_activity.onCreate() |
| 21 | + |
| 22 | + def main_ui_set_defaults(self): |
| 23 | + self.balance_label.set_text(lv.SYMBOL.REFRESH) |
| 24 | + self.payments_label.set_text(lv.SYMBOL.REFRESH) |
| 25 | + self.receive_qr.update("EMPTY", len("EMPTY")) |
| 26 | + |
| 27 | + def qr_clicked_cb(self, event): |
| 28 | + if not self.receive_qr_data: |
| 29 | + return |
| 30 | + print("QR clicked") |
| 31 | + qr_screen = lv.obj() |
| 32 | + big_receive_qr = lv.qrcode(qr_screen) |
| 33 | + big_receive_qr.set_size(240) # TODO: make this dynamic |
| 34 | + big_receive_qr.set_dark_color(lv.color_black()) |
| 35 | + big_receive_qr.set_light_color(lv.color_white()) |
| 36 | + big_receive_qr.center() |
| 37 | + big_receive_qr.set_style_border_color(lv.color_white(), 0) |
| 38 | + big_receive_qr.set_style_border_width(3, 0); |
| 39 | + big_receive_qr.update(self.receive_qr_data, len(self.receive_qr_data)) |
| 40 | + mpos.ui.load_screen(qr_screen) |
| 41 | + |
| 42 | + |
| 43 | + def onCreate(self): |
| 44 | + main_screen = lv.obj() |
| 45 | + main_screen.set_style_pad_all(10, 0) |
| 46 | + self.balance_label = lv.label(main_screen) |
| 47 | + self.balance_label.set_text("") |
| 48 | + self.balance_label.align(lv.ALIGN.TOP_LEFT, 0, 0) |
| 49 | + self.balance_label.set_style_text_font(lv.font_montserrat_22, 0) |
| 50 | + self.receive_qr = lv.qrcode(main_screen) |
| 51 | + self.receive_qr.set_size(50) |
| 52 | + self.receive_qr.set_dark_color(lv.color_black()) |
| 53 | + self.receive_qr.set_light_color(lv.color_white()) |
| 54 | + self.receive_qr.align(lv.ALIGN.TOP_RIGHT,0,0) |
| 55 | + self.receive_qr.set_style_border_color(lv.color_white(), 0) |
| 56 | + self.receive_qr.set_style_border_width(3, 0); |
| 57 | + self.receive_qr.add_flag(lv.obj.FLAG.CLICKABLE) |
| 58 | + self.receive_qr.add_event_cb(self.qr_clicked_cb,lv.EVENT.CLICKED,None) |
| 59 | + balance_line = lv.line(main_screen) |
| 60 | + balance_line.set_points([{'x':0,'y':35},{'x':200,'y':35}],2) |
| 61 | + self.payments_label = lv.label(main_screen) |
| 62 | + self.payments_label.set_text("") |
| 63 | + self.payments_label.align_to(balance_line,lv.ALIGN.OUT_BOTTOM_LEFT,0,10) |
| 64 | + self.payments_label.set_style_text_font(lv.font_montserrat_16, 0) |
| 65 | + settings_button = lv.button(main_screen) |
| 66 | + settings_button.align(lv.ALIGN.BOTTOM_RIGHT, 0, 0) |
| 67 | + snap_label = lv.label(settings_button) |
| 68 | + snap_label.set_text(lv.SYMBOL.SETTINGS) |
| 69 | + snap_label.center() |
| 70 | + settings_button.add_event_cb(self.settings_button_tap,lv.EVENT.CLICKED,None) |
| 71 | + mpos.ui.setContentView(self, main_screen) |
| 72 | + |
| 73 | + def redraw_balance_cb(self): |
| 74 | + # this gets called from another thread (the wallet) so make sure it happens in the LVGL thread using lv.async_call(): |
| 75 | + lv.async_call(lambda l: self.balance_label.set_text(str(self.wallet.last_known_balance)), None) |
| 76 | + |
| 77 | + def redraw_payments_cb(self): |
| 78 | + # this gets called from another thread (the wallet) so make sure it happens in the LVGL thread using lv.async_call(): |
| 79 | + lv.async_call(lambda l: self.payments_label.set_text(str(self.wallet.payment_list)), None) |
| 80 | + |
| 81 | + def onStart(self, main_screen): |
| 82 | + self.main_ui_set_defaults() |
| 83 | + |
| 84 | + def onResume(self, main_screen): |
| 85 | + if not self.wallet or not self.wallet.is_running(): # just started the app or just returned from settings_screen |
| 86 | + config = mpos.config.SharedPreferences("com.lightningpiggy.displaywallet") |
| 87 | + wallet_type = config.get_string("wallet_type") |
| 88 | + if wallet_type == "lnbits": |
| 89 | + try: |
| 90 | + self.receive_qr_data = config.get_string("lnbits_static_receive_code") |
| 91 | + self.wallet = LNBitsWallet(config.get_string("lnbits_url"), config.get_string("lnbits_readkey")) |
| 92 | + except Exception as e: |
| 93 | + self.payments_label.set_text(f"Couldn't initialize LNBitsWallet\nbecause: {e}") |
| 94 | + elif wallet_type == "nwc": |
| 95 | + try: |
| 96 | + self.wallet = NWCWallet(config.get_string("nwc_url")) |
| 97 | + self.receive_qr_data = wallet.lud16 |
| 98 | + except Exception as e: |
| 99 | + self.payments_label.set_text(f"Couldn't initialize NWCWallet\nbecause: {e}") |
| 100 | + else: |
| 101 | + self.payments_label.set_text(f"No or unsupported wallet\ntype configured: '{wallet_type}'") |
| 102 | + if self.receive_qr_data: |
| 103 | + print(f"Setting static_receive_code: {self.receive_qr_data}") |
| 104 | + self.receive_qr.update(self.receive_qr_data, len(self.receive_qr_data)) |
| 105 | + can_check_network = True |
| 106 | + try: |
| 107 | + import network |
| 108 | + except Exception as e: |
| 109 | + can_check_network = False |
| 110 | + if can_check_network and not network.WLAN(network.STA_IF).isconnected(): |
| 111 | + self.payments_label.set_text(f"WiFi is not connected, can't\ntalk to {wallet_type} backend.") |
| 112 | + else: |
| 113 | + if self.wallet: |
| 114 | + self.payments_label.set_text(f"Connecting to {wallet_type} backend...") |
| 115 | + self.wallet.start(self.redraw_balance_cb, self.redraw_payments_cb) |
| 116 | + else: |
| 117 | + self.payments_label.set_text(f"Could not start {wallet_type} backend.") |
| 118 | + |
| 119 | + def onStop(self, main_screen): |
| 120 | + if self.wallet: |
| 121 | + self.wallet.stop() |
| 122 | + |
| 123 | + |
| 124 | +# Used to list and edit all settings: |
| 125 | +class SettingsActivity(Activity): |
| 126 | + def __init__(self): |
| 127 | + self.prefs = mpos.config.SharedPreferences("com.lightningpiggy.displaywallet") |
| 128 | + self.settings = [ |
| 129 | + {"title": "Wallet Type", "key": "wallet_type", "value_label": None, "cont": None}, |
| 130 | + {"title": "LNBits URL", "key": "lnbits_url", "value_label": None, "cont": None}, |
| 131 | + {"title": "LNBits Read Key", "key": "lnbits_readkey", "value_label": None, "cont": None}, |
| 132 | + {"title": "Static receive code", "key": "lnbits_static_receive_code", "value_label": None, "cont": None}, |
| 133 | + {"title": "NWC URL", "key": "nwc_url", "value_label": None, "cont": None}, |
| 134 | + ] |
| 135 | + self.keyboard = None |
| 136 | + self.textarea = None |
| 137 | + self.radio_container = None |
| 138 | + self.active_radio_index = 0 # Track active radio button index |
| 139 | + |
| 140 | + def onCreate(self): |
| 141 | + screen = lv.obj() |
| 142 | + print("creating SettingsActivity ui...") |
| 143 | + screen.set_size(lv.pct(100), lv.pct(100)) |
| 144 | + screen.set_style_pad_all(10, 0) |
| 145 | + screen.set_flex_flow(lv.FLEX_FLOW.COLUMN) |
| 146 | + screen.set_style_border_width(0, 0) |
| 147 | + |
| 148 | + # Create settings entries |
| 149 | + for setting in self.settings: |
| 150 | + # Container for each setting |
| 151 | + setting_cont = lv.obj(screen) |
| 152 | + setting_cont.set_width(lv.pct(100)) |
| 153 | + setting_cont.set_height(lv.SIZE_CONTENT) |
| 154 | + setting_cont.set_style_border_width(1, 0) |
| 155 | + setting_cont.set_style_border_side(lv.BORDER_SIDE.BOTTOM, 0) |
| 156 | + setting_cont.set_style_pad_all(8, 0) |
| 157 | + setting_cont.add_flag(lv.obj.FLAG.CLICKABLE) |
| 158 | + setting["cont"] = setting_cont # Store container reference for visibility control |
| 159 | + |
| 160 | + # Title label (bold, larger) |
| 161 | + title = lv.label(setting_cont) |
| 162 | + title.set_text(setting["title"]) |
| 163 | + title.set_style_text_font(lv.font_montserrat_16, 0) |
| 164 | + title.set_pos(0, 0) |
| 165 | + |
| 166 | + # Value label (smaller, below title) |
| 167 | + value = lv.label(setting_cont) |
| 168 | + value.set_text(self.prefs.get_string(setting["key"], "Not set")) |
| 169 | + value.set_style_text_font(lv.font_montserrat_12, 0) |
| 170 | + value.set_style_text_color(lv.color_hex(0x666666), 0) |
| 171 | + value.set_pos(0, 20) |
| 172 | + setting["value_label"] = value # Store reference for updating |
| 173 | + setting_cont.add_event_cb( |
| 174 | + lambda e, s=setting: self.startSettingActivity(s), lv.EVENT.CLICKED, None |
| 175 | + ) |
| 176 | + mpos.ui.setContentView(self, screen) |
| 177 | + |
| 178 | + def startSettingActivity(self, setting): |
| 179 | + sa = SettingActivity(setting) |
| 180 | + sa.onCreate() |
| 181 | + |
| 182 | + def onResume(self, screen): |
| 183 | + wallet_type = self.prefs.get_string("wallet_type", "lnbits") |
| 184 | + # update setting visibility based on wallet_type: |
| 185 | + for setting in self.settings: |
| 186 | + if setting["key"].startswith("lnbits_"): |
| 187 | + if wallet_type != "lnbits": |
| 188 | + setting["cont"].add_flag(lv.obj.FLAG.HIDDEN) |
| 189 | + else: |
| 190 | + setting["cont"].remove_flag(lv.obj.FLAG.HIDDEN) |
| 191 | + elif setting["key"].startswith("nwc_"): |
| 192 | + if wallet_type != "nwc": |
| 193 | + setting["cont"].add_flag(lv.obj.FLAG.HIDDEN) |
| 194 | + else: |
| 195 | + setting["cont"].remove_flag(lv.obj.FLAG.HIDDEN) |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | +# Used to edit one setting: |
9 | 200 | class SettingActivity(Activity): |
10 | 201 | def __init__(self, setting): |
11 | 202 | super().__init__() |
@@ -170,192 +361,3 @@ def close_popup(self, event): |
170 | 361 | self.hide_keyboard() |
171 | 362 | mpos.ui.back_screen() |
172 | 363 |
|
173 | | - |
174 | | -class SettingsActivity(Activity): |
175 | | - def __init__(self): |
176 | | - self.prefs = mpos.config.SharedPreferences("com.lightningpiggy.displaywallet") |
177 | | - self.settings = [ |
178 | | - {"title": "Wallet Type", "key": "wallet_type", "value_label": None, "cont": None}, |
179 | | - {"title": "LNBits URL", "key": "lnbits_url", "value_label": None, "cont": None}, |
180 | | - {"title": "LNBits Read Key", "key": "lnbits_readkey", "value_label": None, "cont": None}, |
181 | | - {"title": "Static receive code", "key": "lnbits_static_receive_code", "value_label": None, "cont": None}, |
182 | | - {"title": "NWC URL", "key": "nwc_url", "value_label": None, "cont": None}, |
183 | | - ] |
184 | | - self.keyboard = None |
185 | | - self.textarea = None |
186 | | - self.radio_container = None |
187 | | - self.active_radio_index = 0 # Track active radio button index |
188 | | - |
189 | | - def onCreate(self): |
190 | | - screen = lv.obj() |
191 | | - print("creating SettingsActivity ui...") |
192 | | - screen.set_size(lv.pct(100), lv.pct(100)) |
193 | | - screen.set_style_pad_all(10, 0) |
194 | | - screen.set_flex_flow(lv.FLEX_FLOW.COLUMN) |
195 | | - screen.set_style_border_width(0, 0) |
196 | | - |
197 | | - # Create settings entries |
198 | | - for setting in self.settings: |
199 | | - # Container for each setting |
200 | | - setting_cont = lv.obj(screen) |
201 | | - setting_cont.set_width(lv.pct(100)) |
202 | | - setting_cont.set_height(lv.SIZE_CONTENT) |
203 | | - setting_cont.set_style_border_width(1, 0) |
204 | | - setting_cont.set_style_border_side(lv.BORDER_SIDE.BOTTOM, 0) |
205 | | - setting_cont.set_style_pad_all(8, 0) |
206 | | - setting_cont.add_flag(lv.obj.FLAG.CLICKABLE) |
207 | | - setting["cont"] = setting_cont # Store container reference for visibility control |
208 | | - |
209 | | - # Title label (bold, larger) |
210 | | - title = lv.label(setting_cont) |
211 | | - title.set_text(setting["title"]) |
212 | | - title.set_style_text_font(lv.font_montserrat_16, 0) |
213 | | - title.set_pos(0, 0) |
214 | | - |
215 | | - # Value label (smaller, below title) |
216 | | - value = lv.label(setting_cont) |
217 | | - value.set_text(self.prefs.get_string(setting["key"], "Not set")) |
218 | | - value.set_style_text_font(lv.font_montserrat_12, 0) |
219 | | - value.set_style_text_color(lv.color_hex(0x666666), 0) |
220 | | - value.set_pos(0, 20) |
221 | | - setting["value_label"] = value # Store reference for updating |
222 | | - setting_cont.add_event_cb( |
223 | | - lambda e, s=setting: self.startSettingActivity(s), lv.EVENT.CLICKED, None |
224 | | - ) |
225 | | - mpos.ui.setContentView(self, screen) |
226 | | - |
227 | | - def startSettingActivity(self, setting): |
228 | | - sa = SettingActivity(setting) |
229 | | - sa.onCreate() |
230 | | - |
231 | | - def onResume(self, screen): |
232 | | - wallet_type = self.prefs.get_string("wallet_type", "lnbits") |
233 | | - # update setting visibility based on wallet_type: |
234 | | - for setting in self.settings: |
235 | | - if setting["key"].startswith("lnbits_"): |
236 | | - if wallet_type != "lnbits": |
237 | | - setting["cont"].add_flag(lv.obj.FLAG.HIDDEN) |
238 | | - else: |
239 | | - setting["cont"].remove_flag(lv.obj.FLAG.HIDDEN) |
240 | | - elif setting["key"].startswith("nwc_"): |
241 | | - if wallet_type != "nwc": |
242 | | - setting["cont"].add_flag(lv.obj.FLAG.HIDDEN) |
243 | | - else: |
244 | | - setting["cont"].remove_flag(lv.obj.FLAG.HIDDEN) |
245 | | - |
246 | | - |
247 | | -class MainActivity(Activity): |
248 | | - |
249 | | - def __init__(self): |
250 | | - self.wallet = None |
251 | | - self.receive_qr_data = None |
252 | | - # widgets |
253 | | - self.balance_label = None |
254 | | - self.receive_qr = None |
255 | | - self.payments_label = None |
256 | | - |
257 | | - def settings_button_tap(self, event): |
258 | | - settings_activity = SettingsActivity() |
259 | | - settings_activity.onCreate() |
260 | | - |
261 | | - def main_ui_set_defaults(self): |
262 | | - self.balance_label.set_text(lv.SYMBOL.REFRESH) |
263 | | - self.payments_label.set_text(lv.SYMBOL.REFRESH) |
264 | | - self.receive_qr.update("EMPTY", len("EMPTY")) |
265 | | - |
266 | | - def qr_clicked_cb(self, event): |
267 | | - if not self.receive_qr_data: |
268 | | - return |
269 | | - print("QR clicked") |
270 | | - qr_screen = lv.obj() |
271 | | - big_receive_qr = lv.qrcode(qr_screen) |
272 | | - big_receive_qr.set_size(240) # TODO: make this dynamic |
273 | | - big_receive_qr.set_dark_color(lv.color_black()) |
274 | | - big_receive_qr.set_light_color(lv.color_white()) |
275 | | - big_receive_qr.center() |
276 | | - big_receive_qr.set_style_border_color(lv.color_white(), 0) |
277 | | - big_receive_qr.set_style_border_width(3, 0); |
278 | | - big_receive_qr.update(self.receive_qr_data, len(self.receive_qr_data)) |
279 | | - mpos.ui.load_screen(qr_screen) |
280 | | - |
281 | | - |
282 | | - def onCreate(self): |
283 | | - main_screen = lv.obj() |
284 | | - main_screen.set_style_pad_all(10, 0) |
285 | | - self.balance_label = lv.label(main_screen) |
286 | | - self.balance_label.set_text("") |
287 | | - self.balance_label.align(lv.ALIGN.TOP_LEFT, 0, 0) |
288 | | - self.balance_label.set_style_text_font(lv.font_montserrat_22, 0) |
289 | | - self.receive_qr = lv.qrcode(main_screen) |
290 | | - self.receive_qr.set_size(50) |
291 | | - self.receive_qr.set_dark_color(lv.color_black()) |
292 | | - self.receive_qr.set_light_color(lv.color_white()) |
293 | | - self.receive_qr.align(lv.ALIGN.TOP_RIGHT,0,0) |
294 | | - self.receive_qr.set_style_border_color(lv.color_white(), 0) |
295 | | - self.receive_qr.set_style_border_width(3, 0); |
296 | | - self.receive_qr.add_flag(lv.obj.FLAG.CLICKABLE) |
297 | | - self.receive_qr.add_event_cb(self.qr_clicked_cb,lv.EVENT.CLICKED,None) |
298 | | - balance_line = lv.line(main_screen) |
299 | | - balance_line.set_points([{'x':0,'y':35},{'x':200,'y':35}],2) |
300 | | - self.payments_label = lv.label(main_screen) |
301 | | - self.payments_label.set_text("") |
302 | | - self.payments_label.align_to(balance_line,lv.ALIGN.OUT_BOTTOM_LEFT,0,10) |
303 | | - self.payments_label.set_style_text_font(lv.font_montserrat_16, 0) |
304 | | - settings_button = lv.button(main_screen) |
305 | | - settings_button.align(lv.ALIGN.BOTTOM_RIGHT, 0, 0) |
306 | | - snap_label = lv.label(settings_button) |
307 | | - snap_label.set_text(lv.SYMBOL.SETTINGS) |
308 | | - snap_label.center() |
309 | | - settings_button.add_event_cb(self.settings_button_tap,lv.EVENT.CLICKED,None) |
310 | | - mpos.ui.setContentView(self, main_screen) |
311 | | - |
312 | | - def redraw_balance_cb(self): |
313 | | - # this gets called from another thread (the wallet) so make sure it happens in the LVGL thread using lv.async_call(): |
314 | | - lv.async_call(lambda l: self.balance_label.set_text(str(self.wallet.last_known_balance)), None) |
315 | | - |
316 | | - def redraw_payments_cb(self): |
317 | | - # this gets called from another thread (the wallet) so make sure it happens in the LVGL thread using lv.async_call(): |
318 | | - lv.async_call(lambda l: self.payments_label.set_text(str(self.wallet.payment_list)), None) |
319 | | - |
320 | | - def onStart(self, main_screen): |
321 | | - self.main_ui_set_defaults() |
322 | | - |
323 | | - def onResume(self, main_screen): |
324 | | - if not self.wallet or not self.wallet.is_running(): # just started the app or just returned from settings_screen |
325 | | - config = mpos.config.SharedPreferences("com.lightningpiggy.displaywallet") |
326 | | - wallet_type = config.get_string("wallet_type") |
327 | | - if wallet_type == "lnbits": |
328 | | - try: |
329 | | - self.receive_qr_data = config.get_string("lnbits_static_receive_code") |
330 | | - self.wallet = LNBitsWallet(config.get_string("lnbits_url"), config.get_string("lnbits_readkey")) |
331 | | - except Exception as e: |
332 | | - self.payments_label.set_text(f"Couldn't initialize LNBitsWallet\nbecause: {e}") |
333 | | - elif wallet_type == "nwc": |
334 | | - try: |
335 | | - self.wallet = NWCWallet(config.get_string("nwc_url")) |
336 | | - self.receive_qr_data = wallet.lud16 |
337 | | - except Exception as e: |
338 | | - self.payments_label.set_text(f"Couldn't initialize NWCWallet\nbecause: {e}") |
339 | | - else: |
340 | | - self.payments_label.set_text(f"No or unsupported wallet\ntype configured: '{wallet_type}'") |
341 | | - if self.receive_qr_data: |
342 | | - print(f"Setting static_receive_code: {self.receive_qr_data}") |
343 | | - self.receive_qr.update(self.receive_qr_data, len(self.receive_qr_data)) |
344 | | - can_check_network = True |
345 | | - try: |
346 | | - import network |
347 | | - except Exception as e: |
348 | | - can_check_network = False |
349 | | - if can_check_network and not network.WLAN(network.STA_IF).isconnected(): |
350 | | - self.payments_label.set_text(f"WiFi is not connected, can't\ntalk to {wallet_type} backend.") |
351 | | - else: |
352 | | - if self.wallet: |
353 | | - self.payments_label.set_text(f"Connecting to {wallet_type} backend...") |
354 | | - self.wallet.start(self.redraw_balance_cb, self.redraw_payments_cb) |
355 | | - else: |
356 | | - self.payments_label.set_text(f"Could not start {wallet_type} backend.") |
357 | | - |
358 | | - def onStop(self, main_screen): |
359 | | - if self.wallet: |
360 | | - self.wallet.stop() |
361 | | - |
|
0 commit comments