/
PreferencesWindowController.swift
538 lines (422 loc) · 16.4 KB
/
PreferencesWindowController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//
// PreferencesWindowController.swift
// Aerial
//
// Created by John Coates on 10/23/15.
// Copyright © 2015 John Coates. All rights reserved.
//
import Cocoa
import AVKit
import AVFoundation
import ScreenSaver
class TimeOfDay {
let title: String
var videos: [AerialVideo] = [AerialVideo]()
init(title: String) {
self.title = title
}
}
class City {
var night: TimeOfDay = TimeOfDay(title: "night")
var day: TimeOfDay = TimeOfDay(title: "day")
let name: String
init(name: String) {
self.name = name
}
func addVideoForTimeOfDay(_ timeOfDay: String, video: AerialVideo) {
if timeOfDay.lowercased() == "night" {
video.arrayPosition = night.videos.count
night.videos.append(video)
} else {
video.arrayPosition = day.videos.count
day.videos.append(video)
}
}
}
@objc(PreferencesWindowController)
class PreferencesWindowController: NSWindowController, NSOutlineViewDataSource,
NSOutlineViewDelegate, VideoDownloadDelegate {
@IBOutlet var outlineView: NSOutlineView!
@IBOutlet var playerView: AVPlayerView!
@IBOutlet var differentAerialCheckbox: NSButton!
@IBOutlet var projectPageLink: NSButton!
@IBOutlet var cacheLocation: NSPathControl!
@IBOutlet var cacheAerialsAsTheyPlayCheckbox: NSButton!
var player: AVPlayer = AVPlayer()
var videos: [AerialVideo]?
// cities -> time of day -> videos
var cities = [City]()
static var loadedJSON: Bool = false
lazy var preferences = Preferences.sharedInstance
// MARK: - Init
required init?(coder decoder: NSCoder) {
super.init(coder: decoder)
}
override init(window: NSWindow?) {
super.init(window: window)
}
// MARK: - Lifecycle
override func awakeFromNib() {
super.awakeFromNib()
if let previewPlayer = AerialView.previewPlayer {
self.player = previewPlayer
}
outlineView.floatsGroupRows = false
loadJSON()
playerView.player = player
playerView.controlsStyle = .none
if #available(OSX 10.10, *) {
playerView.videoGravity = AVLayerVideoGravityResizeAspectFill
}
if preferences.differentAerialsOnEachDisplay {
differentAerialCheckbox.state = NSOnState
}
if !preferences.cacheAerials {
cacheAerialsAsTheyPlayCheckbox.state = NSOffState
}
colorizeProjectPageLink()
if let cacheDirectory = VideoCache.cacheDirectory {
cacheLocation.url = URL(fileURLWithPath: cacheDirectory as String)
}
cacheStatusLabel.isEditable = false
}
// MARK: - Setup
fileprivate func colorizeProjectPageLink() {
let color = NSColor(calibratedRed: 0.18, green: 0.39, blue: 0.76, alpha: 1)
let link = projectPageLink.attributedTitle
let coloredLink = NSMutableAttributedString(attributedString: link)
let fullRange = NSRange(location: 0, length: coloredLink.length)
coloredLink.addAttribute(NSForegroundColorAttributeName,
value: color,
range: fullRange)
projectPageLink.attributedTitle = coloredLink
}
// MARK: - Preferences
@IBAction func cacheAerialsAsTheyPlayClick(_ button: NSButton!) {
debugLog("cache aerials as they play: \(button.state)")
let onState = (button.state == NSOnState)
preferences.cacheAerials = onState
}
@IBAction func userSetCacheLocation(_ button: NSButton?) {
let openPanel = NSOpenPanel()
openPanel.canChooseDirectories = true
openPanel.canChooseFiles = false
openPanel.canCreateDirectories = true
openPanel.allowsMultipleSelection = false
openPanel.title = "Choose Aerial Cache Directory"
openPanel.prompt = "Choose"
openPanel.directoryURL = cacheLocation.url
openPanel.begin { result in
guard result == NSFileHandlingPanelOKButton,
openPanel.urls.count > 0 else {
return
}
let cacheDirectory = openPanel.urls[0]
self.preferences.customCacheDirectory = cacheDirectory.path
self.cacheLocation.url = cacheDirectory
}
}
@IBAction func resetCacheLocation(_ button: NSButton?) {
preferences.customCacheDirectory = nil
if let cacheDirectory = VideoCache.cacheDirectory {
cacheLocation.url = URL(fileURLWithPath: cacheDirectory as String)
}
}
@IBAction func outlineViewSettingsClick(_ button: NSButton) {
let menu = NSMenu()
menu.insertItem(withTitle: "Uncheck All",
action: #selector(PreferencesWindowController.outlineViewUncheckAll(button:)),
keyEquivalent: "",
at: 0)
menu.insertItem(withTitle: "Check All",
action: #selector(PreferencesWindowController.outlineViewCheckAll(button:)),
keyEquivalent: "",
at: 1)
let event = NSApp.currentEvent
NSMenu.popUpContextMenu(menu, with: event!, for: button)
}
func outlineViewUncheckAll(button: NSButton) {
setAllVideos(inRotation: false)
}
func outlineViewCheckAll(button: NSButton) {
setAllVideos(inRotation: true)
}
func setAllVideos(inRotation: Bool) {
guard let videos = videos else {
return
}
for video in videos {
preferences.setVideo(videoID: video.id,
inRotation: inRotation,
synchronize: false)
}
preferences.synchronize()
outlineView.reloadData()
}
@IBAction func differentAerialsOnEachDisplayCheckClick(_ button: NSButton?) {
let state = differentAerialCheckbox.state
let onState = (state == NSOnState)
preferences.differentAerialsOnEachDisplay = onState
debugLog("set differentAerialsOnEachDisplay to \(onState)")
}
// MARK: - Link
@IBAction func pageProjectClick(_ button: NSButton?) {
let workspace = NSWorkspace.shared()
let url = URL(string: "http://github.com/JohnCoates/Aerial")!
workspace.open(url)
}
// MARK: - Manifest
func loadJSON() {
if PreferencesWindowController.loadedJSON {
return
}
PreferencesWindowController.loadedJSON = true
ManifestLoader.instance.addCallback { manifestVideos in
self.loaded(manifestVideos: manifestVideos)
}
}
func loaded(manifestVideos: [AerialVideo]) {
var videos = [AerialVideo]()
var cities = [String: City]()
for video in manifestVideos {
let name = video.name
if cities.keys.contains(name) == false {
cities[name] = City(name: name)
}
let city = cities[name]!
let timeOfDay = video.timeOfDay
city.addVideoForTimeOfDay(timeOfDay, video: video)
videos.append(video)
}
self.videos = videos
// sort cities by name
let unsortedCities = cities.values
let sortedCities = unsortedCities.sorted { $0.name < $1.name }
self.cities = sortedCities
DispatchQueue.main.async {
self.outlineView.reloadData()
self.outlineView.expandItem(nil, expandChildren: true)
}
}
@IBAction func close(_ sender: AnyObject?) {
NSApp.mainWindow?.endSheet(window!)
}
// MARK: - Outline View Delegate & Data Source
func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
if item == nil {
return cities.count
}
switch item {
case is TimeOfDay:
let timeOfDay = item as! TimeOfDay
return timeOfDay.videos.count
case is City:
let city = item as! City
var count = 0
if city.night.videos.count > 0 {
count += 1
}
if city.day.videos.count > 0 {
count += 1
}
return count
default:
return 0
}
}
func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
switch item {
case is TimeOfDay:
return true
case is City: // cities
return true
default:
return false
}
}
func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
if item == nil {
return cities[index]
}
switch item {
case is City:
let city = item as! City
if index == 0 && city.day.videos.count > 0 {
return city.day
} else {
return city.night
}
case is TimeOfDay:
let timeOfDay = item as! TimeOfDay
return timeOfDay.videos[index]
default:
return false
}
}
func outlineView(_ outlineView: NSOutlineView,
objectValueFor tableColumn: NSTableColumn?, byItem item: Any?) -> Any? {
switch item {
case is City:
let city = item as! City
return city.name
case is TimeOfDay:
let timeOfDay = item as! TimeOfDay
return timeOfDay.title
default:
return "untitled"
}
}
func outlineView(_ outlineView: NSOutlineView, shouldEdit tableColumn: NSTableColumn?, item: Any) -> Bool {
return false
}
func outlineView(_ outlineView: NSOutlineView, dataCellFor tableColumn: NSTableColumn?, item: Any) -> NSCell? {
let row = outlineView.row(forItem: item)
return tableColumn!.dataCell(forRow: row) as? NSCell
}
func outlineView(_ outlineView: NSOutlineView, isGroupItem item: Any) -> Bool {
switch item {
case is TimeOfDay:
return true
case is City:
return true
default:
return false
}
}
func outlineView(_ outlineView: NSOutlineView,
viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
switch item {
case is City:
let city = item as! City
let view = outlineView.make(withIdentifier: "HeaderCell",
owner: self) as! NSTableCellView
view.textField?.stringValue = city.name
return view
case is TimeOfDay:
let timeOfDay = item as! TimeOfDay
let view = outlineView.make(withIdentifier: "DataCell",
owner: self) as! NSTableCellView
view.textField?.stringValue = timeOfDay.title.capitalized
let bundle = Bundle(for: PreferencesWindowController.self)
if let imagePath = bundle.path(forResource: "icon-\(timeOfDay.title)",
ofType:"pdf") {
let image = NSImage(contentsOfFile: imagePath)
view.imageView?.image = image
} else {
print("\(#file) failed to find time of day icon")
}
return view
case is AerialVideo:
let video = item as! AerialVideo
let view = outlineView.make(withIdentifier: "CheckCell",
owner: self) as! CheckCellView
// One based index
let number = video.arrayPosition + 1
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = NumberFormatter.Style.spellOut
guard
let numberString = numberFormatter.string(from: number as NSNumber)
else {
print("failed to create number with formatter")
return nil
}
view.textField?.stringValue = numberString.capitalized
let isInRotation = preferences.videoIsInRotation(videoID: video.id)
if isInRotation {
view.checkButton.state = NSOnState
} else {
view.checkButton.state = NSOffState
}
view.onCheck = { checked in
self.preferences.setVideo(videoID: video.id,
inRotation: checked)
}
return view
default:
return nil
}
}
func outlineView(_ outlineView: NSOutlineView, shouldSelectItem item: Any) -> Bool {
switch item {
case is AerialVideo:
player = AVPlayer()
playerView.player = player
let video = item as! AerialVideo
let asset = CachedOrCachingAsset(video.url)
// let asset = AVAsset(URL: video.url)
let item = AVPlayerItem(asset: asset)
player.replaceCurrentItem(with: item)
player.play()
return true
case is TimeOfDay:
return false
default:
return false
}
}
func outlineView(_ outlineView: NSOutlineView, heightOfRowByItem item: Any) -> CGFloat {
switch item {
case is AerialVideo:
return 18
case is TimeOfDay:
return 18
case is City:
return 18
default:
return 0
}
}
// MARK: - Caching
@IBOutlet var totalProgress: NSProgressIndicator!
@IBOutlet var currentProgress: NSProgressIndicator!
@IBOutlet var cacheStatusLabel: NSTextField!
var currentVideoDownload: VideoDownload?
var manifestVideos: [AerialVideo]?
@IBAction func cacheAllNow(_ button: NSButton) {
cacheStatusLabel.stringValue = "Loading JSON"
currentProgress.maxValue = 1
ManifestLoader.instance.addCallback { (manifestVideos: [AerialVideo]) -> Void in
DispatchQueue.main.async(execute: { () -> Void in
self.manifestVideos = manifestVideos
self.cacheNextVideo()
})
}
}
func cacheNextVideo() {
guard let manifestVideos = self.manifestVideos else {
cacheStatusLabel.stringValue = "Couldn't load manifest!"
return
}
let uncached = manifestVideos.filter { (video) -> Bool in
return video.isAvailableOffline == false
}
if uncached.count == 0 {
cacheStatusLabel.stringValue = "All videos have been cached"
return
}
NSLog("uncached: \(uncached)")
totalProgress.maxValue = Double(manifestVideos.count)
totalProgress.doubleValue = Double(manifestVideos.count) - Double(uncached.count)
NSLog("total process max value: \(totalProgress.maxValue), current value: \(totalProgress.doubleValue)")
let video = uncached[0]
// find video that hasn't been cached
let videoDownload = VideoDownload(video: video, delegate: self)
cacheStatusLabel.stringValue = "Caching video \(video.name) \(video.timeOfDay.capitalized): \(video.id)"
currentVideoDownload = videoDownload
videoDownload.startDownload()
}
// MARK: - Video Download Delegate
func videoDownload(_ videoDownload: VideoDownload,
finished success: Bool, errorMessage: String?) {
if let message = errorMessage {
cacheStatusLabel.stringValue = message
} else {
cacheNextVideo()
}
NSLog("video download finished with success: \(success))")
}
func videoDownload(_ videoDownload: VideoDownload, receivedBytes: Int, progress: Float) {
currentProgress.doubleValue = Double(progress)
// NSLog("received bytes: \(receivedBytes), progress: \(progress)")
}
}