mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-23 14:45:21 +00:00
refactor and cleanup [skip ci]
This commit is contained in:
59
submodules/Postbox/Sources/PreferencesView.swift
Normal file
59
submodules/Postbox/Sources/PreferencesView.swift
Normal file
@@ -0,0 +1,59 @@
|
||||
import Foundation
|
||||
|
||||
final class MutablePreferencesView: MutablePostboxView {
|
||||
fileprivate let keys: Set<ValueBoxKey>
|
||||
fileprivate var values: [ValueBoxKey: PreferencesEntry]
|
||||
|
||||
init(postbox: Postbox, keys: Set<ValueBoxKey>) {
|
||||
self.keys = keys
|
||||
var values: [ValueBoxKey: PreferencesEntry] = [:]
|
||||
for key in keys {
|
||||
if let value = postbox.preferencesTable.get(key: key) {
|
||||
values[key] = value
|
||||
}
|
||||
}
|
||||
self.values = values
|
||||
}
|
||||
|
||||
func replay(postbox: Postbox, transaction: PostboxTransaction) -> Bool {
|
||||
var updated = false
|
||||
for operation in transaction.currentPreferencesOperations {
|
||||
switch operation {
|
||||
case let .update(key, value):
|
||||
if self.keys.contains(key) {
|
||||
let currentValue = self.values[key]
|
||||
var updatedValue = false
|
||||
if let value = value, let currentValue = currentValue {
|
||||
if !value.isEqual(to: currentValue) {
|
||||
updatedValue = true
|
||||
}
|
||||
} else if (value != nil) != (currentValue != nil) {
|
||||
updatedValue = true
|
||||
}
|
||||
if updatedValue {
|
||||
if let value = value {
|
||||
self.values[key] = value
|
||||
} else {
|
||||
self.values.removeValue(forKey: key)
|
||||
}
|
||||
updated = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return updated
|
||||
}
|
||||
|
||||
func immutableView() -> PostboxView {
|
||||
return PreferencesView(self)
|
||||
}
|
||||
}
|
||||
|
||||
public final class PreferencesView: PostboxView {
|
||||
public let values: [ValueBoxKey: PreferencesEntry]
|
||||
|
||||
init(_ view: MutablePreferencesView) {
|
||||
self.values = view.values
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user