This commit is contained in:
Ali
2021-09-03 00:45:22 +04:00
parent 8aefa19d31
commit 1fe0d4a75b
53 changed files with 489 additions and 566 deletions

View File

@@ -1,14 +1,31 @@
import Foundation
public class PeerNotificationSettingsDecodeHelper {
public let decode: (_ data: Data) -> PeerNotificationSettings?
public init(decode: @escaping (_ data: Data) -> PeerNotificationSettings?) {
self.decode = decode
}
}
public enum PeerNotificationSettingsBehavior {
enum CodingKeys: String, CodingKey {
case _case = "_v"
case toValue
case atTimestamp
}
public enum PeerNotificationSettingsBehavior: PostboxCoding {
case none
case reset(atTimestamp: Int32, toValue: PeerNotificationSettings)
public init(decoder: PostboxDecoder) {
public init(from decoder: Decoder, helper: PeerNotificationSettingsDecodeHelper) throws {
let container = try decoder.container(keyedBy: StringCodingKey.self)
switch decoder.decodeInt32ForKey("_v", orElse: 0) {
case 0:
self = .none
case 1:
if let toValue = decoder.decodeObjectForKey("toValue") as? PeerNotificationSettings {
if let toValue = helper.decode(PeerNotificationSettings.self, forKey: "toValue") {
self = .reset(atTimestamp: decoder.decodeInt32ForKey("atTimestamp", orElse: 0), toValue: toValue)
} else {
assertionFailure()
@@ -27,26 +44,29 @@ public enum PeerNotificationSettingsBehavior: PostboxCoding {
case let .reset(atTimestamp, toValue):
encoder.encodeInt32(1, forKey: "_v")
encoder.encodeInt32(atTimestamp, forKey: "atTimestamp")
encoder.encodeObject(toValue, forKey: "toValue")
encoder.encode(toValue, forKey: "toValue")
}
}
}
public protocol PeerNotificationSettings: PostboxCoding {
public protocol PeerNotificationSettings: Codable {
func isRemovedFromTotalUnreadCount(`default`: Bool) -> Bool
var behavior: PeerNotificationSettingsBehavior { get }
func isEqual(to: PeerNotificationSettings) -> Bool
}
public protocol PostboxGlobalNotificationSettings: PostboxCoding {
func defaultIncludePeer(peer: Peer) -> Bool
func isEqualInDefaultPeerInclusion(other: PostboxGlobalNotificationSettings) -> Bool
public final class PostboxGlobalNotificationSettings {
public let defaultIncludePeer: (_ peer: Peer) -> Bool
public init(
defaultIncludePeer: @escaping (_ peer: Peer) -> Bool
) {
self.defaultIncludePeer = defaultIncludePeer
}
}
public func resolvedIsRemovedFromTotalUnreadCount(globalSettings: PostboxGlobalNotificationSettings, peer: Peer, peerSettings: PeerNotificationSettings?) -> Bool {
let defaultValue = !globalSettings.defaultIncludePeer(peer: peer)
let defaultValue = !globalSettings.defaultIncludePeer(peer)
if let peerSettings = peerSettings {
return peerSettings.isRemovedFromTotalUnreadCount(default: defaultValue)
} else {