1- /* contacts.js */
2-
31var Layout = require ( "Layout" ) ;
42
5- var wp = require ( 'Storage' ) . readJSON ( "contacts.json" , true ) || [ ] ;
3+ var contacts = require ( 'Storage' ) . readJSON ( "contacts.json" , true ) || [ ] ;
64
75function writeContacts ( ) {
8- require ( 'Storage' ) . writeJSON ( "contacts.json" , wp ) ;
6+ require ( 'Storage' ) . writeJSON ( "contacts.json" , contacts ) ;
97}
108
119function callNumber ( number ) {
10+ E . showMessage ( 'Calling ' + number + '...' ) ;
11+ setTimeout ( ( ) => mainMenu ( ) , 2000 ) ;
1212 Bluetooth . println ( JSON . stringify ( {
1313 t :"intent" ,
1414 target :"activity" ,
@@ -17,35 +17,49 @@ function callNumber (number) {
1717 categories :[ "android.intent.category.DEFAULT" ] ,
1818 data : 'tel:' + number ,
1919 } ) )
20-
2120}
2221
2322function mainMenu ( ) {
2423 var menu = {
24+ "" : {
25+ "title" : "Contacts" ,
26+ } ,
2527 "< Back" : Bangle . load
2628 } ;
27- if ( ! wp . length ) {
29+ if ( ! contacts . length ) {
2830 menu [ 'No Contacts' ] = ( ) => { } ;
29- } else {
30- for ( const e of wp ) {
31- const closureE = e ;
32- menu [ e . name ] = ( ) => showContact ( closureE ) ;
33- }
3431 }
35- menu [ "Add" ] = addContact ;
36- menu [ "Remove" ] = removeContact ;
32+ contacts . forEach ( ( e , idx ) => {
33+ menu [ e . name ] = ( ) => showContact ( idx )
34+ } )
35+ menu [ "Add Contact" ] = addContact ;
3736 g . clear ( ) ;
3837 E . showMenu ( menu ) ;
3938}
4039
41- function showContact ( i ) {
40+ function showContact ( idx ) {
4241 g . clear ( ) ;
42+ var name = contacts [ idx ] . name ;
43+ let longName = g . setFont ( "6x8:2" ) . stringWidth ( name ) >= g . getWidth ( ) ;
44+ var number = contacts [ idx ] . number ;
45+ let longNumber = g . setFont ( "6x8:2" ) . stringWidth ( number ) >= g . getWidth ( ) ;
4346 ( new Layout ( {
4447 type :"v" ,
4548 c : [
46- { type :"txt" , font :"10%" , pad :1 , fillx :1 , filly :1 , label : i [ "name" ] + "\n" + i [ "number" ] } ,
47- { type :"btn" , font :"10%" , pad :1 , fillx :1 , filly :1 , label : "Call" , cb : l => callNumber ( i [ 'number' ] ) } ,
48- { type :"btn" , font :"10%" , pad :1 , fillx :1 , filly :1 , label : "Back to list" , cb : mainMenu }
49+ { type : 'h' , filly : 3 , fillx :1 , c : [
50+ { type :"btn" , font :"6x8" , pad :1 , fillx :1 , filly :1 , label : "<- Back to list" , cb : mainMenu } ,
51+ { type :"btn" , pad :1 , fillx :1 , filly :3 , src : require ( "heatshrink" ) . decompress ( atob ( "jUawYGDgVJkgQGBAOSBAsJkALBBIoaCDogaCAQYXBgIIFkmAC4IIFyVAgAIGGQUJHwo4FAo2QBwICDNAVAkgCEEAYUFEAQUFE34mRPwgmEcYgmDUg8AgjLGgAA==" ) ) ,
52+ cb : ( ) => (
53+ E . showPrompt ( "Delete Contact '" + name + "'?" , )
54+ . then ( ( res ) => { if ( res ) { deleteContact ( idx ) } else { mainMenu ( ) } } )
55+ )
56+ } ,
57+ ] } ,
58+ { type :"txt" , font :longName ? "6x8" : "6x8:2" , pad :1 , fillx :2 , filly :3 , label : longName ? name . slice ( 0 , name . length / 2 ) + '\n' + name . slice ( name . length / 2 ) : name } ,
59+ { type :"txt" , font : "6x8:2" , pad :1 , fillx :2 , filly :3 , label : longNumber ? number . slice ( 0 , number . length / 2 ) + '\n' + number . slice ( number . length / 2 ) : number } ,
60+ { type : 'h' , filly : 3 , fillx :1 , c : [
61+ { type :"btn" , pad :1 , fillx :1 , filly :3 , src :atob ( "GBiBAAAAAAAAAAAAAB8AAB+AAB+AAB+AAB+AAA+AAA8AAA4AAAYAAAcAAAMAAAGAAAHB8ADz+AA/+AAf+AAH+AAA+AAAAAAAAAAAAA==" ) , cb : l => callNumber ( number ) } ,
62+ ] } ,
4963 ] ,
5064 lazy :true
5165 } ) ) . render ( ) ;
@@ -80,10 +94,10 @@ function showNumpad() {
8094 { type :"h" , filly :1 , c : [ digitBtn ( "4" ) , digitBtn ( "5" ) , digitBtn ( "6" ) ] } ,
8195 { type :"h" , filly :1 , c : [ digitBtn ( "7" ) , digitBtn ( "8" ) , digitBtn ( "9" ) ] } ,
8296 { type :"h" , filly :1 , c : [
83- { type :"btn" , font :ds , width :58 , label :"C" , cb : removeDigit } ,
84- digitBtn ( '0' ) ,
85- { type :"btn" , font :ds , width :58 , id :"OK" , label :"OK" , cb : l => resolve ( number ) }
86- ] }
97+ { type :"btn" , font :ds , width :58 , label :"C" , cb : removeDigit } ,
98+ digitBtn ( '0' ) ,
99+ { type :"btn" , font :ds , width :58 , id :"OK" , label :"OK" , cb : l => resolve ( number ) }
100+ ] }
87101 ] }
88102 ] , lazy :true } ) ;
89103 g . clear ( ) ;
@@ -92,77 +106,28 @@ function showNumpad() {
92106 } ) ;
93107}
94108
95- function removeContact ( ) {
96- var menu = {
97- "" : { title : "Select Contact" } ,
98- "< Back" : mainMenu
99- } ;
100- if ( wp . length === 0 ) Object . assign ( menu , { "No Contacts" :"" } ) ;
101- else {
102- wp . forEach ( ( val , card ) => {
103- const name = wp [ card ] . name ;
104- menu [ name ] = ( ) => {
105- E . showMenu ( ) ;
106- var confirmRemove = new Layout (
107- { type :"v" , c : [
108- { type :"txt" , font :"15%" , pad :1 , fillx :1 , filly :1 , label :"Delete" } ,
109- { type :"txt" , font :"15%" , pad :1 , fillx :1 , filly :1 , label :name } ,
110- { type :"h" , c : [
111- { type :"btn" , font :"15%" , pad :1 , fillx :1 , filly :1 , label : "YES" , cb :l => {
112- wp . splice ( card , 1 ) ;
113- writeContacts ( ) ;
114- mainMenu ( ) ;
115- } } ,
116- { type :"btn" , font :"15%" , pad :1 , fillx :1 , filly :1 , label : " NO" , cb :l => { mainMenu ( ) ; } }
117- ] }
118- ] , lazy :true } ) ;
119- g . clear ( ) ;
120- confirmRemove . render ( ) ;
121- } ;
122- } ) ;
123- }
124- E . showMenu ( menu ) ;
125- }
126-
127-
128- function addNewContact ( name ) {
129- g . clear ( ) ;
130- showNumpad ( ) . then ( ( number ) => {
131- wp . push ( { name : name , number : number } ) ;
132- writeContacts ( ) ;
133- mainMenu ( ) ;
134- } )
135-
136-
137-
138- }
139-
140- function tryAddContact ( name ) {
141- if ( wp . filter ( ( e ) => e . name === name ) . length ) {
142- E . showMenu ( ) ;
143- var alreadyExists = new Layout (
144- { type :"v" , c : [
145- { type :"txt" , font :Math . min ( 15 , 100 / name . length ) + "%" , pad :1 , fillx :1 , filly :1 , label :name } ,
146- { type :"txt" , font :"12%" , pad :1 , fillx :1 , filly :1 , label :"already exists." } ,
147- { type :"h" , c : [
148- { type :"btn" , font :"10%" , pad :1 , fillx :1 , filly :1 , label : "REPLACE" , cb :l => { addNewContact ( name ) ; } } ,
149- { type :"btn" , font :"10%" , pad :1 , fillx :1 , filly :1 , label : "CANCEL" , cb :l => { mainMenu ( ) ; } }
150- ] }
151- ] , lazy :true } ) ;
152- g . clear ( ) ;
153- alreadyExists . render ( ) ;
154- return ;
155- }
156- addNewContact ( name ) ;
109+ function deleteContact ( idx ) {
110+ contacts . splice ( idx , 1 ) ;
111+ writeContacts ( ) ;
112+ mainMenu ( ) ;
157113}
158114
159115function addContact ( ) {
160- require ( "textinput" ) . input ( { text :"" } ) . then ( name => {
161- if ( name !== "" ) {
162- tryAddContact ( name ) ;
163- } else
164- mainMenu ( ) ;
165- } ) ;
116+ require ( "textinput" ) . input ( { text :"" } )
117+ . then ( name => {
118+ name = name . trim ( ) ;
119+ if ( name !== "" ) {
120+ g . clear ( ) ;
121+ showNumpad ( ) . then ( ( number ) => {
122+ contacts . push ( { name : name , number : number } ) ;
123+ writeContacts ( ) ;
124+ mainMenu ( ) ;
125+ } )
126+ } else {
127+ E . showMessage ( "Invalid name" ) ;
128+ setTimeout ( ( ) => mainMenu ( ) , 1000 ) ;
129+ }
130+ } ) ;
166131}
167132
168133g . reset ( ) ;
0 commit comments