diff --git a/lib/localiser/localisation/tag/filter.ex b/lib/localiser/localisation/tag/filter.ex index f79d26d..bdd74c2 100644 --- a/lib/localiser/localisation/tag/filter.ex +++ b/lib/localiser/localisation/tag/filter.ex @@ -93,12 +93,23 @@ defmodule Localiser.Localisation.Tag.Filter do end end - # Room geometry changed: reload the rooms cache so containment checks stay accurate. @impl true - def handle_info({event, _}, state) when event in [:room_created, :room_updated] do + def handle_info({:room_created, _room}, state) do {:noreply, %{state | rooms: load_rooms()}} end + def handle_info({:room_updated, updated_room}, state) do + new_rooms = load_rooms() + old_room = Enum.find(state.rooms, &(&1.id == updated_room.id)) + + if geometry_changed?(old_room, updated_room) do + {:ok, new_filter_state} = state.filter_module.init([], rooms: new_rooms) + {:noreply, %{state | rooms: new_rooms, filter_state: new_filter_state}} + else + {:noreply, %{state | rooms: new_rooms}} + end + end + def handle_info({:room_deleted, _room_id, _floor_id}, state) do {:noreply, %{state | rooms: load_rooms()}} end @@ -131,6 +142,12 @@ defmodule Localiser.Localisation.Tag.Filter do |> Enum.flat_map(& &1.rooms) end + defp geometry_changed?(nil, _), do: true + defp geometry_changed?(old, new) do + old.x != new.x or old.y != new.y or + old.width != new.width or old.height != new.height + end + defp find_room(rooms, x, y) do Enum.find(rooms, fn room -> ox = room.x || 0.0