-
Notifications
You must be signed in to change notification settings - Fork 987
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
174 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
(ns status-im.extensions.camera | ||
(:require [re-frame.core :as re-frame] | ||
[reagent.core :as reagent] | ||
[status-im.utils.fx :as fx] | ||
[status-im.ui.components.list-selection :as list-selection] | ||
[status-im.qr-scanner.core :as qr-scanner] | ||
[status-im.ui.screens.navigation :as navigation] | ||
[status-im.utils.handlers :as handlers])) | ||
|
||
;; Common | ||
|
||
(handlers/register-handler-fx | ||
:extensions/camera-cancel | ||
(fn [_ [_ _ {{:keys [on-failure]} :data}]] | ||
(when on-failure | ||
(re-frame/dispatch (on-failure {:result "user cancelled"}))))) | ||
|
||
(handlers/register-handler-fx | ||
:extensions/camera-denied | ||
(fn [_ [_ {{:keys [on-failure]} :data}]] | ||
(when on-failure | ||
(re-frame/dispatch (on-failure {:result "user denied access to camera"}))))) | ||
|
||
;; Photo taker\picker | ||
|
||
(handlers/register-handler-fx | ||
:extensions/camera-error | ||
(fn [cofx [_ error {:keys [on-failure]}]] | ||
(fx/merge cofx | ||
{:dispatch (on-failure {:result error})} | ||
(navigation/navigate-back)))) | ||
|
||
(handlers/register-handler-fx | ||
:extensions/camera-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} [_ _ params]] | ||
(list-selection/show (status-im.extensions.views/pick-or-take-picture-list-selection {:data params})))) | ||
|
||
;; QR code scanner | ||
|
||
(handlers/register-handler-fx | ||
:extensions/camera-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/camera-qr-code | ||
(fn [{:keys [db] :as cofx} [_ _ {:keys [on-success on-failure]}]] | ||
(qr-scanner/scan-qr-code cofx {:modal? false | ||
:deny-handler :extensions/camera-denied} | ||
{:handler :extensions/camera-qr-code-scanned | ||
:cancel-handler :extensions/camera-cancel | ||
:data {:on-success on-success | ||
:on-failure on-failure}}))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
(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.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.ui.components.toolbar.actions :as actions] | ||
[status-im.ui.components.icons.vector-icons :as icons] | ||
[status-im.i18n :as i18n] | ||
[status-im.ui.screens.profile.photo-capture.styles :as styles] | ||
[status-im.utils.image-processing :as image-processing])) | ||
|
||
;; ensure photo taken or picked are not too big | ||
(def max-width 1024) | ||
(def max-height 1024) | ||
|
||
(defn- process-image-and-finish [path context] | ||
(let [on-success (fn [base64] | ||
(re-frame/dispatch [:extensions/camera-picture-taken base64 context])) | ||
on-error (fn [type error] | ||
(re-frame/dispatch [:extensions/camera-error error context]))] | ||
(image-processing/img->base64 path on-success on-error max-width max-height))) | ||
|
||
(defview take-picture [] | ||
(letsubs [context [:get-screen-params] | ||
camera-ref (reagent/atom false)] | ||
[react/view styles/container | ||
[status-bar/status-bar] | ||
[toolbar/toolbar nil | ||
[toolbar/nav-button (actions/back | ||
#(do | ||
(re-frame/dispatch [:extensions/camera-cancel context]) | ||
(re-frame/dispatch [:navigate-back])))] | ||
[toolbar/content-title (i18n/label :t/extensions-camera-send-picture)]] | ||
[camera/camera {:style {:flex 1} | ||
:aspect (:fill camera/aspects) | ||
:captureTarget (:disk camera/capture-targets) | ||
: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 #(process-image-and-finish (.-path %) context)) | ||
(.catch #(re-frame/dispatch [:extensions/camera-error % context])))))} | ||
[react/view | ||
[icons/icon :icons/camera {:color :white}]]]]]])) | ||
|
||
(defn- open-image-picker [context] | ||
(react/show-image-picker | ||
(fn [image] | ||
(let [path (get (js->clj image) "path")] | ||
(process-image-and-finish path context))))) | ||
|
||
(defn pick-or-take-picture-list-selection [context] | ||
{:title (i18n/label :t/extensions-camera-send-picture) | ||
:options [{:label (i18n/label :t/image-source-gallery) | ||
:action #(open-image-picker context)} | ||
{:label (i18n/label :t/image-source-make-photo) | ||
:action (fn [] | ||
(re-frame/dispatch [:request-permissions {:permissions [:camera :write-external-storage] | ||
:on-allowed #(re-frame/dispatch [:navigate-to :take-picture context]) | ||
:on-denied #(re-frame/dispatch [:extensions/camera-denied context])}]))}] | ||
:on-cancel #(re-frame/dispatch [:extensions/camera-cancel context])}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters