Custom sound support

This commit is contained in:
Ali 2022-03-25 21:08:01 +04:00
parent e1b4e15da8
commit f58c2460d1
3 changed files with 61 additions and 3 deletions

View File

@ -662,6 +662,7 @@ private final class NotificationServiceHandler {
|> deliverOn(self.queue)).start(next: { [weak self] records, sharedData in
var recordId: AccountRecordId?
var isCurrentAccount: Bool = false
var customSoundPath: String?
if let keyId = notificationPayloadKeyId(data: payloadData) {
outer: for listRecord in records.records {
@ -681,6 +682,8 @@ private final class NotificationServiceHandler {
let inAppNotificationSettings = sharedData.entries[ApplicationSpecificSharedDataKeys.inAppNotificationSettings]?.get(InAppNotificationSettings.self) ?? InAppNotificationSettings.defaultSettings
customSoundPath = inAppNotificationSettings.customSound
let voiceCallSettings: VoiceCallSettings
if let value = sharedData.entries[ApplicationSpecificSharedDataKeys.voiceCallSettings]?.get(VoiceCallSettings.self) {
voiceCallSettings = value
@ -923,7 +926,11 @@ private final class NotificationServiceHandler {
}
if let sound = aps["sound"] as? String {
content.sound = sound
if let customSoundPath = customSoundPath {
content.sound = customSoundPath
} else {
content.sound = sound
}
}
if let category = aps["category"] as? String {

View File

@ -460,6 +460,7 @@ func contextMenuForChatPresentationInterfaceState(chatPresentationInterfaceState
loadStickerSaveStatus = file.fileId
}
}
loadCopyMediaResource = file.resource
} else if media is TelegramMediaAction || media is TelegramMediaExpiredContent {
isAction = true
} else if let image = media as? TelegramMediaImage {
@ -818,6 +819,51 @@ func contextMenuForChatPresentationInterfaceState(chatPresentationInterfaceState
}
f(.default)
})))
if resourceAvailable {
for media in message.media {
if let file = media as? TelegramMediaFile, (["audio/mp3", "audio/mpeg3"] as [String]).contains(file.mimeType.lowercased()) {
actions.append(.action(ContextMenuActionItem(text: "Set as Message Tone", icon: { _ in
return nil
}, action: { _, f in
for media in message.media {
if let file = media as? TelegramMediaFile {
let _ = (context.account.postbox.mediaBox.resourceData(file.resource, option: .incremental(waitUntilFetchStatus: false))
|> take(1)
|> deliverOnMainQueue).start(next: { data in
if data.complete {
let documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0]
let soundsDirectoryPath = documentsDirectoryPath + "/Sounds"
let _ = try? FileManager.default.createDirectory(atPath: soundsDirectoryPath, withIntermediateDirectories: true, attributes: nil)
let containerSoundsPath = context.sharedContext.applicationBindings.containerPath + "/Library/Sounds"
let _ = try? FileManager.default.createDirectory(atPath: containerSoundsPath, withIntermediateDirectories: true, attributes: nil)
let soundFileName = "\(UInt32.random(in: 0 ..< UInt32.max)).mp3"
let soundPath = soundsDirectoryPath + "/\(soundFileName)"
let _ = try? FileManager.default.copyItem(atPath: data.path, toPath: soundPath)
let _ = try? FileManager.default.copyItem(atPath: data.path, toPath: "\(containerSoundsPath)/\(soundFileName)")
let _ = updateInAppNotificationSettingsInteractively(accountManager: context.sharedContext.accountManager, { settings in
var settings = settings
settings.customSound = soundFileName
return settings
}).start()
}
})
}
}
f(.default)
})))
}
}
}
}
let (canTranslate, _) = canTranslateText(context: context, text: messageText, showTranslate: translationSettings.showTranslate, ignoredLanguages: translationSettings.ignoredLanguages)

View File

@ -37,12 +37,13 @@ public struct InAppNotificationSettings: Codable, Equatable {
public var totalUnreadCountIncludeTags: PeerSummaryCounterTags
public var displayNameOnLockscreen: Bool
public var displayNotificationsFromAllAccounts: Bool
public var customSound: String?
public static var defaultSettings: InAppNotificationSettings {
return InAppNotificationSettings(playSounds: true, vibrate: false, displayPreviews: true, totalUnreadCountDisplayStyle: .filtered, totalUnreadCountDisplayCategory: .messages, totalUnreadCountIncludeTags: .all, displayNameOnLockscreen: true, displayNotificationsFromAllAccounts: true)
return InAppNotificationSettings(playSounds: true, vibrate: false, displayPreviews: true, totalUnreadCountDisplayStyle: .filtered, totalUnreadCountDisplayCategory: .messages, totalUnreadCountIncludeTags: .all, displayNameOnLockscreen: true, displayNotificationsFromAllAccounts: true, customSound: nil)
}
public init(playSounds: Bool, vibrate: Bool, displayPreviews: Bool, totalUnreadCountDisplayStyle: TotalUnreadCountDisplayStyle, totalUnreadCountDisplayCategory: TotalUnreadCountDisplayCategory, totalUnreadCountIncludeTags: PeerSummaryCounterTags, displayNameOnLockscreen: Bool, displayNotificationsFromAllAccounts: Bool) {
public init(playSounds: Bool, vibrate: Bool, displayPreviews: Bool, totalUnreadCountDisplayStyle: TotalUnreadCountDisplayStyle, totalUnreadCountDisplayCategory: TotalUnreadCountDisplayCategory, totalUnreadCountIncludeTags: PeerSummaryCounterTags, displayNameOnLockscreen: Bool, displayNotificationsFromAllAccounts: Bool, customSound: String?) {
self.playSounds = playSounds
self.vibrate = vibrate
self.displayPreviews = displayPreviews
@ -51,6 +52,7 @@ public struct InAppNotificationSettings: Codable, Equatable {
self.totalUnreadCountIncludeTags = totalUnreadCountIncludeTags
self.displayNameOnLockscreen = displayNameOnLockscreen
self.displayNotificationsFromAllAccounts = displayNotificationsFromAllAccounts
self.customSound = customSound
}
public init(from decoder: Decoder) throws {
@ -83,6 +85,8 @@ public struct InAppNotificationSettings: Codable, Equatable {
}
self.displayNameOnLockscreen = (try container.decodeIfPresent(Int32.self, forKey: "displayNameOnLockscreen") ?? 1) != 0
self.displayNotificationsFromAllAccounts = (try container.decodeIfPresent(Int32.self, forKey: "displayNotificationsFromAllAccounts") ?? 1) != 0
self.customSound = try container.decodeIfPresent(String.self, forKey: "customSound")
}
public func encode(to encoder: Encoder) throws {
@ -96,6 +100,7 @@ public struct InAppNotificationSettings: Codable, Equatable {
try container.encode(self.totalUnreadCountIncludeTags.rawValue, forKey: "totalUnreadCountIncludeTags_2")
try container.encode((self.displayNameOnLockscreen ? 1 : 0) as Int32, forKey: "displayNameOnLockscreen")
try container.encode((self.displayNotificationsFromAllAccounts ? 1 : 0) as Int32, forKey: "displayNotificationsFromAllAccounts")
try container.encodeIfPresent(self.customSound, forKey: "customSound")
}
}