From 562718cd5e2ef08ad11fe157c89a2e069d9e7262 Mon Sep 17 00:00:00 2001 From: Theo Fiedler Date: Sat, 6 Apr 2019 18:49:51 +0200 Subject: [PATCH 1/6] adds unstyled input for renaming playlists --- ui/lib/musicbox/player.ex | 31 +++++++++++++++++++++- ui/lib/musicbox_web/live/playlists_live.ex | 12 +++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/ui/lib/musicbox/player.ex b/ui/lib/musicbox/player.ex index 55ea1d1..1333ba9 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 + {id, playlist_name} = new_playlist_name(id, name) + + MpdClient.Playlists.rename(id, playlist_name) + {:reply, playlist_name, state} + end + def handle_call({:current_volume}, _from, state) do {:reply, current_volume(), state} end @@ -231,7 +239,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 @@ -248,4 +256,25 @@ defmodule Musicbox.Player do end) |> Enum.map(fn playlist -> playlist["playlist"] end) end + + defp new_playlist_name(id, name) do + new_name = case String.starts_with?(id, "#") do + true -> rename_changed_playlist(id, name) + false -> new_playlist_name(id, name) + end + + {id, new_name} + end + + defp rename_changed_playlist(old_name, new_name) do + [id | _] = String.split(old_name) + + id + |> String.slice(1..-1) + |> new_playlist_name(new_name) + end + + defp new_playlist_name(id, name) do + "#" <> id <> " - " <> name + end end diff --git a/ui/lib/musicbox_web/live/playlists_live.ex b/ui/lib/musicbox_web/live/playlists_live.ex index 9ecb6fd..1dd0839 100644 --- a/ui/lib/musicbox_web/live/playlists_live.ex +++ b/ui/lib/musicbox_web/live/playlists_live.ex @@ -32,6 +32,13 @@ defmodule MusicboxWeb.PlaylistsLive do <%= playlist.id %> <%= playlist.song_count %> <%= duration playlist.duration %> + +
+ + + +
+ <% end %> @@ -62,6 +69,11 @@ defmodule MusicboxWeb.PlaylistsLive 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 From b510d00beef205a52409d6b1f6bfa6d8cd76b22d Mon Sep 17 00:00:00 2001 From: Theo Fiedler Date: Sat, 6 Apr 2019 20:54:20 +0200 Subject: [PATCH 2/6] adds a simple ui for renaming playlists --- ui/assets/css/theme.scss | 9 +--- ui/lib/musicbox/player.ex | 4 +- ui/lib/musicbox_web/live/playlists_live.ex | 52 +++++++++++++++---- .../musicbox_web/templates/song/new.html.eex | 3 +- 4 files changed, 48 insertions(+), 20 deletions(-) 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 1333ba9..31c07b2 100644 --- a/ui/lib/musicbox/player.ex +++ b/ui/lib/musicbox/player.ex @@ -121,7 +121,7 @@ defmodule Musicbox.Player do end def handle_call({:rename_playlist, %{"id" => id, "name" => name}}, _from, state) do - {id, playlist_name} = new_playlist_name(id, name) + {id, playlist_name} = set_playlist_name(id, name) MpdClient.Playlists.rename(id, playlist_name) {:reply, playlist_name, state} @@ -257,7 +257,7 @@ defmodule Musicbox.Player do |> Enum.map(fn playlist -> playlist["playlist"] end) end - defp new_playlist_name(id, name) do + defp set_playlist_name(id, name) do new_name = case String.starts_with?(id, "#") do true -> rename_changed_playlist(id, name) false -> new_playlist_name(id, name) diff --git a/ui/lib/musicbox_web/live/playlists_live.ex b/ui/lib/musicbox_web/live/playlists_live.ex index 1dd0839..a13e81f 100644 --- a/ui/lib/musicbox_web/live/playlists_live.ex +++ b/ui/lib/musicbox_web/live/playlists_live.ex @@ -29,16 +29,27 @@ defmodule MusicboxWeb.PlaylistsLive do - <%= playlist.id %> - <%= playlist.song_count %> - <%= duration playlist.duration %> -
- - - -
+ <%= if @edit_playlist == playlist.id do %> +
+ +
+
+ +
+
+ +
+
+
+ <% else %> + + <% end %> + <%= playlist.song_count %> + <%= duration playlist.duration %> <% end %> @@ -48,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 @@ -69,6 +83,13 @@ 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} @@ -78,6 +99,17 @@ defmodule MusicboxWeb.PlaylistsLive 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/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 %> From 8775062e4a6f37e664b1d60919b0086884ec33f2 Mon Sep 17 00:00:00 2001 From: Theo Fiedler Date: Sat, 6 Apr 2019 22:51:21 +0200 Subject: [PATCH 3/6] adds display helper for playlist names --- ui/lib/musicbox/player.ex | 17 ++++++++++++++++- ui/lib/musicbox_web/live/playlists_live.ex | 6 +++--- ui/lib/musicbox_web/live/songs_live.ex | 2 +- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/ui/lib/musicbox/player.ex b/ui/lib/musicbox/player.ex index 31c07b2..87eeced 100644 --- a/ui/lib/musicbox/player.ex +++ b/ui/lib/musicbox/player.ex @@ -225,6 +225,7 @@ defmodule Musicbox.Player do %{ id: id, + name: maybe_display_name(id), song_count: Enum.count(songs), duration: duration, songs: get_playlist_songs(id) @@ -254,7 +255,7 @@ 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_display_name(playlist["playlist"]) end) end defp set_playlist_name(id, name) do @@ -277,4 +278,18 @@ defmodule Musicbox.Player do defp new_playlist_name(id, name) do "#" <> id <> " - " <> name end + + defp maybe_display_name(name) do + case String.starts_with?(name, "#") do + true -> extract_playlist_name(name) + _ -> name + end + end + + defp extract_playlist_name(name) do + case Regex.run(~r/(?<= - ).*$/, name) do + [h] -> h + _ -> name + end + end end diff --git a/ui/lib/musicbox_web/live/playlists_live.ex b/ui/lib/musicbox_web/live/playlists_live.ex index a13e81f..ffb5fa1 100644 --- a/ui/lib/musicbox_web/live/playlists_live.ex +++ b/ui/lib/musicbox_web/live/playlists_live.ex @@ -35,16 +35,16 @@ defmodule MusicboxWeb.PlaylistsLive do
- +
- +
<% else %> <% end %> 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 From 36d18d55236f5c1c280b18585940d6bb10c9069b Mon Sep 17 00:00:00 2001 From: Theo Fiedler Date: Sun, 7 Apr 2019 15:16:31 +0200 Subject: [PATCH 4/6] use named_captures to extract playlist_information --- ui/lib/musicbox/player.ex | 49 +++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/ui/lib/musicbox/player.ex b/ui/lib/musicbox/player.ex index 87eeced..236e212 100644 --- a/ui/lib/musicbox/player.ex +++ b/ui/lib/musicbox/player.ex @@ -121,9 +121,9 @@ defmodule Musicbox.Player do end def handle_call({:rename_playlist, %{"id" => id, "name" => name}}, _from, state) do - {id, playlist_name} = set_playlist_name(id, name) + {old_name, new_name} = set_playlist_name(id, name) - MpdClient.Playlists.rename(id, playlist_name) + MpdClient.Playlists.rename(old_name, new_name) {:reply, playlist_name, state} end @@ -168,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 @@ -225,7 +230,7 @@ defmodule Musicbox.Player do %{ id: id, - name: maybe_display_name(id), + name: maybe_playlist_name(id), song_count: Enum.count(songs), duration: duration, songs: get_playlist_songs(id) @@ -255,41 +260,29 @@ defmodule Musicbox.Player do {:ok, song_list} = MpdClient.Playlists.list(item["playlist"]) Enum.member?(song_list, path) end) - |> Enum.map(fn playlist -> maybe_display_name(playlist["playlist"]) end) + |> Enum.map(fn playlist -> maybe_playlist_name(playlist["playlist"]) end) end - defp set_playlist_name(id, name) do - new_name = case String.starts_with?(id, "#") do - true -> rename_changed_playlist(id, name) - false -> new_playlist_name(id, name) + 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 - {id, new_name} - end - - defp rename_changed_playlist(old_name, new_name) do - [id | _] = String.split(old_name) - - id - |> String.slice(1..-1) - |> new_playlist_name(new_name) + {old_name, new_name} end + defp new_playlist_name(id, ""), do: id defp new_playlist_name(id, name) do - "#" <> id <> " - " <> name + "#{id} - #{name}" end - defp maybe_display_name(name) do - case String.starts_with?(name, "#") do - true -> extract_playlist_name(name) - _ -> name - end - end - defp extract_playlist_name(name) do - case Regex.run(~r/(?<= - ).*$/, name) do - [h] -> h - _ -> name + defp maybe_playlist_name(name) do + case playlist_information(name) do + %{"id" => id, "name" => ""} -> id + %{"name" => name} -> name + _ -> nil end end end From 8247c1a1940074c38be70416fff52824e625f0ef Mon Sep 17 00:00:00 2001 From: Theo Fiedler Date: Sun, 7 Apr 2019 15:17:15 +0200 Subject: [PATCH 5/6] match playlist id only for renamed playlists when scanning rfid --- rfid/lib/rfid/handler.ex | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) 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 From 7d6e1551d7603af36d2e13fb35c723766dbbda65 Mon Sep 17 00:00:00 2001 From: Theo Fiedler Date: Sun, 7 Apr 2019 15:18:20 +0200 Subject: [PATCH 6/6] follow up --- ui/lib/musicbox/player.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/lib/musicbox/player.ex b/ui/lib/musicbox/player.ex index 236e212..483a166 100644 --- a/ui/lib/musicbox/player.ex +++ b/ui/lib/musicbox/player.ex @@ -124,7 +124,7 @@ defmodule Musicbox.Player do {old_name, new_name} = set_playlist_name(id, name) MpdClient.Playlists.rename(old_name, new_name) - {:reply, playlist_name, state} + {:reply, new_name, state} end def handle_call({:current_volume}, _from, state) do