[fix 5870] Added QR code API

Signed-off-by: yenda <eric@status.im>
This commit is contained in:
Julien Eluard 2018-09-13 16:07:42 +02:00 committed by yenda
parent 8459fef358
commit 10373ae6fd
No known key found for this signature in database
GPG Key ID: 0095623C0069DCE6
6 changed files with 72 additions and 12 deletions

View File

@ -47,6 +47,25 @@ WebViewBridge.onMessage = function (message) {
} }
} }
} }
else if (data.type === "scan-qr-code-callback")
{
var id = data.data.messageId;
var callback = callbacks[id];
if (callback) {
var result = data.result;
var regex = new RegExp(callback.regex);
if (regex.test(result)) {
if (callback.resolve) {
callback.resolve(result);
}
} else {
if (callback.reject) {
callback.reject(result);
}
}
}
}
}; };
var StatusHttpProvider = function () {}; var StatusHttpProvider = function () {};
@ -114,10 +133,19 @@ StatusHttpProvider.prototype.sendAsync = function (payload, callback) {
messageId: messageId, messageId: messageId,
payload: payload}); payload: payload});
} }
} }
}; };
StatusHttpProvider.prototype.scanQRCode = function (regex) {
return new Promise(function (resolve, reject) {
var messageId = callbackId++;
callbacks[messageId] = {resolve: resolve, reject: reject, regex: regex};
bridgeSend({type: 'scan-qr-code',
messageId: messageId});
});
};
StatusHttpProvider.prototype.enable = function () { StatusHttpProvider.prototype.enable = function () {
return new Promise(function (resolve, reject) { setTimeout(resolve, 1000);}); return new Promise(function (resolve, reject) { setTimeout(resolve, 1000);});
}; };

View File

@ -7,6 +7,7 @@
[status-im.i18n :as i18n] [status-im.i18n :as i18n]
[status-im.js-dependencies :as dependencies] [status-im.js-dependencies :as dependencies]
[status-im.native-module.core :as status] [status-im.native-module.core :as status]
[status-im.qr-scanner.core :as qr-scanner]
[status-im.ui.components.list-selection :as list-selection] [status-im.ui.components.list-selection :as list-selection]
[status-im.ui.screens.browser.default-dapps :as default-dapps] [status-im.ui.screens.browser.default-dapps :as default-dapps]
[status-im.ui.screens.navigation :as navigation] [status-im.ui.screens.navigation :as navigation]
@ -258,6 +259,12 @@
cofx) cofx)
(web3-send-async payload message-id cofx)))) (web3-send-async payload message-id cofx))))
(defn handle-scanned-qr-code
[data message cofx]
(handlers-macro/merge-fx cofx
(send-to-bridge (assoc message :result data))
(navigation/navigate-back)))
(defn process-bridge-message (defn process-bridge-message
[message {:keys [db] :as cofx}] [message {:keys [db] :as cofx}]
(let [{:browser/keys [options browsers]} db (let [{:browser/keys [options browsers]} db
@ -279,6 +286,13 @@
(= type constants/web3-send-async-read-only) (= type constants/web3-send-async-read-only)
(web3-send-async-read-only dapp-name payload messageId cofx) (web3-send-async-read-only dapp-name payload messageId cofx)
(= type constants/scan-qr-code)
(qr-scanner/scan-qr-code {:modal? false}
(merge {:handler :browser.bridge.callback/qr-code-scanned}
{:type constants/scan-qr-code-callback
:data data})
cofx)
(= type constants/status-api-request) (= type constants/status-api-request)
(browser.permissions/process-permissions dapp-name permissions cofx)))) (browser.permissions/process-permissions dapp-name permissions cofx))))

View File

@ -196,3 +196,5 @@
(def ^:const web3-send-async "web3-send-async") (def ^:const web3-send-async "web3-send-async")
(def ^:const web3-send-async-read-only "web3-send-async-read-only") (def ^:const web3-send-async-read-only "web3-send-async-read-only")
(def ^:const web3-send-async-callback "web3-send-async-callback") (def ^:const web3-send-async-callback "web3-send-async-callback")
(def ^:const scan-qr-code "scan-qr-code")
(def ^:const scan-qr-code-callback "scan-qr-code-callback")

View File

