Skip to content

Commit

Permalink
remove class UpdateManager
Browse files Browse the repository at this point in the history
  • Loading branch information
ddddxxx committed May 10, 2017
1 parent bc9198f commit 839a63c
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 159 deletions.
14 changes: 7 additions & 7 deletions LyricsX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
BB45AC801E9E337F0099432F /* MusicPlayerProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB45AC7F1E9E337F0099432F /* MusicPlayerProtocol.swift */; };
BB45AC841E9F220E0099432F /* LyricsKugou.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB45AC831E9F220E0099432F /* LyricsKugou.swift */; };
BB45AC861E9F65E90099432F /* LyricsSourceProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB45AC851E9F65E90099432F /* LyricsSourceProtocol.swift */; };
BB5596E21EB856140064D278 /* UpdateManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB5596E11EB856140064D278 /* UpdateManager.swift */; };
BB5596E21EB856140064D278 /* Updater.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB5596E11EB856140064D278 /* Updater.swift */; };
BB58A3401E4EDBA300836903 /* LyricsGecimi.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB58A33F1E4EDBA300836903 /* LyricsGecimi.swift */; };
BB58A3421E4EDFF100836903 /* LyricsSourceManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB58A3411E4EDFF100836903 /* LyricsSourceManager.swift */; };
BB58B07F1E82901800598D4F /* AutoActivateWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB58B07E1E82901800598D4F /* AutoActivateWindowController.swift */; };
Expand Down Expand Up @@ -121,7 +121,7 @@
BB45AC7F1E9E337F0099432F /* MusicPlayerProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MusicPlayerProtocol.swift; path = MediaPlayer/MusicPlayerProtocol.swift; sourceTree = "<group>"; };
BB45AC831E9F220E0099432F /* LyricsKugou.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = LyricsKugou.swift; path = Lyrics/LyricsKugou.swift; sourceTree = "<group>"; };
BB45AC851E9F65E90099432F /* LyricsSourceProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = LyricsSourceProtocol.swift; path = Lyrics/LyricsSourceProtocol.swift; sourceTree = "<group>"; };
BB5596E11EB856140064D278 /* UpdateManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UpdateManager.swift; sourceTree = "<group>"; };
BB5596E11EB856140064D278 /* Updater.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Updater.swift; sourceTree = "<group>"; };
BB58A33F1E4EDBA300836903 /* LyricsGecimi.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = LyricsGecimi.swift; path = Lyrics/LyricsGecimi.swift; sourceTree = "<group>"; };
BB58A3411E4EDFF100836903 /* LyricsSourceManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = LyricsSourceManager.swift; path = Lyrics/LyricsSourceManager.swift; sourceTree = "<group>"; };
BB58B07E1E82901800598D4F /* AutoActivateWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AutoActivateWindowController.swift; path = Controller/AutoActivateWindowController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -223,13 +223,12 @@
children = (
BB4141AD1E458BA800A51775 /* AppDelegate.swift */,
BBC1D35A1E485BA7002538A2 /* AppController.swift */,
BB5596E11EB856140064D278 /* UpdateManager.swift */,
BB4141B31E458BA900A51775 /* Main.storyboard */,
BB063B2F1E8A93CE00A25C3E /* View */,
BB7EC7B41E4ECFFB00A0AC3F /* Controller */,
BB7EC7B51E4ED02000A0AC3F /* Lyrics */,
BBBB6EC31E86B08E003B3ED6 /* MusicPlayer */,
BB7EC7B61E4ED03900A0AC3F /* Others */,
BB7EC7B61E4ED03900A0AC3F /* Utility */,
BB4141BC1E45B26800A51775 /* Support Files */,
);
path = LyricsX;
Expand Down Expand Up @@ -296,13 +295,14 @@
name = Lyrics;
sourceTree = "<group>";
};
BB7EC7B61E4ED03900A0AC3F /* Others */ = {
BB7EC7B61E4ED03900A0AC3F /* Utility */ = {
isa = PBXGroup;
children = (
BBC1D35E1E499A6D002538A2 /* Global.swift */,
BB9BCEB91E843157001BC54B /* Extension.swift */,
BB5596E11EB856140064D278 /* Updater.swift */,
);
name = Others;
name = Utility;
sourceTree = "<group>";
};
BB861AA31E99E56A00AC9194 /* Sources */ = {
Expand Down Expand Up @@ -543,7 +543,7 @@
BB60F2FC1E87CF9200BA1FD2 /* Vox.swift in Sources */,
BBB9AAE31E57EE880078F91A /* SearchLyricsViewController.swift in Sources */,
BB1B631B1E8CD7CA004CC9E0 /* ScrollLyricsView.swift in Sources */,
BB5596E21EB856140064D278 /* UpdateManager.swift in Sources */,
BB5596E21EB856140064D278 /* Updater.swift in Sources */,
BBD03C371E9B2C7200EFB975 /* DragNDropView.swift in Sources */,
BB4141AE1E458BA800A51775 /* AppDelegate.swift in Sources */,
BBC7C5551E6AE28600E3EC4F /* PreferenceGeneralViewController.swift in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions LyricsX/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

DispatchQueue.global().async {
UpdateManager.shared.checkForUpdate()
checkForUpdate()
}
}

Expand All @@ -65,7 +65,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

@IBAction func checkUpdateAction(_ sender: Any) {
DispatchQueue.global().async {
UpdateManager.shared.checkForUpdate(force: true)
checkForUpdate(force: true)
}
}

Expand Down
2 changes: 1 addition & 1 deletion LyricsX/Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ extension Lyrics {
ID3: true,
timeTag: true,
translation: true)
try content.write(to: lrcFileURL, atomically: false, encoding: .utf8)
try content.write(to: lrcFileURL, atomically: true, encoding: .utf8)
} catch let error as NSError{
print(error)
return
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>1083</string>
<string>1084</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>LSUIElement</key>
Expand Down
148 changes: 0 additions & 148 deletions LyricsX/UpdateManager.swift

This file was deleted.

140 changes: 140 additions & 0 deletions LyricsX/Updater.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//
// Updater.swift
// LyricsX
//
// Created by 邓翔 on 2017/5/2.
//
// Copyright (C) 2017 Xander Deng
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

import Cocoa
import SwiftyJSON

var remoteVersion: Semver? {
let gitHubPath = "XQS6LB3A/LyricsX"
let url = URL(string: "https://api.github.com/repos/\(gitHubPath)/releases/latest")!
guard let data = try? Data(contentsOf: url) else { return nil }
let json = JSON(data: data)
guard var tag = json["tag_name"].string,
json["draft"].bool != true,
json["prerelease"].bool != true else {
return nil
}
if tag[tag.startIndex] == "v" {
tag.remove(at: tag.startIndex)
}
return Semver(tag)
}

var localVersion: Semver {
let info = Bundle.main.infoDictionary!
let shortVersion = info["CFBundleShortVersionString"] as! String
return Semver(shortVersion)!
}

func checkForUpdate(force: Bool = false) {
let local = localVersion
guard let remote = remoteVersion else {
return
}

guard remote > local else {
if force {
DispatchQueue.main.async {
let alert = NSAlert()
alert.messageText = "You're up-to-date!"
alert.informativeText = "LyricsX \(local) is currently the newest version available."
NSApp.activate(ignoringOtherApps: true)
alert.runModal()
}
}
return
}

if !force,
let skipVersionString = defaults[.NotifiedUpdateVersion],
let skipVersion = Semver(skipVersionString),
skipVersion >= remote {
return
}

defaults[.NotifiedUpdateVersion] = remote.description

DispatchQueue.main.async {
let alert = NSAlert()
alert.messageText = "A new version of LyricsX is available!"
alert.informativeText = "LyricsX \(remote) is now available -- you have \(localVersion). Would you like to download it now?"
alert.addButton(withTitle: "Download")
alert.addButton(withTitle: "Skip")
NSApp.activate(ignoringOtherApps: true)
let response = alert.runModal()
if response == NSAlertFirstButtonReturn {
let url = URL(string: "https://github.com/XQS6LB3A/LyricsX/releases")!
NSWorkspace.shared().open(url)
}
}
}

struct Semver {

var major: Int
var minor: Int
var patch: Int

init?(_ string:String) {
let components = string.components(separatedBy: ".").flatMap { Int($0) }
guard components.count == 3 else {
return nil
}
major = components[0]
minor = components[1]
patch = components[2]
}
}

extension Semver: Comparable {

public static func ==(lhs: Semver, rhs: Semver) -> Bool {
return lhs.major == rhs.major && lhs.minor == rhs.minor && lhs.patch == rhs.patch
}

public static func <(lhs: Semver, rhs: Semver) -> Bool {
return (lhs.major < rhs.major) ||
(lhs.major == rhs.major && lhs.minor < rhs.minor) ||
(lhs.major == rhs.major && lhs.minor == rhs.minor && lhs.patch < rhs.patch)
}

public static func <=(lhs: Semver, rhs: Semver) -> Bool {
return !(lhs > rhs)
}

public static func >=(lhs: Semver, rhs: Semver) -> Bool {
return !(lhs < rhs)
}

public static func >(lhs: Semver, rhs: Semver) -> Bool {
return (lhs.major > rhs.major) ||
(lhs.major == rhs.major && lhs.minor > rhs.minor) ||
(lhs.major == rhs.major && lhs.minor == rhs.minor && lhs.patch > rhs.patch)
}
}

extension Semver: CustomStringConvertible {

public var description: String {
return "\(major).\(minor).\(patch)"
}
}

0 comments on commit 839a63c

Please sign in to comment.