1
1
mirror of https://github.com/danbee/persephone synced 2025-03-04 08:39:11 +00:00

Wire up "update database"

* Menu option starts a database update.

* Spinner to show that update is taking place.
This commit is contained in:
Daniel Barber 2019-03-19 21:56:14 -04:00
parent 0147ffbfda
commit 8983106cda
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
12 changed files with 102 additions and 1 deletions

View File

@ -36,6 +36,7 @@
E41EA46C221636AF0068EF46 /* GeneralPrefsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E41EA46B221636AF0068EF46 /* GeneralPrefsViewController.swift */; };
E421ACA3221F73C4008B2449 /* MediaKeyTap.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E421ACA1221F73B8008B2449 /* MediaKeyTap.framework */; };
E421ACA4221F73C4008B2449 /* MediaKeyTap.framework in Embed Libraries */ = {isa = PBXBuildFile; fileRef = E421ACA1221F73B8008B2449 /* MediaKeyTap.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
E42410B62241B956005ED6DF /* MPDClient+Database.swift in Sources */ = {isa = PBXBuildFile; fileRef = E42410B52241B956005ED6DF /* MPDClient+Database.swift */; };
E42A8F3B22176D6400A13ED9 /* LICENSE.md in Resources */ = {isa = PBXBuildFile; fileRef = E42A8F3922176D6400A13ED9 /* LICENSE.md */; };
E42A8F3C22176D6400A13ED9 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = E42A8F3A22176D6400A13ED9 /* README.md */; };
E435E3E2221CD4E200184CFC /* NSFont.swift in Sources */ = {isa = PBXBuildFile; fileRef = E435E3E1221CD4E200184CFC /* NSFont.swift */; };
@ -195,6 +196,7 @@
E41E5311223EF74A00173814 /* AlbumArtService+Filesystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AlbumArtService+Filesystem.swift"; sourceTree = "<group>"; };
E41EA46B221636AF0068EF46 /* GeneralPrefsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralPrefsViewController.swift; sourceTree = "<group>"; };
E421ACA1221F73B8008B2449 /* MediaKeyTap.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MediaKeyTap.framework; path = Carthage/Build/Mac/MediaKeyTap.framework; sourceTree = "<group>"; };
E42410B52241B956005ED6DF /* MPDClient+Database.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MPDClient+Database.swift"; sourceTree = "<group>"; };
E42A8F3922176D6400A13ED9 /* LICENSE.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = LICENSE.md; sourceTree = "<group>"; };
E42A8F3A22176D6400A13ED9 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
E435E3E1221CD4E200184CFC /* NSFont.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSFont.swift; sourceTree = "<group>"; };
@ -351,6 +353,7 @@
isa = PBXGroup;
children = (
E41E52FC223BF87300173814 /* MPDClient+Connection.swift */,
E42410B52241B956005ED6DF /* MPDClient+Database.swift */,
E41E52FE223BF95E00173814 /* MPDClient+Transport.swift */,
E41E5300223BF99300173814 /* MPDClient+Queue.swift */,
E41E5302223BF9C300173814 /* MPDClient+Idle.swift */,
@ -749,6 +752,7 @@
E41E5307223C019100173814 /* MPDClient+Status.swift in Sources */,
E41E5310223EF6CE00173814 /* AlbumArtService+Remote.swift in Sources */,
E41E530B223C033700173814 /* MPDClient+Album.swift in Sources */,
E42410B62241B956005ED6DF /* MPDClient+Database.swift in Sources */,
E4A642DA22090CBE00067D21 /* Status.swift in Sources */,
E47E2FD72220720300F747E6 /* AlbumItemView.swift in Sources */,
E450AD9522262DF10091BED3 /* AlbumArtQueue.swift in Sources */,

View File

@ -68,4 +68,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, MediaKeyTapDelegate {
func disconnect() {
AppDelegate.mpdClient.disconnect()
}
@IBAction func updateDatabase(_ sender: NSMenuItem) {
AppDelegate.mpdClient.updateDatabase()
}
}

View File

