Put status in button, and gray it out if morse is in progress.

Fix hint button.
This commit is contained in:
Pim Kunis 2019-10-24 21:07:26 +02:00
parent 2cb9946faf
commit d912ae289d
3 changed files with 33 additions and 15 deletions

View file

@ -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