Skip to content

Commit

Permalink
feat: mark wrong lyrics and never search again, close #24, close #32
Browse files Browse the repository at this point in the history
  • Loading branch information
ddddxxx committed May 10, 2017
1 parent 839a63c commit ea1f617
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 1 deletion.
4 changes: 4 additions & 0 deletions LyricsX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -156,6 +157,7 @@
BBC811791E9E1D0000331CFC /* MusicPlayerManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MusicPlayerManager.swift; path = MediaPlayer/MusicPlayerManager.swift; sourceTree = "<group>"; };
BBD03C361E9B2C7200EFB975 /* DragNDropView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = DragNDropView.swift; path = View/DragNDropView.swift; sourceTree = "<group>"; };
BBE557961EC0291E00F1EC31 /* GenericID.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GenericID.framework; path = Carthage/Build/Mac/GenericID.framework; sourceTree = "<group>"; };
BBE5579A1EC2AB3700F1EC31 /* WrongLyricsUtil.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WrongLyricsUtil.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -301,6 +303,7 @@
BBC1D35E1E499A6D002538A2 /* Global.swift */,
BB9BCEB91E843157001BC54B /* Extension.swift */,
BB5596E11EB856140064D278 /* Updater.swift */,
BBE5579A1EC2AB3700F1EC31 /* WrongLyricsUtil.swift */,
);
name = Utility;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
7 changes: 7 additions & 0 deletions LyricsX/AppController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions LyricsX/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 6 additions & 0 deletions LyricsX/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@
<segue destination="Fyy-jy-wIT" kind="show" id="QlQ-Pc-nUk"/>
</connections>
</menuItem>
<menuItem title="Wrong Lyrics" id="fmS-zv-RcJ">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="wrongLyrics:" target="Voe-Tx-rLC" id="PSJ-f3-tnK"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="Gb0-Hc-403"/>
<menuItem title="Preference..." keyEquivalent="," id="CWo-Qx-2Ut">
<connections>
Expand Down
16 changes: 16 additions & 0 deletions LyricsX/Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion LyricsX/Support Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>1.1.2</string>
<key>CFBundleVersion</key>
<string>1084</string>
<string>1088</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>LSUIElement</key>
Expand Down
69 changes: 69 additions & 0 deletions LyricsX/WrongLyricsUtil.swift
Original file line number Diff line number Diff line change
@@ -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)
}

}

0 comments on commit ea1f617

Please sign in to comment.