Created
November 27, 2017 18:40
-
-
Save ray-delossantos/5e428c8e275cde6a01905b7dd9a26840 to your computer and use it in GitHub Desktop.
Trying Cordova with Barcode Scanner Plugin and Karax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# <Araq> I don't know what to say, callbacks are not special. a Nim proc is mapped to a JS function, just pass it around | |
# <Araq> proc takesCallback(x: proc()) {.importc.} | |
# <Araq> takesCallback proc = | |
# <Araq> echo "my code here" | |
include karax / prelude | |
from future import `=>` | |
import karax / errors | |
type | |
CordovaGlobal* {.importc.} = ref object of RootObj | |
plugins*: Plugins | |
Plugins* {.importc.} = ref object of RootObj | |
barcodeScanner*: BarcodeScanner | |
BarcodeScanner* {.importc.} = ref object of RootObj | |
BarcodeScannerScanResult* {.importc, nodecl.} = ref object of RootObj | |
cancelled*: bool | |
format*: kstring | |
resultText*: kstring | |
BarcodeScannerScanOptions* {.importc.} = ref object of RootObj | |
preferFrontCamera*, showFlipCameraButton*, showTorchButton*, torchOn*, | |
disableAnimations*, disableSuccessBeep*: bool | |
prompt*, formats*, orientation*, : kstring | |
resultDisplayDuration*: int | |
var | |
barcodeScanOptions* = BarcodeScannerScanOptions( | |
preferFrontCamera: false, | |
showFlipCameraButton: false, | |
showTorchButton: false, | |
torchOn: false, | |
prompt: "Coloque una imagen QR dentro del recuadro de escaneo", | |
resultDisplayDuration: 0, | |
formats: "QR_CODE", | |
orientation: "portrait", | |
disableAnimations: true, | |
disableSuccessBeep: false | |
) | |
cordova* {.importc, nodecl.}: CordovaGlobal | |
proc scan*(barcode:BarcodeScanner, | |
success: proc(result:BarcodeScannerScanResult): void, | |
error: proc(error:kstring): void, | |
options:BarcodeScannerScanOptions): void {. importcpp: "#.scan(@)" .} | |
# some consts in order to prevent typos: | |
const | |
username = kstring"username" | |
password = kstring"password" | |
# | |
proc barcodeScanSuccessHandler(success:BarcodeScannerScanResult): void = | |
echo success.format | |
proc barcodeScanErrorHandler(error:kstring): void = | |
echo error | |
proc loginField(desc, field, class: kstring; | |
validator: proc (field: kstring): proc ()): VNode = | |
result = buildHtml(tdiv): | |
label(`for` = field): | |
text desc | |
input(class = class, id = field, onchange = validator(field)) | |
proc validateNotEmpty(field: kstring): proc () = | |
result = proc () = | |
let x = getVNodeById(field) | |
if x.text.isNil or x.text == "": | |
errors.setError(field, field & " must not be empty") | |
else: | |
errors.setError(field, "") | |
var loggedIn: bool | |
proc loginDialog(): VNode = | |
result = buildHtml(tdiv): | |
if not loggedIn: | |
loginField("Name :", username, "input", validateNotEmpty) | |
loginField("Password: ", password, "password", validateNotEmpty) | |
button(onclick = () => ( | |
cordova.plugins.barcodeScanner.scan( | |
barcodeScanSuccessHandler, barcodeScanErrorHandler, barcodeScanOptions ) | |
), disabled = errors.disableOnError()): | |
text "Login" | |
p: | |
text errors.getError(username) | |
p: | |
text errors.getError(password) | |
else: | |
p: | |
text "You are now logged in." | |
setError username, username & " must not be empty" | |
setError password, password & " must not be empty" | |
setRenderer loginDialog |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment