Skip to content

Commit

Permalink
Add fancy progress bar for devices list (#1791)
Browse files Browse the repository at this point in the history
We were already receiving these events, might as well display the progress
  • Loading branch information
lawik authored Jan 22, 2025
1 parent adfe3c6 commit a1cfeff
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/nerves_hub_web/channels/device_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,10 @@ defmodule NervesHubWeb.DeviceChannel do
end

def handle_in("fwup_progress", %{"value" => percent}, %{assigns: %{device: device}} = socket) do
device_internal_broadcast!(socket, device, "fwup_progress", %{percent: percent})
device_internal_broadcast!(socket, device, "fwup_progress", %{
device_id: device.id,
percent: percent
})

# if this is the first fwup we see, then mark it as an update attempt
if socket.assigns[:update_started?] do
Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub_web/live/devices/index-new.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
</tr>
</thead>
<tbody>
<tr :for={device <- @devices} class={["border-b border-zinc-800", device.id in @selected_devices && "selected-row"]}>
<tr :for={device <- @devices} class={["border-b border-zinc-800 relative", device.id in @selected_devices && "selected-row"]} style={progress_style(@progress[device.id])}>
<td class="checkbox">
<input
id={"checkbox-device-#{device.id}"}
Expand Down
37 changes: 37 additions & 0 deletions lib/nerves_hub_web/live/devices/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ defmodule NervesHubWeb.Live.Devices.Index do
|> assign(:currently_filtering, false)
|> assign(:selected_devices, [])
|> assign(:target_product, nil)
|> assign(:progress, %{})
|> assign(:valid_tags, true)
|> assign(:device_tags, "")
|> assign(:total_entries, 0)
Expand All @@ -113,6 +114,7 @@ defmodule NervesHubWeb.Live.Devices.Index do
|> assign(:sort_direction, Map.get(unsigned_params, "sort_direction", "asc"))
|> assign(:current_filters, filters)
|> assign(:paginate_opts, pagination_opts)
|> assign(:progress, socket.assigns[:progress] || %{})
|> assign(:currently_filtering, filters != @default_filters)
|> assign(:params, unsigned_params)
|> assign_display_devices()
Expand Down Expand Up @@ -402,6 +404,28 @@ defmodule NervesHubWeb.Live.Devices.Index do
update_device_statuses(socket, payload)
end

def handle_info(
%Broadcast{
event: "fwup_progress",
payload: %{device_id: device_id, percent: percent}
},
socket
)
when percent > 99 do
socket
|> assign(:progress, Map.delete(socket.assigns.progress, device_id))
|> noreply()
end

def handle_info(
%Broadcast{event: "fwup_progress", payload: %{device_id: device_id, percent: percent}},
socket
) do
socket
|> assign(:progress, Map.put(socket.assigns.progress, device_id, percent))
|> noreply()
end

# Unknown broadcasts get ignored, likely from the device:id:internal channel
def handle_info(%Broadcast{}, socket) do
{:noreply, socket}
Expand Down Expand Up @@ -651,4 +675,17 @@ defmodule NervesHubWeb.Live.Devices.Index do
)
end
end

defp progress_style(nil) do
nil
end

defp progress_style(progress) do
"""
background-repeat: no-repeat, no-repeat;
background-image: linear-gradient(90deg, rgba(16, 185, 129, 1.00) 0%, rgba(16, 185, 129, 1.0) 100%),
radial-gradient(ellipse 80% 80% at 50% -10%, rgba(16, 185, 129, 0.3) 0, rgba(16, 185, 129, 0.0) 80%);
background-size: #{progress}% 1px, #{progress}% 100%;
"""
end
end

0 comments on commit a1cfeff

Please sign in to comment.