Skip to content

Commit 1751971

Browse files
committed
trying camera barcode reader
1 parent 0efbd59 commit 1751971

File tree

14 files changed

+27440
-27379
lines changed

14 files changed

+27440
-27379
lines changed

ghcjs/lightning-verifier/package-lock.json

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ghcjs/lightning-verifier/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"@capacitor/share": "^6.0.1",
1818
"@capacitor/toast": "^6.0.2",
1919
"@ionic/pwa-elements": "^3.3.0",
20-
"capacitor-webview-print": "^6.0.1"
20+
"capacitor-webview-print": "^6.0.1",
21+
"html5-qrcode": "^2.3.8"
2122
},
2223
"devDependencies": {
2324
"@capacitor/assets": "^3.0.4",

ghcjs/lightning-verifier/src/App/Widgets/Main.hs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,19 @@ screenWidget st@Model {modelState = St {stScreen = Converter}} =
178178
{ Field.optsFilledOrOutlined = Outlined,
179179
Field.optsPlaceholder = "Invoice",
180180
Field.optsLeadingWidget =
181-
pasteWidget $ #modelState . #stDoc . #stDocLnInvoice
181+
pasteWidget
182+
"content_paste"
183+
Jsm.selectClipboard
184+
$ #modelState
185+
. #stDoc
186+
. #stDocLnInvoice,
187+
Field.optsTrailingWidget =
188+
pasteWidget
189+
"qr_code_scanner"
190+
Jsm.selectBarcode
191+
$ #modelState
192+
. #stDoc
193+
. #stDocLnInvoice
182194
}
183195
),
184196
Field.textField
@@ -191,29 +203,44 @@ screenWidget st@Model {modelState = St {stScreen = Converter}} =
191203
{ Field.optsFilledOrOutlined = Outlined,
192204
Field.optsPlaceholder = "Preimage",
193205
Field.optsLeadingWidget =
194-
pasteWidget $ #modelState . #stDoc . #stDocLnPreimage
206+
pasteWidget
207+
"content_paste"
208+
Jsm.selectClipboard
209+
$ #modelState
210+
. #stDoc
211+
. #stDocLnPreimage,
212+
Field.optsTrailingWidget =
213+
pasteWidget
214+
"qr_code_scanner"
215+
Jsm.selectBarcode
216+
$ #modelState
217+
. #stDoc
218+
. #stDocLnPreimage
195219
}
196220
]
197221
<> Bolt11.bolt11 st
198222

199223
pasteWidget ::
224+
MisoString ->
225+
((Maybe MisoString -> JSM ()) -> JSM ()) ->
200226
ATraversal' Model (Field MisoString Unique) ->
201227
Maybe (Field.OptsWidget Model Action)
202-
pasteWidget optic =
228+
pasteWidget icon selector optic =
203229
Just
204-
. Field.ActionWidget "content_paste" mempty
230+
. Field.ActionWidget icon mempty
205231
. PushUpdate
206232
. Instant
207233
$ \prev -> do
208-
Jsm.selectClipboard $ \case
234+
selector $ \case
209235
Nothing ->
210-
Jsm.popupText @MisoString "Failed to paste!"
211-
Just clip ->
236+
Jsm.popupText @MisoString "Failure!"
237+
Just res -> do
212238
Misc.pushActionQueue prev
213239
. Instant
214240
$ pure
215-
. (& cloneTraversal optic . #fieldOutput .~ clip)
216-
. (& cloneTraversal optic . #fieldInput . #uniqueValue .~ clip)
241+
. (& cloneTraversal optic . #fieldOutput .~ res)
242+
. (& cloneTraversal optic . #fieldInput . #uniqueValue .~ res)
243+
Jsm.popupText @MisoString "Success!"
217244
pure prev
218245

219246
tosWidget :: View Action

ghcjs/lightning-verifier/static/app.css

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,13 @@ textarea {
4040
justify-content: center;
4141
}
4242

43-
.app-success > span > div > .mdc-icon-button {
44-
color: white !important;
45-
}
46-
4743
.app-failure > span {
4844
color: white;
4945
background-color: #b00020 !important;
5046
display: flex;
5147
justify-content: center;
5248
}
5349

54-
.app-failure > span > div > .mdc-icon-button {
55-
color: white !important;
56-
}
57-
5850
@media print {
5951
body {
6052
color-adjust: exact !important;

ghcjs/lightning-verifier/static/cap.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import { CapacitorBarcodeScanner } from "@capacitor/barcode-scanner";
21
import { defineCustomElements } from "@ionic/pwa-elements/loader";
32
import { WebviewPrint } from "capacitor-webview-print";
43
import { Preferences } from "@capacitor/preferences";
54
import { Clipboard } from "@capacitor/clipboard";
65
import { Browser } from "@capacitor/browser";
76
import { Share } from "@capacitor/share";
87
import { Toast } from "@capacitor/toast";
8+
import { Capacitor } from "@capacitor/core";
9+
import { Html5Qrcode } from "html5-qrcode";
10+
import {
11+
CapacitorBarcodeScanner,
12+
CapacitorBarcodeScannerTypeHint,
13+
} from "@capacitor/barcode-scanner";
914

1015
async function printCurrentPage(name) {
1116
return await WebviewPrint.print({ name: name });
@@ -46,8 +51,16 @@ async function popupText(text) {
4651
return await Toast.show({ text: text });
4752
}
4853

49-
async function scanBarcode() {
50-
const { ScanResult } = await CapacitorBarcodeScanner.scanBarcode({});
54+
async function selectBarcode() {
55+
if (!Capacitor.isNativePlatform()) {
56+
const devices = await Html5Qrcode.getCameras();
57+
if (!(devices && devices.length)) {
58+
throw new Error("Camera not found!");
59+
}
60+
}
61+
const { ScanResult } = await CapacitorBarcodeScanner.scanBarcode({
62+
hint: CapacitorBarcodeScannerTypeHint.ALL,
63+
});
5164
return ScanResult;
5265
}
5366

@@ -59,4 +72,4 @@ globalThis.selectClipboard = selectClipboard;
5972
globalThis.openBrowserPage = openBrowserPage;
6073
globalThis.shareText = shareText;
6174
globalThis.popupText = popupText;
62-
globalThis.scanBarcode = scanBarcode;
75+
globalThis.selectBarcode = selectBarcode;

0 commit comments

Comments
 (0)