From 5bd2e1df84dbe07a2cf216d81e9f410e3fef5e3f Mon Sep 17 00:00:00 2001 From: Miguel Ángel Moreno Date: Mon, 22 Jan 2024 01:16:14 +0100 Subject: feat(frontend): dispatch more effects on audio load --- deps.edn | 3 ++- src/frontend/tubo/events.cljs | 28 +++++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/deps.edn b/deps.edn index 6ae47c4..6ec2094 100644 --- a/deps.edn +++ b/deps.edn @@ -24,6 +24,7 @@ re-frame/re-frame {:mvn/version "1.3.0"} day8.re-frame/http-fx {:mvn/version "0.2.4"} cljs-ajax/cljs-ajax {:mvn/version "0.8.4"} - akiroz.re-frame/storage {:mvn/version "0.1.4"}} + akiroz.re-frame/storage {:mvn/version "0.1.4"} + re-frame-utils/re-frame-utils {:mvn/version "0.1.0"}} :main-opts ["-m" "shadow.cljs.devtools.cli"]} :run {:main-opts ["-m" "tubo.core"]}}} diff --git a/src/frontend/tubo/events.cljs b/src/frontend/tubo/events.cljs index 309aa93..8f72bb2 100644 --- a/src/frontend/tubo/events.cljs +++ b/src/frontend/tubo/events.cljs @@ -6,7 +6,8 @@ [re-frame.core :as rf] [reitit.frontend.easy :as rfe] [reitit.frontend.controllers :as rfc] - [tubo.api :as api])) + [tubo.api :as api] + [vimsical.re-frame.cofx.inject :as inject])) (reg-co-fx! :tubo {:fx :store :cofx :store}) @@ -324,7 +325,7 @@ :store (-> store (assoc :show-audio-player true) (assoc :media-queue (:media-queue updated-db))) - :fx [[:dispatch [::fetch-audio-player-stream (:url stream) idx]]]}))) + :fx [[:dispatch [::fetch-audio-player-stream (:url stream) idx (= (count (:media-queue db)) 0)]]]}))) (rf/reg-event-fx ::enqueue-related-streams @@ -338,7 +339,7 @@ (conj {:service-color service-color} %)]]) streams) [:dispatch [::fetch-audio-player-stream (:url (first streams)) - (count (:media-queue db))]]))})) + (count (:media-queue db)) (= (count (:media-queue db)) 0)]]))})) (rf/reg-event-fx ::add-to-bookmarks @@ -499,12 +500,21 @@ (rf/reg-event-fx ::load-audio-player-stream - (fn [{:keys [db]} [_ idx res]] + [(rf/inject-cofx ::inject/sub [:player])] + (fn [{:keys [db player]} [_ idx play? res]] (let [stream-res (js->clj res :keywordize-keys true)] {:db (assoc db :show-audio-player-loading false) - :fx [[:dispatch [::change-media-queue-stream - (-> stream-res :audio-streams first :content) - idx]]]}))) + :fx (apply conj [[:dispatch [::change-media-queue-stream + (-> stream-res :audio-streams first :content) + idx]]] + (when play? + [[::stream-metadata {:title (:name stream-res) + :artist (:uploader-name stream-res) + :artwork [{:src (:thumbnail-url stream-res)}]}] + [::media-session {:current-pos (:media-queue-pos db) :player player}] + [::player-playback {:player player + :stream (-> stream-res :audio-streams first :content) + :current-pos (:media-queue-pos db)}]]))}))) (rf/reg-event-fx ::load-stream-page @@ -525,10 +535,10 @@ (rf/reg-event-fx ::fetch-audio-player-stream - (fn [{:keys [db]} [_ uri idx]] + (fn [{:keys [db]} [_ uri idx play?]] (assoc (api/get-request (str "/api/streams/" (js/encodeURIComponent uri)) - [::load-audio-player-stream idx] [::bad-response]) + [::load-audio-player-stream idx play?] [::bad-response]) :db (assoc db :show-audio-player-loading true)))) (rf/reg-event-fx -- cgit v1.2.3