mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
48 lines
1.9 KiB
Swift
48 lines
1.9 KiB
Swift
import Foundation
|
|
import SwiftSignalKit
|
|
import TelegramCore
|
|
|
|
public struct ChatArchiveSettings: Equatable, Codable {
|
|
public var isHiddenByDefault: Bool
|
|
public var hiddenPsaPeerId: EnginePeer.Id?
|
|
|
|
public static var `default`: ChatArchiveSettings {
|
|
return ChatArchiveSettings(isHiddenByDefault: false, hiddenPsaPeerId: nil)
|
|
}
|
|
|
|
public init(isHiddenByDefault: Bool, hiddenPsaPeerId: EnginePeer.Id?) {
|
|
self.isHiddenByDefault = isHiddenByDefault
|
|
self.hiddenPsaPeerId = hiddenPsaPeerId
|
|
}
|
|
|
|
public init(from decoder: Decoder) throws {
|
|
let container = try decoder.container(keyedBy: StringCodingKey.self)
|
|
|
|
self.isHiddenByDefault = (try container.decode(Int32.self, forKey: "isHiddenByDefault")) != 0
|
|
self.hiddenPsaPeerId = (try container.decodeIfPresent(Int64.self, forKey: "hiddenPsaPeerId")).flatMap(EnginePeer.Id.init)
|
|
}
|
|
|
|
public func encode(to encoder: Encoder) throws {
|
|
var container = encoder.container(keyedBy: StringCodingKey.self)
|
|
|
|
try container.encode((self.isHiddenByDefault ? 1 : 0) as Int32, forKey: "isHiddenByDefault")
|
|
if let hiddenPsaPeerId = self.hiddenPsaPeerId {
|
|
try container.encode(hiddenPsaPeerId.toInt64(), forKey: "hiddenPsaPeerId")
|
|
} else {
|
|
try container.encodeNil(forKey: "hiddenPsaPeerId")
|
|
}
|
|
}
|
|
}
|
|
|
|
public func updateChatArchiveSettings(engine: TelegramEngine, _ f: @escaping (ChatArchiveSettings) -> ChatArchiveSettings) -> Signal<Never, NoError> {
|
|
return engine.preferences.update(id: ApplicationSpecificPreferencesKeys.chatArchiveSettings, { entry in
|
|
let currentSettings: ChatArchiveSettings
|
|
if let entry = entry?.get(ChatArchiveSettings.self) {
|
|
currentSettings = entry
|
|
} else {
|
|
currentSettings = .default
|
|
}
|
|
return SharedPreferencesEntry(f(currentSettings))
|
|
})
|
|
}
|