diff --git a/morse/lib/morse/server.ex b/morse/lib/morse/server.ex index a8975d8..a27bf4d 100644 --- a/morse/lib/morse/server.ex +++ b/morse/lib/morse/server.ex @@ -17,6 +17,10 @@ defmodule Morse.Server do GenServer.call(__MODULE__, :progress) end + def in_progress? do + GenServer.call(__MODULE__, :in_progress?) + end + @impl true def init(state) do {:ok, state} @@ -24,11 +28,11 @@ defmodule Morse.Server do @impl true def handle_call(:start, _from, {pid, _progress} = state) do - if pid == nil or not Process.alive?(pid) do + if worker_alive?(pid) do + {:reply, {:error, :already_started}, state} + else pid = spawn(&Morse.Worker.signal/0) {:reply, :ok, {pid, 0}} - else - {:reply, {:error, :already_started}, state} end end @@ -36,6 +40,10 @@ defmodule Morse.Server do {:reply, progress, state} end + def handle_call(:in_progress?, _from, {pid, _progress} = state) do + {:reply, worker_alive?(pid), state} + end + @impl true def handle_cast({:progress, new_progress}, {pid, _progress}) do apply(pubsub(), :broadcast, [Ui.PubSub, "morse_progress", new_progress]) @@ -45,4 +53,8 @@ defmodule Morse.Server do defp pubsub do Application.fetch_env!(:morse, :pubsub) end + + defp worker_alive?(pid) do + pid != nil and Process.alive?(pid) + end end diff --git a/ui/lib/ui_web/live/morse_live.ex b/ui/lib/ui_web/live/morse_live.ex index e6de0cb..5556730 100644 --- a/ui/lib/ui_web/live/morse_live.ex +++ b/ui/lib/ui_web/live/morse_live.ex @@ -17,13 +17,19 @@ defmodule UiWeb.MorseLive do {:noreply, socket} end + def handle_event("toggle_hint", _value, socket) do + {:noreply, update(socket, :hints_visible, &(not &1))} + end + def handle_info(progress, socket) do - {:noreply, assign(socket, progress: progress)} + {:noreply, assign(socket, progress: progress, in_progress?: Morse.Server.in_progress?())} end defp default_assigns do [ progress: Morse.Server.progress(), + in_progress?: Morse.Server.in_progress?(), + hints_visible: false ] end end diff --git a/ui/lib/ui_web/templates/page/morse.html.leex b/ui/lib/ui_web/templates/page/morse.html.leex index c1826cb..80efaaf 100644 --- a/ui/lib/ui_web/templates/page/morse.html.leex +++ b/ui/lib/ui_web/templates/page/morse.html.leex @@ -3,10 +3,12 @@