Make songs temporary assigns

This commit is contained in:
Chris McCord 2021-12-15 21:51:09 -05:00
parent ce1a1fdc5f
commit 8da032fed3
6 changed files with 56 additions and 30 deletions

View file

@ -184,14 +184,16 @@ defmodule LiveBeats.MediaLibrary do
case LiveBeats.Repo.transaction(multi) do
{:ok, results} ->
{:ok,
songs =
results
|> Enum.filter(&match?({{:song, _ref}, _}, &1))
|> Enum.map(fn {{:song, ref}, song} ->
consume_file.(ref, fn tmp_path -> store_mp3(song, tmp_path) end)
{ref, song}
end)
|> Enum.into(%{})}
broadcast_imported(user, songs)
{:ok, Enum.into(songs, %{})}
{:error, failed_op, failed_val, _changes} ->
failed_op =
@ -205,6 +207,11 @@ defmodule LiveBeats.MediaLibrary do
end
end
defp broadcast_imported(%Accounts.User{} = user, songs) do
songs = Enum.map(songs, fn {_ref, song} -> song end)
broadcast!(user.id, %Events.SongsImported{user_id: user.id, songs: songs})
end
def parse_file_name(name) do
case Regex.split(~r/[-]/, Path.rootname(name), parts: 2) do
[title] -> %{title: String.trim(title), artist: nil}

View file

@ -10,4 +10,8 @@ defmodule LiveBeats.MediaLibrary.Events do
defmodule PublicProfileUpdated do
defstruct profile: nil
end
defmodule SongsImported do
defstruct user_id: nil, songs: []
end
end

View file

@ -557,7 +557,7 @@ defmodule LiveBeatsWeb.LiveHelpers do
<% end %>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-100">
<tbody id={@id} class="bg-white divide-y divide-gray-100" phx-update="append">
<%= for {row, i} <- Enum.with_index(@rows) do %>
<.live_component
module={@module}

View file

@ -43,6 +43,7 @@ defmodule LiveBeatsWeb.ProfileLive do
<:title let={user}><%= user.username %></:title>
</Presence.listening_now>
<div id="dialogs" phx-update="append">
<%= for song <- if(@owns_profile?, do: @songs, else: []), id = "delete-modal-#{song.id}" do %>
<.modal
id={id}
@ -53,8 +54,10 @@ defmodule LiveBeatsWeb.ProfileLive do
<:confirm>Delete</:confirm>
</.modal>
<% end %>
</div>
<.live_table
id="songs"
module={SongRowComponent}
rows={@songs}
row_id={fn song -> "song-#{song.id}" end}
@ -88,7 +91,7 @@ defmodule LiveBeatsWeb.ProfileLive do
active_song_id =
if song = MediaLibrary.get_current_active_song(profile) do
SongRowComponent.send_status(song.id, song.status)
SongRowComponent.send_status(song, song.status)
song.id
end
@ -158,12 +161,17 @@ defmodule LiveBeatsWeb.ProfileLive do
{:noreply, pause_song(socket, song.id)}
end
def handle_info({MediaLibrary, %MediaLibrary.Events.SongsImported{songs: songs}}, socket) do
{:noreply, update(socket, :songs, &(&1 ++ songs))}
end
def handle_info({MediaLibrary, _}, socket), do: {:noreply, socket}
def handle_info({Accounts, _}, socket), do: {:noreply, socket}
defp stop_song(socket, song_id) do
SongRowComponent.send_status(song_id, :stopped)
song = MediaLibrary.get_song!(song_id)
SongRowComponent.send_status(song, :stopped)
if socket.assigns.active_song_id == song_id do
assign(socket, :active_song_id, nil)
@ -173,7 +181,8 @@ defmodule LiveBeatsWeb.ProfileLive do
end
defp pause_song(socket, song_id) do
SongRowComponent.send_status(song_id, :paused)
song = MediaLibrary.get_song!(song_id)
SongRowComponent.send_status(song, :paused)
socket
end
@ -182,18 +191,18 @@ defmodule LiveBeatsWeb.ProfileLive do
cond do
active_song_id == song.id ->
SongRowComponent.send_status(song.id, :playing)
SongRowComponent.send_status(song, :playing)
socket
active_song_id ->
SongRowComponent.send_status(song.id, :playing)
SongRowComponent.send_status(song, :playing)
socket
|> stop_song(active_song_id)
|> assign(active_song_id: song.id)
true ->
SongRowComponent.send_status(song.id, :playing)
SongRowComponent.send_status(song, :playing)
assign(socket, active_song_id: song.id)
end
end

View file

@ -1,8 +1,10 @@
defmodule LiveBeatsWeb.ProfileLive.SongRowComponent do
use LiveBeatsWeb, :live_component
def send_status(id, status) when status in [:playing, :paused, :stopped] do
send_update(__MODULE__, id: "song-#{id}", action: :send, status: status)
alias LiveBeats.MediaLibrary.Song
def send_status(%Song{} = song, status) when status in [:playing, :paused, :stopped] do
send_update(__MODULE__, id: "song-#{song.id}", action: :send, song: song, status: status)
end
def render(assigns) do
@ -42,8 +44,12 @@ defmodule LiveBeatsWeb.ProfileLive.SongRowComponent do
"""
end
def update(%{action: :send, status: status}, socket) when status in [:playing, :paused, :stopped] do
{:ok, assign(socket, status: status)}
def mount(socket) do
{:ok, socket, temporary_assigns: [song: nil]}
end
def update(%{action: :send, status: status, song: song}, socket) when status in [:playing, :paused, :stopped] do
{:ok, assign(socket, status: status, song: song)}
end
def update(assigns, socket) do

View file

@ -54,7 +54,7 @@ defmodule LiveBeatsWeb.ProfileLive.UploadFormComponent do
{:noreply,
socket
|> put_flash(:info, "#{map_size(songs)} song(s) uploaded")
|> push_redirect(to: profile_path(current_user))}
|> push_patch(to: profile_path(current_user))}
{:error, {failed_op, reason}} ->
{:noreply, put_error(socket, {failed_op, reason})}