Skip to content
This repository was archived by the owner on May 12, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions rfid/lib/rfid/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ defmodule RFID.Handler do
end

defp extract_tag_id(playlist) do
~r/\A(?<tag_id>\d+)/
|> Regex.named_captures(playlist)
|> case do
%{"tag_id" => tag_id} -> tag_id
case Musicbox.Player.playlist_information(playlist) do
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably time for the extra Playlist module. But it works as is

%{"id" => id} -> id
_ -> nil
end
end
Expand Down
9 changes: 2 additions & 7 deletions ui/assets/css/theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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%;
}
41 changes: 39 additions & 2 deletions ui/lib/musicbox/player.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(?<id>\d+)(?:\W+(?<name>.+))?\z/
Regex.named_captures(re, playlist)
end

defp initialize_player do
set_volume(20)
end
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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
50 changes: 47 additions & 3 deletions ui/lib/musicbox_web/live/playlists_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,25 @@ defmodule MusicboxWeb.PlaylistsLive do
</span>
</button>
</td>
<td><%= playlist.id %></td>
<td>
<%= if @edit_playlist == playlist.id do %>
<form phx-submit="set_playlist_name">
<input name="id" type="hidden" value="<%= playlist.id %>">
<div class="field has-addons">
<div class="control">
<input name="name" autocomplete="off" class="input" placeholder="<%= playlist.name %>" />
</div>
<div class="control">
<button class="button" type="submit">Update name</button>
</div>
</div>
</form>
<% else %>
<button phx-click="edit_playlist_name" value="<%= playlist.id %>" class="button">
<%= playlist.name %>
</button>
<% end %>
</td>
<td><%= playlist.song_count %></td>
<td><%= duration playlist.duration %></td>
</tr>
Expand All @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion ui/lib/musicbox_web/live/songs_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule MusicboxWeb.SongsLive do
<select name="playlist" id="playlist">
<option value="" selected>-- Select a Playlist --</option>
<%= for playlist <- @playlists do %>
<option value="<%= playlist.id %>"><%= playlist.id %></option>
<option value="<%= playlist.id %>"><%= playlist.name %></option>
<% end %>
</select>
</form>
Expand Down
3 changes: 2 additions & 1 deletion ui/lib/musicbox_web/templates/song/new.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
<%= file_input f, :file, multiple: true, accept: ".mp3" %>
</div>

<div>
<!-- lols until we change something here -->
<div style="margin-top: 20px;">
<%= submit "Submit", class: "button is-primary" %>
</div>
<% end %>