diff --git a/rfid/lib/rfid/handler.ex b/rfid/lib/rfid/handler.ex index 838553f..c2dea36 100644 --- a/rfid/lib/rfid/handler.ex +++ b/rfid/lib/rfid/handler.ex @@ -24,10 +24,8 @@ defmodule RFID.Handler do end defp extract_tag_id(playlist) do - ~r/\A(?\d+)/ - |> Regex.named_captures(playlist) - |> case do - %{"tag_id" => tag_id} -> tag_id + case Musicbox.Player.playlist_information(playlist) do + %{"id" => id} -> id _ -> nil end end diff --git a/ui/assets/css/theme.scss b/ui/assets/css/theme.scss index 4346fd2..e82bdb4 100644 --- a/ui/assets/css/theme.scss +++ b/ui/assets/css/theme.scss @@ -30,11 +30,6 @@ section.player { padding-bottom: 10px; } -form button, -a.button { - margin-top: 20px; -} - -table button.is-small { - padding-top: .2em; +table { + width: 100%; } diff --git a/ui/lib/musicbox/player.ex b/ui/lib/musicbox/player.ex index 55ea1d1..483a166 100644 --- a/ui/lib/musicbox/player.ex +++ b/ui/lib/musicbox/player.ex @@ -22,6 +22,7 @@ defmodule Musicbox.Player do def list_playlists, do: GenServer.call(__MODULE__, {:list_playlists}) def shuffle, do: GenServer.cast(__MODULE__, {:shuffle}) def create_playlist(name), do: GenServer.call(__MODULE__, {:create_playlist, name}) + def rename_playlist(playlist), do: GenServer.call(__MODULE__, {:rename_playlist, playlist}) def volume_up(step \\ 5), do: GenServer.call(__MODULE__, {:volume_up, step}) def volume_down(step \\ 5), do: GenServer.call(__MODULE__, {:volume_down, step}) def volume, do: GenServer.call(__MODULE__, {:current_volume}) @@ -119,6 +120,13 @@ defmodule Musicbox.Player do {:reply, info, state} end + def handle_call({:rename_playlist, %{"id" => id, "name" => name}}, _from, state) do + {old_name, new_name} = set_playlist_name(id, name) + + MpdClient.Playlists.rename(old_name, new_name) + {:reply, new_name, state} + end + def handle_call({:current_volume}, _from, state) do {:reply, current_volume(), state} end @@ -160,6 +168,11 @@ defmodule Musicbox.Player do {:noreply, put_player_status(state, fetch_player_status())} end + def playlist_information(playlist) do + re = ~r/\A(?\d+)(?:\W+(?.+))?\z/ + Regex.named_captures(re, playlist) + end + defp initialize_player do set_volume(20) end @@ -217,6 +230,7 @@ defmodule Musicbox.Player do %{ id: id, + name: maybe_playlist_name(id), song_count: Enum.count(songs), duration: duration, songs: get_playlist_songs(id) @@ -231,7 +245,7 @@ defmodule Musicbox.Player do |> Enum.filter(&valid_song?/1) |> Enum.map(fn item -> song = Musicbox.Song.from_mpd(item) - %{ song | playlists: get_playlist_from_song(song) } + %{song | playlists: get_playlist_from_song(song)} end) end @@ -246,6 +260,29 @@ defmodule Musicbox.Player do {:ok, song_list} = MpdClient.Playlists.list(item["playlist"]) Enum.member?(song_list, path) end) - |> Enum.map(fn playlist -> playlist["playlist"] end) + |> Enum.map(fn playlist -> maybe_playlist_name(playlist["playlist"]) end) + end + + defp set_playlist_name(old_name, name) do + new_name = case playlist_information(old_name) do + %{"id" => id} -> new_playlist_name(id, name) + _ -> nil + end + + {old_name, new_name} + end + + defp new_playlist_name(id, ""), do: id + defp new_playlist_name(id, name) do + "#{id} - #{name}" + end + + + defp maybe_playlist_name(name) do + case playlist_information(name) do + %{"id" => id, "name" => ""} -> id + %{"name" => name} -> name + _ -> nil + end end end diff --git a/ui/lib/musicbox_web/live/playlists_live.ex b/ui/lib/musicbox_web/live/playlists_live.ex index 9ecb6fd..ffb5fa1 100644 --- a/ui/lib/musicbox_web/live/playlists_live.ex +++ b/ui/lib/musicbox_web/live/playlists_live.ex @@ -29,7 +29,25 @@ defmodule MusicboxWeb.PlaylistsLive do - <%= playlist.id %> + + <%= if @edit_playlist == playlist.id do %> +
+ +
+
+ +
+
+ +
+
+
+ <% else %> + + <% end %> + <%= playlist.song_count %> <%= duration playlist.duration %> @@ -41,10 +59,13 @@ defmodule MusicboxWeb.PlaylistsLive do def mount(_session, socket) do if connected?(socket), do: :timer.send_interval(10_000, self(), :tick) - Player.subscribe(self()) - {:ok, put_status(socket)} + socket = socket + |> put_status() + |> set_edit_playlist() + + {:ok, socket} end def handle_info(:tick, socket) do @@ -62,10 +83,33 @@ defmodule MusicboxWeb.PlaylistsLive do {:noreply, socket} end + def handle_event("edit_playlist_name", playlist, socket) when is_binary(playlist) do + {:noreply, set_edit_playlist(socket, playlist)} + end + def handle_event("edit_playlist_name", _, socket) do + {:noreply, socket} + end + + def handle_event("set_playlist_name", playlist, socket) do + Player.rename_playlist(playlist) + {:noreply, socket} + end + defp put_status(socket) do assign(socket, player: Player.status()) end + defp set_edit_playlist(socket) do + assign(socket, edit_playlist: nil) + end + + defp set_edit_playlist(socket, playlist) when is_binary(playlist) do + assign(socket, edit_playlist: playlist) + end + defp set_edit_playlist(socket, _playlist) do + assign(socket, edit_playlist: nil) + end + defp duration(seconds) when is_binary(seconds) do {seconds, _} = Integer.parse(seconds) duration(seconds) diff --git a/ui/lib/musicbox_web/live/songs_live.ex b/ui/lib/musicbox_web/live/songs_live.ex index 2801936..398b12e 100644 --- a/ui/lib/musicbox_web/live/songs_live.ex +++ b/ui/lib/musicbox_web/live/songs_live.ex @@ -43,7 +43,7 @@ defmodule MusicboxWeb.SongsLive do diff --git a/ui/lib/musicbox_web/templates/song/new.html.eex b/ui/lib/musicbox_web/templates/song/new.html.eex index a73f550..1d61ecf 100644 --- a/ui/lib/musicbox_web/templates/song/new.html.eex +++ b/ui/lib/musicbox_web/templates/song/new.html.eex @@ -6,7 +6,8 @@ <%= file_input f, :file, multiple: true, accept: ".mp3" %> -
+ +
<%= submit "Submit", class: "button is-primary" %>
<% end %>