Archived
1
This repository has been archived on 2020-06-04. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
tagtunes/TagTunes/SaveOperation.swift
Kim Wittenburg 0a485ff42a Stuff…
2019-02-01 22:59:01 +01:00

99 lines
3.0 KiB
Swift
Executable File

//
// 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()
}
}