From 77f06bfbc878e6032949683209a2ebcd6730753d Mon Sep 17 00:00:00 2001 From: Kieran Date: Fri, 9 Feb 2024 19:21:51 -0800 Subject: [PATCH] Media UI v1 (#17) * Updated comments * Added basic media tables to sources#show * Adds a very basic listing and show page for media --- .../workers/media_indexing_worker.ex | 1 + .../media/media_item_controller.ex | 11 ++++++ .../controllers/media/media_item_html.ex | 5 +++ .../media/media_item_html/show.html.heex | 18 +++++++++ .../media_sources/source_controller.ex | 6 ++- .../media_sources/source_html/show.html.heex | 38 +++++++++++++++++++ lib/pinchflat_web/router.ex | 6 ++- .../media_item_controller_test.exs | 19 ++++++++++ 8 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 lib/pinchflat_web/controllers/media/media_item_controller.ex create mode 100644 lib/pinchflat_web/controllers/media/media_item_html.ex create mode 100644 lib/pinchflat_web/controllers/media/media_item_html/show.html.heex create mode 100644 test/pinchflat_web/controllers/media_item_controller_test.exs diff --git a/lib/pinchflat/workers/media_indexing_worker.ex b/lib/pinchflat/workers/media_indexing_worker.ex index e8e5b37..52b4849 100644 --- a/lib/pinchflat/workers/media_indexing_worker.ex +++ b/lib/pinchflat/workers/media_indexing_worker.ex @@ -48,6 +48,7 @@ defmodule Pinchflat.Workers.MediaIndexingWorker do defp index_media_and_reschedule(source) do SourceTasks.index_media_items(source) + # This method handles the case where a source is set to not download media SourceTasks.enqueue_pending_media_downloads(source) source diff --git a/lib/pinchflat_web/controllers/media/media_item_controller.ex b/lib/pinchflat_web/controllers/media/media_item_controller.ex new file mode 100644 index 0000000..3db81d4 --- /dev/null +++ b/lib/pinchflat_web/controllers/media/media_item_controller.ex @@ -0,0 +1,11 @@ +defmodule PinchflatWeb.Media.MediaItemController do + use PinchflatWeb, :controller + + alias Pinchflat.Media + + def show(conn, %{"id" => id}) do + media_item = Media.get_media_item!(id) + + render(conn, :show, media_item: media_item) + end +end diff --git a/lib/pinchflat_web/controllers/media/media_item_html.ex b/lib/pinchflat_web/controllers/media/media_item_html.ex new file mode 100644 index 0000000..d0c0667 --- /dev/null +++ b/lib/pinchflat_web/controllers/media/media_item_html.ex @@ -0,0 +1,5 @@ +defmodule PinchflatWeb.Media.MediaItemHTML do + use PinchflatWeb, :html + + embed_templates "media_item_html/*" +end diff --git a/lib/pinchflat_web/controllers/media/media_item_html/show.html.heex b/lib/pinchflat_web/controllers/media/media_item_html/show.html.heex new file mode 100644 index 0000000..ac65a28 --- /dev/null +++ b/lib/pinchflat_web/controllers/media/media_item_html/show.html.heex @@ -0,0 +1,18 @@ +
+
+ <.link :if={@conn.params["source_id"]} navigate={~p"/sources/#{@media_item.source_id}"}> + <.icon name="hero-arrow-left" class="w-10 h-10 hover:dark:text-white" /> + +

+ Media Item #<%= @media_item.id %> +

+
+
+
+
+
+

Attributes

+ <.list_items_from_map map={Map.from_struct(@media_item)} /> +
+
+
diff --git a/lib/pinchflat_web/controllers/media_sources/source_controller.ex b/lib/pinchflat_web/controllers/media_sources/source_controller.ex index 63f4cbe..e4b28ad 100644 --- a/lib/pinchflat_web/controllers/media_sources/source_controller.ex +++ b/lib/pinchflat_web/controllers/media_sources/source_controller.ex @@ -2,6 +2,7 @@ defmodule PinchflatWeb.MediaSources.SourceController do use PinchflatWeb, :controller alias Pinchflat.Repo + alias Pinchflat.Media alias Pinchflat.Profiles alias Pinchflat.MediaSource alias Pinchflat.MediaSource.Source @@ -36,7 +37,10 @@ defmodule PinchflatWeb.MediaSources.SourceController do |> MediaSource.get_source!() |> Repo.preload(:media_profile) - render(conn, :show, source: source) + pending_media = Media.list_pending_media_items_for(source) + downloaded_media = Media.list_downloaded_media_items_for(source) + + render(conn, :show, source: source, pending_media: pending_media, downloaded_media: downloaded_media) end def edit(conn, %{"id" => id}) do diff --git a/lib/pinchflat_web/controllers/media_sources/source_html/show.html.heex b/lib/pinchflat_web/controllers/media_sources/source_html/show.html.heex index 1d947d5..97332cf 100644 --- a/lib/pinchflat_web/controllers/media_sources/source_html/show.html.heex +++ b/lib/pinchflat_web/controllers/media_sources/source_html/show.html.heex @@ -33,6 +33,44 @@

Attributes

<.list_items_from_map map={Map.from_struct(@source)} /> + +

Downloaded Media

+ <%= if length(@downloaded_media) > 0 do %> + <.table rows={@downloaded_media} table_class="text-black dark:text-white"> + <:col :let={media_item} label="Title"> + <%= String.slice(media_item.title, 0..50) %>... + + <:col :let={media_item} label="" class="flex place-content-evenly"> + <.link + navigate={~p"/sources/#{@source.id}/media/#{media_item.id}"} + class="hover:text-secondary duration-200 ease-in-out mx-0.5" + > + <.icon name="hero-eye" /> + + + + <% else %> +

Nothing Here!

+ <% end %> + +

Pending Media

+ <%= if length(@pending_media) > 0 do %> + <.table rows={@pending_media} table_class="text-black dark:text-white"> + <:col :let={media_item} label="Title"> + <%= String.slice(media_item.title, 0..50) %>... + + <:col :let={media_item} label="" class="flex place-content-evenly"> + <.link + navigate={~p"/sources/#{@source.id}/media/#{media_item.id}"} + class="hover:text-secondary duration-200 ease-in-out mx-0.5" + > + <.icon name="hero-eye" /> + + + + <% else %> +

Nothing Here!

+ <% end %> diff --git a/lib/pinchflat_web/router.ex b/lib/pinchflat_web/router.ex index 3610003..55f177a 100644 --- a/lib/pinchflat_web/router.ex +++ b/lib/pinchflat_web/router.ex @@ -20,7 +20,11 @@ defmodule PinchflatWeb.Router do get "/", PageController, :home resources "/media_profiles", MediaProfiles.MediaProfileController - resources "/sources", MediaSources.SourceController + resources "/media", Media.MediaItemController, only: [:show] + + resources "/sources", MediaSources.SourceController do + resources "/media", Media.MediaItemController, only: [:show] + end end # Other scopes may use custom stacks. diff --git a/test/pinchflat_web/controllers/media_item_controller_test.exs b/test/pinchflat_web/controllers/media_item_controller_test.exs new file mode 100644 index 0000000..eb0eab2 --- /dev/null +++ b/test/pinchflat_web/controllers/media_item_controller_test.exs @@ -0,0 +1,19 @@ +defmodule PinchflatWeb.MediaItemControllerTest do + use PinchflatWeb.ConnCase + + import Pinchflat.MediaFixtures + + describe "show media" do + setup [:create_media_item] + + test "renders the page", %{conn: conn, media_item: media_item} do + conn = get(conn, ~p"/media/#{media_item}") + assert html_response(conn, 200) =~ "Media Item ##{media_item.id}" + end + end + + defp create_media_item(_) do + media_item = media_item_fixture() + %{media_item: media_item} + end +end