Skip to content

Commit 9294492

Browse files
some refactor on audio loading
1 parent b2b740b commit 9294492

File tree

1 file changed

+43
-45
lines changed

1 file changed

+43
-45
lines changed

src/status_im/ui/screens/chat/message/audio.cljs

+43-45
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
[status-im.utils.utils :as utils]
1414
[taoensso.timbre :as log]
1515
[quo2.foundations.colors :as colors]
16-
[quo2.core :as quo]))
16+
[quo2.core :as quo]
17+
[react-native.core :as rn]))
1718

1819
(defonce player-ref (atom nil))
1920
(defonce current-player-message-id (atom nil))
@@ -101,10 +102,11 @@
101102

102103
(defn download-audio-http
103104
[base64-uri on-success]
105+
(println base64-uri)
104106
(-> (.config ReactNativeBlobUtil (clj->js {:trusty platform/ios?}))
105107
(.fetch "GET" (str base64-uri))
106108
(.then #(on-success (.base64 ^js %)))
107-
(.catch #(log/error "could not fetch audio"))))
109+
(.catch #(println "could not fetch audio" %))))
108110

109111
(defn reload-player
110112
[{:keys [message-id state-ref] :as params} audio-url on-success]
@@ -154,10 +156,8 @@
154156
(when @player-ref
155157
(audio/toggle-playpause-player
156158
@player-ref
157-
#(do
158-
(update-state params))
159-
#(do
160-
(update-state params))
159+
#(update-state params)
160+
#(update-state params)
161161
#(update-state (merge params {:error (:message %)}))))))
162162

163163
(defn- play-pause-button
@@ -174,43 +174,41 @@
174174
:accessibility-label :play-pause-audio-message-button
175175
:color colors/white}]])
176176

177-
(defview message-content
177+
(defn message-content
178178
[{:keys [audio audio-duration-ms message-id]}]
179-
(letsubs [state (reagent/atom nil)
180-
progress (reagent/atom 0)
181-
seeking-audio? (reagent/atom false)]
182-
{:component-did-mount (fn []
183-
(update-state {:state-ref state
184-
:audio-duration-ms audio-duration-ms
185-
:message-id message-id
186-
:unloaded? true
187-
:progress-ref progress}))
188-
:component-will-unmount (fn []
189-
(destroy-player {:state-ref state :message-id message-id})
190-
(when (= @current-player-message-id message-id)
191-
(reset! current-active-state-ref-ref nil)
192-
(reset! current-player-message-id nil))
193-
(reset! state nil))}
194-
195-
(let [base-params {:state-ref state
196-
:message-id message-id
197-
:progress-ref progress}]
198-
(if (= (:general @state) :error)
199-
[react/text
200-
{:style {:typography :main-medium
201-
:margin-bottom 16}} (:error-msg @state)]
202-
[react/view {:style (style/container)}
203-
[play-pause-button state #(play-pause base-params audio seeking-audio?)]
204-
[quo/soundtrack
205-
{:style style/slider-container
206-
:audio-current-time-ms progress
207-
:player-ref player-ref
208-
:seeking-audio? seeking-audio?}]
209-
[quo/text
210-
{:style style/timestamp
211-
:accessibility-label :audio-duration-label
212-
:weight :medium
213-
:size :paragraph-2}
214-
(let [duration-ms (:duration @state)
215-
duration-secs (quot duration-ms 1000)]
216-
(gstring/format "%02d:%02d" (quot duration-secs 60) (mod duration-secs 60)))]]))))
179+
[:f>
180+
(fn []
181+
(let [state (reagent/atom nil)
182+
progress (reagent/atom 0)
183+
seeking-audio? (reagent/atom false)
184+
base-params {:state-ref state
185+
:message-id message-id
186+
:progress-ref progress}
187+
time-ms (if (#{:playing :paused :seeking} (:general @state))
188+
@progress
189+
(:duration @state))
190+
time-secs (quot time-ms 1000)]
191+
(rn/use-effect
192+
(fn []
193+
(update-state {:state-ref state
194+
:audio-duration-ms audio-duration-ms
195+
:message-id message-id
196+
:unloaded? true
197+
:progress-ref progress})))
198+
(if (= (:general @state) :error)
199+
[react/text
200+
{:style {:typography :main-medium
201+
:margin-bottom 16}} (:error-msg @state)]
202+
[react/view {:style (style/container)}
203+
[play-pause-button state #(play-pause base-params audio seeking-audio?)]
204+
[quo/soundtrack
205+
{:style style/slider-container
206+
:audio-current-time-ms progress
207+
:player-ref player-ref
208+
:seeking-audio? seeking-audio?}]
209+
[quo/text
210+
{:style style/timestamp
211+
:accessibility-label :audio-duration-label
212+
:weight :medium
213+
:size :paragraph-2}
214+
(gstring/format "%02d:%02d" (quot time-secs 60) (mod time-secs 60))]])))])

0 commit comments

Comments
 (0)