@ -400,12 +400,19 @@
(fn [cofx [_ log-level]] (fn [cofx [_ log-level]]
(log-level/show-change-log-level-confirmation log-level cofx))) (log-level/show-change-log-level-confirmation log-level cofx)))
;; Browser bridge module
(handlers/register-handler-fx
:browser.bridge.callback/qr-code-scanned
(fn [cofx [_ _ data message]]
(browser/handle-scanned-qr-code data message cofx)))
;; qr-scanner module ;; qr-scanner module
(handlers/register-handler-fx (handlers/register-handler-fx
:qr-scanner.ui/scan-qr-code-pressed :qr-scanner.ui/scan-qr-code-pressed
(fn [cofx [_ identifier handler]] (fn [cofx [_ identifier handler & [opts]]]
(qr-scanner/scan-qr-code identifier handler cofx))) (qr-scanner/scan-qr-code identifier (merge {:handler handler} opts) cofx)))
(handlers/register-handler-fx (handlers/register-handler-fx
:qr-scanner.callback/scan-qr-code-success :qr-scanner.callback/scan-qr-code-success

View File

@ -4,10 +4,11 @@
[status-im.utils.utils :as utils])) [status-im.utils.utils :as utils]))
(defn scan-qr-code (defn scan-qr-code
[identifier handler {:keys [db]}] [{:keys [modal?] :as identifier} qr-codes {:keys [db]}]
{:db (assoc-in db [:qr-codes identifier] handler) {:db (assoc db :qr-codes qr-codes)
:request-permissions-fx {:permissions [:camera] :request-permissions-fx {:permissions [:camera]
:on-allowed #(re-frame/dispatch [:navigate-to :qr-scanner {:current-qr-context identifier}]) :on-allowed #(re-frame/dispatch [(if modal? :navigate-to-modal :navigate-to)
:qr-scanner {:current-qr-context identifier}])
:on-denied (fn [] :on-denied (fn []
(utils/set-timeout (utils/set-timeout
#(utils/show-popup (i18n/label :t/error) #(utils/show-popup (i18n/label :t/error)
@ -19,5 +20,5 @@
(merge {:db (-> db (merge {:db (-> db
(update :qr-codes dissoc context) (update :qr-codes dissoc context)
(dissoc :current-qr-context))} (dissoc :current-qr-context))}
(when-let [handler (get-in db [:qr-codes context])] (when-let [qr-codes (:qr-codes db)]
{:dispatch [handler context data]}))) {:dispatch [(:handler qr-codes) context data (dissoc qr-codes :handler)]})))

View File

@ -2,6 +2,7 @@
(:require-macros [status-im.utils.views :refer [defview letsubs]]) (:require-macros [status-im.utils.views :refer [defview letsubs]])
(:require [reagent.core :as reagent] (:require [reagent.core :as reagent]
[re-frame.core :as re-frame] [re-frame.core :as re-frame]
[status-im.i18n :as i18n]
[status-im.ui.components.react :as react] [status-im.ui.components.react :as react]
[status-im.ui.components.camera :as camera] [status-im.ui.components.camera :as camera]
[status-im.ui.components.status-bar.view :as status-bar] [status-im.ui.components.status-bar.view :as status-bar]
@ -14,13 +15,20 @@
[status-bar/status-bar] [status-bar/status-bar]
[toolbar/simple-toolbar title]])) [toolbar/simple-toolbar title]]))
(defn on-barcode-read [identifier data]
(re-frame/dispatch [:qr-scanner.callback/scan-qr-code-success identifier (camera/get-qr-code-data data)]))
(defview qr-scanner [] (defview qr-scanner []
(letsubs [{identifier :current-qr-context} [:get-screen-params] (letsubs [{identifier :current-qr-context} [:get-screen-params]
camera-initialized? (reagent/atom false)] camera-initialized? (reagent/atom false)
barcode-read? (reagent/atom false)]
[react/view styles/barcode-scanner-container [react/view styles/barcode-scanner-container
[qr-scanner-toolbar (:toolbar-title identifier) (not @camera-initialized?)] [qr-scanner-toolbar (or (:toolbar-title identifier) (i18n/label :t/scan-qr)) (not @camera-initialized?)]
[camera/camera {:onBarCodeRead #(re-frame/dispatch [:qr-scanner.callback/scan-qr-code-success identifier (camera/get-qr-code-data %)]) [camera/camera {:onBarCodeRead #(if (:multiple? identifier)
(on-barcode-read identifier %)
(when-not @barcode-read?
(do (reset! barcode-read? true)
(on-barcode-read identifier %))))
:ref #(reset! camera-initialized? true) :ref #(reset! camera-initialized? true)
:captureAudio false :captureAudio false
:style styles/barcode-scanner}] :style styles/barcode-scanner}]