From d912ae289d28f5bf3173d3fe612c4dbdaf58da8a Mon Sep 17 00:00:00 2001 From: Pim Kunis Date: Thu, 24 Oct 2019 21:07:26 +0200 Subject: [PATCH] Put status in button, and gray it out if morse is in progress. Fix hint button. --- morse/lib/morse/server.ex | 18 +++++++++++++--- ui/lib/ui_web/live/morse_live.ex | 8 ++++++- ui/lib/ui_web/templates/page/morse.html.leex | 22 ++++++++++---------- 3 files changed, 33 insertions(+), 15 deletions(-) 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 @@

nl:
Druk op de Start knop hieronder om UVWXYZ te vinden. Je kunt dan de geocache vinden op N 52° 40.UVW' E 004° 53.XYZ'

- + <%= if @in_progress? do %> + + <% else %> + + <% end %>
-

Response status:

-

Progress:

<%= @progress %>%
@@ -15,16 +17,14 @@ en: check your coordinate on GeoChecker.com nl: controleer je coördinaat op GeoChecker.

- -