Remove duplication in stats live lifecycle
This commit is contained in:
@@ -7,28 +7,9 @@ defmodule MusicLibraryWeb.StatsLive.Index do
|
|||||||
alias Records.Record
|
alias Records.Record
|
||||||
|
|
||||||
def mount(_params, _session, socket) do
|
def mount(_params, _session, socket) do
|
||||||
collection_count_by_format = Collection.count_records_by_format()
|
|
||||||
|
|
||||||
collection_count_by_type = Collection.count_records_by_type()
|
|
||||||
|
|
||||||
collection_count =
|
|
||||||
Enum.reduce(collection_count_by_format, 0, fn {_, count}, acc -> acc + count end)
|
|
||||||
|
|
||||||
wishlist_count = Wishlist.count()
|
|
||||||
|
|
||||||
latest_record = Collection.get_latest_record!()
|
latest_record = Collection.get_latest_record!()
|
||||||
|
|
||||||
recent_tracks = LastFm.Feed.all_tracks()
|
recent_tracks = LastFm.Feed.all_tracks()
|
||||||
|
|
||||||
recent_release_ids = recent_release_ids(recent_tracks)
|
|
||||||
|
|
||||||
collected_release_ids = Collection.collected_release_ids(recent_release_ids)
|
|
||||||
wishlisted_release_ids = Wishlist.wishlisted_release_ids(recent_release_ids)
|
|
||||||
|
|
||||||
all_artist_ids = Artists.get_all_artist_ids()
|
|
||||||
recent_artist_ids = recent_artist_ids(recent_tracks)
|
|
||||||
artist_ids = MapSet.intersection(all_artist_ids, recent_artist_ids)
|
|
||||||
|
|
||||||
if connected?(socket) do
|
if connected?(socket) do
|
||||||
LastFm.Feed.subscribe()
|
LastFm.Feed.subscribe()
|
||||||
end
|
end
|
||||||
@@ -39,16 +20,11 @@ defmodule MusicLibraryWeb.StatsLive.Index do
|
|||||||
dom_id: fn track -> "track-#{track.scrobbled_at_uts}" end
|
dom_id: fn track -> "track-#{track.scrobbled_at_uts}" end
|
||||||
)
|
)
|
||||||
|> stream(:recent_tracks, recent_tracks)
|
|> stream(:recent_tracks, recent_tracks)
|
||||||
|
|> assign_counts()
|
||||||
|
|> assign_scrobble_activity(recent_tracks)
|
||||||
|> assign(
|
|> assign(
|
||||||
page_title: gettext("Stats"),
|
|
||||||
collection_count_by_format: collection_count_by_format,
|
|
||||||
collection_count_by_type: collection_count_by_type,
|
|
||||||
collection_count: collection_count,
|
|
||||||
wishlist_count: wishlist_count,
|
|
||||||
latest_record: latest_record,
|
latest_record: latest_record,
|
||||||
collected_release_ids: collected_release_ids,
|
page_title: gettext("Stats"),
|
||||||
wishlisted_release_ids: wishlisted_release_ids,
|
|
||||||
artist_ids: artist_ids,
|
|
||||||
nav_section: :stats
|
nav_section: :stats
|
||||||
)}
|
)}
|
||||||
end
|
end
|
||||||
@@ -85,6 +61,31 @@ defmodule MusicLibraryWeb.StatsLive.Index do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def handle_info(%{tracks: recent_tracks}, socket) do
|
def handle_info(%{tracks: recent_tracks}, socket) do
|
||||||
|
{:noreply,
|
||||||
|
socket
|
||||||
|
|> assign_scrobble_activity(recent_tracks)
|
||||||
|
|> stream(:recent_tracks, recent_tracks, reset: true)}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp assign_counts(socket) do
|
||||||
|
collection_count_by_format = Collection.count_records_by_format()
|
||||||
|
|
||||||
|
collection_count_by_type = Collection.count_records_by_type()
|
||||||
|
|
||||||
|
collection_count =
|
||||||
|
Enum.reduce(collection_count_by_format, 0, fn {_, count}, acc -> acc + count end)
|
||||||
|
|
||||||
|
wishlist_count = Wishlist.count()
|
||||||
|
|
||||||
|
assign(socket,
|
||||||
|
collection_count_by_format: collection_count_by_format,
|
||||||
|
collection_count_by_type: collection_count_by_type,
|
||||||
|
collection_count: collection_count,
|
||||||
|
wishlist_count: wishlist_count
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp assign_scrobble_activity(socket, recent_tracks) do
|
||||||
recent_release_ids = recent_release_ids(recent_tracks)
|
recent_release_ids = recent_release_ids(recent_tracks)
|
||||||
|
|
||||||
collected_release_ids = Collection.collected_release_ids(recent_release_ids)
|
collected_release_ids = Collection.collected_release_ids(recent_release_ids)
|
||||||
@@ -94,14 +95,11 @@ defmodule MusicLibraryWeb.StatsLive.Index do
|
|||||||
recent_artist_ids = recent_artist_ids(recent_tracks)
|
recent_artist_ids = recent_artist_ids(recent_tracks)
|
||||||
artist_ids = MapSet.intersection(all_artist_ids, recent_artist_ids)
|
artist_ids = MapSet.intersection(all_artist_ids, recent_artist_ids)
|
||||||
|
|
||||||
{:noreply,
|
assign(socket,
|
||||||
socket
|
collected_release_ids: collected_release_ids,
|
||||||
|> assign(
|
wishlisted_release_ids: wishlisted_release_ids,
|
||||||
collected_release_ids: collected_release_ids,
|
artist_ids: artist_ids
|
||||||
wishlisted_release_ids: wishlisted_release_ids,
|
)
|
||||||
artist_ids: artist_ids
|
|
||||||
)
|
|
||||||
|> stream(:recent_tracks, recent_tracks, reset: true)}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp recent_release_ids(recent_tracks) do
|
defp recent_release_ids(recent_tracks) do
|
||||||
|
|||||||
@@ -61,8 +61,8 @@ msgstr ""
|
|||||||
|
|
||||||
#: lib/music_library_web/live/collection_live/index.ex:124
|
#: lib/music_library_web/live/collection_live/index.ex:124
|
||||||
#: lib/music_library_web/live/collection_live/index.ex:131
|
#: lib/music_library_web/live/collection_live/index.ex:131
|
||||||
#: lib/music_library_web/live/stats_live/index.ex:77
|
#: lib/music_library_web/live/stats_live/index.ex:53
|
||||||
#: lib/music_library_web/live/stats_live/index.ex:83
|
#: lib/music_library_web/live/stats_live/index.ex:59
|
||||||
#: lib/music_library_web/live/wishlist_live/index.ex:121
|
#: lib/music_library_web/live/wishlist_live/index.ex:121
|
||||||
#: lib/music_library_web/live/wishlist_live/index.ex:128
|
#: lib/music_library_web/live/wishlist_live/index.ex:128
|
||||||
#, elixir-autogen, elixir-format
|
#, elixir-autogen, elixir-format
|
||||||
@@ -186,7 +186,7 @@ msgid "Purchased on"
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: lib/music_library_web/live/collection_live/index.ex:116
|
#: lib/music_library_web/live/collection_live/index.ex:116
|
||||||
#: lib/music_library_web/live/stats_live/index.ex:69
|
#: lib/music_library_web/live/stats_live/index.ex:45
|
||||||
#: lib/music_library_web/live/wishlist_live/index.ex:113
|
#: lib/music_library_web/live/wishlist_live/index.ex:113
|
||||||
#, elixir-autogen, elixir-format
|
#, elixir-autogen, elixir-format
|
||||||
msgid "Record imported successfully"
|
msgid "Record imported successfully"
|
||||||
@@ -248,7 +248,7 @@ msgid "Something went wrong!"
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: lib/music_library_web/components/layouts/app.html.heex:8
|
#: lib/music_library_web/components/layouts/app.html.heex:8
|
||||||
#: lib/music_library_web/live/stats_live/index.ex:43
|
#: lib/music_library_web/live/stats_live/index.ex:27
|
||||||
#, elixir-autogen, elixir-format
|
#, elixir-autogen, elixir-format
|
||||||
msgid "Stats"
|
msgid "Stats"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|||||||
Reference in New Issue
Block a user