Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interface normalizer #200

Merged
merged 2 commits into from
Aug 30, 2024
Merged
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
39 changes: 38 additions & 1 deletion lib/live_view_native/content_negotiator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ defmodule LiveViewNative.ContentNegotiator do
|> assign_params(params)
end

defp assign_params(%{assigns: %{:_format => _format}} = socket, %{"_interface" => interface} = params) do
defp assign_params(%{assigns: %{:_format => format}} = socket, %{"_interface" => interface} = params) do
params = Map.delete(params, "_interface")

interface = normalize_interface(interface, format)

socket
|> assign(:_interface, interface)
|> assign_params(params)
Expand All @@ -72,4 +74,39 @@ defmodule LiveViewNative.ContentNegotiator do
layouts -> layouts[format]
end
end

defp normalize_interface(interface, format) do
plugin =
format
|> LiveViewNative.fetch_plugin!()
|> Map.get(:__struct__)

interface
|> normalize_os_version(plugin)
|> normalize_app_version(plugin)
end

defp normalize_os_version(%{"os_version" => os_version} = interface, plugin) when not is_nil(os_version) do
if Module.defines?(plugin, {:normalize_os_version, 1}) do
os_version = plugin.normalize_os_version(os_version)
Map.put(interface, "os_version", os_version)
else
interface
end
end

defp normalize_os_version(interface, _plugin),
do: interface

defp normalize_app_version(%{"app_version" => app_version} = interface, plugin) when not is_nil(app_version) do
if Module.defines?(plugin, {:normalize_app_version, 1}) do
app_version = plugin.normalize_app_version(app_version)
Map.put(interface, "app_version", app_version)
else
interface
end
end

defp normalize_app_version(interface, _plugin),
do: interface
end
25 changes: 25 additions & 0 deletions test/live_view_native/component_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule LiveViewNativeTest.ComponentTest do
use ExUnit.Case
import LiveViewNativeTest.Utils, only: [render: 1]

alias Phoenix.LiveView.Socket
alias LiveViewNativeTest.{HomeLive}

setup do
{:ok, socket: %Socket{private: %{connect_info: %{params: %{"target" => "classic"}}}}}
end

describe "embed_templates" do
test "will compile templates into render/2 functions", %{socket: socket} do
assert render(HomeLive.home_live(%{foo: :bar, socket: socket}, %{})) =~ "Hello, GameBoy! bar"

assert render(HomeLive.home_live_other(%{foo: :bar, socket: socket}, %{})) =~ "Hello, Other GameBoy! bar"
end

test "will compile templates into render/1 functions for use with `render_with`", %{socket: socket} do
assert render(HomeLive.home_live(%{foo: :bar, socket: socket})) =~ "Hello, GameBoy! bar"

assert render(HomeLive.home_live_other(%{foo: :bar, socket: socket})) =~ "Hello, Other GameBoy! bar"
end
end
end
80 changes: 0 additions & 80 deletions test/live_view_native/components_test.exs

This file was deleted.

Loading