Skip to content

Commit

Permalink
ref(resize): using promesa instead of passing cb
Browse files Browse the repository at this point in the history
  • Loading branch information
clauxx committed Feb 8, 2024
1 parent 7a120de commit 5223017
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
9 changes: 8 additions & 1 deletion src/react_native/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
["react-native" :as react-native]
[cljs-bean.core :as bean]
[oops.core :as oops]
[promesa.core :as p]
[react-native.flat-list :as flat-list]
[react-native.platform :as platform]
[react-native.section-list :as section-list]
Expand All @@ -30,7 +31,13 @@
(assoc props :source {:uri source})
props)])

(defn image-get-size [uri callback] (.getSize ^js (.-Image ^js react-native) uri callback))
(defn image-get-size
[uri]
(p/create (fn [res rej]
(.getSize ^js (.-Image ^js react-native)
uri
(fn [width height] (res [width height]))
rej))))
(def text (reagent/adapt-react-class (.-Text ^js react-native)))
(def text-input (reagent/adapt-react-class (.-TextInput ^js react-native)))

Expand Down
6 changes: 2 additions & 4 deletions src/react_native/image_resizer.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@
["react-native-image-resizer" :default image-resizer]))

(defn resize
[path max-width max-height quality on-resize on-error]
(-> (.createResizedImage image-resizer path max-width max-height "JPEG" quality 0 nil false)
(.then on-resize)
(.catch on-error)))
[{:keys [path max-width max-height quality]}]
(.createResizedImage image-resizer path max-width max-height "JPEG" quality 0 nil false))
40 changes: 22 additions & 18 deletions src/status_im/contexts/chat/messenger/photo_selector/effects.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns status-im.contexts.chat.messenger.photo-selector.effects
(:require
[clojure.string :as string]
[promesa.core :as p]
[react-native.cameraroll :as cameraroll]
[react-native.core :as rn]
[react-native.image-resizer :as image-resizer]
Expand Down Expand Up @@ -29,27 +30,30 @@
#(rf/dispatch [:on-camera-roll-get-photos (:edges %) (:page_info %) end-cursor])))})))

(defn- resize-photo
[uri callback]
(rn/image-get-size
uri
(fn [width height]
(let [resize? (> (max width height) maximum-image-size-px)]
(image-resizer/resize
uri
(if resize? maximum-image-size-px width)
(if resize? maximum-image-size-px height)
60
(fn [^js resized-image]
(let [path (.-path resized-image)
path (if (string/starts-with? path "file") path (str "file://" path))]
(callback {:resized-uri path
:width width
:height height})))
#(log/error "could not resize image" %))))))
[uri]
(p/let [[width height] (rn/image-get-size uri)
resize? (> (max width height)
maximum-image-size-px)
resized-uri (-> (image-resizer/resize
{:max-width (if resize? maximum-image-size-px width)
:max-height (if resize? maximum-image-size-px height)
:path uri
:quality 60})
(p/then (fn [^js resized-image]
(let [path (.-path resized-image)]
(if (string/starts-with? path "file")
path
(str "file://" path))))))]
{:resized-uri resized-uri
:width width
:height height}))

(rf/reg-fx :effects.camera-roll/image-selected
(fn [[image chat-id]]
(resize-photo (:uri image) #(rf/dispatch [:photo-selector/image-selected chat-id image %]))))
(-> (resize-photo (:uri image))
(p/then #(rf/dispatch
[:photo-selector/image-selected chat-id image %]))
(p/catch #(log/error "could not resize image" %)))))

(defn- get-albums
[callback]
Expand Down

0 comments on commit 5223017

Please sign in to comment.