Refactoring

This commit is contained in:
Ali
2021-04-18 03:10:18 +04:00
parent 4637ed672e
commit 60d959bff8
93 changed files with 221 additions and 191 deletions

View File

@@ -0,0 +1,41 @@
import Foundation
import Postbox
import SwiftSignalKit
import MtProtoKit
import SyncCore
public func updateNetworkSettingsInteractively(postbox: Postbox, network: Network, _ f: @escaping (NetworkSettings) -> NetworkSettings) -> Signal<Void, NoError> {
return postbox.transaction { transaction -> Void in
updateNetworkSettingsInteractively(transaction: transaction, network: network, f)
}
}
extension NetworkSettings {
var mtNetworkSettings: MTNetworkSettings {
return MTNetworkSettings(reducedBackupDiscoveryTimeout: self.reducedBackupDiscoveryTimeout)
}
}
public func updateNetworkSettingsInteractively(transaction: Transaction, network: Network?, _ f: @escaping (NetworkSettings) -> NetworkSettings) {
var updateNetwork = false
var updatedSettings: NetworkSettings?
transaction.updatePreferencesEntry(key: PreferencesKeys.networkSettings, { current in
let previous = (current as? NetworkSettings) ?? NetworkSettings.defaultSettings
let updated = f(previous)
updatedSettings = updated
if updated.reducedBackupDiscoveryTimeout != previous.reducedBackupDiscoveryTimeout {
updateNetwork = true
}
if updated.backupHostOverride != previous.backupHostOverride {
updateNetwork = true
}
return updated
})
if let network = network, updateNetwork, let updatedSettings = updatedSettings {
network.context.updateApiEnvironment { current in
return current?.withUpdatedNetworkSettings(updatedSettings.mtNetworkSettings)
}
}
}