Skip to content

Commit

Permalink
更新功能实现
Browse files Browse the repository at this point in the history
  • Loading branch information
vouv committed Jan 4, 2021
1 parent 72527c8 commit dab5ff8
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 21 deletions.
8 changes: 5 additions & 3 deletions SrunBar/AboutWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ class AboutWindow: NSWindowController, NSWindowDelegate {

@IBOutlet weak var versionLabel: NSTextField!

let version = "v0.2.8"
let version = UserDefaults.standard.string(forKey: "version") ?? ""

let link = "https://github.com/vouv/SrunBar"

override var windowNibName : String! { "AboutWindow" }

override func windowDidLoad() {
super.windowDidLoad()
self.versionLabel?.stringValue = "SrunBar " + version
debugPrint("load about")
versionLabel?.stringValue = "SrunBar " + version
self.window?.center()
self.window?.makeKeyAndOrderFront(nil)
NSApp.activate(ignoringOtherApps: true)
}

@IBAction func linkClicked(_ sender: NSButtonCell) {
let url = URL(string: link)
NSWorkspace.shared.open(url!)
Expand Down
7 changes: 4 additions & 3 deletions SrunBar/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

let version = "v0.3.0"

func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
// check update
// 每天执行一次更新
// let al = NSAlert.init();
// al.messageText = "update!"
// al.runModal()
let def = UserDefaults.standard
def.set(version, forKey: "version")
}

func applicationWillTerminate(_ aNotification: Notification) {
Expand Down
16 changes: 12 additions & 4 deletions SrunBar/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
<customObject id="sS3-a3-GGc" userLabel="StatusMenuController" customClass="StatusMenuController" customModule="SrunBar" customModuleProvider="target">
<connections>
<outlet property="aboutItem" destination="qwZ-1E-LEa" id="peZ-Tm-A4n"/>
<outlet property="infoView" destination="HhN-DY-IST" id="hvL-Yk-HVZ"/>
<outlet property="loginItem" destination="CyZ-K7-RM6" id="R4Y-Uy-Hul"/>
<outlet property="statusMenu" destination="qZp-Ad-uDX" id="XFG-js-aKj"/>
<outlet property="statusMenuItem" destination="CyZ-K7-RM6" id="GW0-wd-eur"/>
</connections>
</customObject>
<menu id="qZp-Ad-uDX">
<items>
<menuItem title="登录" keyEquivalent="r" id="CyZ-K7-RM6">
<menuItem title="已联网" image="NSStatusAvailable" enabled="NO" id="CyZ-K7-RM6">
<attributedString key="attributedTitle"/>
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
<menuItem isSeparatorItem="YES" id="Xmn-xj-Qek"/>
<menuItem title="登录" keyEquivalent="r" id="PaX-ye-kHd">
<attributedString key="attributedTitle"/>
<connections>
<action selector="loginClicked:" target="sS3-a3-GGc" id="bF4-6b-JbB"/>
<action selector="loginClicked:" target="sS3-a3-GGc" id="5G1-fz-K1N"/>
</connections>
</menuItem>
<menuItem title="注销" keyEquivalent="z" id="uto-Y4-n9N">
Expand Down Expand Up @@ -252,4 +257,7 @@
<point key="canvasLocation" x="-101.5" y="425.5"/>
</customView>
</objects>
<resources>
<image name="NSStatusAvailable" width="16" height="16"/>
</resources>
</document>
54 changes: 47 additions & 7 deletions SrunBar/StatusMenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

import Cocoa

enum SrunStatus : Int {
case connected = 1
case unKnown = 2
case ready = 3
}


class StatusMenuController: NSObject, NSMenuDelegate {
@IBOutlet weak var statusMenu: NSMenu!
@IBOutlet weak var infoView: InfoView!
Expand All @@ -18,9 +25,10 @@ class StatusMenuController: NSObject, NSMenuDelegate {
var aboutWindow: AboutWindow!
var updateWindow: UpdateWindow!

@IBOutlet weak var loginItem: NSMenuItem!

@IBOutlet weak var aboutItem: NSMenuItem!
@IBOutlet weak var statusMenuItem: NSMenuItem!
// @IBOutlet weak var loginItem: NSMenuItem!
//
// @IBOutlet weak var aboutItem: NSMenuItem!

var statusIcon: NSImage? {
let icon = NSImage(named: "statusIcon")
Expand All @@ -32,6 +40,18 @@ class StatusMenuController: NSObject, NSMenuDelegate {
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
let srun = SrunAPI()

var srunStatus = SrunStatus.connected
let srunStatusText = [
SrunStatus.connected: "已联网",
SrunStatus.ready: "未登录",
SrunStatus.unKnown: "未知网络",
]
let srunStatusImage = [
SrunStatus.connected: NSImage(named: "NSStatusAvailable")!,
SrunStatus.ready: NSImage(named: "NSStatusPartiallyAvailable")!,
SrunStatus.unKnown: NSImage(named: "NSStatusNone")!,
]

override func awakeFromNib() {
statusItem.menu = statusMenu
// 监听显示事件
Expand Down Expand Up @@ -61,7 +81,7 @@ class StatusMenuController: NSObject, NSMenuDelegate {
self.doInfo()
}
}

func doLogin() {
let defaults = UserDefaults.standard
guard let username = defaults.string(forKey: "username") else {
Expand Down Expand Up @@ -97,8 +117,12 @@ class StatusMenuController: NSObject, NSMenuDelegate {
srun.logout(username: username) { (err) in
if let err = err {
self.notify(title: "注销失败", subtitle: err.localizedDescription)
self.setSrunStatus(.unKnown)

}else {
self.notify(title: "注销成功", subtitle: "账号\(username)")
self.setSrunStatus(.ready)
return
}
}
}
Expand All @@ -113,12 +137,28 @@ class StatusMenuController: NSObject, NSMenuDelegate {
}
srun.info(username: username, password: password) { (res, error) in
if error != nil {
return

}else if let info = res {
DispatchQueue.main.async {
self.infoView.update(info)
// 如未登录,返回数据为空
if info.user_name == username {
DispatchQueue.main.async {
self.setSrunStatus(.connected)
self.infoView.update(info)
}
}else {
self.setSrunStatus(.ready)
}
return
}
self.setSrunStatus(.unKnown)
}
}

func setSrunStatus(_ status: SrunStatus) {
DispatchQueue.main.async {
self.srunStatus = status
self.statusMenuItem.title = self.srunStatusText[self.srunStatus] ?? ""
self.statusMenuItem.image = self.srunStatusImage[self.srunStatus]
}
}

Expand Down
121 changes: 121 additions & 0 deletions SrunBar/UpdateWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,137 @@ class UpdateWindow: NSWindowController, NSWindowDelegate {

override var windowNibName : String! { "UpdateWindow" }

var task : URLSessionTask!

let titleNewVersion = "发现新版本"
let titleUpdateError = "更新错误"
let titleUpToDate = "无需更新"


override func windowDidLoad() {
super.windowDidLoad()
self.window?.center()
self.window?.makeKeyAndOrderFront(nil)
NSApp.activate(ignoringOtherApps: true)
self.progressBar.startAnimation(nil)

}

override func showWindow(_ sender: Any?) {
super.showWindow(sender)
self.task = self.requestTask()
DispatchQueue.global().async {
self.task.resume()
}
}

func requestTask() -> URLSessionTask {
let timeout = TimeInterval.init(10)
let dst = "https://github.com/vouv/SrunBar/releases/latest"

let addr = URLComponents(string: dst)!
var urlRequest = URLRequest(url: addr.url!)
urlRequest.httpMethod = "GET"

let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = timeout
let session = URLSession(configuration: config)
let task = session.dataTask(
with: urlRequest,
completionHandler: { (data, response, error) in
DispatchQueue.main.async {
self.window?.close()
}
var errInfo = ""
if let error = error {
errInfo = error.localizedDescription
}else if let response = response {
let target = response.url?.absoluteString ?? ""

let base = "https://github.com/vouv/SrunBar/releases/tag/"
if target.contains(base) {
let startIndex = target.index(target.startIndex, offsetBy: base.count)
let newVersion = String(target[startIndex..<target.endIndex])
let currentVersion = self.getCurrentVersion()

if self.compareVersion(current: currentVersion, remote: newVersion) {
DispatchQueue.main.async {
let alt = NSAlert.init()
alt.messageText = self.titleNewVersion + newVersion
alt.informativeText = "当前版本 " + currentVersion
let btn = alt.addButton(withTitle: "前往更新")
btn.target = self
btn.action = #selector(self.openVersionUrl)
let btnCancel = alt.addButton(withTitle: "稍后")
btnCancel.target = self
btnCancel.action = #selector(self.stopModal)
alt.runModal()
}
}else {
DispatchQueue.main.async {
let alt = NSAlert.init()
alt.messageText = self.titleUpToDate
alt.informativeText = currentVersion + " 是最新版本"
alt.runModal()
}
}
return
}
errInfo = "版本解析失败, 请联系开发者"
}
DispatchQueue.main.async {
let alt = NSAlert.init()
alt.messageText = self.titleUpdateError
alt.informativeText = errInfo
alt.runModal()
}
})
return task
}

// 关闭请求
func windowWillClose(_ notification: Notification) {
self.task.cancel()
}

func getCurrentVersion() -> String {
let def = UserDefaults.standard
return def.string(forKey: "version") ?? ""
}

func compareVersion(current:String, remote:String) -> Bool {
// 先去掉v
let nowVersion = String(current[current.index(current.startIndex, offsetBy: 1)..<current.endIndex])
let newVersion = String(remote[remote.index(remote.startIndex, offsetBy: 1)..<remote.endIndex])
let nowArray = nowVersion.split(separator: ".")

let newArray = newVersion.split(separator: ".")

let big = nowArray.count > newArray.count ? newArray.count : nowArray.count

for index in 0...big - 1 {
let first = nowArray[index]
let second = newArray[index]
if Int(first)! < Int(second)! {
return true
}
}
return false
}

@objc func stopModal(_ sender: NSButton) {
NSApp.stopModal()
}

@objc func openVersionUrl(_ sender: NSButton) {
// 关闭弹窗
NSApp.stopModal()

let link = "https://github.com/vouv/SrunBar/releases/latest"

let url = URL(string: link)
DispatchQueue.global().async {
NSWorkspace.shared.open(url!)
}
}
}
17 changes: 13 additions & 4 deletions SrunBar/srun/SrunAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import Cocoa

enum SrunError : Error {
case RequestError
}

class SrunAPI {

Expand All @@ -17,6 +20,8 @@ class SrunAPI {
let InfoUrl = "/cgi-bin/rad_user_info"

let hash = Hash()

let timeout = TimeInterval.init(2)

func login(username: String, password: String, handle: @escaping (RespLogin?, Error?) -> Void) {
let (acid, err) = getAcid()
Expand All @@ -25,8 +30,8 @@ class SrunAPI {
return
}

let (respChallenge, errc) = self.getChallenge(username: username)
if let err = errc {
let (respChallenge, errChallenge) = getChallenge(username: username)
if let err = errChallenge {
handle(nil, err)
return
}
Expand Down Expand Up @@ -158,7 +163,7 @@ class SrunAPI {
private func getAcid() -> (Int, Error?) {

let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = TimeInterval.init(3)
config.timeoutIntervalForRequest = self.timeout
let session = URLSession(configuration: config)

let semaphore = DispatchSemaphore.init(value: 0)
Expand Down Expand Up @@ -215,7 +220,7 @@ class SrunAPI {
urlRequest.httpMethod = "GET"

let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = TimeInterval.init(3)
config.timeoutIntervalForRequest = timeout
let session = URLSession(configuration: config)
let task = session.dataTask(
with: urlRequest,
Expand All @@ -228,6 +233,10 @@ class SrunAPI {

let body = String(data: data!, encoding: String.Encoding.utf8)!

if body.count < queryCallback.count {
handle(nil, SrunError.RequestError)
return
}

let startIndex = body.index(body.startIndex, offsetBy: queryCallback.count+1)
let endIndex = body.index(body.endIndex, offsetBy: -1)
Expand Down

0 comments on commit dab5ff8

Please sign in to comment.