From 3c9a18a9fe5bd7129b52e2aa820d90e461c7f55b Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Sun, 14 Apr 2019 20:09:32 -0400 Subject: [PATCH] Move notification into its own service --- Persephone.xcodeproj/project.pbxproj | 4 ++++ .../Controllers/QueueViewController.swift | 8 ++----- Persephone/Services/SongNotifierService.swift | 23 +++++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 Persephone/Services/SongNotifierService.swift diff --git a/Persephone.xcodeproj/project.pbxproj b/Persephone.xcodeproj/project.pbxproj index a3f500c..70eef21 100644 --- a/Persephone.xcodeproj/project.pbxproj +++ b/Persephone.xcodeproj/project.pbxproj @@ -42,6 +42,7 @@ E42A8F3C22176D6400A13ED9 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = E42A8F3A22176D6400A13ED9 /* README.md */; }; E435E3E2221CD4E200184CFC /* NSFont.swift in Sources */ = {isa = PBXBuildFile; fileRef = E435E3E1221CD4E200184CFC /* NSFont.swift */; }; E435E3E4221CD75D00184CFC /* NSImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = E435E3E3221CD75D00184CFC /* NSImage.swift */; }; + E439109822640213002982E9 /* SongNotifierService.swift in Sources */ = {isa = PBXBuildFile; fileRef = E439109722640213002982E9 /* SongNotifierService.swift */; }; E450AD7E222620A10091BED3 /* Album.swift in Sources */ = {isa = PBXBuildFile; fileRef = E450AD7D222620A10091BED3 /* Album.swift */; }; E450AD8622262AE60091BED3 /* SwiftyJSON.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E450AD8522262AE60091BED3 /* SwiftyJSON.framework */; }; E450AD8822262AEC0091BED3 /* SwiftyJSON.framework in Embed Libraries */ = {isa = PBXBuildFile; fileRef = E450AD8522262AE60091BED3 /* SwiftyJSON.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; @@ -210,6 +211,7 @@ E42A8F3A22176D6400A13ED9 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; E435E3E1221CD4E200184CFC /* NSFont.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSFont.swift; sourceTree = ""; }; E435E3E3221CD75D00184CFC /* NSImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSImage.swift; sourceTree = ""; }; + E439109722640213002982E9 /* SongNotifierService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SongNotifierService.swift; sourceTree = ""; }; E450AD7D222620A10091BED3 /* Album.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Album.swift; sourceTree = ""; }; E450AD8522262AE60091BED3 /* SwiftyJSON.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SwiftyJSON.framework; path = Carthage/Build/Mac/SwiftyJSON.framework; sourceTree = ""; }; E450AD8C22262C590091BED3 /* PromiseKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = PromiseKit.framework; path = Carthage/Build/Mac/PromiseKit.framework; sourceTree = ""; }; @@ -523,6 +525,7 @@ isa = PBXGroup; children = ( E4A83BF3222207D50098FED6 /* AlbumArtService.swift */, + E439109722640213002982E9 /* SongNotifierService.swift */, E41E530C223EF4BA00173814 /* Extensions */, ); path = Services; @@ -797,6 +800,7 @@ E40F41F3221EDE27004B6CB8 /* Preferences.swift in Sources */, E47E2FDD2220A6D100F747E6 /* Time.swift in Sources */, E419E2872249B96600216A8C /* Song.swift in Sources */, + E439109822640213002982E9 /* SongNotifierService.swift in Sources */, E407861C2110CE6E006887B1 /* AppDelegate.swift in Sources */, E41E5309223C020400173814 /* MPDClient+Command.swift in Sources */, E47E2FE52220AA0700F747E6 /* AlbumViewLayout.swift in Sources */, diff --git a/Persephone/Controllers/QueueViewController.swift b/Persephone/Controllers/QueueViewController.swift index acd79bd..586be21 100644 --- a/Persephone/Controllers/QueueViewController.swift +++ b/Persephone/Controllers/QueueViewController.swift @@ -55,12 +55,8 @@ class QueueViewController: NSViewController, status.state == .playing else { return } - let notification = NSUserNotification() - notification.title = currentSong.title - notification.subtitle = "\(currentSong.artist) — \(currentSong.album.title)" - notification.contentImage = queueAlbumArtImage.image - - NSUserNotificationCenter.default.deliver(notification) + SongNotifierService(song: currentSong, image: queueAlbumArtImage.image) + .deliver() } @objc func queueChanged(_ notification: Notification) { diff --git a/Persephone/Services/SongNotifierService.swift b/Persephone/Services/SongNotifierService.swift new file mode 100644 index 0000000..aadabe3 --- /dev/null +++ b/Persephone/Services/SongNotifierService.swift @@ -0,0 +1,23 @@ +// +// SongNotifierService.swift +// Persephone +// +// Created by Daniel Barber on 2019/4/14. +// Copyright © 2019 Dan Barber. All rights reserved. +// + +import Cocoa + +struct SongNotifierService { + let song: Song + let image: NSImage? + + func deliver() { + let notification = NSUserNotification() + notification.title = song.title + notification.subtitle = "\(song.artist) — \(song.album.title)" + notification.contentImage = image + + NSUserNotificationCenter.default.deliver(notification) + } +}