优化自定义上传铃声的存储位置

This commit is contained in:
Fin 2025-01-17 12:10:23 +08:00
parent f55a776874
commit 05faa8d192

View File

@ -66,7 +66,8 @@ class SoundsViewModel: ViewModel, ViewModelType {
do {
let files = try fileManager.contentsOfDirectory(atPath: directory)
return files.compactMap { file -> URL? in
if file.hasSuffix(suffix) {
if file.hasSuffix(suffix), !file.hasPrefix(kBarkSoundPrefix) {
// kBarkSoundPrefix call=1 30s ,
return URL(fileURLWithPath: directory).appendingPathComponent(file)
}
return nil
@ -104,13 +105,15 @@ class SoundsViewModel: ViewModel, ViewModelType {
).share(replay: 1)
//
let sounds: Observable<([SoundItem], [SoundItem])> = soundsListUpdated.map { _ in
let sounds: Observable<([SoundItem], [SoundItem])> = soundsListUpdated.map { [weak self] _ in
guard let self else { return ([], []) }
let defaultSounds = self.getSounds(
urls: Bundle.main.urls(forResourcesWithExtension: "caf", subdirectory: nil) ?? []
)
let customSounds: [SoundItem] = {
guard let soundsDirectoryUrl = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true).first?.appending("/Sounds") else {
guard let soundsDirectoryUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.bark")?.appendingPathComponent("Library/Sounds").path else {
return [.addSound]
}
return self.getSounds(
@ -179,52 +182,27 @@ class SoundFileStorage: SoundFileStorageProtocol {
/// Library/Sound
func saveSound(url: URL) {
// Sounds
let soundsDirectoryUrl = getSoundsDirectory()
guard let soundsDirectoryUrl = getSoundsDirectory() else {
return
}
let soundUrl = soundsDirectoryUrl.appendingPathComponent(url.lastPathComponent)
try? fileManager.copyItem(at: url, to: soundUrl)
//
saveSoundToGroupDirectory(url: url)
}
func deleteSound(url: URL) {
// sounds
try? fileManager.removeItem(at: url)
//
if let groupSoundUrl = getSoundsGroupDirectory()?.appendingPathComponent(url.lastPathComponent) {
try? fileManager.removeItem(at: groupSoundUrl)
}
}
/// Library Sounds
///
private func getSoundsDirectory() -> URL {
let soundsDirectoryUrl = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true).first!.appending("/Sounds")
if !fileManager.fileExists(atPath: soundsDirectoryUrl) {
try? fileManager.createDirectory(atPath: soundsDirectoryUrl, withIntermediateDirectories: true, attributes: nil)
private func getSoundsDirectory() -> URL? {
guard let soundsDirectoryUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.bark")?.appendingPathComponent("Library/Sounds") else {
return nil
}
return URL(fileURLWithPath: soundsDirectoryUrl)
}
/// NotificationServiceExtension 使
private func saveSoundToGroupDirectory(url: URL) {
guard let groupUrl = getSoundsGroupDirectory() else {
return
if !fileManager.fileExists(atPath: soundsDirectoryUrl.path) {
try? fileManager.createDirectory(atPath: soundsDirectoryUrl.path, withIntermediateDirectories: true, attributes: nil)
}
let soundUrl = groupUrl.appendingPathComponent(url.lastPathComponent)
try? fileManager.copyItem(at: url, to: soundUrl)
}
/// Sounds
///
private func getSoundsGroupDirectory() -> URL? {
if let directoryUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.bark")?.appendingPathComponent("Sounds") {
if !fileManager.fileExists(atPath: directoryUrl.path) {
try? fileManager.createDirectory(atPath: directoryUrl.path, withIntermediateDirectories: true, attributes: nil)
}
return directoryUrl
}
return nil
return URL(fileURLWithPath: soundsDirectoryUrl.path)
}
}