[ios] fix Live activity widget issue when it recreates on every app opening

Description: https://github.com/organicmaps/organicmaps/issues/10222
Also this commit fixes the issue when the Live Activity is Disabled in the settings then the start recording fails

Signed-off-by: Kiryl Kaveryn <kirylkaveryn@gmail.com>
This commit is contained in:
Kiryl Kaveryn 2025-02-10 12:50:02 +04:00 committed by Roman Tsisyk
parent d8a594855a
commit 19701d83e6

View file

@ -59,7 +59,6 @@ final class TrackRecordingManager: NSObject {
self.trackRecorder = trackRecorder
self.activityManager = activityManager
super.init()
subscribeOnAppLifecycleEvents()
}
// MARK: - Public methods
@ -96,39 +95,11 @@ final class TrackRecordingManager: NSObject {
// MARK: - Private methods
private func checkIsLocationEnabled() throws {
guard !LocationManager.isLocationProhibited() else {
if LocationManager.isLocationProhibited() {
throw TrackRecordingError.locationIsProhibited
}
}
// MARK: - Handle lifecycle events
private func subscribeOnAppLifecycleEvents() {
NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: UIApplication.willResignActiveNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(prepareForTermination), name: UIApplication.willTerminateNotification, object: nil)
}
@objc
private func willResignActive() {
guard let activityManager, recordingState == .active else { return }
do {
try activityManager.start(with: trackRecordingInfo ?? .empty())
} catch {
handleError(error)
}
}
@objc
private func willEnterForeground() {
activityManager?.stop()
}
@objc
private func prepareForTermination() {
activityManager?.stop()
}
// MARK: - Handle track recording process
private func subscribeOnTrackRecordingProgressUpdates() {
@ -155,6 +126,7 @@ final class TrackRecordingManager: NSObject {
subscribeOnTrackRecordingProgressUpdates()
trackRecorder.startTrackRecording()
notifyObservers()
try? activityManager?.start(with: trackRecordingInfo ?? .empty())
case .active:
break
}