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

chore: Add flex_zone pages to Mercury screen requests #2317

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 14 additions & 1 deletion lib/screens/v2/screen_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule Screens.V2.ScreenData do

@type t :: %{type: atom()}
@type simulation_data :: %{full_page: t(), flex_zone: [t()]}
@type data_with_flex_zone :: {t(), [t()]}
@type variants(data) :: {data, %{String.t() => data}}
@type screen_id :: String.t()
@type options :: [
Expand All @@ -31,6 +32,12 @@ defmodule Screens.V2.ScreenData do
select_variant(screen_id, opts, &layout_to_data/2)
end

@spec get_with_flex_zone(screen_id()) :: data_with_flex_zone()
@spec get_with_flex_zone(screen_id(), options()) :: data_with_flex_zone()
def get_with_flex_zone(screen_id, opts \\ []) do
digitalcora marked this conversation as resolved.
Show resolved Hide resolved
select_variant(screen_id, opts, &layout_to_data_with_flex_zone/2)
end

@spec simulation(screen_id()) :: simulation_data()
@spec simulation(screen_id(), options()) :: simulation_data()
def simulation(screen_id, opts \\ []) do
Expand All @@ -50,7 +57,7 @@ defmodule Screens.V2.ScreenData do
end

@spec select_variant(screen_id(), options(), (Layout.t(), Screen.t() -> data)) :: data
when data: t() | simulation_data()
when data: t() | simulation_data() | data_with_flex_zone()
defp select_variant(screen_id, opts, then_fn) do
config = get_config(screen_id, opts)
selected_variant = Keyword.get(opts, :generator_variant)
Expand Down Expand Up @@ -98,6 +105,12 @@ defmodule Screens.V2.ScreenData do
layout |> resolve_paging(config) |> serialize()
end

@spec layout_to_data(Layout.t(), Screen.t()) :: data_with_flex_zone()
defp layout_to_data_with_flex_zone(layout, config) do
{layout |> resolve_paging(config) |> serialize(),
serialize_paged_slots(layout, config.app_id)}
end

@spec layout_to_simulation_data(Layout.t(), Screen.t()) :: simulation_data()
defp layout_to_simulation_data(layout, config) do
%{
Expand Down
18 changes: 14 additions & 4 deletions lib/screens_web/controllers/v2/screen_api_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule ScreensWeb.V2.ScreenApiController do
alias Screens.V2.{ScreenAudioData, ScreenData}
alias ScreensConfig.Screen

@base_response %{data: nil, disabled: false, force_reload: false}
@base_response %{data: nil, disabled: false, force_reload: false, flex_zone: nil}
digitalcora marked this conversation as resolved.
Show resolved Hide resolved
@disabled_response %{@base_response | disabled: true}
@outdated_response %{@base_response | force_reload: true}

Expand Down Expand Up @@ -84,19 +84,29 @@ defmodule ScreensWeb.V2.ScreenApiController do

response =
screen_id
|> screen_response(variant, run_all_variants?: true, update_visible_alerts?: true)
|> screen_response(variant, screen,
run_all_variants?: true,
update_visible_alerts?: true
)
|> put_extra_fields(screen_id, screen)

json(conn, response)
end
end

defp screen_response(screen_id, "all", opts) do
defp screen_response(screen_id, "all", _, opts) do
digitalcora marked this conversation as resolved.
Show resolved Hide resolved
{default, variants} = ScreenData.variants(screen_id, opts)
Map.put(%{@base_response | data: default}, :variants, variants)
end

defp screen_response(screen_id, variant, opts) do
defp screen_response(screen_id, variant, %Screen{vendor: :mercury}, opts) do
{data, flex_zone} =
ScreenData.get_with_flex_zone(screen_id, Keyword.put(opts, :generator_variant, variant))

%{@base_response | data: data, flex_zone: flex_zone}
end

defp screen_response(screen_id, variant, _, opts) do
data = ScreenData.get(screen_id, Keyword.put(opts, :generator_variant, variant))
%{@base_response | data: data}
end
Expand Down
27 changes: 27 additions & 0 deletions test/screens/v2/screen_data_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,33 @@ defmodule Screens.V2.ScreenDataTest do
end
end

describe "get_with_flex_zone/2" do
setup do
stub(@parameters, :refresh_rate, fn _app_id -> 0 end)
:ok
end

defp build_config(attrs) do
struct!(
%Screen{app_id: :test_app, app_params: %{}, device_id: "", name: "", vendor: :mercury},
attrs
)
end

test "gets widget data and all flex_zone pages for a screen ID" do
expect(@config_cache, :screen, fn "test_id" -> build_config(%{app_id: :test_app}) end)

expect(
@parameters,
:candidate_generator,
fn %Screen{app_id: :test_app}, nil -> GrayGenerator end
)

assert ScreenData.get_with_flex_zone("test_id") ==
{%{type: :normal, main: %{type: :placeholder, color: :gray}}, []}
end
end

describe "variants/2" do
setup do
stub(@parameters, :refresh_rate, fn _app_id -> 0 end)
Expand Down