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

@@ -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)