From ea1f61775b67a3b00f6731cb9208bce3d47e522b Mon Sep 17 00:00:00 2001 From: Xander Deng Date: Wed, 10 May 2017 10:55:09 +0800 Subject: [PATCH] feat: mark wrong lyrics and never search again, close #24, close #32 --- LyricsX.xcodeproj/project.pbxproj | 4 ++ LyricsX/AppController.swift | 7 +++ LyricsX/AppDelegate.swift | 8 ++++ LyricsX/Base.lproj/Main.storyboard | 6 +++ LyricsX/Extension.swift | 16 +++++++ LyricsX/Support Files/Info.plist | 2 +- LyricsX/WrongLyricsUtil.swift | 69 ++++++++++++++++++++++++++++++ 7 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 LyricsX/WrongLyricsUtil.swift diff --git a/LyricsX.xcodeproj/project.pbxproj b/LyricsX.xcodeproj/project.pbxproj index 317bf776..6007623f 100644 --- a/LyricsX.xcodeproj/project.pbxproj +++ b/LyricsX.xcodeproj/project.pbxproj @@ -59,6 +59,7 @@ BBD03C371E9B2C7200EFB975 /* DragNDropView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBD03C361E9B2C7200EFB975 /* DragNDropView.swift */; }; BBE557971EC0291E00F1EC31 /* GenericID.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BBE557961EC0291E00F1EC31 /* GenericID.framework */; }; BBE557981EC0291E00F1EC31 /* GenericID.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = BBE557961EC0291E00F1EC31 /* GenericID.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; + BBE5579B1EC2AB3700F1EC31 /* WrongLyricsUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBE5579A1EC2AB3700F1EC31 /* WrongLyricsUtil.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -156,6 +157,7 @@ BBC811791E9E1D0000331CFC /* MusicPlayerManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MusicPlayerManager.swift; path = MediaPlayer/MusicPlayerManager.swift; sourceTree = ""; }; BBD03C361E9B2C7200EFB975 /* DragNDropView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = DragNDropView.swift; path = View/DragNDropView.swift; sourceTree = ""; }; BBE557961EC0291E00F1EC31 /* GenericID.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GenericID.framework; path = Carthage/Build/Mac/GenericID.framework; sourceTree = ""; }; + BBE5579A1EC2AB3700F1EC31 /* WrongLyricsUtil.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WrongLyricsUtil.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -301,6 +303,7 @@ BBC1D35E1E499A6D002538A2 /* Global.swift */, BB9BCEB91E843157001BC54B /* Extension.swift */, BB5596E11EB856140064D278 /* Updater.swift */, + BBE5579A1EC2AB3700F1EC31 /* WrongLyricsUtil.swift */, ); name = Utility; sourceTree = ""; @@ -552,6 +555,7 @@ BBBB6EC51E86B44B003B3ED6 /* iTunes.swift in Sources */, BBC1D35F1E499A6D002538A2 /* Global.swift in Sources */, BB58B07F1E82901800598D4F /* AutoActivateWindowController.swift in Sources */, + BBE5579B1EC2AB3700F1EC31 /* WrongLyricsUtil.swift in Sources */, BB4141DA1E4761F900A51775 /* Lyrics.swift in Sources */, BB44F8781E702863004194D9 /* Lyrics163.swift in Sources */, BBB8DA3C1E86653700EF0A18 /* SpotifyBridge.swift in Sources */, diff --git a/LyricsX/AppController.swift b/LyricsX/AppController.swift index 5c1fddbd..719441c7 100644 --- a/LyricsX/AppController.swift +++ b/LyricsX/AppController.swift @@ -59,6 +59,9 @@ class AppController: NSObject, MusicPlayerDelegate, LyricsConsuming { if currentLyrics?.metadata.source != .Local { currentLyrics?.saveToLocal() } + if lyrics == nil { + NotificationCenter.default.post(name: .PositionChange, object: nil) + } } // MARK: MediaPlayerDelegate @@ -85,6 +88,10 @@ class AppController: NSObject, MusicPlayerDelegate, LyricsConsuming { let title = track.name let artist = track.artist + guard !WrongLyricsUtil.shared.isNoMatching(title: title, artist: artist) else { + return + } + if let localLyrics = Lyrics.loadFromLocal(title: title, artist: artist) { setCurrentLyrics(lyrics: localLyrics) } else { diff --git a/LyricsX/AppDelegate.swift b/LyricsX/AppDelegate.swift index d28c0ff9..4170cc2b 100644 --- a/LyricsX/AppDelegate.swift +++ b/LyricsX/AppDelegate.swift @@ -69,6 +69,14 @@ class AppDelegate: NSObject, NSApplicationDelegate { } } + @IBAction func wrongLyrics(_ sender: Any) { + let track = MusicPlayerManager.shared.player?.currentTrack + let title = track?.name ?? "" + let artist = track?.artist ?? "" + WrongLyricsUtil.shared.noMatching(title: title, artist: artist) + AppController.shared.setCurrentLyrics(lyrics: nil) + } + func registerUserDefaults() { let defaultsUrl = Bundle.main.url(forResource: "UserDefaults", withExtension: "plist")! var defaults = NSDictionary(contentsOf: defaultsUrl) as! [String: AnyObject] diff --git a/LyricsX/Base.lproj/Main.storyboard b/LyricsX/Base.lproj/Main.storyboard index d2a413e2..2153ee00 100644 --- a/LyricsX/Base.lproj/Main.storyboard +++ b/LyricsX/Base.lproj/Main.storyboard @@ -152,6 +152,12 @@ + + + + + + diff --git a/LyricsX/Extension.swift b/LyricsX/Extension.swift index 2f47a21a..aa7a03cf 100644 --- a/LyricsX/Extension.swift +++ b/LyricsX/Extension.swift @@ -22,6 +22,22 @@ import Foundation +extension Dictionary where Key == String, Value == Any { + + init?(contentsOf url: URL) { + if let data = try? Data(contentsOf: url), + let plist = (try? PropertyListSerialization.propertyList(from: data, options: .mutableContainers, format: nil)) as? [String: Any] { + self = plist + } + return nil + } + + func write(to url: URL) throws { + let data = try PropertyListSerialization.data(fromPropertyList: self, format: .xml, options: 0) + try data.write(to: url) + } +} + extension UserDefaults { func reset() { diff --git a/LyricsX/Support Files/Info.plist b/LyricsX/Support Files/Info.plist index d813562e..94f4fcf2 100644 --- a/LyricsX/Support Files/Info.plist +++ b/LyricsX/Support Files/Info.plist @@ -19,7 +19,7 @@ CFBundleShortVersionString 1.1.2 CFBundleVersion - 1084 + 1088 LSMinimumSystemVersion $(MACOSX_DEPLOYMENT_TARGET) LSUIElement diff --git a/LyricsX/WrongLyricsUtil.swift b/LyricsX/WrongLyricsUtil.swift new file mode 100644 index 00000000..d826ab22 --- /dev/null +++ b/LyricsX/WrongLyricsUtil.swift @@ -0,0 +1,69 @@ +// +// WrongLyricsUtil.swift +// LyricsX +// +// Created by 邓翔 on 2017/5/10. +// Copyright © 2017年 ddddxxx. All rights reserved. +// + +import Foundation + +class WrongLyricsUtil { + + static let shared = WrongLyricsUtil() + + var tracks: [[String: String]] = [] + + private init() { + NotificationCenter.default.addObserver(self, selector: #selector(synchronize), name: .NSApplicationWillTerminate, object: nil) + + guard let (url, security) = defaults.lyricsSavingPath() else { + return + } + if security { + guard url.startAccessingSecurityScopedResource() else { + return + } + } + defer { + if security { + url.stopAccessingSecurityScopedResource() + } + } + + let dictUrl = url.appendingPathComponent("NoMaching.plist") + tracks = [String: Any](contentsOf: dictUrl)?["Tracks"] as? [[String: String]] ?? [] + } + + func isNoMatching(title: String, artist: String) -> Bool { + return tracks.contains { $0["Title"] == title && $0["Artist"] == artist } == true + } + + func noMatching(title: String, artist: String) { + tracks.append(["Title": title, "Artist": artist]) + } + + @objc func synchronize() { + guard let (url, security) = defaults.lyricsSavingPath() else { + return + } + if security { + guard url.startAccessingSecurityScopedResource() else { + return + } + } + defer { + if security { + url.stopAccessingSecurityScopedResource() + } + } + + let dictUrl = url.appendingPathComponent("NoMaching.plist") + let dict: [String: Any] = [ + "Version": 1, + "Tracks": tracks + ] + try? dict.write(to: dictUrl) + } + +}