diff --git a/src/status_im/contacts/handlers.cljs b/src/status_im/contacts/handlers.cljs index 4d6237bd55..6e50de8e78 100644 --- a/src/status_im/contacts/handlers.cljs +++ b/src/status_im/contacts/handlers.cljs @@ -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)) \ No newline at end of file +(register-handler :set-new-contact-from-qr set-new-contact-from-qr) diff --git a/src/status_im/contacts/views/new_contact.cljs b/src/status_im/contacts/views/new_contact.cljs index 955e92b648..3920aeed49 100644 --- a/src/status_im/contacts/views/new_contact.cljs +++ b/src/status_im/contacts/views/new_contact.cljs @@ -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)] - ]])) \ No newline at end of file + [text (str "Whisper identity: " whisper-identity)]]]) diff --git a/src/status_im/db.cljs b/src/status_im/db.cljs index c8ebde0d3b..283a09a1a9 100644 --- a/src/status_im/db.cljs +++ b/src/status_im/db.cljs @@ -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 "" diff --git a/src/status_im/qr_scanner/handlers.cljs b/src/status_im/qr_scanner/handlers.cljs index b8bc1bb300..27ae0b5eca 100644 --- a/src/status_im/qr_scanner/handlers.cljs +++ b/src/status_im/qr_scanner/handlers.cljs @@ -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]))))) diff --git a/src/status_im/qr_scanner/screen.cljs b/src/status_im/qr_scanner/screen.cljs index f0846830ca..430afc3582 100644 --- a/src/status_im/qr_scanner/screen.cljs +++ b/src/status_im/qr_scanner/screen.cljs @@ -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}]]] - ]) \ No newline at end of file + :style st/corner-left-bottom}]]]]) diff --git a/src/status_im/utils/types.cljs b/src/status_im/utils/types.cljs index 6ce9fdfce8..93c942df77 100644 --- a/src/status_im/utils/types.cljs +++ b/src/status_im/utils/types.cljs @@ -12,4 +12,4 @@ (.stringify js/JSON (clj->js data))) (defn json->clj [data] - (js->clj (.parse js/JSON data))) \ No newline at end of file + (js->clj (.parse js/JSON data) :keywordize-keys true))