Skip to content

Commit

Permalink
删除铃声
Browse files Browse the repository at this point in the history
  • Loading branch information
Finb committed Apr 1, 2024
1 parent f74946e commit 5af2a55
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 10 additions & 1 deletion Controller/SoundsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class SoundsViewController: BaseViewController<SoundsViewModel> {
let output = viewModel.transform(
input: SoundsViewModel.Input(
soundSelected: self.tableView.rx.modelSelected(SoundItem.self).asDriver(),
importSound: self.importSoundActionRelay.asDriver(onErrorDriveWith: .empty())
importSound: self.importSoundActionRelay.asDriver(onErrorDriveWith: .empty()),
soundDeleted: self.tableView.rx.modelDeleted(SoundItem.self).asDriver()
)
)

Expand All @@ -62,6 +63,14 @@ class SoundsViewController: BaseViewController<SoundsViewModel> {

} titleForHeaderInSection: { dataSource, section in
return dataSource[section].model
} canEditRowAtIndexPath: { dataSource, indexPath in
guard indexPath.section == 0 else {
return false
}
guard case SoundItem.sound = dataSource[indexPath.section].items[indexPath.row] else {
return false
}
return true
}

output.audios
Expand Down
18 changes: 17 additions & 1 deletion Controller/SoundsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class SoundsViewModel: ViewModel, ViewModelType {
var soundSelected: Driver<SoundItem>
/// 铃声导入
var importSound: Driver<URL>
var soundDeleted: Driver<SoundItem>
}

struct Output {
Expand Down Expand Up @@ -83,12 +84,22 @@ class SoundsViewModel: ViewModel, ViewModelType {
}
.disposed(by: rx.disposeBag)

// 删除铃声
input.soundDeleted.drive(onNext: { item in
guard case SoundItem.sound(let model) = item else {
return
}
self.dependencies.soundFileStorage.deleteSound(url: model.model.url)
}).disposed(by: rx.disposeBag)

// 铃声列表有更新,
let soundsListUpdated = Observable.merge(
// 刚进页面
Observable.just(()),
// 上传了新铃声
input.importSound.map { _ in () }.asObservable()
input.importSound.map { _ in () }.asObservable(),
// 删除了铃声
input.soundDeleted.map { _ in () }.asObservable()
).share(replay: 1)

// 所有铃声列表,包含自定义铃声和默认铃声
Expand Down Expand Up @@ -154,6 +165,7 @@ class SoundsViewModel: ViewModel, ViewModelType {
/// 保存铃声文件协议
protocol SoundFileStorageProtocol {
func saveSound(url: URL)
func deleteSound(url: URL)
}

/// 用于将铃声文件保存在 /Library/Sounds 文件夹中
Expand All @@ -170,6 +182,10 @@ class SoundFileStorage: SoundFileStorageProtocol {
try? fileManager.copyItem(at: url, to: soundUrl)
}

func deleteSound(url: URL) {
try? fileManager.removeItem(at: url)
}

/// 获取 Library 目录下的 Sounds 文件夹
/// 如果不存在就创建
func getSoundsDirectory() -> URL {
Expand Down

0 comments on commit 5af2a55

Please sign in to comment.