Skip to content

Commit

Permalink
Allow entry of e.g. .830, disable other keyboard events while band se…
Browse files Browse the repository at this point in the history
…lector dialog is open
  • Loading branch information
tonyc committed Aug 13, 2023
1 parent 135ea1c commit ddb1f2a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/open890/frequency_entry_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ defmodule Open890.FrequencyEntryParser do
["" = _mhz | [khz] = _rest] ->
pad_mhz_khz("0", khz)

[mhz | [khz]] ->
[mhz | rest] ->
khz = rest |> Enum.join()
pad_mhz_khz(mhz, khz)
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/open890/keyboard_entry_state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ defmodule Open890.KeyboardEntryState do

defmodule ClearMarkers do
end

defmodule DirectFrequencyEntry do
end
end
34 changes: 24 additions & 10 deletions lib/open890_web/live/radio.ex
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,12 @@ defmodule Open890Web.Live.Radio do
end

def handle_event("toggle_band_selector", _params, socket) do
socket = assign(socket, :display_band_selector, !socket.assigns.display_band_selector)
socket = if socket.assigns.display_band_selector do
close_modals(socket)
else
open_band_selector(socket)
end

{:noreply, socket}
end

Expand Down Expand Up @@ -319,8 +324,6 @@ defmodule Open890Web.Live.Radio do

def handle_event("window_keyup", %{"key" => key} = params, socket) do
Logger.debug("live/radio.ex: window_keyup: #{inspect(params)}")
Logger.info("KeyboardEntryState: #{socket.assigns.keyboard_entry_state}")

socket = handle_keyboard_state(socket.assigns.keyboard_entry_state, key, socket)

{:noreply, socket}
Expand Down Expand Up @@ -473,7 +476,18 @@ defmodule Open890Web.Live.Radio do
end

defp close_modals(socket) do
socket |> assign(display_band_selector: false, display_screen_id: 0)
socket
|> assign(%{
display_band_selector: false,
keyboard_entry_state: KeyboardEntryState.Normal,
display_screen_id: 0
})
end

defp open_band_selector(socket) do
socket
|> assign(:keyboard_entry_state, KeyboardEntryState.DirectFrequencyEntry)
|> assign(:display_band_selector, true)
end

def radio_classes(debug \\ false) do
Expand Down Expand Up @@ -510,6 +524,11 @@ defmodule Open890Web.Live.Radio do
end
end

defp handle_keyboard_state(KeyboardEntryState.DirectFrequencyEntry, _key, socket) do
# FIXME: handle ESC here to close the modal?
socket
end

defp handle_keyboard_state(KeyboardEntryState.Normal, key, socket) do
radio_state = socket.assigns.radio_state
conn = socket.assigns.radio_connection
Expand Down Expand Up @@ -552,12 +571,7 @@ defmodule Open890Web.Live.Radio do
socket

"Enter" ->
if !socket.assigns.display_band_selector do
socket
|> assign(:display_band_selector, true)
else
socket
end
open_band_selector(socket)

_ ->
socket
Expand Down
7 changes: 6 additions & 1 deletion lib/open890_web/live/radio.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@
<div class="ui grid">
<div class="six wide column">
<div class="">
<input id="direct-frequency-entry-field" type="text" name="freq" value={format_raw_frequency(@radio_state.active_frequency)} />
<input
id="direct-frequency-entry-field"
type="text"
name="freq"
value={format_raw_frequency(@radio_state.active_frequency)}
/>
</div>
</div>

Expand Down
8 changes: 6 additions & 2 deletions test/frequency_entry_parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ defmodule Open890.FrequencyEntryParserTest do
end

test "parses frequency over 10 mhz with decimal point" do
assert Parser.parse("14.2") == "00014200000"
assert Parser.parse("14.2") == "00014200000"
assert Parser.parse("14.234") == "00014234000"
assert Parser.parse("50.") == "00050000000"
assert Parser.parse("50.") == "00050000000"
end

test "parses frequency over 10 mhz with several decimal points" do
assert Parser.parse("14.234.567") == "00014234567"
end

test "parses frequency under 10 mhz with decimal point" do
Expand Down

0 comments on commit ddb1f2a

Please sign in to comment.