updated qr flow

Former-commit-id: db46a3a3cb
This commit is contained in:
Roman Volosovskyi 2016-06-02 17:39:26 +03:00
parent 18e6b80e18
commit 1d3a91c0ee
6 changed files with 68 additions and 50 deletions

View File

@ -112,12 +112,9 @@
(after save-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))))
(defn set-new-contact-from-qr
[{:keys [new-contact] :as db} [_ _ qr-contact]]
(println "WWUUUUUUTTT" qr-contact)
(assoc db :new-contact (merge new-contact qr-contact)))
(register-handler :set-new-contact-from-qr
(-> set-new-contact-from-qr))
(register-handler :set-new-contact-from-qr set-new-contact-from-qr)

View File

@ -27,47 +27,43 @@
[status-im.utils.logging :as log]))
(defn import-qr-button []
[touchable-highlight {:on-press #(dispatch [:scan-qr-code :new-contact])}
[touchable-highlight
{:on-press #(dispatch [:scan-qr-code :new-contact :set-new-contact-from-qr])}
[view st/import-qr-button
[view st/import-qr-button-content
[icon {:name :qr-scanner
[icon {:name :qr-scanner
:style import-qr-icon}]
[text {:style st/import-qr-text} (label :t/import-qr)]]]])
(defview contact-name-input [name]
[]
[text-input
{:underlineColorAndroid color-purple
:style form-text-input
:autoFocus true
:placeholder (label :t/contact-name)
:onChangeText #(dispatch [:set-in [:new-contact :name] %])}
name])
[]
[text-input
{:underlineColorAndroid color-purple
:style form-text-input
:autoFocus true
:placeholder (label :t/contact-name)
:onChangeText #(dispatch [:set-in [:new-contact :name] %])}
name])
(defview contact-address-input [address]
[]
[text-input
{:underlineColorAndroid color-purple
:style form-text-input
:autoFocus true
:placeholder (label :t/contact-address)
:onChangeText #(dispatch [:set-in [:new-contact :address] %])}
address])
[text-input
{:underlineColorAndroid color-purple
:style form-text-input
:autoFocus true
:placeholder (label :t/contact-address)
:onChangeText #(dispatch [:set-in [:new-contact :address] %])}
address])
(defview 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]))]
[{:keys [name address whisper-identity phone-number] :as new-contact} [:get :new-contact]]
[drawer-view
[view st/contact-form-container
[toolbar {:title (label :t/new-contact)
:background-color toolbar-background2
:action {:image {:source {:uri :icon_add_gray}
:style search-icon}
:handler (fn [] (dispatch [:add-new-contact new-contact]))}}]
:handler #(dispatch [:add-new-contact new-contact])}}]
[import-qr-button]
[contact-name-input name]
[contact-address-input address]
[text (str "Whisper identity: " whisper-identity)]
]]))
[text (str "Whisper identity: " whisper-identity)]]])

View File

@ -29,7 +29,7 @@
:email "myemail@gmail.com"
:status "Hi, this is my status"
:current-tag nil
:qr-codes {:identifier nil}
:qr-codes {}
:new-contact {:name ""
:address ""
:whisper-identity ""

View File

@ -1,12 +1,39 @@
(ns status-im.qr-scanner.handlers
(:require [re-frame.core :refer [register-handler after dispatch debug enrich]]))
(:require [re-frame.core :refer [register-handler after dispatch debug enrich]]
[status-im.navigation.handlers :as nav]
[status-im.utils.handlers :as u]))
(defn set-current-identifier [db [_ identifier]]
(assoc-in db [:qr-codes :identifier] identifier))
(defmethod nav/preload-data! :qr-scanner
[db [_ _ identifier]]
(assoc db :current-qr-context identifier))
(defn set-current-identifier [db [_ identifier handler]]
(assoc-in db [:qr-codes identifier] handler))
(defn navigate-to-scanner
[_ [_ identifier]]
(dispatch [:navigate-to :qr-scanner identifier]))
(register-handler :scan-qr-code
(-> set-current-identifier
((after (fn [_ _] (dispatch [:navigate-to :qr-scanner]))))))
(after navigate-to-scanner)
set-current-identifier)
(register-handler :clear-qr-code [db [_ identifier]]
(update-in db [:qr-codes] dissoc identifier))
(register-handler :clear-qr-code
(fn [db [_ identifier]]
(update db :qr-codes dissoc identifier)))
(defn handle-qr-request
[db [_ context data]]
(let [handler (get-in db [:qr-codes context])]
(println handler context data)
(dispatch [handler context data])))
(defn clear-qr-request [db [_ context]]
(-> db
(update :qr-codes dissoc context)
(dissoc :current-qr-context)))
(register-handler :set-qr-code
(-> (u/side-effect! handle-qr-request)
((enrich clear-qr-request))
((after #(dispatch [:navigate-back])))))

View File

@ -21,14 +21,13 @@
:background-color toolbar-background2}])
(defview qr-scanner []
[identifier [:get-in [:qr-codes :identifier]]]
[identifier [:get :current-qr-context]]
[view st/barcode-scanner-container
[qr-scanner-toolbar]
[camera {:onBarCodeRead (fn [data]
(let [data (json->clj (.-data data))]
(dispatch [:set-in [:qr-codes (or identifier :current)] data])
(dispatch [:navigate-back])))
:style st/barcode-scanner}]
[camera {;:on-bar-code-read #(js/alert "ok")
:onBarCodeRead #(let [data (json->clj (.-data %))]
(dispatch [:set-qr-code identifier data]))
:style st/barcode-scanner}]
[view st/rectangle-container
[view st/rectangle
[image {:source {:uri :corner_left_top}
@ -38,5 +37,4 @@
[image {:source {:uri :corner_right_bottom}
:style st/corner-right-bottom}]
[image {:source {:uri :corner_left_bottom}
:style st/corner-left-bottom}]]]
])
:style st/corner-left-bottom}]]]])

View File

@ -12,4 +12,4 @@
(.stringify js/JSON (clj->js data)))
(defn json->clj [data]
(js->clj (.parse js/JSON data)))
(js->clj (.parse js/JSON data) :keywordize-keys true))