Remove an optional var.
This commit is contained in:
1 parent
447d822058
commit
fde4c46cda
1 file changed
+67
-36
@@ -27,14 +27,13 @@ import Cocoa
|
|||||||
private enum ChapterSearchState {
|
private enum ChapterSearchState {
|
||||||
case none
|
case none
|
||||||
case searching(task: MetadataSearchTask)
|
case searching(task: MetadataSearchTask)
|
||||||
case completed(results: [ChapterResult])
|
case completed(results: [ChapterResult], selectedResult: ChapterResult)
|
||||||
}
|
}
|
||||||
|
|
||||||
private let delegate: ChapterSearchControllerDelegate
|
private let delegate: ChapterSearchControllerDelegate
|
||||||
private let duration: UInt64
|
private let duration: UInt64
|
||||||
private var searchTerm: String
|
private var searchTerm: String
|
||||||
private var state: ChapterSearchState
|
private var state: ChapterSearchState
|
||||||
private var selectedResult: ChapterResult?
|
|
||||||
|
|
||||||
@objc init(delegate: ChapterSearchControllerDelegate, title: String, duration: UInt64) {
|
@objc init(delegate: ChapterSearchControllerDelegate, title: String, duration: UInt64) {
|
||||||
if let info = title.parsedAsFilename() {
|
if let info = title.parsedAsFilename() {
|
||||||
@@ -80,7 +79,12 @@ import Cocoa
|
|||||||
|
|
||||||
private func searchDone(results: [ChapterResult]) {
|
private func searchDone(results: [ChapterResult]) {
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.state = .completed(results: results)
|
if let first = results.first {
|
||||||
|
self.state = .completed(results: results, selectedResult: first)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
self.state = .none
|
||||||
|
}
|
||||||
self.updateUI()
|
self.updateUI()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -102,16 +106,20 @@ import Cocoa
|
|||||||
}
|
}
|
||||||
|
|
||||||
@IBAction func addChapter(_ sender: Any) {
|
@IBAction func addChapter(_ sender: Any) {
|
||||||
if let chapters = selectedResult?.chapters {
|
switch state {
|
||||||
|
case .completed(_, let result):
|
||||||
var textChapters: [MP42TextSample] = Array()
|
var textChapters: [MP42TextSample] = Array()
|
||||||
for chapter in chapters {
|
for chapter in result.chapters {
|
||||||
let sample = MP42TextSample()
|
let sample = MP42TextSample()
|
||||||
sample.timestamp = chapter.timestamp
|
sample.timestamp = chapter.timestamp
|
||||||
sample.title = chapter.name
|
sample.title = chapter.name
|
||||||
textChapters.append(sample)
|
textChapters.append(sample)
|
||||||
}
|
}
|
||||||
delegate.chapterImportDone(chaptersToBeImported: textChapters)
|
delegate.chapterImportDone(chaptersToBeImported: textChapters)
|
||||||
|
default:
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
self.window?.sheetParent?.endSheet(self.window!, returnCode: NSApplication.ModalResponse.OK)
|
self.window?.sheetParent?.endSheet(self.window!, returnCode: NSApplication.ModalResponse.OK)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,38 +135,51 @@ import Cocoa
|
|||||||
|
|
||||||
// MARK - UI state
|
// MARK - UI state
|
||||||
|
|
||||||
private func updateUI() {
|
private func startProgressReport() {
|
||||||
switch state {
|
|
||||||
case .none:
|
|
||||||
searchButton.keyEquivalent = "\r"
|
|
||||||
updateSearchButtonVisibility()
|
|
||||||
case .searching(_):
|
|
||||||
progress.startAnimation(self)
|
progress.startAnimation(self)
|
||||||
progress.isHidden = false
|
progress.isHidden = false
|
||||||
progressText.stringValue = NSLocalizedString("Searching for chapter information…", comment: "ChapterDB")
|
progressText.stringValue = NSLocalizedString("Searching for chapter information…", comment: "ChapterDB")
|
||||||
progressText.isHidden = false
|
progressText.isHidden = false
|
||||||
resultsTable.isEnabled = false
|
}
|
||||||
chapterTable.isEnabled = false
|
|
||||||
resultsTable.reloadData()
|
private func stopProgressReport() {
|
||||||
case .completed(let results):
|
|
||||||
progress.stopAnimation(self)
|
progress.stopAnimation(self)
|
||||||
progress.isHidden = true
|
progress.isHidden = true
|
||||||
progressText.isHidden = true
|
progressText.isHidden = true
|
||||||
resultsTable.isEnabled = true
|
}
|
||||||
chapterTable.isEnabled = true
|
|
||||||
|
private func reloadTableData(enabled: Bool) {
|
||||||
resultsTable.reloadData()
|
resultsTable.reloadData()
|
||||||
if results.count > 0 {
|
chapterTable.reloadData()
|
||||||
addButton.isEnabled = true
|
resultsTable.isEnabled = enabled
|
||||||
addButton.keyEquivalent = "\r"
|
chapterTable.isEnabled = enabled
|
||||||
searchButton.keyEquivalent = ""
|
}
|
||||||
|
|
||||||
|
private func swithDefaultButton(from oldDefault: NSButton, to newDefault: NSButton, disableOldButton: Bool)
|
||||||
|
{
|
||||||
|
oldDefault.keyEquivalent = ""
|
||||||
|
oldDefault.isEnabled = !disableOldButton
|
||||||
|
newDefault.keyEquivalent = "\r"
|
||||||
|
newDefault.isEnabled = true
|
||||||
|
}
|
||||||
|
|
||||||
|
private func updateUI() {
|
||||||
|
switch state {
|
||||||
|
case .none:
|
||||||
|
stopProgressReport()
|
||||||
|
reloadTableData(enabled: false)
|
||||||
|
swithDefaultButton(from: addButton, to: searchButton, disableOldButton: true)
|
||||||
|
updateSearchButtonVisibility()
|
||||||
|
case .searching(_):
|
||||||
|
startProgressReport()
|
||||||
|
reloadTableData(enabled: false)
|
||||||
|
swithDefaultButton(from: addButton, to: searchButton, disableOldButton: true)
|
||||||
|
case .completed(_, _):
|
||||||
|
stopProgressReport()
|
||||||
|
reloadTableData(enabled: true)
|
||||||
|
swithDefaultButton(from: searchButton, to: addButton, disableOldButton: false)
|
||||||
window?.makeFirstResponder(resultsTable)
|
window?.makeFirstResponder(resultsTable)
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
searchButton.keyEquivalent = "\r"
|
|
||||||
addButton.isEnabled = false
|
|
||||||
addButton.keyEquivalent = ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func updateSearchButtonVisibility() {
|
private func updateSearchButtonVisibility() {
|
||||||
@@ -177,9 +198,9 @@ import Cocoa
|
|||||||
if notification.object as? NSTableView == resultsTable {
|
if notification.object as? NSTableView == resultsTable {
|
||||||
switch state {
|
switch state {
|
||||||
case .none, .searching:
|
case .none, .searching:
|
||||||
selectedResult = nil
|
break
|
||||||
case .completed(let results):
|
case .completed(let results, _):
|
||||||
selectedResult = results[resultsTable.selectedRow]
|
state = .completed(results: results, selectedResult: results[resultsTable.selectedRow])
|
||||||
chapterTable.reloadData()
|
chapterTable.reloadData()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -188,14 +209,19 @@ import Cocoa
|
|||||||
func numberOfRows(in tableView: NSTableView) -> Int {
|
func numberOfRows(in tableView: NSTableView) -> Int {
|
||||||
if tableView == resultsTable {
|
if tableView == resultsTable {
|
||||||
switch state {
|
switch state {
|
||||||
case .completed(let results):
|
case .completed(let results, _):
|
||||||
return results.count
|
return results.count
|
||||||
default:
|
default:
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if tableView == chapterTable, let result = selectedResult {
|
else if tableView == chapterTable {
|
||||||
|
switch state {
|
||||||
|
case .completed(_, let result):
|
||||||
return result.chapters.count
|
return result.chapters.count
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@@ -203,7 +229,7 @@ import Cocoa
|
|||||||
func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
|
func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
|
||||||
if tableView == resultsTable {
|
if tableView == resultsTable {
|
||||||
switch state {
|
switch state {
|
||||||
case .completed(let results):
|
case .completed(let results, _):
|
||||||
let result = results[row]
|
let result = results[row]
|
||||||
|
|
||||||
if tableColumn?.identifier.rawValue == "title" {
|
if tableColumn?.identifier.rawValue == "title" {
|
||||||
@@ -219,18 +245,23 @@ import Cocoa
|
|||||||
return NSNumber(value: result.confimations)
|
return NSNumber(value: result.confimations)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return 0
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if tableView == chapterTable, let result = selectedResult {
|
else if tableView == chapterTable {
|
||||||
let chapter = result.chapters[row]
|
|
||||||
|
|
||||||
|
switch state {
|
||||||
|
case .completed(_, let result):
|
||||||
|
let chapter = result.chapters[row]
|
||||||
if tableColumn?.identifier.rawValue == "time" {
|
if tableColumn?.identifier.rawValue == "time" {
|
||||||
return StringFromTime(Int64(chapter.timestamp), 1000).boldMonospacedAttributedString()
|
return StringFromTime(Int64(chapter.timestamp), 1000).boldMonospacedAttributedString()
|
||||||
}
|
}
|
||||||
else if tableColumn?.identifier.rawValue == "name" {
|
else if tableColumn?.identifier.rawValue == "name" {
|
||||||
return chapter.name
|
return chapter.name
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user