refactored contacts.app.js, added function to call contact from the watch by triggering intent on phone

master
Tom Wallroth 2024-05-18 21:37:46 +02:00
parent 7ed4d23874
commit 000f6efe92
1 changed files with 73 additions and 78 deletions

View File

@ -3,11 +3,8 @@
var Layout = require("Layout"); var Layout = require("Layout");
var wp = require('Storage').readJSON("contacts.json", true) || []; var wp = require('Storage').readJSON("contacts.json", true) || [];
// var wp = [];
var key; /* Shared between functions, typically wp name */ function writeContacts() {
function writeContact() {
require('Storage').writeJSON("contacts.json", wp); require('Storage').writeJSON("contacts.json", wp);
} }
@ -35,8 +32,8 @@ function mainMenu() {
menu[e.name] = () => showContact(closureE); menu[e.name] = () => showContact(closureE);
} }
} }
menu["Add"] = addCard; menu["Add"] = addContact;
menu["Remove"] = removeCard; menu["Remove"] = removeContact;
g.clear(); g.clear();
E.showMenu(menu); E.showMenu(menu);
} }
@ -46,49 +43,56 @@ function showContact(i) {
(new Layout ({ (new Layout ({
type:"v", type:"v",
c: [ c: [
{type:"txt", font:"10%", pad:1, fillx:1, filly:1, label:i["name"] + "\n" + i["number"]}, {type:"txt", font:"10%", pad:1, fillx:1, filly:1, label: i["name"] + "\n" + i["number"]},
{type:"btn", font:"10%", pad:1, fillx:1, filly:1, label:"Call", cb:l=>{callNumber(i['number']);}}, {type:"btn", font:"10%", pad:1, fillx:1, filly:1, label: "Call", cb: l => callNumber(i['number'])},
{type:"btn", font:"10%", pad:1, fillx:1, filly:1, label:"Back to list", cb:l=>{mainMenu();}} {type:"btn", font:"10%", pad:1, fillx:1, filly:1, label: "Back to list", cb: mainMenu}
], ],
lazy:true lazy:true
})).render(); })).render();
} }
function showNumpad(prompt, callback) { function showNumpad() {
let number = '' return new Promise((resolve, reject) => {
E.showMenu(); let number = ''
function addDigit(digit) { E.showMenu();
number += digit; function addDigit(digit) {
Bangle.buzz(20); number += digit;
update(); Bangle.buzz(20);
} update();
function update() { }
g.reset(); function removeDigit() {
g.clearRect(0,0,g.getWidth(),23); number = number.slice(0, -1);
g.setFont("Vector:24").setFontAlign(1,0).drawString(prompt + number, g.getWidth(),12); Bangle.buzz(20);
} update();
const ds="12%"; }
const digitBtn = (digit) => ({type:"btn", font:ds, width:58, label:digit, cb:l=>{addDigit(digit);}}); function update() {
var numPad = new Layout ({ g.reset();
type:"v", c: [{ g.clearRect(0,0,g.getWidth(),23);
type:"v", c: [ g.setFont("Vector:24").setFontAlign(1,0).drawString(number, g.getWidth(),12);
{type:"", height:24}, }
{type:"h",filly:1, c: [digitBtn("1"), digitBtn("2"), digitBtn("3")]}, const ds="12%";
{type:"h",filly:1, c: [digitBtn("4"), digitBtn("5"), digitBtn("6")]}, const digitBtn = (digit) => ({type:"btn", font:ds, width:58, label:digit, cb:l=>{addDigit(digit);}});
{type:"h",filly:1, c: [digitBtn("7"), digitBtn("8"), digitBtn("9")]}, var numPad = new Layout ({
{type:"h",filly:1, c: [ type:"v", c: [{
{type:"btn", font:ds, width:58, label:"C", cb:l=>{key=key.slice(0,-1); update();}}, type:"v", c: [
{type:"btn", font:ds, width:58, label:"0", cb:l=>{addDigit("0");}}, {type:"", height:24},
{type:"btn", font:ds, width:58, id:"OK", label:"OK", cb: callback} {type:"h",filly:1, c: [digitBtn("1"), digitBtn("2"), digitBtn("3")]},
{type:"h",filly:1, c: [digitBtn("4"), digitBtn("5"), digitBtn("6")]},
{type:"h",filly:1, c: [digitBtn("7"), digitBtn("8"), digitBtn("9")]},
{type:"h",filly:1, c: [
{type:"btn", font:ds, width:58, label:"C", cb: removeDigit},
digitBtn('0'),
{type:"btn", font:ds, width:58, id:"OK", label:"OK", cb: l => resolve(number)}
]}
]} ]}
]} ], lazy:true});
], lazy:true}); g.clear();
g.clear(); numPad.render();
numPad.render(); update();
update(); });
} }
function removeCard() { function removeContact() {
var menu = { var menu = {
"" : {title : "Select Contact"}, "" : {title : "Select Contact"},
"< Back" : mainMenu "< Back" : mainMenu
@ -106,7 +110,7 @@ function removeCard() {
{type:"h", c: [ {type:"h", c: [
{type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "YES", cb:l=>{ {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "YES", cb:l=>{
wp.splice(card, 1); wp.splice(card, 1);
writeContact(); writeContacts();
mainMenu(); mainMenu();
}}, }},
{type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: " NO", cb:l=>{mainMenu();}} {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: " NO", cb:l=>{mainMenu();}}
@ -120,51 +124,42 @@ function removeCard() {
E.showMenu(menu); E.showMenu(menu);
} }
function askPosition(callback) {
showNumpad("", "", function() {
callback(key, "");
});
}
function createContact(lat, name) { function addNewContact(name) {
wp.push({lat: lat, name: name});
print("add -- contacts", wp);
writeContact();
}
function addCardName2(key) {
g.clear(); g.clear();
askPosition(function(lat, lon) { showNumpad().then((number) => {
print("position -- ", lat, lon); wp.push({name: name, number: number});
createContact(lat, result); writeContacts();
mainMenu(); mainMenu();
}); })
} }
function addCardName(key) { function tryAddContact(name) {
result = key; if (wp.filter((e) => e.name === name).length) {
if (wp[result] !== undefined) { E.showMenu();
E.showMenu(); var alreadyExists = new Layout (
var alreadyExists = new Layout ( {type:"v", c: [
{type:"v", c: [ {type:"txt", font:Math.min(15,100/name.length)+"%", pad:1, fillx:1, filly:1, label:name},
{type:"txt", font:Math.min(15,100/result.length)+"%", pad:1, fillx:1, filly:1, label:result}, {type:"txt", font:"12%", pad:1, fillx:1, filly:1, label:"already exists."},
{type:"txt", font:"12%", pad:1, fillx:1, filly:1, label:"already exists."}, {type:"h", c: [
{type:"h", c: [ {type:"btn", font:"10%", pad:1, fillx:1, filly:1, label: "REPLACE", cb:l=>{ addNewContact(name); }},
{type:"btn", font:"10%", pad:1, fillx:1, filly:1, label: "REPLACE", cb:l=>{ addCardName2(key); }}, {type:"btn", font:"10%", pad:1, fillx:1, filly:1, label: "CANCEL", cb:l=>{mainMenu();}}
{type:"btn", font:"10%", pad:1, fillx:1, filly:1, label: "CANCEL", cb:l=>{mainMenu();}} ]}
]} ], lazy:true});
], lazy:true}); g.clear();
g.clear(); alreadyExists.render();
alreadyExists.render();
return; return;
} }
addCardName2(key); addNewContact(name);
} }
function addCard() { function addContact() {
require("textinput").input({text:""}).then(result => { require("textinput").input({text:""}).then(name => {
if (result !== "") { if (name !== "") {
addCardName(result); tryAddContact(name);
} else } else
mainMenu(); mainMenu();
}); });