@ -36,6 +36,14 @@ class NotificationsController: MPDClientDelegate {
)
}
func willUpdateDatabase(mpdClient: MPDClient) {
sendNotification(name: Notification.databaseUpdateStarted)
}
func didUpdateDatabase(mpdClient: MPDClient) {
sendNotification(name: Notification.databaseUpdated)
}
func didUpdateQueue(mpdClient: MPDClient, queue: [MPDClient.Song]) {
sendNotification(
name: Notification.queueChanged,

View File

@ -36,6 +36,20 @@ class WindowController: NSWindowController {
object: AppDelegate.mpdClient
)
NotificationCenter.default.addObserver(
self,
selector: #selector(startDatabaseUpdatingIndicator),
name: Notification.databaseUpdateStarted,
object: AppDelegate.mpdClient
)
NotificationCenter.default.addObserver(
self,
selector: #selector(stopDatabaseUpdatingIndicator),
name: Notification.databaseUpdated,
object: AppDelegate.mpdClient
)
trackProgress.font = .timerFont
trackRemaining.font = .timerFont
}
@ -132,6 +146,14 @@ class WindowController: NSWindowController {
setTimeRemaining()
}
@objc func startDatabaseUpdatingIndicator() {
databaseUpdatingIndicator.startAnimation(self)
}
@objc func stopDatabaseUpdatingIndicator() {
databaseUpdatingIndicator.stopAnimation(self)
}
func setTimeElapsed() {
guard let elapsedTimeMs = elapsedTimeMs else { return }
@ -194,4 +216,5 @@ class WindowController: NSWindowController {
@IBOutlet var trackProgress: NSTextField!
@IBOutlet var trackProgressBar: NSSlider!
@IBOutlet var trackRemaining: NSTextField!
@IBOutlet var databaseUpdatingIndicator: NSProgressIndicator!
}

View File

@ -14,6 +14,8 @@ extension Notification {
static let stateChanged = Name("MPDClientStateChanged")
static let timeChanged = Name("MPDClientTimeChanged")
static let databaseUpdateStarted = Name("MPDClientDatabaseUpdateStarted")
static let databaseUpdated = Name("MPDClientDatabaseUpdated")
static let queueChanged = Name("MPDClientQueueChanged")
static let queuePosChanged = Name("MPDClientQueuePosChanged")
static let loadedAlbums = Name("MPDClientLoadedAlbums")

View File

@ -29,6 +29,10 @@ extension MPDClient {
else { return }
sendSeekCurrentSong(timeInSeconds: timeInSeconds)
// Database commands
case .updateDatabase:
sendUpdateDatabase()
// Status commands
case .fetchStatus:
sendRunStatus()

View File

@ -0,0 +1,20 @@
//
// MPDClient+Database.swift
// Persephone
//
// Created by Daniel Barber on 2019/3/19.
// Copyright © 2019 Dan Barber. All rights reserved.
//
import Foundation
import mpdclient
extension MPDClient {
func updateDatabase() {
enqueueCommand(command: .updateDatabase)
}
func sendUpdateDatabase() {
mpd_run_update(connection, nil)
}
}

View File

@ -32,6 +32,9 @@ extension MPDClient {
let mpdIdle = Idle(rawValue: result.rawValue)
if mpdIdle.contains(.database) {
self.fetchAllAlbums()
}
if mpdIdle.contains(.queue) {
self.fetchQueue()
self.delegate?.didUpdateQueue(mpdClient: self, queue: self.queue)
@ -45,6 +48,15 @@ extension MPDClient {
self.delegate?.didUpdateQueuePos(mpdClient: self, song: status.song)
}
}
if mpdIdle.contains(.update) {
self.fetchStatus()
if self.status?.updating ?? false {
self.delegate?.willUpdateDatabase(mpdClient: self)
} else {
self.delegate?.didUpdateDatabase(mpdClient: self)
}
}
if !mpdIdle.isEmpty {
self.idle()
}

View File

@ -17,6 +17,9 @@ extension MPDClient {
case stop
case seekCurrentSong
// Database commands
case updateDatabase
// Status commands
case fetchStatus

View File

@ -49,5 +49,11 @@ extension MPDClient {
var song: Int {
return Int(mpd_status_get_song_pos(mpdStatus))
}
var updating: Bool {
let updating = mpd_status_get_update_id(mpdStatus)
return updating > 0
}
}
}

View File

@ -15,6 +15,9 @@ protocol MPDClientDelegate {
func didUpdateState(mpdClient: MPDClient, state: MPDClient.Status.State)
func didUpdateTime(mpdClient: MPDClient, total: UInt, elapsedMs: UInt)
func willUpdateDatabase(mpdClient: MPDClient)
func didUpdateDatabase(mpdClient: MPDClient)
func didUpdateQueue(mpdClient: MPDClient, queue: [MPDClient.Song])
func didUpdateQueuePos(mpdClient: MPDClient, song: Int)

View File

@ -66,8 +66,11 @@
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Database" id="rFP-zL-1X4">
<items>
<menuItem title="Update" enabled="NO" keyEquivalent="u" id="EJg-93-1F6">
<menuItem title="Update" keyEquivalent="u" id="EJg-93-1F6">
<modifierMask key="keyEquivalentModifierMask" option="YES" command="YES"/>
<connections>
<action selector="updateDatabase:" target="Voe-Tx-rLC" id="FO1-Ge-TUL"/>
</connections>
</menuItem>
</items>
</menu>
@ -195,9 +198,17 @@
</textFieldCell>
</textField>
</toolbarItem>
<toolbarItem implicitItemIdentifier="6B3CE282-B6C1-48BF-8962-1A02892D8DF8" label="" paletteLabel="" tag="-1" sizingBehavior="auto" id="fw4-Lp-bWJ">
<nil key="toolTip"/>
<progressIndicator key="view" identifier="databaseUpdatingIndicator" wantsLayer="YES" horizontalHuggingPriority="750" verticalHuggingPriority="750" maxValue="100" displayedWhenStopped="NO" bezeled="NO" indeterminate="YES" controlSize="small" style="spinning" id="LpV-iM-o6t">
<rect key="frame" x="0.0" y="14" width="16" height="16"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
</progressIndicator>
</toolbarItem>
</allowedToolbarItems>
<defaultToolbarItems>
<toolbarItem reference="p3r-ty-Pxf"/>
<toolbarItem reference="fw4-Lp-bWJ"/>
<toolbarItem reference="9ol-aR-mzv"/>
<toolbarItem reference="n52-8S-6kR"/>
<toolbarItem reference="s1h-EC-nvL"/>
@ -210,6 +221,7 @@
</connections>
</window>
<connections>
<outlet property="databaseUpdatingIndicator" destination="LpV-iM-o6t" id="y0T-eR-ygY"/>
<outlet property="trackProgress" destination="kx6-xm-TAN" id="XDv-Th-Agj"/>
<outlet property="trackProgressBar" destination="KMy-xf-rmN" id="a67-JU-cyQ"/>
<outlet property="trackRemaining" destination="9WZ-ij-lrb" id="0pH-d7-wvD"/>