diff --git a/src/react_native/core.cljs b/src/react_native/core.cljs index c9e41a74272..7d89a8ac33c 100644 --- a/src/react_native/core.cljs +++ b/src/react_native/core.cljs @@ -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] @@ -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))) diff --git a/src/react_native/image_resizer.cljs b/src/react_native/image_resizer.cljs index 87cbeb08501..4c4a96ffe25 100644 --- a/src/react_native/image_resizer.cljs +++ b/src/react_native/image_resizer.cljs @@ -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)) diff --git a/src/status_im/contexts/chat/messenger/photo_selector/effects.cljs b/src/status_im/contexts/chat/messenger/photo_selector/effects.cljs index 35e4032a769..2c5d2736deb 100644 --- a/src/status_im/contexts/chat/messenger/photo_selector/effects.cljs +++ b/src/status_im/contexts/chat/messenger/photo_selector/effects.cljs @@ -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] @@ -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]