implement APIs for LLS observation

This commit is contained in:
Mauro Romito
2026-04-14 12:45:10 +02:00
committed by Mauro
parent 78240ebdee
commit facba008e7
10 changed files with 237 additions and 0 deletions
@@ -10414,6 +10414,70 @@ class JoinedRoomProxyMock: JoinedRoomProxyProtocol, @unchecked Sendable {
return clearDraftThreadRootEventIDReturnValue
}
}
//MARK: - getLiveLocationSharesService
var getLiveLocationSharesServiceUnderlyingCallsCount = 0
var getLiveLocationSharesServiceCallsCount: Int {
get {
if Thread.isMainThread {
return getLiveLocationSharesServiceUnderlyingCallsCount
} else {
var returnValue: Int? = nil
DispatchQueue.main.sync {
returnValue = getLiveLocationSharesServiceUnderlyingCallsCount
}
return returnValue!
}
}
set {
if Thread.isMainThread {
getLiveLocationSharesServiceUnderlyingCallsCount = newValue
} else {
DispatchQueue.main.sync {
getLiveLocationSharesServiceUnderlyingCallsCount = newValue
}
}
}
}
var getLiveLocationSharesServiceCalled: Bool {
return getLiveLocationSharesServiceCallsCount > 0
}
var getLiveLocationSharesServiceUnderlyingReturnValue: LiveLocationSharesServiceProtocol!
var getLiveLocationSharesServiceReturnValue: LiveLocationSharesServiceProtocol! {
get {
if Thread.isMainThread {
return getLiveLocationSharesServiceUnderlyingReturnValue
} else {
var returnValue: LiveLocationSharesServiceProtocol? = nil
DispatchQueue.main.sync {
returnValue = getLiveLocationSharesServiceUnderlyingReturnValue
}
return returnValue!
}
}
set {
if Thread.isMainThread {
getLiveLocationSharesServiceUnderlyingReturnValue = newValue
} else {
DispatchQueue.main.sync {
getLiveLocationSharesServiceUnderlyingReturnValue = newValue
}
}
}
}
var getLiveLocationSharesServiceClosure: (() async -> LiveLocationSharesServiceProtocol)?
func getLiveLocationSharesService() async -> LiveLocationSharesServiceProtocol {
getLiveLocationSharesServiceCallsCount += 1
if let getLiveLocationSharesServiceClosure = getLiveLocationSharesServiceClosure {
return await getLiveLocationSharesServiceClosure()
} else {
return getLiveLocationSharesServiceReturnValue
}
}
//MARK: - startLiveLocationShare
var startLiveLocationShareDurationUnderlyingCallsCount = 0
@@ -11904,6 +11968,14 @@ class LiveLocationManagerMock: LiveLocationManagerProtocol, @unchecked Sendable
await stopLiveLocationRoomIDClosure?(roomID)
}
}
class LiveLocationSharesServiceMock: LiveLocationSharesServiceProtocol, @unchecked Sendable {
var liveLocationSharesPublisher: AnyPublisher<[LiveLocationShareProxy], Never> {
get { return underlyingLiveLocationSharesPublisher }
set(value) { underlyingLiveLocationSharesPublisher = value }
}
var underlyingLiveLocationSharesPublisher: AnyPublisher<[LiveLocationShareProxy], Never>!
}
class MediaLoaderMock: MediaLoaderProtocol, @unchecked Sendable {
//MARK: - loadMediaContentForSource
@@ -68,6 +68,8 @@ extension JoinedRoomProxyMock {
typingMembersPublisher = CurrentValueSubject([]).asCurrentValuePublisher()
identityStatusChangesPublisher = CurrentValueSubject([]).asCurrentValuePublisher()
getLiveLocationSharesServiceReturnValue = LiveLocationSharesServiceMock(.init())
updateMembersClosure = { }
setNameClosure = { _ in .success(()) }
setTopicClosure = { _ in .success(()) }
@@ -0,0 +1,20 @@
//
// Copyright 2026 Element Creations Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
import Combine
import Foundation
struct LiveLocationSharesServiceMockConfiguration {
var shares: [LiveLocationShareProxy] = []
}
extension LiveLocationSharesServiceMock {
convenience init(_ configuration: LiveLocationSharesServiceMockConfiguration = .init()) {
self.init()
liveLocationSharesPublisher = CurrentValueSubject(configuration.shares).eraseToAnyPublisher()
}
}
+6
View File
@@ -197,6 +197,12 @@ extension SDKListener: KnockRequestsListener where T == [KnockRequest] {
}
}
extension SDKListener: LiveLocationShareListener where T == [LiveLocationShareUpdate] {
func onUpdate(updates: [LiveLocationShareUpdate]) {
onUpdateClosure(updates)
}
}
extension SDKListener: ThreadListEntriesListener where T == [ThreadListUpdate] {
func onUpdate(diff: [ThreadListUpdate]) {
onUpdateClosure(diff)
@@ -0,0 +1,29 @@
//
// Copyright 2026 Element Creations Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
import Foundation
import MatrixRustSDK
struct LiveLocationShareProxy: Hashable {
let userID: String
let geoURI: GeoURI?
let timestamp: Date
let timeoutDate: Date
}
extension LiveLocationShareProxy {
init(liveLocationShare: LiveLocationShare) {
userID = liveLocationShare.userId
if let geoURI = liveLocationShare.lastLocation?.location.geoUri {
self.geoURI = GeoURI(string: geoURI)
} else {
geoURI = nil
}
timestamp = Date(timeIntervalSince1970: Double(liveLocationShare.startTs))
timeoutDate = timestamp.addingTimeInterval(Double(liveLocationShare.timeout) / 1000)
}
}
@@ -0,0 +1,72 @@
//
// Copyright 2026 Element Creations Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
import Combine
import Foundation
import MatrixRustSDK
final class LiveLocationSharesService: LiveLocationSharesServiceProtocol {
// periphery:ignore - required for instance retention in the rust codebase
private let liveLocationShares: LiveLocationShares
// periphery:ignore - required for instance retention in the rust codebase
private var observationToken: TaskHandle?
private let liveLocationSharesSubject = PassthroughSubject<[LiveLocationShareProxy], Never>()
var liveLocationSharesPublisher: AnyPublisher<[LiveLocationShareProxy], Never> {
liveLocationSharesSubject.eraseToAnyPublisher()
}
private var previousLiveLocationShares: [LiveLocationShareProxy] = []
init(liveLocationShares: LiveLocationShares) {
self.liveLocationShares = liveLocationShares
observationToken = liveLocationShares
.subscribe(listener: SDKListener { [weak self] updates in
guard let self else { return }
MXLog.info("Received live location shares update")
let updatedShares = handleLiveLocationShareUpdates(updates)
liveLocationSharesSubject.send(updatedShares)
})
}
// MARK: - Private
private func handleLiveLocationShareUpdates(_ updates: [LiveLocationShareUpdate]) -> [LiveLocationShareProxy] {
var shares = previousLiveLocationShares
for update in updates {
switch update {
case .append(let values):
shares.append(contentsOf: values.map(LiveLocationShareProxy.init))
case .clear:
shares.removeAll()
case .pushFront(let value):
shares.insert(LiveLocationShareProxy(liveLocationShare: value), at: 0)
case .pushBack(let value):
shares.append(LiveLocationShareProxy(liveLocationShare: value))
case .popFront:
shares.removeFirst()
case .popBack:
shares.removeLast()
case .insert(let index, let value):
shares.insert(LiveLocationShareProxy(liveLocationShare: value), at: Int(index))
case .set(let index, let value):
shares[Int(index)] = LiveLocationShareProxy(liveLocationShare: value)
case .remove(let index):
shares.remove(at: Int(index))
case .truncate(let length):
shares.removeSubrange(Int(length)..<shares.count)
case .reset(let values):
shares = values.map(LiveLocationShareProxy.init)
}
}
previousLiveLocationShares = shares
return shares
}
}
@@ -0,0 +1,14 @@
//
// Copyright 2026 Element Creations Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
import Combine
import Foundation
// sourcery: AutoMockable
protocol LiveLocationSharesServiceProtocol {
var liveLocationSharesPublisher: AnyPublisher<[LiveLocationShareProxy], Never> { get }
}
@@ -754,6 +754,10 @@ class JoinedRoomProxy: JoinedRoomProxyProtocol {
// MARK: - Live Location
func getLiveLocationSharesService() async -> LiveLocationSharesServiceProtocol {
await LiveLocationSharesService(liveLocationShares: room.liveLocationShares())
}
func startLiveLocationShare(duration: Duration) async -> Result<Void, RoomProxyError> {
do {
try await room.startLiveLocationShare(durationMillis: UInt64(duration.seconds * 1000))
@@ -197,6 +197,8 @@ protocol JoinedRoomProxyProtocol: RoomProxyProtocol {
// MARK: - Live Location
func getLiveLocationSharesService() async -> LiveLocationSharesServiceProtocol
func startLiveLocationShare(duration: Duration) async -> Result<Void, RoomProxyError>
func sendLiveLocation(geoURI: GeoURI) async -> Result<Void, RoomProxyError>
func stopLiveLocationShare() async -> Result<Void, RoomProxyError>