make qr-scanner callable multiple times from different screens

This commit is contained in:
Adrian Tiberius 2016-06-02 13:43:26 +03:00
parent 13c506491f
commit 6eeefb2efb
7 changed files with 38 additions and 14 deletions

View File

@ -110,4 +110,14 @@
(register-handler :add-new-contact
(after save-contact)
add-new-contact)
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))

View File

@ -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)]
]])
]]))

View File

@ -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 ""

View File

@ -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 ------------------------------------------------------------

View File

@ -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]

View File

@ -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))

View File

@ -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