live_beats/lib/live_beats_web/live/song_live/index.ex

141 lines
4.1 KiB
Elixir
Raw Normal View History

2021-10-29 16:12:23 +00:00
defmodule LiveBeatsWeb.SongLive.Index do
use LiveBeatsWeb, :live_view
2021-11-05 00:49:19 +00:00
alias LiveBeats.{MediaLibrary, MP3Stat}
2021-10-29 16:12:23 +00:00
alias LiveBeats.MediaLibrary.Song
2021-11-05 00:49:19 +00:00
alias LiveBeatsWeb.LayoutComponent
2021-10-29 16:12:23 +00:00
def render(assigns) do
~H"""
<.title_bar>
Listing Songs
<:actions>
2021-11-02 03:25:28 +00:00
<.button primary patch_to={Routes.song_index_path(@socket, :new)}>Upload Songs</.button>
2021-10-29 16:12:23 +00:00
</:actions>
</.title_bar>
2021-11-05 00:49:19 +00:00
<%= for song <- @songs, id = "delete-modal-#{song.id}" do %>
<.modal
id={id}
on_confirm={JS.push("delete", value: %{id: song.id}) |> hide_modal(id) |> hide("#song-#{song.id}")}
>
Are you sure you want to delete "<%= song.title %>"?
<:cancel>Cancel</:cancel>
<:confirm>Delete</:confirm>
2021-10-29 16:12:23 +00:00
</.modal>
<% end %>
2021-11-05 00:49:19 +00:00
<.live_table
module={LiveBeatsWeb.SongLive.SongRow}
rows={@songs}
row_id={fn song -> "song-#{song.id}" end}
>
<:col let={%{song: song}} label="Title"><%= song.title %></:col>
<:col let={%{song: song}} label="Artist"><%= song.artist %></:col>
<:col let={%{song: song}} label="Duration"><%= MP3Stat.to_mmss(song.duration) %></:col>
<:col let={%{song: song}} label="">
<.link phx-click={show_modal("delete-modal-#{song.id}")} class="inline-flex items-center px-3 py-2 text-sm leading-4 font-medium">
2021-11-05 01:20:31 +00:00
<.icon name={:trash} class="-ml-0.5 mr-2 h-4 w-4"/>
2021-11-05 00:49:19 +00:00
Delete
</.link>
2021-10-29 16:12:23 +00:00
</:col>
2021-11-05 00:49:19 +00:00
</.live_table>
2021-10-29 16:12:23 +00:00
"""
end
def mount(_params, _session, socket) do
2021-11-05 00:49:19 +00:00
if connected?(socket) do
MediaLibrary.subscribe(socket.assigns.current_user)
end
{:ok, assign(socket, songs: list_songs(), active_id: nil)}
2021-10-29 16:12:23 +00:00
end
def handle_params(params, _url, socket) do
2021-11-05 00:49:19 +00:00
{:noreply, socket |> apply_action(socket.assigns.live_action, params) |> maybe_show_modal()}
end
def handle_event("play-song", %{"id" => id}, socket) do
MediaLibrary.play_song(id)
{:noreply, socket}
end
def handle_event("delete", %{"id" => id}, socket) do
song = MediaLibrary.get_song!(id)
{:ok, _} = MediaLibrary.delete_song(song)
{:noreply, socket}
end
def handle_info({_ref, {:duration, entry_ref, result}}, socket) do
IO.inspect({:async_duration, entry_ref, result})
{:noreply, socket}
end
def handle_info({:play, %Song{} = song, _meta}, socket) do
{:noreply, play_song(socket, song)}
end
def handle_info({:pause, %Song{} = song}, socket) do
{:noreply, pause_song(socket, song.id)}
end
defp pause_song(socket, song_id) do
if old = Enum.find(socket.assigns.songs, fn song -> song.id == song_id end) do
send_update(LiveBeatsWeb.SongLive.SongRow, id: "song-#{old.id}", action: :deactivate)
end
socket
end
defp play_song(socket, %Song{} = song) do
socket = pause_song(socket, socket.assigns.active_id)
next = Enum.find(socket.assigns.songs, &(&1.id == song.id))
if next do
send_update(LiveBeatsWeb.SongLive.SongRow, id: "song-#{next.id}", action: :activate)
end
assign(socket, active_id: next.id)
end
defp maybe_show_modal(socket) do
if socket.assigns.live_action in [:new, :edit] do
LayoutComponent.show_modal(LiveBeatsWeb.SongLive.UploadFormComponent, %{
confirm: {"Save", type: "submit", form: "song-form"},
patch_to: Routes.song_index_path(socket, :index),
id: socket.assigns.song.id || :new,
title: socket.assigns.page_title,
action: socket.assigns.live_action,
song: socket.assigns.song,
current_user: socket.assigns.current_user,
genres: socket.assigns.genres
})
else
LayoutComponent.hide_modal()
end
socket
2021-10-29 16:12:23 +00:00
end
defp apply_action(socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, "Edit Song")
|> assign(:song, MediaLibrary.get_song!(id))
end
defp apply_action(socket, :new, _params) do
socket
2021-11-05 00:49:19 +00:00
|> assign(:page_title, "Add Songs")
2021-10-29 16:12:23 +00:00
|> assign(:song, %Song{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Songs")
|> assign(:song, nil)
end
defp list_songs do
2021-11-05 00:49:19 +00:00
MediaLibrary.list_songs(50)
2021-10-29 16:12:23 +00:00
end
end