-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAppDelegate.swift
426 lines (369 loc) · 15.4 KB
/
AppDelegate.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
//
// AppDelegate.swift
// Milkshake
//
// Created by Dean Liu on 11/27/17.
// Copyright © 2017 Dean Liu. All rights reserved.
//
import Cocoa
import HotKey
extension NSStoryboard {
private class func mainStoryboard() -> NSStoryboard { return NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil) }
private class func loginStoryboard() -> NSStoryboard { return NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil) }
class func mainViewController() -> MainViewController {
return self.mainStoryboard().instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "MainViewController")) as! MainViewController
}
class func windowController() -> NSWindowController {
return self.mainStoryboard().instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "WindowController")) as! NSWindowController
}
class func loginWindowController() -> NSWindowController {
return self.mainStoryboard().instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "LoginWindowController")) as! NSWindowController
}
}
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, LoginProtocol {
var api: API
var music: Music? // pointer to either dj or radio
var dj: DJ
var radio: Radio
var history: History
var windowController: NSWindowController?
var loginWindowController: NSWindowController?
var isArtAnimate: Bool = false
var isSpectrumAnimate: Bool = true
var isPremium: Bool = false
var listenerId: String = ""
var statusItem: NSStatusItem?
@IBOutlet weak var menuKeepWindowFront: NSMenuItem!
@IBOutlet weak var menuCrossFade: NSMenuItem!
@IBOutlet weak var menuShuffle: NSMenuItem!
@IBOutlet weak var menuSpectrum: NSMenuItem!
@IBOutlet weak var menuPlaylist: NSMenuItem!
@IBOutlet weak var menuArtists: NSMenuItem!
@IBOutlet weak var menuHistory: NSMenuItem!
private var hotKey: HotKey? {
didSet {
guard let hotKey = hotKey else {
return
}
hotKey.keyDownHandler = { [weak self] in
print(hotKey.keyCombo)
let keyCode = hotKey.keyCombo.carbonKeyCode
let modifier = hotKey.keyCombo.carbonModifiers
let mainVc = self?.windowController?.contentViewController as! MainViewController
if keyCode == 0x12 && modifier == 0x100 { mainVc.loadNowPlaying(self!) }
else if keyCode == 0x13 && modifier == 0x100 {
mainVc.loadStationResults(self!)
}
else if keyCode == 0x14 && modifier == 0x100 && self!.isPremium {
mainVc.loadPlaylistResults(self!)
}
// u
else if keyCode == 0x2E && modifier == 0x1100 { mainVc.showWindow() }
// p
else if keyCode == 0x23 && modifier == 0x1100 {
if let music = self?.music {
if music.isPlaying() {
music.playerPause()
} else {
music.playerPlay()
}
}
}
// r
else if keyCode == 0xF && modifier == 0x1100 {
if let music = self?.music {
if music.isPlaying() {
music.playerRepeat()
}
}
}
// [
else if keyCode == 0x1E && modifier == 0x1100 {
if let music = self?.music {
if music.isPlaying() {
music.playNext()
}
}
}
// ]
else if keyCode == 0x21 && modifier == 0x1100 {
if let music = self?.music {
if music.isPlaying() {
music.playPrev()
}
}
}
// -
else if keyCode == 0x1B && modifier == 0x1100 {
if (self?.radio.isPlaying())! {
self?.radio.thumbDown()
}
}
// +
else if keyCode == 0x18 && modifier == 0x1100 {
if (self?.radio.isPlaying())! {
mainVc.nowPlayingViewController.thumbsUp(self!)
}
}
}
}
}
override init() {
self.api = API()
self.dj = DJ()
self.radio = Radio()
self.history = History()
super.init()
}
@IBOutlet weak var toolbarMenu: NSMenu!
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
self.loginWindowController = NSStoryboard.loginWindowController()
(self.loginWindowController!.contentViewController as! LoginViewController).delegate = self
self.loginWindowController?.window?.isMovableByWindowBackground = true
self.loginWindowController!.showWindow(self)
OperationQueue.main.addOperation {
self.loginWindowController!.window?.makeKeyAndOrderFront(nil)
NSApplication.shared.activate(ignoringOtherApps: true)
}
self.loginWindowController!.window?.makeKey()
self.loginWindowController!.window?.makeKeyAndOrderFront(nil)
self.api.X_AuthToken = "BI2rvoepGAXRGYy3Yr9iVFh7fL+FAYfWG4hJpRtIVdB2DZenI/yXST/g=="
initStatusItem()
// self.launchMain()
}
private func initStatusItem() {
self.statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
// statusItem?.title = "Test Item"
let icon = NSImage(named: NSImage.Name(rawValue: "milkshake_20x20.png"))
icon?.isTemplate = true
statusItem?.image = icon
statusItem?.menu = self.toolbarMenu
}
func applicationDidBecomeActive(_ notification: Notification) {
NSEvent.addLocalMonitorForEvents(matching: NSEvent.EventTypeMask.keyDown, handler: keyDown);
}
func applicationWillBecomeActive(_ notification: Notification) {
if let cvc = self.windowController?.contentViewController {
let mainVC = cvc as! MainViewController
mainVC.showWindow()
}
}
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if let window = self.windowController?.window {
if flag {
window.orderFront(nil)
} else {
window.makeKeyAndOrderFront(nil)
}
} else if let window = self.loginWindowController?.window {
if flag {
window.orderFront(nil)
} else {
window.makeKeyAndOrderFront(nil)
}
}
return true
}
func keyDown(event: NSEvent!) -> NSEvent {
if let mvc = NSApplication.shared.mainWindow?.windowController?.contentViewController as? MainViewController {
// cmd + w
if Int(event.modifierFlags.rawValue) == 0x100108 && event.keyCode == 0xD {
mvc.hideWindow()
} else if event.keyCode == 0x7D || event.keyCode == 0x7E {
if mvc._curViewController is ResultsViewController {
var idx = 0
let rc = mvc._curViewController as! ResultsViewController
if(rc.searchTableView.selectedRow < 0) {
idx = 0;
} else {
idx = rc.searchTableView.selectedRow ;
}
rc.searchTableView.selectRowIndexes(NSIndexSet(index: idx) as IndexSet, byExtendingSelection: false)
mvc.view.window?.makeFirstResponder(rc.searchTableView)
}
} else if event.keyCode == 0x35 {
mvc.escKeyProtocol()
} else {
// mvc.view.window?.makeFirstResponder(mvc.searchField)
}
}
return event
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
func launchMain() {
OperationQueue.main.addOperation {
self.windowController = NSStoryboard.windowController()
self.windowController!.showWindow(self)
self.windowController!.loadWindow()
self.windowController!.window?.makeKeyAndOrderFront(self)
self.windowController!.window?.center()
self.windowController?.window?.isMovableByWindowBackground = true
self.register(self) // hotkeys
self.loginWindowController?.close()
}
// if let bid = Bundle.main.bundleIdentifier {
// UserDefaults.standard.removePersistentDomain(forName: bid)
// }
}
func handleSuccessLogin(results: Dictionary<String, AnyObject>) {
let authToken = results["userAuthToken"] as! String
self.listenerId = results["userId"] as! String
let defaults = UserDefaults.standard
defaults.set(authToken, forKey: "authToken")
self.api.X_AuthToken = authToken
self.api.billingInfo() { results in
self.isPremium = ((results["activeProduct"]?["productTier"] as? String) ?? "").lowercased() == "pandora_premium"
if self.isPremium == false {
self.menuArtists.isHidden = true
self.menuPlaylist.isHidden = true
self.menuShuffle.isHidden = true
}
self.launchMain()
}
}
func isRadio() -> Bool {
return self.music == self.radio
}
@IBAction func showWindowAction(_ sender: Any) {
if let cvc = self.windowController?.contentViewController {
let mainVC = cvc as! MainViewController
mainVC.showWindow()
}
}
@IBAction func playPause(_ sender: Any) {
if let music = self.music {
if music.isPlaying() {
music.playerPause()
} else {
music.playerPlay()
}
}
}
@IBAction func previousSong(_ sender: Any) {
if let music = self.music {
music.playPrev()
}
}
@IBAction func nextSong(_ sender: Any) {
if let music = self.music {
music.playNext()
}
}
@IBAction func repeatSong(_ sender: Any) {
if let music = self.music {
music.playerRepeat()
}
}
@IBAction func thumbDown(_ sender: Any) {
if let music = self.music {
if music is Radio {
(music as! Radio).thumbDown()
}
}
}
@IBAction func thumbUp(_ sender: Any) {
if let cvc = self.windowController?.contentViewController {
let mainVC = cvc as! MainViewController
mainVC.nowPlayingViewController.thumbsUp(self)
} else if let music = self.music {
if music is Radio {
(music as! Radio).thumbUp()
}
}
}
@IBAction func loadPlaying(_ sender: Any) {
if let cvc = self.windowController?.contentViewController {
let mainVC = cvc as! MainViewController
mainVC.loadNowPlaying(self)
}
}
@IBAction func loadStations(_ sender: Any) {
if let cvc = self.windowController?.contentViewController {
let mainVC = cvc as! MainViewController
mainVC.loadStationResults(self)
}
}
@IBAction func loadPlaylist(_ sender: Any) {
if let cvc = self.windowController?.contentViewController {
let mainVC = cvc as! MainViewController
mainVC.loadPlaylistResults(self)
}
}
@IBAction func loadArtists(_ sender: Any) {
if let cvc = self.windowController?.contentViewController {
let mainVC = cvc as! MainViewController
mainVC.loadArtistResults(self)
}
}
@IBAction func loadHistory(_ sender: Any) {
if let cvc = self.windowController?.contentViewController {
let mainVC = cvc as! MainViewController
mainVC.loadHistoryResults(self)
}
}
@IBAction func keepWindowFront(_ sender: Any) {
if self.menuKeepWindowFront.title == "Keep Window Front" {
self.menuKeepWindowFront.title = "Disable Window Front"
self.windowController?.window?.level = NSWindow.Level.statusBar
} else {
self.menuKeepWindowFront.title = "Keep Window Front"
self.windowController?.window?.level = NSWindow.Level.normal
}
}
@IBAction func enableSpectrum(_ sender: Any) {
if self.menuSpectrum.title == "Enable Spectrum" {
self.menuSpectrum.title = "Disable Spectrum"
self.isSpectrumAnimate = true
if let cvc = self.windowController?.contentViewController {
let mainVC = cvc as! MainViewController
mainVC.nowPlayingViewController.animateSpectrum = true
}
} else {
self.menuSpectrum.title = "Enable Spectrum"
self.isSpectrumAnimate = false
if let cvc = self.windowController?.contentViewController {
let mainVC = cvc as! MainViewController
mainVC.nowPlayingViewController.animateSpectrum = false
}
}
}
@IBAction func enableCrossFade(_ sender: Any) {
if self.menuCrossFade.title == "Enable Crossfade" {
self.menuCrossFade.title = "Disable Crossfade"
self.dj.crossFade = true
self.radio.crossFade = true
} else {
self.menuCrossFade.title = "Enable Crossfade"
self.dj.crossFade = false
self.radio.crossFade = false
}
}
@IBAction func enableShuffle(_ sender: Any) {
if self.menuShuffle.title == "Enable Shuffle" {
self.menuShuffle.title = "Disable Shuffle"
self.dj.shuffle = true
} else {
self.menuShuffle.title = "Enable Shuffle"
self.dj.shuffle = false
}
}
@IBAction func unregister(_ sender: Any?) {
hotKey = nil
}
@IBAction func register(_ sender: Any?) {
hotKey = HotKey(keyCombo: KeyCombo(key: .minus, modifiers: [.command, .control]))
hotKey = HotKey(keyCombo: KeyCombo(key: .equal, modifiers: [.command, .control]))
hotKey = HotKey(keyCombo: KeyCombo(key: .m, modifiers: [.command, .control])) // bring up menu
hotKey = HotKey(keyCombo: KeyCombo(key: .p, modifiers: [.command, .control])) //play/pause
hotKey = HotKey(keyCombo: KeyCombo(key: .r, modifiers: [.command, .control])) //repeat
hotKey = HotKey(keyCombo: KeyCombo(key: .leftBracket, modifiers: [.command, .control])) //next track
hotKey = HotKey(keyCombo: KeyCombo(key: .rightBracket, modifiers: [.command, .control])) //prev track
// hotKey = HotKey(keyCombo: KeyCombo(key: .one, modifiers: [.command])) //now playing
// hotKey = HotKey(keyCombo: KeyCombo(key: .two, modifiers: [.command])) //stations
// hotKey = HotKey(keyCombo: KeyCombo(key: .three, modifiers: [.command])) //playlist
}
}