From 115356df2d37aea8c5e5469b4f8eeb19fe490b36 Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Sat, 28 Jan 2023 14:18:34 -0500 Subject: [PATCH] WIP --- lib/live_beats/media_library.ex | 12 ++++----- lib/live_beats_web/live/profile_live.ex | 34 ++++++++++++++++++------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/lib/live_beats/media_library.ex b/lib/live_beats/media_library.ex index 59e62c5..adc99b9 100644 --- a/lib/live_beats/media_library.ex +++ b/lib/live_beats/media_library.ex @@ -360,27 +360,27 @@ defmodule LiveBeats.MediaLibrary do multi = Ecto.Multi.new() |> lock_playlist(song.user_id) - |> Ecto.Multi.run(:valid_index, fn repo, _changes -> + |> Ecto.Multi.run(:index, fn repo, _changes -> case repo.one(from s in Song, where: s.user_id == ^song.user_id, select: count(s.id)) do - count when new_index < count -> {:ok, count} - _count -> {:error, :index_out_of_range} + count when new_index < count -> {:ok, new_index} + count -> {:ok, count - 1} end end) - |> multi_update_all(:dec_positions, fn _ -> + |> multi_update_all(:dec_positions, fn %{index: new_index} -> from(s in Song, where: s.user_id == ^song.user_id and s.id != ^song.id, where: s.position > ^old_index and s.position <= ^new_index, update: [inc: [position: -1]] ) end) - |> multi_update_all(:inc_positions, fn _ -> + |> multi_update_all(:inc_positions, fn %{index: new_index} -> from(s in Song, where: s.user_id == ^song.user_id and s.id != ^song.id, where: s.position < ^old_index and s.position >= ^new_index, update: [inc: [position: 1]] ) end) - |> multi_update_all(:position, fn _ -> + |> multi_update_all(:position, fn %{index: new_index} -> from(s in Song, where: s.id == ^song.id, update: [set: [position: ^new_index]] diff --git a/lib/live_beats_web/live/profile_live.ex b/lib/live_beats_web/live/profile_live.ex index 2d01151..00ddea0 100644 --- a/lib/live_beats_web/live/profile_live.ex +++ b/lib/live_beats_web/live/profile_live.ex @@ -16,6 +16,7 @@ defmodule LiveBeatsWeb.ProfileLive do <%= if @owns_profile? do %> (you) <% end %> + <%= @songs_count %> <.link href={@profile.external_homepage_url} target="_blank" class="text-sm text-gray-600"> <.icon name={:code} /> <%= url_text(@profile.external_homepage_url) %> @@ -59,8 +60,8 @@ defmodule LiveBeatsWeb.ProfileLive do total_count={@presences_count} /> -
- <%= for {_id, song} <- if(@owns_profile?, do: @songs, else: []), id = "delete-modal-#{song.id}" do %> +
+ <%= for {_id, song} <- if(@owns_profile?, do: @streams.songs, else: []), id = "delete-modal-#{song.id}" do %> <.modal id={id} on_confirm={ @@ -80,14 +81,18 @@ defmodule LiveBeatsWeb.ProfileLive do <.table id="songs" - rows={@songs} + rows={@streams.songs} row_id={fn {id, _song} -> id end} row_click={fn {_id, song} -> JS.push("play_or_pause", value: %{id: song.id}) end} row_remove={fn {id, _song} -> hide("##{id}") end} streamable sortable_drop="row_dropped" > - <:col :let={{_id, song}} label="Title" class!="px-6 py-3 text-sm font-medium text-gray-900 min-w-[20rem] cursor-pointer"> + <:col + :let={{_id, song}} + label="Title" + class!="px-6 py-3 text-sm font-medium text-gray-900 md:min-w-[20rem] cursor-pointer" + > <.icon name={:volume_up} class="h-5 w-5 -mt-1 -ml-1" aria-label="Playing" role="button" /> @@ -117,7 +122,7 @@ defmodule LiveBeatsWeb.ProfileLive do label="Attribution" class="max-w-5xl break-words text-gray-600 font-light" > - <%= song.attribution %> + <%= song.position %> <:col :let={{_id, song}} label="Duration"><%= MP3Stat.to_mmss(song.duration) %> <:col :let={{_id, song}} :if={@owns_profile?} label=""> @@ -151,15 +156,18 @@ defmodule LiveBeatsWeb.ProfileLive do song.id end + songs = MediaLibrary.list_profile_songs(profile, 50) + socket = socket |> assign( active_song_id: active_song_id, active_profile_id: current_user.active_profile_user_id, profile: profile, - owns_profile?: MediaLibrary.owns_profile?(current_user, profile) + owns_profile?: MediaLibrary.owns_profile?(current_user, profile), + songs_count: Enum.count(songs) ) - |> stream(:songs, MediaLibrary.list_profile_songs(profile, 50)) + |> stream(:songs, songs) |> assign_presences() {:ok, socket, temporary_assigns: [presences: %{}]} @@ -248,11 +256,19 @@ defmodule LiveBeatsWeb.ProfileLive do end def handle_info({MediaLibrary, %MediaLibrary.Events.SongsImported{songs: songs}}, socket) do - {:noreply, Enum.reduce(songs, socket, fn song, acc -> stream_insert(acc, :songs, song) end)} + {:noreply, + Enum.reduce(songs, socket, fn song, acc -> + acc + |> update(:songs_count, &(&1 + 1)) + |> stream_insert(:songs, song) + end)} end def handle_info({MediaLibrary, %MediaLibrary.Events.SongDeleted{song: song}}, socket) do - {:noreply, stream_delete(socket, :songs, song)} + {:noreply, + socket + |> update(:songs_count, &(&1 - 1)) + |> stream_delete(:songs, song)} end def handle_info({MediaLibrary, {:ping, ping}}, socket) do