mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2026-01-03 11:44:46 +00:00
Refactoring
This commit is contained in:
@@ -15,32 +15,34 @@ public struct AccountBackupData: Codable, Equatable {
|
||||
}
|
||||
}
|
||||
|
||||
public final class AccountBackupDataAttribute: AccountRecordAttribute, Equatable {
|
||||
public final class AccountBackupDataAttribute: Codable, Equatable {
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case data
|
||||
}
|
||||
|
||||
public let data: AccountBackupData?
|
||||
|
||||
public init(data: AccountBackupData?) {
|
||||
self.data = data
|
||||
}
|
||||
|
||||
public init(decoder: PostboxDecoder) {
|
||||
self.data = try? JSONDecoder().decode(AccountBackupData.self, from: decoder.decodeDataForKey("data") ?? Data())
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
|
||||
self.data = try? JSONDecoder().decode(AccountBackupData.self, from: (try? container.decode(Data.self, forKey: .data)) ?? Data())
|
||||
}
|
||||
|
||||
public func encode(_ encoder: PostboxEncoder) {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
|
||||
if let data = self.data, let serializedData = try? JSONEncoder().encode(data) {
|
||||
encoder.encodeData(serializedData, forKey: "data")
|
||||
try container.encode(serializedData, forKey: .data)
|
||||
} else {
|
||||
try container.encodeNil(forKey: .data)
|
||||
}
|
||||
}
|
||||
|
||||
public static func ==(lhs: AccountBackupDataAttribute, rhs: AccountBackupDataAttribute) -> Bool {
|
||||
return lhs.data == rhs.data
|
||||
}
|
||||
|
||||
public func isEqual(to: AccountRecordAttribute) -> Bool {
|
||||
if let to = to as? AccountBackupDataAttribute {
|
||||
return self == to
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,26 +6,33 @@ public enum AccountEnvironment: Int32 {
|
||||
case test = 1
|
||||
}
|
||||
|
||||
public final class AccountEnvironmentAttribute: AccountRecordAttribute {
|
||||
public final class AccountEnvironmentAttribute: Codable, Equatable {
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case environment
|
||||
}
|
||||
|
||||
public let environment: AccountEnvironment
|
||||
|
||||
public init(environment: AccountEnvironment) {
|
||||
self.environment = environment
|
||||
}
|
||||
|
||||
public init(decoder: PostboxDecoder) {
|
||||
self.environment = AccountEnvironment(rawValue: decoder.decodeInt32ForKey("environment", orElse: 0)) ?? .production
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
|
||||
let environmentValue: Int32 = (try? container.decode(Int32.self, forKey: .environment)) ?? 0
|
||||
|
||||
self.environment = AccountEnvironment(rawValue: environmentValue) ?? .production
|
||||
}
|
||||
|
||||
public func encode(_ encoder: PostboxEncoder) {
|
||||
encoder.encodeInt32(self.environment.rawValue, forKey: "environment")
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
|
||||
try container.encode(self.environment.rawValue, forKey: .environment)
|
||||
}
|
||||
|
||||
public func isEqual(to: AccountRecordAttribute) -> Bool {
|
||||
guard let to = to as? AccountEnvironmentAttribute else {
|
||||
return false
|
||||
}
|
||||
if self.environment != to.environment {
|
||||
|
||||
public static func ==(lhs: AccountEnvironmentAttribute, rhs: AccountEnvironmentAttribute) -> Bool {
|
||||
if lhs.environment != rhs.environment {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
||||
@@ -1,26 +1,31 @@
|
||||
import Foundation
|
||||
import Postbox
|
||||
|
||||
public final class AccountSortOrderAttribute: AccountRecordAttribute {
|
||||
public final class AccountSortOrderAttribute: Codable, Equatable {
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case order
|
||||
}
|
||||
|
||||
public let order: Int32
|
||||
|
||||
public init(order: Int32) {
|
||||
self.order = order
|
||||
}
|
||||
|
||||
public init(decoder: PostboxDecoder) {
|
||||
self.order = decoder.decodeInt32ForKey("order", orElse: 0)
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
|
||||
self.order = (try? container.decode(Int32.self, forKey: .order)) ?? 0
|
||||
}
|
||||
|
||||
public func encode(_ encoder: PostboxEncoder) {
|
||||
encoder.encodeInt32(self.order, forKey: "order")
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
|
||||
try container.encode(self.order, forKey: .order)
|
||||
}
|
||||
|
||||
public func isEqual(to: AccountRecordAttribute) -> Bool {
|
||||
guard let to = to as? AccountSortOrderAttribute else {
|
||||
return false
|
||||
}
|
||||
if self.order != to.order {
|
||||
|
||||
public static func ==(lhs: AccountSortOrderAttribute, rhs: AccountSortOrderAttribute) -> Bool {
|
||||
if lhs.order != rhs.order {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
import Foundation
|
||||
import Postbox
|
||||
|
||||
public final class LoggedOutAccountAttribute: AccountRecordAttribute {
|
||||
public final class LoggedOutAccountAttribute: Codable, Equatable {
|
||||
public init() {
|
||||
}
|
||||
|
||||
public init(decoder: PostboxDecoder) {
|
||||
public init(from decoder: Decoder) throws {
|
||||
}
|
||||
|
||||
public func encode(_ encoder: PostboxEncoder) {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
}
|
||||
|
||||
public func isEqual(to: AccountRecordAttribute) -> Bool {
|
||||
return to is LoggedOutAccountAttribute
|
||||
|
||||
public static func ==(lhs: LoggedOutAccountAttribute, rhs: LoggedOutAccountAttribute) -> Bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public struct WallpapersState: PreferencesEntry, Equatable {
|
||||
}
|
||||
|
||||
public extension WallpapersState {
|
||||
static func update(transaction: AccountManagerModifier, _ f: (WallpapersState) -> WallpapersState) {
|
||||
static func update(transaction: AccountManagerModifier<TelegramAccountManagerTypes>, _ f: (WallpapersState) -> WallpapersState) {
|
||||
transaction.updateSharedData(SharedDataKeys.wallapersState, { current in
|
||||
let item = (transaction.getSharedData(SharedDataKeys.wallapersState) as? WallpapersState) ?? WallpapersState(wallpapers: [])
|
||||
return f(item)
|
||||
|
||||
Reference in New Issue
Block a user