mirror of
https://github.com/fly-apps/live_beats.git
synced 2024-11-21 15:41:00 +00:00
Use event structs
This commit is contained in:
parent
ed8ad74141
commit
b3fe0ab666
6 changed files with 33 additions and 18 deletions
|
@ -3,7 +3,7 @@ defmodule LiveBeats.Accounts do
|
|||
import Ecto.Changeset
|
||||
|
||||
alias LiveBeats.Repo
|
||||
alias LiveBeats.Accounts.{User, Identity}
|
||||
alias LiveBeats.Accounts.{User, Identity, Events}
|
||||
|
||||
@admin_emails ["chris@chrismccord.com"]
|
||||
@pubsub LiveBeats.PubSub
|
||||
|
@ -64,19 +64,19 @@ defmodule LiveBeats.Accounts do
|
|||
current_user
|
||||
end
|
||||
|
||||
def update_active_profile(%User{} = current_user, profile_user_id) do
|
||||
def update_active_profile(%User{} = current_user, profile_uid) do
|
||||
{1, _} =
|
||||
Repo.update_all(from(u in User, where: u.id == ^current_user.id),
|
||||
set: [active_profile_user_id: profile_user_id]
|
||||
set: [active_profile_user_id: profile_uid]
|
||||
)
|
||||
|
||||
Phoenix.PubSub.broadcast!(
|
||||
@pubsub,
|
||||
topic(current_user.id),
|
||||
{__MODULE__, :active_profile_changed, current_user, %{user_id: profile_user_id}}
|
||||
%Events.ActiveProfileChanged{current_user: current_user, new_profile_user_id: profile_uid}
|
||||
)
|
||||
|
||||
%User{current_user | active_profile_user_id: profile_user_id}
|
||||
%User{current_user | active_profile_user_id: profile_uid}
|
||||
end
|
||||
|
||||
## User registration
|
||||
|
|
5
lib/live_beats/accounts/events.ex
Normal file
5
lib/live_beats/accounts/events.ex
Normal file
|
@ -0,0 +1,5 @@
|
|||
defmodule LiveBeats.Accounts.Events do
|
||||
defmodule ActiveProfileChanged do
|
||||
defstruct current_user: nil, new_profile_user_id: nil
|
||||
end
|
||||
end
|
|
@ -6,7 +6,7 @@ defmodule LiveBeats.MediaLibrary do
|
|||
require Logger
|
||||
import Ecto.Query, warn: false
|
||||
alias LiveBeats.{Repo, MP3Stat, Accounts}
|
||||
alias LiveBeats.MediaLibrary.{Profile, Song, Genre}
|
||||
alias LiveBeats.MediaLibrary.{Profile, Song, Events, Genre}
|
||||
alias Ecto.{Multi, Changeset}
|
||||
|
||||
@pubsub LiveBeats.PubSub
|
||||
|
@ -71,11 +71,10 @@ defmodule LiveBeats.MediaLibrary do
|
|||
|
||||
elapsed = elapsed_playback(new_song)
|
||||
|
||||
Phoenix.PubSub.broadcast!(
|
||||
@pubsub,
|
||||
topic(song.user_id),
|
||||
{__MODULE__, :play, song, %{elapsed: elapsed}}
|
||||
)
|
||||
Phoenix.PubSub.broadcast!(@pubsub, topic(song.user_id), %Events.Play{
|
||||
song: song,
|
||||
elapsed: elapsed
|
||||
})
|
||||
|
||||
new_song
|
||||
end
|
||||
|
@ -96,7 +95,7 @@ defmodule LiveBeats.MediaLibrary do
|
|||
|> Multi.update_all(:now_paused, fn _ -> pause_query end, [])
|
||||
|> Repo.transaction()
|
||||
|
||||
Phoenix.PubSub.broadcast!(@pubsub, topic(song.user_id), {__MODULE__, :pause, song})
|
||||
Phoenix.PubSub.broadcast!(@pubsub, topic(song.user_id), %Events.Pause{song: song})
|
||||
end
|
||||
|
||||
def play_next_song_auto(%Profile{} = profile) do
|
||||
|
|
9
lib/live_beats/media_library/events.ex
Normal file
9
lib/live_beats/media_library/events.ex
Normal file
|
@ -0,0 +1,9 @@
|
|||
defmodule LiveBeats.MediaLibrary.Events do
|
||||
defmodule Play do
|
||||
defstruct song: nil, elapsed: nil
|
||||
end
|
||||
|
||||
defmodule Pause do
|
||||
defstruct song: nil
|
||||
end
|
||||
end
|
|
@ -215,7 +215,7 @@ defmodule LiveBeatsWeb.PlayerLive do
|
|||
{:noreply, socket}
|
||||
end
|
||||
|
||||
def handle_info({Accounts, :active_profile_changed, _cur_user, %{user_id: user_id}}, socket) do
|
||||
def handle_info(%Accounts.Events.ActiveProfileChanged{new_profile_user_id: user_id}, socket) do
|
||||
if user_id do
|
||||
{:noreply, assign(socket, profile: get_profile(user_id))}
|
||||
else
|
||||
|
@ -227,11 +227,11 @@ defmodule LiveBeatsWeb.PlayerLive do
|
|||
{:noreply, play_current_song(socket)}
|
||||
end
|
||||
|
||||
def handle_info({MediaLibrary, :pause, _}, socket) do
|
||||
def handle_info(%MediaLibrary.Events.Pause{}, socket) do
|
||||
{:noreply, push_pause(socket)}
|
||||
end
|
||||
|
||||
def handle_info({MediaLibrary, :play, %Song{} = song, %{elapsed: elapsed}}, socket) do
|
||||
def handle_info(%MediaLibrary.Events.Play{song: song, elapsed: elapsed}, socket) do
|
||||
{:noreply, play_song(socket, song, elapsed)}
|
||||
end
|
||||
|
||||
|
|
|
@ -117,21 +117,23 @@ defmodule LiveBeatsWeb.SongLive.Index do
|
|||
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
song = MediaLibrary.get_song!(id)
|
||||
|
||||
if song.user_id == socket.assigns.current_user.id do
|
||||
{:ok, _} = MediaLibrary.delete_song(song)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
def handle_info({Accounts, :active_profile_changed, _cur_user, %{user_id: user_id}}, socket) do
|
||||
def handle_info(%Accounts.Events.ActiveProfileChanged{new_profile_user_id: user_id}, socket) do
|
||||
{:noreply, assign(socket, active_profile_id: user_id)}
|
||||
end
|
||||
|
||||
def handle_info({MediaLibrary, :play, %MediaLibrary.Song{} = song, _meta}, socket) do
|
||||
def handle_info(%MediaLibrary.Events.Play{song: song}, socket) do
|
||||
{:noreply, play_song(socket, song)}
|
||||
end
|
||||
|
||||
def handle_info({MediaLibrary, :pause, %MediaLibrary.Song{} = song}, socket) do
|
||||
def handle_info(%MediaLibrary.Events.Pause{song: song}, socket) do
|
||||
{:noreply, pause_song(socket, song.id)}
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue