|
| 1 | +/* CARDS is a list of: |
| 2 | + {id:int, |
| 3 | + name, |
| 4 | + value, |
| 5 | + type, |
| 6 | + expiration, |
| 7 | + color, |
| 8 | + balance, |
| 9 | + note, |
| 10 | + ... |
| 11 | + } |
| 12 | +*/ |
| 13 | + |
| 14 | +Bangle.loadWidgets(); |
| 15 | +Bangle.drawWidgets(); |
| 16 | + |
| 17 | +//may make it configurable in the future |
| 18 | +const WHITE=-1 |
| 19 | +const BLACK=0 |
| 20 | + |
| 21 | +var FILE = "android.cards.json"; |
| 22 | + |
| 23 | +var Locale = require("locale"); |
| 24 | + |
| 25 | +var fontSmall = "6x8"; |
| 26 | +var fontMedium = g.getFonts().includes("6x15")?"6x15":"6x8:2"; |
| 27 | +var fontBig = g.getFonts().includes("12x20")?"12x20":"6x8:2"; |
| 28 | +var fontLarge = g.getFonts().includes("6x15")?"6x15:2":"6x8:4"; |
| 29 | + |
| 30 | +var CARDS = require("Storage").readJSON("android.cards.json",true)||[]; |
| 31 | +var settings = require("Storage").readJSON("cards.settings.json",true)||{}; |
| 32 | + |
| 33 | +function getDate(timestamp) { |
| 34 | + return new Date(timestamp*1000); |
| 35 | +} |
| 36 | +function formatDay(date) { |
| 37 | + let formattedDate = Locale.dow(date,1) + " " + Locale.date(date).replace(/\d\d\d\d/,""); |
| 38 | + if (!settings.useToday) { |
| 39 | + return formattedDate; |
| 40 | + } |
| 41 | + const today = new Date(Date.now()); |
| 42 | + if (date.getDay() == today.getDay() && date.getMonth() == today.getMonth()) |
| 43 | + return /*LANG*/"Today "; |
| 44 | + else { |
| 45 | + const tomorrow = new Date(Date.now() + 86400 * 1000); |
| 46 | + if (date.getDay() == tomorrow.getDay() && date.getMonth() == tomorrow.getMonth()) { |
| 47 | + return /*LANG*/"Tomorrow "; |
| 48 | + } |
| 49 | + return formattedDate; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function getColor(intColor) { |
| 54 | + return "#"+(0x1000000+Number(intColor)).toString(16).padStart(6,"0"); |
| 55 | +} |
| 56 | +function isLight(color) { |
| 57 | + var r = +("0x"+color.slice(1,3)); |
| 58 | + var g = +("0x"+color.slice(3,5)); |
| 59 | + var b = +("0x"+color.slice(5,7)); |
| 60 | + var threshold = 0x88 * 3; |
| 61 | + return (r+g+b) > threshold; |
| 62 | +} |
| 63 | + |
| 64 | +function printSquareCode(binary, size) { |
| 65 | + var padding = 5; |
| 66 | + var ratio = (g.getWidth()-(2*padding))/size; |
| 67 | + for (var y = 0; y < size; y++) { |
| 68 | + for (var x = 0; x < size; x++) { |
| 69 | + if (binary[x + y * size]) { |
| 70 | + g.setColor(BLACK).fillRect({x:x*ratio+padding, y:y*ratio+padding, w:ratio, h:ratio}); |
| 71 | + } else { |
| 72 | + g.setColor(WHITE).fillRect({x:x*ratio+padding, y:y*ratio+padding, w:ratio, h:ratio}); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | +function printLinearCode(binary) { |
| 78 | + var yFrom = 15; |
| 79 | + var yTo = 28; |
| 80 | + var width = g.getWidth()/binary.length; |
| 81 | + for(var b = 0; b < binary.length; b++){ |
| 82 | + var x = b * width; |
| 83 | + if(binary[b] === "1"){ |
| 84 | + g.setColor(BLACK).fillRect({x:x, y:yFrom, w:width, h:g.getHeight() - (yTo+yFrom)}); |
| 85 | + } |
| 86 | + else if(binary[b]){ |
| 87 | + g.setColor(WHITE).fillRect({x:x, y:yFrom, w:width, h:g.getHeight() - (yTo+yFrom)}); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +function showCode(card) { |
| 93 | + E.showScroller(); |
| 94 | + // keeping it on rising edge would come back twice.. |
| 95 | + setWatch(()=>showCard(card), BTN, {edge:"falling"}); |
| 96 | + // theme independent |
| 97 | + g.setColor(WHITE).fillRect(0, 0, g.getWidth(), g.getHeight()); |
| 98 | + switch (card.type) { |
| 99 | + case "QR_CODE": { |
| 100 | + const getBinaryQR = require("cards.qrcode.js"); |
| 101 | + let code = getBinaryQR(card.value); |
| 102 | + printSquareCode(code.data, code.size); |
| 103 | + break; |
| 104 | + } |
| 105 | + case "CODE_39": { |
| 106 | + g.setFont("Vector:20"); |
| 107 | + g.setFontAlign(0,1).setColor(BLACK); |
| 108 | + g.drawString(card.value, g.getWidth()/2, g.getHeight()); |
| 109 | + const CODE39 = require("cards.code39.js"); |
| 110 | + let code = new CODE39(card.value, {}); |
| 111 | + printLinearCode(code.encode().data); |
| 112 | + break; |
| 113 | + } |
| 114 | + case "CODABAR": { |
| 115 | + g.setFont("Vector:20"); |
| 116 | + g.setFontAlign(0,1).setColor(BLACK); |
| 117 | + g.drawString(card.value, g.getWidth()/2, g.getHeight()); |
| 118 | + const codabar = require("cards.codabar.js"); |
| 119 | + let code = new codabar(card.value, {}); |
| 120 | + printLinearCode(code.encode().data); |
| 121 | + break; |
| 122 | + } |
| 123 | + default: |
| 124 | + g.clear(true); |
| 125 | + g.setFont("Vector:30"); |
| 126 | + g.setFontAlign(0,0); |
| 127 | + g.drawString(card.value, g.getWidth()/2, g.getHeight()/2); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +function showCard(card) { |
| 132 | + var lines = []; |
| 133 | + var bodyFont = fontBig; |
| 134 | + if(!card) return; |
| 135 | + g.setFont(bodyFont); |
| 136 | + //var lines = []; |
| 137 | + if (card.name) lines = g.wrapString(card.name, g.getWidth()-10); |
| 138 | + var titleCnt = lines.length; |
| 139 | + var start = getDate(card.expiration); |
| 140 | + var includeDay = true; |
| 141 | + lines = lines.concat("", /*LANG*/"View code"); |
| 142 | + var valueLine = lines.length - 1; |
| 143 | + if (card.expiration) |
| 144 | + lines = lines.concat("",/*LANG*/"Expires"+": ", g.wrapString(formatDay(getDate(card.expiration)), g.getWidth()-10)); |
| 145 | + if(card.balance) |
| 146 | + lines = lines.concat("",/*LANG*/"Balance"+": ", g.wrapString(card.balance, g.getWidth()-10)); |
| 147 | + if(card.note && card.note.trim()) |
| 148 | + lines = lines.concat("",g.wrapString(card.note, g.getWidth()-10)); |
| 149 | + lines = lines.concat("",/*LANG*/"< Back"); |
| 150 | + var titleBgColor = card.color ? getColor(card.color) : g.theme.bg2; |
| 151 | + var titleColor = g.theme.fg2; |
| 152 | + if (card.color) |
| 153 | + titleColor = isLight(titleBgColor) ? BLACK : WHITE; |
| 154 | + E.showScroller({ |
| 155 | + h : g.getFontHeight(), // height of each menu item in pixels |
| 156 | + c : lines.length, // number of menu items |
| 157 | + // a function to draw a menu item |
| 158 | + draw : function(idx, r) { |
| 159 | + // FIXME: in 2v13 onwards, clearRect(r) will work fine. There's a bug in 2v12 |
| 160 | + g.setBgColor(idx<titleCnt ? titleBgColor : g.theme.bg). |
| 161 | + setColor(idx<titleCnt ? titleColor : g.theme.fg). |
| 162 | + clearRect(r.x,r.y,r.x+r.w, r.y+r.h); |
| 163 | + g.setFont(bodyFont).drawString(lines[idx], r.x, r.y); |
| 164 | + }, select : function(idx) { |
| 165 | + if (idx>=lines.length-2) |
| 166 | + showList(); |
| 167 | + else if (idx==valueLine) |
| 168 | + showCode(card); |
| 169 | + }, |
| 170 | + back : () => showList() |
| 171 | + }); |
| 172 | +} |
| 173 | + |
| 174 | +// https://github.com/metafloor/bwip-js |
| 175 | +// https://github.com/lindell/JsBarcode |
| 176 | + |
| 177 | +function showList() { |
| 178 | + if(CARDS.length == 0) { |
| 179 | + E.showMessage(/*LANG*/"No cards"); |
| 180 | + return; |
| 181 | + } |
| 182 | + E.showScroller({ |
| 183 | + h : 52, |
| 184 | + c : Math.max(CARDS.length,3), // workaround for 2v10.219 firmware (min 3 not needed for 2v11) |
| 185 | + draw : function(idx, r) {"ram" |
| 186 | + var card = CARDS[idx]; |
| 187 | + g.setColor(g.theme.fg); |
| 188 | + g.clearRect(r.x,r.y,r.x+r.w, r.y+r.h); |
| 189 | + if (!card) return; |
| 190 | + var isPast = false; |
| 191 | + var x = r.x+2, name = card.name; |
| 192 | + var body = card.expiration ? formatDay(getDate(card.expiration)) : ""; |
| 193 | + if (card.balance) body += "\n" + card.balance; |
| 194 | + if (name) g.setFontAlign(-1,-1).setFont(fontBig) |
| 195 | + .setColor(isPast ? "#888" : g.theme.fg).drawString(name, x+4,r.y+2); |
| 196 | + if (body) { |
| 197 | + g.setFontAlign(-1,-1).setFont(fontMedium).setColor(isPast ? "#888" : g.theme.fg); |
| 198 | + g.drawString(body, x+10,r.y+20); |
| 199 | + } |
| 200 | + g.setColor("#888").fillRect(r.x,r.y+r.h-1,r.x+r.w-1,r.y+r.h-1); // dividing line between items |
| 201 | + if(card.color) { |
| 202 | + g.setColor(getColor(card.color)); |
| 203 | + g.fillRect(r.x,r.y+4,r.x+3, r.y+r.h-4); |
| 204 | + } |
| 205 | + }, |
| 206 | + select : idx => showCard(CARDS[idx]), |
| 207 | + back : () => load() |
| 208 | + }); |
| 209 | +} |
| 210 | +showList(); |
0 commit comments