Stuff…
This commit is contained in:
99
TagTunes/SaveOperation.swift
Executable file
99
TagTunes/SaveOperation.swift
Executable file
@@ -0,0 +1,99 @@
|
||||
//
|
||||
// iTunesSaveOperation.swift
|
||||
// TagTunes
|
||||
//
|
||||
// Created by Kim Wittenburg on 08.04.16.
|
||||
// Copyright © 2016 Kim Wittenburg. All rights reserved.
|
||||
//
|
||||
|
||||
import AppKitPlus
|
||||
|
||||
// TODO: Documentation
|
||||
|
||||
class SaveOperation<TrackType: TagTunesTrack>: Operation, NSProgressReporting {
|
||||
|
||||
// TODO: Support Cancellation
|
||||
let progress = NSProgress(parent: nil, userInfo: nil)
|
||||
|
||||
let track: TrackType
|
||||
let entity: TagTunesEntityItem
|
||||
let parentEntity: TagTunesGroupItem?
|
||||
|
||||
init(track: TrackType, entity: TagTunesEntityItem) {
|
||||
self.track = track
|
||||
self.entity = entity
|
||||
self.parentEntity = entity.parentItem
|
||||
super.init()
|
||||
|
||||
progress.cancellable = false
|
||||
progress.localizedDescription = entity.entity.name
|
||||
progress.localizedAdditionalDescription = "Pending…" // TODO: Localize
|
||||
addCondition(MutuallyExclusive<TrackType>())
|
||||
}
|
||||
|
||||
override func execute() {
|
||||
dispatch_sync(dispatch_get_main_queue()) {
|
||||
self.progress.totalUnitCount = Int64(Tag.allTags.count)
|
||||
self.progress.completedUnitCount = 0
|
||||
}
|
||||
if track.supportsBatchSaving {
|
||||
executeBatchSave()
|
||||
} else {
|
||||
executeNormalSave()
|
||||
}
|
||||
}
|
||||
|
||||
private func executeBatchSave() {
|
||||
dispatch_sync(dispatch_get_main_queue()) {
|
||||
self.progress.localizedAdditionalDescription = "Saving…" // TODO: Localize
|
||||
}
|
||||
var tags = [Tag: AnyObject?]()
|
||||
for tag in Tag.allTags {
|
||||
do {
|
||||
switch Preferences.sharedPreferences.tagSavingBehaviors[tag]! {
|
||||
case .Save:
|
||||
tags[tag] = try entity.valueForTag(tag)
|
||||
case .Clear:
|
||||
tags[tag] = nil
|
||||
case .Ignore:
|
||||
break
|
||||
}
|
||||
} catch {
|
||||
logError(error as NSError)
|
||||
}
|
||||
}
|
||||
progress.becomeCurrentWithPendingUnitCount(progress.totalUnitCount)
|
||||
do {
|
||||
try track.saveTags(tags)
|
||||
finish()
|
||||
} catch {
|
||||
finishWithError(error as NSError)
|
||||
}
|
||||
progress.resignCurrent()
|
||||
}
|
||||
|
||||
private func executeNormalSave() {
|
||||
for tag in Tag.allTags {
|
||||
dispatch_sync(dispatch_get_main_queue()) {
|
||||
self.progress.localizedAdditionalDescription = String(format: "Saving %@…", tag.localizedName) // TODO: Localize
|
||||
}
|
||||
do {
|
||||
switch Preferences.sharedPreferences.tagSavingBehaviors[tag]! {
|
||||
case .Save:
|
||||
try track.saveValue(entity.valueForTag(tag), forTag: tag)
|
||||
case .Clear:
|
||||
try track.saveValue(nil, forTag: tag)
|
||||
case .Ignore:
|
||||
break
|
||||
}
|
||||
} catch {
|
||||
logError(error as NSError)
|
||||
}
|
||||
dispatch_sync(dispatch_get_main_queue()) {
|
||||
self.progress.completedUnitCount += 1
|
||||
}
|
||||
}
|
||||
finish()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user