Swiftgram/TelegramUI/InAppNotificationSettings.swift
Peter f110dd2b66 Support for updated global 3d touch API
Fixed media sharing to secret chats
2018-11-01 19:10:11 +04:00

102 lines
4.6 KiB
Swift

import Foundation
import Postbox
import SwiftSignalKit
public enum TotalUnreadCountDisplayStyle: Int32 {
case filtered = 0
case raw = 1
var category: ChatListTotalUnreadStateCategory {
switch self {
case .filtered:
return .filtered
case .raw:
return .raw
}
}
}
public enum TotalUnreadCountDisplayCategory: Int32 {
case chats = 0
case messages = 1
var statsType: ChatListTotalUnreadStateStats {
switch self {
case .chats:
return .chats
case .messages:
return .messages
}
}
}
public struct InAppNotificationSettings: PreferencesEntry, Equatable {
public var playSounds: Bool
public var vibrate: Bool
public var displayPreviews: Bool
public var totalUnreadCountDisplayStyle: TotalUnreadCountDisplayStyle
public var totalUnreadCountDisplayCategory: TotalUnreadCountDisplayCategory
public var totalUnreadCountIncludeTags: PeerSummaryCounterTags
public var displayNameOnLockscreen: Bool
public static var defaultSettings: InAppNotificationSettings {
return InAppNotificationSettings(playSounds: true, vibrate: false, displayPreviews: true, totalUnreadCountDisplayStyle: .raw, totalUnreadCountDisplayCategory: .messages, totalUnreadCountIncludeTags: [.regularChatsAndPrivateGroups], displayNameOnLockscreen: true)
}
init(playSounds: Bool, vibrate: Bool, displayPreviews: Bool, totalUnreadCountDisplayStyle: TotalUnreadCountDisplayStyle, totalUnreadCountDisplayCategory: TotalUnreadCountDisplayCategory, totalUnreadCountIncludeTags: PeerSummaryCounterTags, displayNameOnLockscreen: Bool) {
self.playSounds = playSounds
self.vibrate = vibrate
self.displayPreviews = displayPreviews
self.totalUnreadCountDisplayStyle = totalUnreadCountDisplayStyle
self.totalUnreadCountDisplayCategory = totalUnreadCountDisplayCategory
self.totalUnreadCountIncludeTags = totalUnreadCountIncludeTags
self.displayNameOnLockscreen = displayNameOnLockscreen
}
public init(decoder: PostboxDecoder) {
self.playSounds = decoder.decodeInt32ForKey("s", orElse: 0) != 0
self.vibrate = decoder.decodeInt32ForKey("v", orElse: 0) != 0
self.displayPreviews = decoder.decodeInt32ForKey("p", orElse: 0) != 0
self.totalUnreadCountDisplayStyle = TotalUnreadCountDisplayStyle(rawValue: decoder.decodeInt32ForKey("tds", orElse: 1)) ?? .raw
self.totalUnreadCountDisplayCategory = TotalUnreadCountDisplayCategory(rawValue: decoder.decodeInt32ForKey("totalUnreadCountDisplayCategory", orElse: 1)) ?? .messages
if let value = decoder.decodeOptionalInt32ForKey("totalUnreadCountIncludeTags") {
self.totalUnreadCountIncludeTags = PeerSummaryCounterTags(rawValue: value)
} else {
self.totalUnreadCountIncludeTags = [.regularChatsAndPrivateGroups]
}
self.displayNameOnLockscreen = decoder.decodeInt32ForKey("displayNameOnLockscreen", orElse: 1) != 0
}
public func encode(_ encoder: PostboxEncoder) {
encoder.encodeInt32(self.playSounds ? 1 : 0, forKey: "s")
encoder.encodeInt32(self.vibrate ? 1 : 0, forKey: "v")
encoder.encodeInt32(self.displayPreviews ? 1 : 0, forKey: "p")
encoder.encodeInt32(self.totalUnreadCountDisplayStyle.rawValue, forKey: "tds")
encoder.encodeInt32(self.totalUnreadCountDisplayCategory.rawValue, forKey: "totalUnreadCountDisplayCategory")
encoder.encodeInt32(self.totalUnreadCountIncludeTags.rawValue, forKey: "totalUnreadCountIncludeTags")
encoder.encodeInt32(self.displayNameOnLockscreen ? 1 : 0, forKey: "displayNameOnLockscreen")
}
public func isEqual(to: PreferencesEntry) -> Bool {
if let to = to as? InAppNotificationSettings {
return self == to
} else {
return false
}
}
}
func updateInAppNotificationSettingsInteractively(postbox: Postbox, _ f: @escaping (InAppNotificationSettings) -> InAppNotificationSettings) -> Signal<Void, NoError> {
return postbox.transaction { transaction -> Void in
transaction.updatePreferencesEntry(key: ApplicationSpecificPreferencesKeys.inAppNotificationSettings, { entry in
let currentSettings: InAppNotificationSettings
if let entry = entry as? InAppNotificationSettings {
currentSettings = entry
} else {
currentSettings = InAppNotificationSettings.defaultSettings
}
return f(currentSettings)
})
}
}