From af244c109a2eae332a78c017e002f418fb7e0ea1 Mon Sep 17 00:00:00 2001 From: Peter Iakovlev Date: Tue, 5 Feb 2019 18:18:33 +0400 Subject: [PATCH] Update shared context --- Telegram-iOS/AppDelegate.swift | 32 ++++++++++++++++++-- Telegram-iOS/SharedNotificationManager.swift | 8 +++-- Telegram-iOS/SharedWakeupManager.swift | 14 ++++++--- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/Telegram-iOS/AppDelegate.swift b/Telegram-iOS/AppDelegate.swift index 69efc671d3..61e68cdbbb 100644 --- a/Telegram-iOS/AppDelegate.swift +++ b/Telegram-iOS/AppDelegate.swift @@ -643,11 +643,39 @@ private final class SharedApplicationContext { } } - let notificationManager = SharedNotificationManager(episodeId: self.episodeId, clearNotificationsManager: clearNotificationsManager, inForeground: applicationBindings.applicationInForeground, accounts: sharedContext.activeAccounts |> map { primary, accounts, _ in Array(accounts.values.map({ ($0, $0.id == primary?.id) })) }) + let notificationManager = SharedNotificationManager(episodeId: self.episodeId, clearNotificationsManager: clearNotificationsManager, inForeground: applicationBindings.applicationInForeground, accounts: sharedContext.activeAccounts |> map { primary, accounts, _ in Array(accounts.values.map({ ($0, $0.id == primary?.id) })) }, pollLiveLocationOnce: { accountId in + let _ = (self.context.get() + |> filter { + return $0 != nil + } + |> take(1) + |> deliverOnMainQueue).start(next: { context in + if let context = context, context.context.account.id == accountId { + context.context.liveLocationManager?.pollOnce() + } + }) + }) setPresentationCall = { call in notificationManager.setNotificationCall(call, strings: sharedContext.currentPresentationData.with({ $0 }).strings) } - let wakeupManager = SharedWakeupManager(activeAccounts: sharedContext.activeAccounts |> map { ($0.0, $0.1) }, inForeground: applicationBindings.applicationInForeground, hasActiveAudioSession: hasActiveAudioSession.get(), notificationManager: notificationManager, mediaManager: sharedContext.mediaManager, callManager: sharedContext.callManager) + let liveLocationPolling = self.context.get() + |> mapToSignal { context -> Signal in + if let context = context, let liveLocationManager = context.context.liveLocationManager { + let accountId = context.context.account.id + return liveLocationManager.isPolling + |> distinctUntilChanged + |> map { value -> AccountRecordId? in + if value { + return accountId + } else { + return nil + } + } + } else { + return .single(nil) + } + } + let wakeupManager = SharedWakeupManager(activeAccounts: sharedContext.activeAccounts |> map { ($0.0, $0.1) }, liveLocationPolling: liveLocationPolling, inForeground: applicationBindings.applicationInForeground, hasActiveAudioSession: hasActiveAudioSession.get(), notificationManager: notificationManager, mediaManager: sharedContext.mediaManager, callManager: sharedContext.callManager) let sharedApplicationContext = SharedApplicationContext(sharedContext: sharedContext, notificationManager: notificationManager, wakeupManager: wakeupManager) self.sharedContextPromise.set( accountManager.transaction { transaction -> (SharedApplicationContext, LoggingSettings) in diff --git a/Telegram-iOS/SharedNotificationManager.swift b/Telegram-iOS/SharedNotificationManager.swift index 3c97781ab6..49516d322d 100644 --- a/Telegram-iOS/SharedNotificationManager.swift +++ b/Telegram-iOS/SharedNotificationManager.swift @@ -23,6 +23,7 @@ final class SharedNotificationManager { private let episodeId: UInt32 private let clearNotificationsManager: ClearNotificationsManager + private let pollLiveLocationOnce: (AccountRecordId) -> Void private var inForeground: Bool = false private var inForegroundDisposable: Disposable? @@ -34,11 +35,12 @@ final class SharedNotificationManager { private var pollStateContexts: [AccountRecordId: PollStateContext] = [:] - init(episodeId: UInt32, clearNotificationsManager: ClearNotificationsManager, inForeground: Signal, accounts: Signal<[(Account, Bool)], NoError>) { + init(episodeId: UInt32, clearNotificationsManager: ClearNotificationsManager, inForeground: Signal, accounts: Signal<[(Account, Bool)], NoError>, pollLiveLocationOnce: @escaping (AccountRecordId) -> Void) { assert(Queue.mainQueue().isCurrent()) self.episodeId = episodeId self.clearNotificationsManager = clearNotificationsManager + self.pollLiveLocationOnce = pollLiveLocationOnce self.inForegroundDisposable = (inForeground |> deliverOnMainQueue).start(next: { [weak self] value in @@ -348,7 +350,9 @@ final class SharedNotificationManager { } } if isLocationPolling { - //addedWakeups.insert(.backgroundLocation) + if !self.inForeground || !isCurrent { + self.pollLiveLocationOnce(account.id) + } } if let readMessageId = readMessageId { diff --git a/Telegram-iOS/SharedWakeupManager.swift b/Telegram-iOS/SharedWakeupManager.swift index 23d4b33310..1f2c0a2014 100644 --- a/Telegram-iOS/SharedWakeupManager.swift +++ b/Telegram-iOS/SharedWakeupManager.swift @@ -49,7 +49,7 @@ final class SharedWakeupManager { private var accountsAndTasks: [(Account, Bool, AccountTasks)] = [] - init(activeAccounts: Signal<(primary: Account?, accounts: [AccountRecordId: Account]), NoError>, inForeground: Signal, hasActiveAudioSession: Signal, notificationManager: SharedNotificationManager, mediaManager: MediaManager, callManager: PresentationCallManager?) { + init(activeAccounts: Signal<(primary: Account?, accounts: [AccountRecordId: Account]), NoError>, liveLocationPolling: Signal, inForeground: Signal, hasActiveAudioSession: Signal, notificationManager: SharedNotificationManager, mediaManager: MediaManager, callManager: PresentationCallManager?) { assert(Queue.mainQueue().isCurrent()) self.inForegroundDisposable = (inForeground @@ -102,9 +102,15 @@ final class SharedWakeupManager { } |> distinctUntilChanged - return combineLatest(queue: .mainQueue(), account.importantTasksRunning, notificationManager.isPollingState(accountId: account.id), hasActiveAudio, hasActiveCalls) - |> map { importantTasksRunning, isPollingState, hasActiveAudio, hasActiveCalls -> (Account, Bool, AccountTasks) in - return (account, primary?.id == account.id, AccountTasks(stateSynchronization: isPollingState, importantTasks: importantTasksRunning, backgroundLocation: false, backgroundDownloads: false, backgroundAudio: hasActiveAudio, activeCalls: hasActiveCalls)) + let hasActiveLiveLocationPolling = liveLocationPolling + |> map { id in + return id == account.id + } + |> distinctUntilChanged + + return combineLatest(queue: .mainQueue(), account.importantTasksRunning, notificationManager.isPollingState(accountId: account.id), hasActiveAudio, hasActiveCalls, hasActiveLiveLocationPolling) + |> map { importantTasksRunning, isPollingState, hasActiveAudio, hasActiveCalls, hasActiveLiveLocationPolling -> (Account, Bool, AccountTasks) in + return (account, primary?.id == account.id, AccountTasks(stateSynchronization: isPollingState, importantTasks: importantTasksRunning, backgroundLocation: hasActiveLiveLocationPolling, backgroundDownloads: false, backgroundAudio: hasActiveAudio, activeCalls: hasActiveCalls)) } } return combineLatest(signals)