From a156c8f865fa3671cd05bb2eda8299ed371a50fe Mon Sep 17 00:00:00 2001 From: Adrian Tiberius Date: Thu, 2 Jun 2016 13:43:26 +0300 Subject: [PATCH] make qr-scanner callable multiple times from different screens Former-commit-id: 6eeefb2efbd460376eb00b9714422e1c23724402 --- src/status_im/contacts/handlers.cljs | 12 +++++++++++- src/status_im/contacts/views/new_contact.cljs | 13 ++++++++----- src/status_im/db.cljs | 1 + src/status_im/handlers.cljs | 1 + src/status_im/i18n.cljs | 4 ++-- src/status_im/qr_scanner/handlers.cljs | 12 ++++++++++++ src/status_im/qr_scanner/screen.cljs | 9 +++------ 7 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 src/status_im/qr_scanner/handlers.cljs diff --git a/src/status_im/contacts/handlers.cljs b/src/status_im/contacts/handlers.cljs index aad3ec2bd6..4d6237bd55 100644 --- a/src/status_im/contacts/handlers.cljs +++ b/src/status_im/contacts/handlers.cljs @@ -110,4 +110,14 @@ (register-handler :add-new-contact (after save-contact) - add-new-contact) \ No newline at end of file + add-new-contact) + +(defn set-new-contact-from-qr [db [_ identifier]] + (let [new-contact (:new-contact db) + qr-contact (get-in db [:qr-codes identifier])] + (-> db + (assoc :new-contact (merge new-contact qr-contact)) + (update-in db [:qr-codes] dissoc identifier)))) + +(register-handler :set-new-contact-from-qr + (-> set-new-contact-from-qr)) \ No newline at end of file diff --git a/src/status_im/contacts/views/new_contact.cljs b/src/status_im/contacts/views/new_contact.cljs index 8b5864be3f..955e92b648 100644 --- a/src/status_im/contacts/views/new_contact.cljs +++ b/src/status_im/contacts/views/new_contact.cljs @@ -23,11 +23,11 @@ toolbar-background2 form-text-input]] [status-im.i18n :refer [label]] - [status-im.contacts.styles :as st])) + [status-im.contacts.styles :as st] + [status-im.utils.logging :as log])) (defn import-qr-button [] - [touchable-highlight {:on-press #(dispatch [:navigate-to - :qr-scanner])} + [touchable-highlight {:on-press #(dispatch [:scan-qr-code :new-contact])} [view st/import-qr-button [view st/import-qr-button-content [icon {:name :qr-scanner @@ -55,7 +55,10 @@ address]) (defview new-contact [] - [{:keys [name address whisper-identity phone-number] :as new-contact} [:get :new-contact]] + [{:keys [name address whisper-identity phone-number] :as new-contact} [:get :new-contact] + qr-contact [:get-in [:qr-codes :new-contact]]] + (let [_ (log/debug qr-contact) + _ (when qr-contact (dispatch [:set-new-contact-from-qr :new-contact]))] [drawer-view [view st/contact-form-container [toolbar {:title (label :t/new-contact) @@ -67,4 +70,4 @@ [contact-name-input name] [contact-address-input address] [text (str "Whisper identity: " whisper-identity)] - ]]) \ No newline at end of file + ]])) \ No newline at end of file diff --git a/src/status_im/db.cljs b/src/status_im/db.cljs index 4dee5734f3..c8ebde0d3b 100644 --- a/src/status_im/db.cljs +++ b/src/status_im/db.cljs @@ -29,6 +29,7 @@ :email "myemail@gmail.com" :status "Hi, this is my status" :current-tag nil + :qr-codes {:identifier nil} :new-contact {:name "" :address "" :whisper-identity "" diff --git a/src/status_im/handlers.cljs b/src/status_im/handlers.cljs index 4010049665..a5c839bf61 100644 --- a/src/status_im/handlers.cljs +++ b/src/status_im/handlers.cljs @@ -17,6 +17,7 @@ status-im.discovery.handlers status-im.new-group.handlers status-im.participants.handlers + status-im.qr-scanner.handlers status-im.protocol.handlers)) ;; -- Middleware ------------------------------------------------------------ diff --git a/src/status_im/i18n.cljs b/src/status_im/i18n.cljs index ae33e2f243..de557041fc 100644 --- a/src/status_im/i18n.cljs +++ b/src/status_im/i18n.cljs @@ -11,8 +11,8 @@ (defn label [path & options] (.t js/I18n (name path) (clj->js options))) -(defn label-pluralize [count path] - (.p js/I18n count (name path))) +(defn label-pluralize [count path & options] + (.p js/I18n count (name path) (clj->js options))) (comment (defn deep-merge [& maps] diff --git a/src/status_im/qr_scanner/handlers.cljs b/src/status_im/qr_scanner/handlers.cljs new file mode 100644 index 0000000000..b8bc1bb300 --- /dev/null +++ b/src/status_im/qr_scanner/handlers.cljs @@ -0,0 +1,12 @@ +(ns status-im.qr-scanner.handlers + (:require [re-frame.core :refer [register-handler after dispatch debug enrich]])) + +(defn set-current-identifier [db [_ identifier]] + (assoc-in db [:qr-codes :identifier] identifier)) + +(register-handler :scan-qr-code + (-> set-current-identifier + ((after (fn [_ _] (dispatch [:navigate-to :qr-scanner])))))) + +(register-handler :clear-qr-code [db [_ identifier]] + (update-in db [:qr-codes] dissoc identifier)) diff --git a/src/status_im/qr_scanner/screen.cljs b/src/status_im/qr_scanner/screen.cljs index a791042f87..f0846830ca 100644 --- a/src/status_im/qr_scanner/screen.cljs +++ b/src/status_im/qr_scanner/screen.cljs @@ -21,15 +21,12 @@ :background-color toolbar-background2}]) (defview qr-scanner [] - [] + [identifier [:get-in [:qr-codes :identifier]]] [view st/barcode-scanner-container [qr-scanner-toolbar] [camera {:onBarCodeRead (fn [data] - (let [{:keys [name address whisper-identity phone-number] :as contact} (json->clj (.-data data))] - (when name (dispatch [:set-in [:new-contact :name] name])) - (when address (dispatch [:set-in [:new-contact :address] address])) - (when whisper-identity (dispatch [:set-in [:new-contact :whisper-identity] whisper-identity])) - (when phone-number (dispatch [:set-in [:new-contact :phone-number] phone-number])) + (let [data (json->clj (.-data data))] + (dispatch [:set-in [:qr-codes (or identifier :current)] data]) (dispatch [:navigate-back]))) :style st/barcode-scanner}] [view st/rectangle-container