implemented the LiveLocationManager as part of the user session and setup of permissions in the plist.

This commit is contained in:
Mauro Romito
2026-03-31 02:09:25 +02:00
committed by Mauro
parent 3ba636aa37
commit b31d0cad75
9 changed files with 200 additions and 6 deletions
@@ -0,0 +1,64 @@
//
// 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 CoreLocation
class LiveLocationManager: NSObject, LiveLocationManagerProtocol, CLLocationManagerDelegate {
private let clientProxy: ClientProxyProtocol
private let locationManager: CLLocationManager
private let appSettings: AppSettings
private let authorizationStatusSubject: CurrentValueSubject<CLAuthorizationStatus, Never>
var authorizationStatus: CurrentValuePublisher<CLAuthorizationStatus, Never> {
authorizationStatusSubject.asCurrentValuePublisher()
}
@MainActor
init(clientProxy: ClientProxyProtocol,
appSettings: AppSettings) {
self.clientProxy = clientProxy
self.appSettings = appSettings
// Very important, the CLLocationManager needs to be initialised on the main thread
// or the delegate functions won't be handled!
// https://developer.apple.com/documentation/corelocation/cllocationmanagerdelegate
locationManager = CLLocationManager()
authorizationStatusSubject = CurrentValueSubject(locationManager.authorizationStatus)
super.init()
locationManager.delegate = self
}
// MARK: - LiveLocationManagerProtocol
@discardableResult
func requestAlwaysAuthorizationIfPossible() -> Bool {
guard !appSettings.hasRequestedAlwaysLocationAuthorization else { return false }
appSettings.hasRequestedAlwaysLocationAuthorization = true
locationManager.requestAlwaysAuthorization()
return true
}
// MARK: - CLLocationManagerDelegate
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
// If the system resets authorization to notDetermined (e.g. after app reinstall or
// settings reset), clear the flag so we can request again.
if manager.authorizationStatus == .notDetermined {
appSettings.hasRequestedAlwaysLocationAuthorization = false
}
authorizationStatusSubject.send(manager.authorizationStatus)
}
// MARK: - Private
// TODO: Add CLLocationManager location update handling to forward updates to rooms
// TODO: Track which rooms are currently sharing live location
// TODO: Send location updates to all active rooms via clientProxy
}
@@ -0,0 +1,23 @@
//
// 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 CoreLocation
import Foundation
// sourcery: AutoMockable
protocol LiveLocationManagerProtocol: AnyObject {
/// Publishes the current "Always" location authorization status.
var authorizationStatus: CurrentValuePublisher<CLAuthorizationStatus, Never> { get }
/// Requests "Always" location authorization from the user if the system allows it.
///
/// - Returns: `true` if the request was forwarded to the system and a prompt will be shown;
/// `false` if the request was already made before and iOS would silently ignore it.
@discardableResult
func requestAlwaysAuthorizationIfPossible() -> Bool
}