Skip to content

Commit

Permalink
Fix high CPU usage when app was started manually
Browse files Browse the repository at this point in the history
  • Loading branch information
Ty3uK committed Mar 20, 2019
1 parent a1b7b11 commit 63cca7a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 35 deletions.
48 changes: 26 additions & 22 deletions native/YMC/Application/AppDelegate/AppDelegate+Messaging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension AppDelegate {
}

@objc func refreshInterval(_ timer: Timer) {
writeMessage("REFRESH")
sendMessageToFrontend("REFRESH")
}

func readMessages() {
Expand All @@ -43,30 +43,34 @@ extension AppDelegate {
message = String(data: data.subdata(in: 4..<(length + 4)), encoding: .utf8) ?? ""
}

Logger.instance.log(logLevel: .info, message: "MESSAGE FROM EXT: \(message)")

if message.count == 0 { return }

guard let json = try? JSONDecoder().decode(IncomingMessage.self, from: message.data(using: .utf8)!) else { return }
self.handleMessage(message.data(using: .utf8)!)
}
}

func handleMessage(_ message: Data) {
guard let json = try? JSONDecoder().decode(IncomingMessage.self, from: message) else { return }

switch json.data {
case let data as PlayerState:
Player.instance.changeCoverImage(data.currentTrack.cover)
Player.instance.changeCurrentTrack(data.currentTrack)
Player.instance.changeControls(data.controls)
Player.instance.changePlayingState(data.isPlaying)
case let data as CurrentTrack:
Player.instance.changeCoverImage(data.cover)
Player.instance.changeCurrentTrack(data)
case let data as Controls:
Player.instance.changeControls(data)
case let data as IsPlaying:
Player.instance.changePlayingState(data.state)
case let data as IsLiked:
Player.instance.changeLikedState(data.state)
default:
return
}
Logger.instance.log(logLevel: .info, message: "MESSAGE FROM EXT: \(json)")

switch json.data {
case let data as PlayerState:
Player.instance.changeCoverImage(data.currentTrack.cover)
Player.instance.changeCurrentTrack(data.currentTrack)
Player.instance.changeControls(data.controls)
Player.instance.changePlayingState(data.isPlaying)
case let data as CurrentTrack:
Player.instance.changeCoverImage(data.cover)
Player.instance.changeCurrentTrack(data)
case let data as Controls:
Player.instance.changeControls(data)
case let data as IsPlaying:
Player.instance.changePlayingState(data.state)
case let data as IsLiked:
Player.instance.changeLikedState(data.state)
default:
return
}
}
}
30 changes: 19 additions & 11 deletions native/YMC/Application/AppDelegate/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
let popover = NSPopover()

let runStdinMonitoring = CommandLine.arguments.first(where: { $0.contains("ymc.json") }) != nil
var playerViewController: PlayerViewController? = nil
var mediaKeyTap: MediaKeyTap? = nil
var isAccessibilityAvailable = false

let disposeBag = DisposeBag()

override init() {
super.init()
}

func applicationDidFinishLaunching(_ aNotification: Notification) {
prepareLaunch()

Expand All @@ -37,7 +34,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
setupPopover()
setupRefreshInterval()
readKeys()
readMessages()

if runStdinMonitoring {
readMessages()
}

Logger.instance.log(logLevel: .info, message: "runStdinMonitoring: \(runStdinMonitoring)")

Player.instance.buttonPress$.subscribe { event in
guard let button = event.element else { return }
Expand All @@ -48,29 +50,35 @@ class AppDelegate: NSObject, NSApplicationDelegate {
case .LINK:
self.copyTrackLinkToClipboard()
case .PLAY_PAUSE:
writeMessage("PLAY_PAUSE")
self.sendMessageToFrontend("PLAY_PAUSE")
case .LIKE:
writeMessage("TOGGLE_LIKE")
self.sendMessageToFrontend("TOGGLE_LIKE")
case .NEXT:
writeMessage("NEXT")
self.sendMessageToFrontend("NEXT")
case .PREV:
writeMessage("PREV")
self.sendMessageToFrontend("PREV")
case .REFRESH:
writeMessage("REFRESH")
self.sendMessageToFrontend("REFRESH")
default:
return
}
}.disposed(by: disposeBag)

DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
writeMessage("REFRESH")
self.sendMessageToFrontend("REFRESH")
}
}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}

func sendMessageToFrontend(_ messageType: String) {
if runStdinMonitoring {
writeMessage(messageType)
}
}

@objc func togglePopover(_ sender: NSButton) {
if popover.isShown {
popover.performClose(sender)
Expand Down
15 changes: 13 additions & 2 deletions native/YMC/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,20 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0.1</string>
<string>1.0.2</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>info.karelov.YMC</string>
<key>CFBundleURLSchemes</key>
<array>
<string>ymc</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>2</string>
<string>3</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>LSUIElement</key>
Expand Down

0 comments on commit 63cca7a

Please sign in to comment.