From 5a896d94b605933ba2faaa7db9da0dbbb840514e Mon Sep 17 00:00:00 2001 From: Peter <> Date: Fri, 1 Feb 2019 20:20:20 +0400 Subject: [PATCH] Move legacy preferences --- TelegramUI.xcodeproj/project.pbxproj | 4 + TelegramUI/CallController.swift | 14 +-- TelegramUI/CallControllerNode.swift | 10 +- TelegramUI/MediaManager.swift | 7 ++ TelegramUI/PresentationCall.swift | 2 +- TelegramUI/SharedAccountContext.swift | 17 ++- TelegramUI/UpgradedAccounts.swift | 145 ++++++++++++++++++++++++++ 7 files changed, 179 insertions(+), 20 deletions(-) create mode 100644 TelegramUI/UpgradedAccounts.swift diff --git a/TelegramUI.xcodeproj/project.pbxproj b/TelegramUI.xcodeproj/project.pbxproj index b2f24cbc4d..303ffbf216 100644 --- a/TelegramUI.xcodeproj/project.pbxproj +++ b/TelegramUI.xcodeproj/project.pbxproj @@ -161,6 +161,7 @@ D0147BA7206E8B4F00E40378 /* SecureIdAuthAcceptNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0147BA6206E8B4F00E40378 /* SecureIdAuthAcceptNode.swift */; }; D0147BA9206EA35000E40378 /* SecureIdDocumentGalleryController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0147BA8206EA35000E40378 /* SecureIdDocumentGalleryController.swift */; }; D0147BAB206EA6C100E40378 /* SecureIdDocumentImageGalleryItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0147BAA206EA6C100E40378 /* SecureIdDocumentImageGalleryItem.swift */; }; + D017734C22049BF800DA06A7 /* UpgradedAccounts.swift in Sources */ = {isa = PBXBuildFile; fileRef = D017734B22049BF800DA06A7 /* UpgradedAccounts.swift */; }; D01776B31F1D69A80044446D /* RadialStatusNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = D01776B21F1D69A80044446D /* RadialStatusNode.swift */; }; D01776B51F1D6CCC0044446D /* RadialStatusContentNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = D01776B41F1D6CCC0044446D /* RadialStatusContentNode.swift */; }; D01776B81F1D6FB30044446D /* RadialProgressContentNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = D01776B71F1D6FB30044446D /* RadialProgressContentNode.swift */; }; @@ -1324,6 +1325,7 @@ D01749541E1082770057C89A /* StoredMessageFromSearchPeer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoredMessageFromSearchPeer.swift; sourceTree = ""; }; D01749561E1087CC0057C89A /* ChatBotStartInputPanelNode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChatBotStartInputPanelNode.swift; sourceTree = ""; }; D01749611E11DB240057C89A /* NetworkStatusTitleView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkStatusTitleView.swift; sourceTree = ""; }; + D017734B22049BF800DA06A7 /* UpgradedAccounts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpgradedAccounts.swift; sourceTree = ""; }; D01776B21F1D69A80044446D /* RadialStatusNode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RadialStatusNode.swift; sourceTree = ""; }; D01776B41F1D6CCC0044446D /* RadialStatusContentNode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RadialStatusContentNode.swift; sourceTree = ""; }; D01776B71F1D6FB30044446D /* RadialProgressContentNode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RadialProgressContentNode.swift; sourceTree = ""; }; @@ -2738,6 +2740,7 @@ D02B198921F1DA9E0094A764 /* SharedAccountContext.swift */, D05811931DD5F9380057C769 /* AccountContext.swift */, D02B198D21FA453F0094A764 /* AccountUserInterface.swift */, + D017734B22049BF800DA06A7 /* UpgradedAccounts.swift */, ); name = "Account Management"; sourceTree = ""; @@ -5592,6 +5595,7 @@ D0EC6D9A1EB9F58900EBF1C3 /* ChatMessageReplyInfoNode.swift in Sources */, D0FE4DE41F0AEBB900E8A0B3 /* SharedVideoContextManager.swift in Sources */, D0EC6D9B1EB9F58900EBF1C3 /* ChatMessageStickerItemNode.swift in Sources */, + D017734C22049BF800DA06A7 /* UpgradedAccounts.swift in Sources */, D0EC6D9C1EB9F58900EBF1C3 /* ChatMessageInstantVideoItemNode.swift in Sources */, D0EC6D9D1EB9F58900EBF1C3 /* ChatMessageTextBubbleContentNode.swift in Sources */, D0E9BA2B1F0557A600F079A4 /* STPFormEncoder.m in Sources */, diff --git a/TelegramUI/CallController.swift b/TelegramUI/CallController.swift index b6af206ac1..c4adcc34dd 100644 --- a/TelegramUI/CallController.swift +++ b/TelegramUI/CallController.swift @@ -15,7 +15,8 @@ public final class CallController: ViewController { return self._ready } - private let context: AccountContext + private let sharedContext: SharedAccountContext + private let account: Account public let call: PresentationCall private var presentationData: PresentationData @@ -32,11 +33,12 @@ public final class CallController: ViewController { private var audioOutputStateDisposable: Disposable? private var audioOutputState: ([AudioSessionOutput], AudioSessionOutput?)? - public init(context: AccountContext, call: PresentationCall) { - self.context = context + public init(sharedContext: SharedAccountContext, account: Account, call: PresentationCall) { + self.sharedContext = sharedContext + self.account = account self.call = call - self.presentationData = context.sharedContext.currentPresentationData.with { $0 } + self.presentationData = sharedContext.currentPresentationData.with { $0 } super.init(navigationBarPresentationData: nil) @@ -89,7 +91,7 @@ public final class CallController: ViewController { } override public func loadDisplayNode() { - self.displayNode = CallControllerNode(context: self.context, presentationData: self.presentationData, statusBar: self.statusBar, debugInfo: self.call.debugInfo(), shouldStayHiddenUntilConnection: !self.call.isOutgoing && self.call.isIntegratedWithCallKit) + self.displayNode = CallControllerNode(sharedContext: self.sharedContext, account: self.account, presentationData: self.presentationData, statusBar: self.statusBar, debugInfo: self.call.debugInfo(), shouldStayHiddenUntilConnection: !self.call.isOutgoing && self.call.isIntegratedWithCallKit) self.displayNodeDidLoad() self.controllerNode.toggleMute = { [weak self] in @@ -167,7 +169,7 @@ public final class CallController: ViewController { self?.presentingViewController?.dismiss(animated: false, completion: nil) } - self.peerDisposable = (context.account.postbox.peerView(id: self.call.peerId) + self.peerDisposable = (self.account.postbox.peerView(id: self.call.peerId) |> deliverOnMainQueue).start(next: { [weak self] view in if let strongSelf = self { if let peer = view.peers[view.peerId] { diff --git a/TelegramUI/CallControllerNode.swift b/TelegramUI/CallControllerNode.swift index 3d23899106..47f34be8dc 100644 --- a/TelegramUI/CallControllerNode.swift +++ b/TelegramUI/CallControllerNode.swift @@ -8,7 +8,8 @@ import SwiftSignalKit import TelegramUIPrivateModule final class CallControllerNode: ASDisplayNode { - private let context: AccountContext + private let sharedContext: SharedAccountContext + private let account: Account private let statusBar: StatusBar @@ -52,8 +53,9 @@ final class CallControllerNode: ASDisplayNode { var back: (() -> Void)? var dismissedInteractively: (() -> Void)? - init(context: AccountContext, presentationData: PresentationData, statusBar: StatusBar, debugInfo: Signal<(String, String), NoError>, shouldStayHiddenUntilConnection: Bool = false) { - self.context = context + init(sharedContext: SharedAccountContext, account: Account, presentationData: PresentationData, statusBar: StatusBar, debugInfo: Signal<(String, String), NoError>, shouldStayHiddenUntilConnection: Bool = false) { + self.sharedContext = sharedContext + self.account = account self.presentationData = presentationData self.statusBar = statusBar self.debugInfo = debugInfo @@ -152,7 +154,7 @@ final class CallControllerNode: ASDisplayNode { self.peer = peer if let peerReference = PeerReference(peer), !peer.profileImageRepresentations.isEmpty { let representations: [ImageRepresentationWithReference] = peer.profileImageRepresentations.map({ ImageRepresentationWithReference(representation: $0, reference: .avatar(peer: peerReference, resource: $0.resource)) }) - self.imageNode.setSignal(chatAvatarGalleryPhoto(account: self.context.account, representations: representations, autoFetchFullSize: true)) + self.imageNode.setSignal(chatAvatarGalleryPhoto(account: self.account, representations: representations, autoFetchFullSize: true)) self.dimNode.isHidden = false } else { self.imageNode.setSignal(callDefaultBackground()) diff --git a/TelegramUI/MediaManager.swift b/TelegramUI/MediaManager.swift index 72b251b14b..eff813d26e 100644 --- a/TelegramUI/MediaManager.swift +++ b/TelegramUI/MediaManager.swift @@ -129,6 +129,13 @@ public final class MediaManager: NSObject { var globalMediaPlayerState: Signal<(Account, SharedMediaPlayerItemPlaybackStateOrLoading, MediaManagerPlayerType)?, NoError> { return self.globalMediaPlayerStateValue.get() } + public var activeGlobalMediaPlayerAccountId: Signal { + return self.globalMediaPlayerStateValue.get() + |> map { state in + return state?.0.id + } + |> distinctUntilChanged + } private let setPlaylistByTypeDisposables = DisposableDict() diff --git a/TelegramUI/PresentationCall.swift b/TelegramUI/PresentationCall.swift index f9ec66de51..4d4d778f62 100644 --- a/TelegramUI/PresentationCall.swift +++ b/TelegramUI/PresentationCall.swift @@ -154,7 +154,7 @@ private final class PresentationCallToneRenderer { } public final class PresentationCall { - private let account: Account + public let account: Account private let audioSession: ManagedAudioSession private let callSessionManager: CallSessionManager private let callKitIntegration: CallKitIntegration? diff --git a/TelegramUI/SharedAccountContext.swift b/TelegramUI/SharedAccountContext.swift index fda4d8fc82..7cac18d9a6 100644 --- a/TelegramUI/SharedAccountContext.swift +++ b/TelegramUI/SharedAccountContext.swift @@ -83,7 +83,7 @@ public final class SharedAccountContext { public var presentGlobalController: (ViewController, Any?) -> Void = { _, _ in } public var presentCrossfadeController: () -> Void = {} - public init(mainWindow: Window1?, accountManager: AccountManager, applicationBindings: TelegramApplicationBindings, initialPresentationDataAndSettings: InitialPresentationDataAndSettings, networkArguments: NetworkInitializationArguments, rootPath: String, apsNotificationToken: Signal, voipNotificationToken: Signal) { + public init(mainWindow: Window1?, accountManager: AccountManager, applicationBindings: TelegramApplicationBindings, initialPresentationDataAndSettings: InitialPresentationDataAndSettings, networkArguments: NetworkInitializationArguments, rootPath: String, apsNotificationToken: Signal, voipNotificationToken: Signal, setNotificationCall: @escaping (PresentationCall?) -> Void) { assert(Queue.mainQueue().isCurrent()) self.mainWindow = mainWindow self.applicationBindings = applicationBindings @@ -323,7 +323,7 @@ public final class SharedAccountContext { }) }, audioSession: self.mediaManager.audioSession, activeAccounts: self.activeAccounts |> map { _, accounts, _ in return Array(accounts.values) - }) + }) self.callManager = callManager self.callDisposable = (callManager.currentCallSignal @@ -336,18 +336,17 @@ public final class SharedAccountContext { if let call = call { mainWindow.hostView.containerView.endEditing(true) - /*let callController = CallController(context: strongSelf.context, call: call) + let callController = CallController(sharedContext: strongSelf, account: call.account, call: call) strongSelf.callController = callController - strongSelf.mainWindow.present(callController, on: .calls) + strongSelf.mainWindow?.present(callController, on: .calls) strongSelf.callState.set(call.state - |> map(Optional.init)) - strongSelf.hasOngoingCall.set(true)*/ - - //strongSelf.notificationManager.setNotificationCall(call, strings: strongSelf.context.sharedContext.currentPresentationData.with({ $0 }).strings) + |> map(Optional.init)) + strongSelf.hasOngoingCall.set(true) + setNotificationCall(call) } else { strongSelf.callState.set(.single(nil)) strongSelf.hasOngoingCall.set(false) - //strongSelf.notificationManager.setNotificationCall(nil, strings: strongSelf.context.sharedContext.currentPresentationData.with({ $0 }).strings) + setNotificationCall(nil) } } } diff --git a/TelegramUI/UpgradedAccounts.swift b/TelegramUI/UpgradedAccounts.swift new file mode 100644 index 0000000000..14fba4afe3 --- /dev/null +++ b/TelegramUI/UpgradedAccounts.swift @@ -0,0 +1,145 @@ +import Foundation +import TelegramCore +import Postbox +import SwiftSignalKit + +private enum LegacyPreferencesKeyValues: Int32 { + case cacheStorageSettings = 1 + case localizationSettings = 2 + case proxySettings = 5 + + var key: ValueBoxKey { + let key = ValueBoxKey(length: 4) + key.setInt32(0, value: self.rawValue) + return key + } +} + +private enum UpgradedSharedDataKeyValues: Int32 { + case cacheStorageSettings = 2 + case localizationSettings = 3 + case proxySettings = 4 + + var key: ValueBoxKey { + let key = ValueBoxKey(length: 4) + key.setInt32(0, value: self.rawValue) + return key + } +} + +private enum LegacyApplicationSpecificPreferencesKeyValues: Int32 { + case inAppNotificationSettings = 0 + case presentationPasscodeSettings = 1 + case automaticMediaDownloadSettings = 2 + case generatedMediaStoreSettings = 3 + case voiceCallSettings = 4 + case presentationThemeSettings = 5 + case instantPagePresentationSettings = 6 + case callListSettings = 7 + case experimentalSettings = 8 + case musicPlaybackSettings = 9 + case mediaInputSettings = 10 + case experimentalUISettings = 11 + case contactSynchronizationSettings = 12 + case stickerSettings = 13 + case watchPresetSettings = 14 + case webSearchSettings = 15 + case voipDerivedState = 16 + + var key: ValueBoxKey { + return applicationSpecificPreferencesKey(self.rawValue) + } +} + +private enum UpgradedApplicationSpecificSharedDataKeyValues: Int32 { + case inAppNotificationSettings = 0 + case presentationPasscodeSettings = 1 + case automaticMediaDownloadSettings = 2 + case generatedMediaStoreSettings = 3 + case voiceCallSettings = 4 + case presentationThemeSettings = 5 + case instantPagePresentationSettings = 6 + case callListSettings = 7 + case experimentalSettings = 8 + case musicPlaybackSettings = 9 + case mediaInputSettings = 10 + case experimentalUISettings = 11 + case stickerSettings = 12 + case watchPresetSettings = 13 + case webSearchSettings = 14 + case contactSynchronizationSettings = 15 + + var key: ValueBoxKey { + return applicationSpecificSharedDataKey(self.rawValue) + } +} + +private let preferencesKeyMapping: [LegacyPreferencesKeyValues: UpgradedSharedDataKeyValues] = [ + .cacheStorageSettings: .cacheStorageSettings, + .localizationSettings: .localizationSettings, + .proxySettings: .proxySettings +] + +private let applicationSpecificPreferencesKeyMapping: [LegacyApplicationSpecificPreferencesKeyValues: UpgradedApplicationSpecificSharedDataKeyValues] = [ + .inAppNotificationSettings: .inAppNotificationSettings, + .presentationPasscodeSettings: .presentationPasscodeSettings, + .automaticMediaDownloadSettings: .automaticMediaDownloadSettings, + .generatedMediaStoreSettings: .generatedMediaStoreSettings, + .voiceCallSettings: .voiceCallSettings, + .presentationThemeSettings: .presentationThemeSettings, + .instantPagePresentationSettings: .instantPagePresentationSettings, + .callListSettings: .callListSettings, + .experimentalSettings: .experimentalSettings, + .musicPlaybackSettings: .musicPlaybackSettings, + .mediaInputSettings: .mediaInputSettings, + .experimentalUISettings: .experimentalUISettings, + .stickerSettings: .stickerSettings, + .watchPresetSettings: .watchPresetSettings, + .webSearchSettings: .webSearchSettings, + .contactSynchronizationSettings: .contactSynchronizationSettings +] + +public func upgradedAccounts(accountManager: AccountManager, rootPath: String) -> Signal { + return accountManager.transaction { transaction -> (Int32, AccountRecordId?) in + return (transaction.getVersion(), transaction.getCurrent()?.0) + } + |> mapToSignal { version, currentId -> Signal in + if version == 0 { + if let currentId = currentId { + return accountPreferenceEntries(rootPath: rootPath, id: currentId, keys: Set(preferencesKeyMapping.keys.map({ $0.key }) + applicationSpecificPreferencesKeyMapping.keys.map({ $0.key }))) + |> mapToSignal { values -> Signal in + return accountManager.transaction { transaction -> Void in + for (key, value) in values { + var upgradedKey: ValueBoxKey? + for (k, v) in preferencesKeyMapping { + if k.key == key { + upgradedKey = v.key + break + } + } + for (k, v) in applicationSpecificPreferencesKeyMapping { + if k.key == key { + upgradedKey = v.key + break + } + } + if let upgradedKey = upgradedKey { + transaction.updateSharedData(upgradedKey, { _ in + return value + }) + } + } + + transaction.setVersion(1) + } + } + } else { + return accountManager.transaction { transaction -> Void in + transaction.setVersion(1) + } + } + } else { + return .complete() + } + } +}