|
| 1 | +// |
| 2 | +// Copyright 2022 The Matrix.org Foundation C.I.C |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +import Foundation |
| 18 | + |
| 19 | +/// MXBeaconAggregations aggregates related beacon info events and beacon info events into a summary object MXBeaconInfoSummary |
| 20 | +@objcMembers |
| 21 | +public class MXBeaconAggregations: NSObject { |
| 22 | + |
| 23 | + // MARK: - Properties |
| 24 | + |
| 25 | + private unowned let session: MXSession |
| 26 | + |
| 27 | + private var listeners: [MXBeaconInfoSummaryListener] = [] |
| 28 | + |
| 29 | + private var beaconInfoSummaryStore: MXBeaconInfoSummaryStoreProtocol |
| 30 | + |
| 31 | + // MARK: - Setup |
| 32 | + |
| 33 | + public init(session: MXSession, store: MXBeaconInfoSummaryStoreProtocol) { |
| 34 | + self.session = session |
| 35 | + self.beaconInfoSummaryStore = store |
| 36 | + |
| 37 | + super.init() |
| 38 | + } |
| 39 | + |
| 40 | + // MARK: - Public |
| 41 | + |
| 42 | + /// Get MXBeaconInfoSummary from the first beacon info event id |
| 43 | + public func beaconInfoSummary(for eventId: String, inRoomWithId roomId: String) -> MXBeaconInfoSummaryProtocol? { |
| 44 | + return self.beaconInfoSummaryStore.getBeaconInfoSummary(withIdentifier: eventId, inRoomWithId: roomId) |
| 45 | + } |
| 46 | + |
| 47 | + /// Update a MXBeaconInfoSummary device id that belongs to the current user. |
| 48 | + /// Enables to recognize that a beacon info has been started on the device |
| 49 | + public func updateBeaconInfoSummary(with eventId: String, deviceId: String, inRoomWithId roomId: String) { |
| 50 | + guard let beaconInfoSummary = self.beaconInfoSummaryStore.getBeaconInfoSummary(withIdentifier: eventId, inRoomWithId: roomId) else { |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + guard beaconInfoSummary.userId == session.myUserId else { |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + if beaconInfoSummary.updateWithDeviceId(deviceId) { |
| 59 | + self.notifyBeaconInfoSummaryListeners(ofRoomWithId: roomId, beaconInfoSummary: beaconInfoSummary) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public func clearData(inRoomWithId roomId: String) { |
| 64 | + // TODO: Notify data clear |
| 65 | + self.beaconInfoSummaryStore.deleteAllBeaconInfoSummaries(inRoomWithId: roomId) |
| 66 | + } |
| 67 | + |
| 68 | + // MARK: Data update |
| 69 | + |
| 70 | + public func handleBeacon(event: MXEvent) { |
| 71 | + guard let roomId = event.roomId else { |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + guard let beacon = MXBeacon(mxEvent: event) else { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + guard let beaconInfoSummary = self.getBeaconInfoSummary(withIdentifier: beacon.beaconInfoEventId, inRoomWithId: roomId), self.canAddBeacon(beacon, to: beaconInfoSummary) else { |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + if beaconInfoSummary.updateWithLastBeacon(beacon) { |
| 84 | + self.notifyBeaconInfoSummaryListeners(ofRoomWithId: roomId, beaconInfoSummary: beaconInfoSummary) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public func handleBeaconInfo(event: MXEvent) { |
| 89 | + guard let roomId = event.roomId else { |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + guard let beaconInfo = MXBeaconInfo(mxEvent: event) else { |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + self.addOrUpdateBeaconInfo(beaconInfo, inRoomWithId: roomId) |
| 98 | + } |
| 99 | + |
| 100 | + // MARK: Data update listener |
| 101 | + |
| 102 | + public func listenToBeaconInfoSummaryUpdateInRoom(withId roomId: String, handler: @escaping (MXBeaconInfoSummaryProtocol) -> Void) -> Any? { |
| 103 | + let listener = MXBeaconInfoSummaryListener(roomId: roomId, notificationHandler: handler) |
| 104 | + |
| 105 | + listeners.append(listener) |
| 106 | + |
| 107 | + return listener |
| 108 | + } |
| 109 | + |
| 110 | + public func removeListener(_ listener: Any) { |
| 111 | + guard let beaconInfoSummaryListener = listener as? MXBeaconInfoSummaryListener else { |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + listeners.removeAll(where: { $0 === beaconInfoSummaryListener }) |
| 116 | + } |
| 117 | + |
| 118 | + // MARK: - Private |
| 119 | + |
| 120 | + private func addOrUpdateBeaconInfo(_ beaconInfo: MXBeaconInfo, inRoomWithId roomId: String) { |
| 121 | + |
| 122 | + guard let eventId = beaconInfo.originalEvent?.eventId else { |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + var beaconInfoSummary: MXBeaconInfoSummary? |
| 127 | + |
| 128 | + if let existingBeaconInfoSummary = self.getBeaconInfoSummary(withIdentifier: eventId, inRoomWithId: roomId) { |
| 129 | + |
| 130 | + // If beacon info is older than existing one, do not take it into account |
| 131 | + if beaconInfo.timestamp > existingBeaconInfoSummary.beaconInfo.timestamp { |
| 132 | + existingBeaconInfoSummary.updateWithBeaconInfo(beaconInfo) |
| 133 | + beaconInfoSummary = existingBeaconInfoSummary |
| 134 | + } |
| 135 | + } else { |
| 136 | + beaconInfoSummary = MXBeaconInfoSummary(beaconInfo: beaconInfo) |
| 137 | + } |
| 138 | + |
| 139 | + if let beaconInfoSummary = beaconInfoSummary { |
| 140 | + self.beaconInfoSummaryStore.addOrUpdateBeaconInfoSummary(beaconInfoSummary, inRoomWithId: roomId) |
| 141 | + |
| 142 | + self.notifyBeaconInfoSummaryListeners(ofRoomWithId: roomId, beaconInfoSummary: beaconInfoSummary) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private func canAddBeacon(_ beacon: MXBeacon, to beaconInfoSummary: MXBeaconInfoSummary) -> Bool { |
| 147 | + |
| 148 | + guard beaconInfoSummary.hasStopped == false, beaconInfoSummary.hasExpired == false, |
| 149 | + beacon.timestamp < beaconInfoSummary.expiryTimestamp else { |
| 150 | + return false |
| 151 | + } |
| 152 | + |
| 153 | + if let lastBeacon = beaconInfoSummary.lastBeacon, beacon.timestamp < lastBeacon.timestamp { |
| 154 | + return false |
| 155 | + } |
| 156 | + |
| 157 | + return true |
| 158 | + } |
| 159 | + |
| 160 | + private func notifyBeaconInfoSummaryListeners(ofRoomWithId roomId: String, beaconInfoSummary: MXBeaconInfoSummary) { |
| 161 | + |
| 162 | + for listener in listeners where listener.roomId == roomId { |
| 163 | + listener.notificationHandler(beaconInfoSummary) |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + /// Get MXBeaconInfoSummary class instead of MXBeaconInfoSummaryProtocol to have access to internal methods |
| 168 | + private func getBeaconInfoSummary(withIdentifier identifier: String, inRoomWithId roomId: String) -> MXBeaconInfoSummary? { |
| 169 | + return self.beaconInfoSummaryStore.getBeaconInfoSummary(withIdentifier: identifier, inRoomWithId: roomId) |
| 170 | + } |
| 171 | +} |
0 commit comments