Skip to content

Commit ecfa593

Browse files
committed
Added forward/backward compatiblity for LiveView 0.18.0 #31
1 parent 74820c2 commit ecfa593

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

lib/desktop.ex

+4-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ defmodule Desktop do
6161
best_match != nil ->
6262
put_default_locale(best_match)
6363

64-
# We have seen windows return an empty string for language_code
64+
# We have seen windows return an empty string for language_code
6565
byte_size(language_code) >= 2 ->
6666
# Looking for a prefix match 'xx' == 'yy'
6767
prefix = binary_part(language_code, 0, 2)
@@ -108,4 +108,7 @@ defmodule Desktop do
108108
# :persistent_term.put(@key, locale)
109109
Application.put_env(:gettext, :default_locale, locale)
110110
end
111+
112+
@live_view_version elem(:application.get_key(:phoenix_live_view, :vsn), 1) |> List.to_string()
113+
def live_view_version(), do: @live_view_version
111114
end

lib/desktop/endpoint.ex

+10-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ defmodule Desktop.Endpoint do
66
defoverridable url: 0
77

88
def url do
9+
# Checking for pre LiveView 0.18.0
910
url =
10-
Phoenix.Config.cache(
11-
__MODULE__,
12-
:__phoenix_url__,
13-
&Phoenix.Endpoint.Supervisor.url/1
14-
)
11+
if Version.compare(Desktop.live_view_version(), "0.18.0") == :lt do
12+
Phoenix.Config.cache(
13+
__MODULE__,
14+
:__phoenix_url__,
15+
fn a -> apply(Phoenix.Endpoint.Supervisor, :url, [a]) end
16+
)
17+
else
18+
:persistent_term.get({Phoenix.Endpoint, __MODULE__}, nil).url
19+
end
1520

1621
endpoint = Module.safe_concat(__MODULE__, HTTP)
1722
String.replace(url, ":0", ":#{:ranch.get_port(endpoint)}")

lib/desktop/menu.ex

+22-9
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,28 @@ defmodule Desktop.Menu do
135135

136136
Module.put_attribute(__CALLER__.module, :is_menu_server, Keyword.get(opts, :server, true))
137137

138-
# Phoenix.LiveView.HTMLEngine vs. Phoenix.HTML.Engine
139-
quote do
140-
@behaviour Desktop.Menu
141-
import Desktop.Menu, only: [assign: 2, connected?: 1]
142-
import Phoenix.HTML, only: [sigil_e: 2, sigil_E: 2]
143-
import Phoenix.LiveView.Helpers, only: [sigil_L: 2, sigil_H: 2]
144-
alias Desktop.Menu
145-
146-
@before_compile Desktop.Menu
138+
# Checking for pre LiveView 0.18.0
139+
if Version.compare(Desktop.live_view_version(), "0.18.0") == :lt do
140+
quote do
141+
@behaviour Desktop.Menu
142+
import Desktop.Menu, only: [assign: 2, connected?: 1]
143+
import Phoenix.HTML, only: [sigil_e: 2, sigil_E: 2]
144+
import Phoenix.LiveView.Helpers, only: [sigil_L: 2, sigil_H: 2]
145+
alias Desktop.Menu
146+
147+
@before_compile Desktop.Menu
148+
end
149+
else
150+
quote do
151+
@behaviour Desktop.Menu
152+
import Desktop.Menu, only: [assign: 2, connected?: 1]
153+
import Phoenix.HTML, only: [sigil_e: 2, sigil_E: 2]
154+
import Phoenix.LiveView.Helpers, only: [sigil_L: 2]
155+
import Phoenix.Component, only: [sigil_H: 2]
156+
alias Desktop.Menu
157+
158+
@before_compile Desktop.Menu
159+
end
147160
end
148161
end
149162

0 commit comments

Comments
 (0)