Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent crash on empty list #2113

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions lib/screens/v2/widget_instance/reconstructed_alert.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,10 @@ defmodule Screens.V2.WidgetInstance.ReconstructedAlert do
list_of_directions_and_routes =
informed_entities
|> Enum.map(fn entity -> get_direction_and_route_from_entity(entity, location) end)
|> Enum.filter(& &1)
|> Enum.reject(&is_nil/1)
|> Enum.uniq()

{direction_id, route_id} =
if length(list_of_directions_and_routes) == 1 do
hd(list_of_directions_and_routes)

# If there are multiple route ids in that informed entities list, then the alert includes branching
else
select_direction_and_route(list_of_directions_and_routes)
end
{direction_id, route_id} = select_direction_and_route(list_of_directions_and_routes)

cond do
# When the alert is non-directional but the station is at the boundary:
Expand Down Expand Up @@ -230,17 +223,12 @@ defmodule Screens.V2.WidgetInstance.ReconstructedAlert do
do: {direction_id, route}

# Select 1 direction + route from this list of directions + routes for multiple branches
defp select_direction_and_route(list_of_directions_and_routes) do
direction_id =
list_of_directions_and_routes
|> hd()
|> elem(0)

case list_of_directions_and_routes do
[{direction_id, "Red" <> _} | _] -> {direction_id, "Red"}
_ -> {direction_id, "Green-trunk"}
end
end
defp select_direction_and_route([]), do: {nil, nil}
defp select_direction_and_route([direction_and_route]), do: direction_and_route

# If there are multiple route ids in that informed entities list, then the alert includes branching
defp select_direction_and_route([{direction_id, "Red" <> _} | _]), do: {direction_id, "Red"}
defp select_direction_and_route([{direction_id, _} | _]), do: {direction_id, "Green-trunk"}

defp get_route_pills(t, location \\ nil)

Expand Down
Loading