Skip to content

Commit

Permalink
fixes #6596
Browse files Browse the repository at this point in the history
  • Loading branch information
tbenr committed Jan 23, 2019
1 parent fab20ff commit d55fb38
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 6 deletions.
54 changes: 54 additions & 0 deletions src/status_im/extensions/camera.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
(ns status-im.extensions.camera
(:require [re-frame.core :as re-frame]
[reagent.core :as reagent]
[status-im.utils.fx :as fx]
[status-im.qr-scanner.core :as qr-scanner]
[status-im.ui.screens.navigation :as navigation]
[status-im.utils.handlers :as handlers]))

;; Photo taker

(handlers/register-handler-fx
:extensions/picture-taken
(fn [cofx [_ _ base64 {{:keys [on-success]} :data}]]
(fx/merge cofx
{:dispatch (on-success {:result base64})}
(navigation/navigate-back))))

(handlers/register-handler-fx
:extensions/camera-picture
(fn [{db :db} [_ _ {:keys [on-success on-failure]}]]
(re-frame/dispatch [:request-permissions {:permissions [:camera :write-external-storage]
:on-allowed #(re-frame/dispatch [:navigate-to :take-picture {:test-param "test value"}])
:on-denied #(print "denied!")}])))

;; QR code scanner

(handlers/register-handler-fx
:extensions/qr-code-scanned
(fn [cofx [_ _ qr-code {{:keys [on-success]} :data}]]
(fx/merge cofx
{:dispatch (on-success {:result qr-code})}
(navigation/navigate-back))))

(handlers/register-handler-fx
:extensions/qr-code-cancel
(fn [_ [_ _ {{:keys [on-failure]} :data}]]
(when on-failure
(re-frame/dispatch (on-failure {:result "user cancelled"})))))

(handlers/register-handler-fx
:extensions/qr-code-denied
(fn [_ [_ _ {{:keys [on-failure]} :data}]]
(when on-failure
(re-frame/dispatch (on-failure {:result "user denied access to camera"})))))

(handlers/register-handler-fx
:extensions/camera-qr-code
(fn [{:keys [db] :as cofx} [_ _ {:keys [on-success on-failure]}]]
(qr-scanner/scan-qr-code cofx {:modal? false
:deny-handler :extensions/qr-code-denied}
{:handler :extensions/qr-code-scanned
:cancel-handler :extensions/qr-code-cancel
:data {:on-success on-success
:on-failure on-failure}})))
11 changes: 11 additions & 0 deletions src/status_im/extensions/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
[status-im.utils.handlers :as handlers]
[status-im.utils.fx :as fx]
status-im.extensions.ethereum
status-im.extensions.camera
[status-im.utils.ethereum.tokens :as tokens]
[status-im.utils.ethereum.core :as ethereum]
[status-im.chat.commands.sending :as commands-sending]))
Expand Down Expand Up @@ -459,6 +460,16 @@
:arguments {:values :vector
:operation {:one-of #{:plus :minus :times :divide}}
:on-result :event}}
'camera/picture
{:permissions [:read]
:value :extensions/camera-picture
:arguments {:on-success :event
:on-failure? :event}}
'camera/qr-code
{:permissions [:read]
:value :extensions/camera-qr-code
:arguments {:on-success :event
:on-failure? :event}}
'schedule/start
{:permissions [:read]
:value :extensions/schedule-start
Expand Down
50 changes: 50 additions & 0 deletions src/status_im/extensions/views.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
(ns status-im.extensions.views
(:require-macros [status-im.utils.views :refer [defview letsubs]])
(:require [re-frame.core :as re-frame]
[reagent.core :as reagent]
[status-im.utils.handlers :as handlers]
[status-im.ui.screens.navigation :as navigation]
[status-im.ui.components.camera :as camera]
[status-im.ui.components.react :as react]
[status-im.ui.components.status-bar.view :as status-bar]
[status-im.ui.components.toolbar.view :as toolbar]
[status-im.i18n :as i18n]
[taoensso.timbre :as log]
[status-im.ui.screens.profile.photo-capture.styles :as styles]
[status-im.utils.image-processing :as image-processing]
[status-im.ui.components.icons.vector-icons :as icons]))

(defn image-captured [data param]
(let [path (.-path data)
_ (log/debug "Captured image: " path)
on-success (fn [base64]
(print "Captured success: " base64 "param: " param)
(re-frame/dispatch [:extensions/picture-taken base64])
(re-frame/dispatch [:navigate-back]))
on-error (fn [type error]
(log/debug type error))]
(image-processing/img->base64 path on-success on-error)))

(defview take-picture []
(letsubs [{param :test-param} [:get-screen-params]
camera-ref (reagent/atom false)]
[react/view styles/container
[status-bar/status-bar]
[toolbar/toolbar {}
toolbar/default-nav-back
[toolbar/content-title (i18n/label :t/image-source-title)]]
[camera/camera {:style {:flex 1}
:aspect (:fill camera/aspects)
:captureQuality "480p"
:captureTarget (:disk camera/capture-targets)
:type "front"
:ref #(reset! camera-ref %)}]
[react/view styles/button-container
[react/view styles/button
[react/touchable-highlight {:on-press (fn []
(let [camera @camera-ref]
(-> (.capture camera)
(.then image-captured param)
(.catch #(print "ERROR! " %)))))}
[react/view
[icons/icon :icons/camera {:color :white}]]]]]]))
14 changes: 8 additions & 6 deletions src/status_im/qr_scanner/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
[status-im.utils.fx :as fx]))

(fx/defn scan-qr-code
[{:keys [db]} {:keys [modal?] :as identifier} qr-codes]
[{:keys [db]} {:keys [modal? deny-handler] :as identifier} qr-codes]
{:db (assoc db :qr-codes qr-codes)
:request-permissions-fx {:permissions [:camera]
:on-allowed #(re-frame/dispatch [(if modal? :navigate-to-modal :navigate-to)
:qr-scanner {:current-qr-context identifier}])
:on-denied (fn []
(utils/set-timeout
#(utils/show-popup (i18n/label :t/error)
(i18n/label :t/camera-access-error))
50))}})
:on-denied (if (nil? deny-handler)
(fn []
(utils/set-timeout
#(utils/show-popup (i18n/label :t/error)
(i18n/label :t/camera-access-error))
50))
#(re-frame/dispatch [deny-handler qr-codes]))}})

(fx/defn set-qr-code
[{:keys [db]} context data]
Expand Down
2 changes: 2 additions & 0 deletions src/status_im/ui/screens/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
[status-im.ui.screens.profile.contact.views :as profile.contact]
[status-im.ui.screens.profile.group-chat.views :as profile.group-chat]
[status-im.ui.screens.profile.photo-capture.views :refer [profile-photo-capture]]
[status-im.extensions.views :refer [take-picture]]
[status-im.ui.screens.wallet.main.views :as wallet.main]
[status-im.ui.screens.wallet.collectibles.views :refer [collectibles-list]]
[status-im.ui.screens.wallet.send.views :refer [send-transaction send-transaction-modal sign-message-modal]]
Expand Down Expand Up @@ -158,6 +159,7 @@
:new add-new
:new-chat new-chat
:qr-scanner qr-scanner
:take-picture take-picture
:new-group new-group
:add-participants-toggle-list add-participants-toggle-list
:contact-toggle-list contact-toggle-list
Expand Down

0 comments on commit d55fb38

Please sign in to comment.