Permalink
Browse files

refactor update manager

  • Loading branch information...
youknowone committed Jan 1, 2019
1 parent 67c7da9 commit 95519560b3eb70051790ac378f69332c2c62f144
Showing with 92 additions and 63 deletions.
  1. +4 −0 Gureum.xcodeproj/project.pbxproj
  2. +1 −1 OSX/CIMInputControllerGureum.swift
  3. +16 −62 OSX/GureumAppDelegate.swift
  4. +71 −0 OSX/UpdateManager.swift
@@ -34,6 +34,7 @@
382E686D1A1314D900031D1D /* Preferences.tiff in Resources */ = {isa = PBXBuildFile; fileRef = 382E686C1A1314D900031D1D /* Preferences.tiff */; };
382E68701A1314D900031D1D /* Preferences.xib in Resources */ = {isa = PBXBuildFile; fileRef = 382E686E1A1314D900031D1D /* Preferences.xib */; };
382E68761A13156700031D1D /* Preferences.prefPane in Resources */ = {isa = PBXBuildFile; fileRef = 382E68641A1314D900031D1D /* Preferences.prefPane */; };
3831983C21DB744900E20D78 /* UpdateManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3831983B21DB744900E20D78 /* UpdateManager.swift */; };
3835B5251F5DAB5E00896BEC /* all.txt in Copy Hanja Files */ = {isa = PBXBuildFile; fileRef = 3835B4E51F5DAA7100896BEC /* all.txt */; };
3835B5281F5DAB5E00896BEC /* hanjac.txt in Copy Hanja Files */ = {isa = PBXBuildFile; fileRef = 3835B4EC1F5DAA7100896BEC /* hanjac.txt */; };
3835B5291F5DAB5E00896BEC /* hanjar.txt in Copy Hanja Files */ = {isa = PBXBuildFile; fileRef = 3835B4ED1F5DAA7100896BEC /* hanjar.txt */; };
@@ -261,6 +262,7 @@
382E68671A1314D900031D1D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
382E686C1A1314D900031D1D /* Preferences.tiff */ = {isa = PBXFileReference; lastKnownFileType = image.tiff; path = Preferences.tiff; sourceTree = "<group>"; };
382E686F1A1314D900031D1D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/Preferences.xib; sourceTree = "<group>"; };
3831983B21DB744900E20D78 /* UpdateManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdateManager.swift; sourceTree = "<group>"; };
3835B4E51F5DAA7100896BEC /* all.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = all.txt; sourceTree = "<group>"; };
3835B4EC1F5DAA7100896BEC /* hanjac.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = hanjac.txt; sourceTree = "<group>"; };
3835B4ED1F5DAA7100896BEC /* hanjar.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = hanjar.txt; sourceTree = "<group>"; };
@@ -422,6 +424,7 @@
381B32731DA826B100EA1975 /* Bridge.h */,
E5ED2DE3213030B700BD9B13 /* CIMInputControllerGureum.swift */,
E5A0EA092139098800D4AD69 /* GureumAppDelegate.swift */,
3831983B21DB744900E20D78 /* UpdateManager.swift */,
38FA21DF14231BCC00444D67 /* Composers */,
38475AEB14129BAA0062100D /* Icons */,
38162DF7141263280077AA2D /* Supporting Files */,
@@ -1112,6 +1115,7 @@
878898012160AA6E00BF5770 /* CIMInputManager.swift in Sources */,
38F85961215BD27000CD80AE /* main.swift in Sources */,
381DB19B21678B74005A37B9 /* Debug.swift in Sources */,
3831983C21DB744900E20D78 /* UpdateManager.swift in Sources */,
E5D234EF2141260E00595FA0 /* CIMApplicationDelegate.swift in Sources */,
38162E0F141264EB0077AA2D /* HanjaComposer.swift in Sources */,
38699DC21F36191500FD093E /* RomanComposer.swift in Sources */,
@@ -11,7 +11,7 @@ import Cocoa

extension CIMInputController {
@IBAction func checkRecentVersion(_ sender: Any) {
guard let info = (NSApp.delegate as! GureumAppDelegate).getRecentVersion() else {
guard let info = UpdateManager.shared.requestRecentVersion() else {
return
}

@@ -11,18 +11,20 @@ import Hangul
import Fabric
import Crashlytics

class NotificationCenterDelegate: NSObject, NSUserNotificationCenterDelegate{
var download: String!

override init(){
super.init()
NSUserNotificationCenter.default.delegate = self
}
class NotificationCenterDelegate: NSObject, NSUserNotificationCenterDelegate {
static let appDefault = NotificationCenterDelegate()

func userNotificationCenter(_ center: NSUserNotificationCenter, didActivate notification: NSUserNotification) {
guard let userInfo = notification.userInfo else {
return
}
guard let download = userInfo["download"] as? String else {
return
}
switch (notification.activationType) {
case .actionButtonClicked:
NSWorkspace.shared.open(URL(string: download)!)
fallthrough
case .contentsClicked:
NSWorkspace.shared.open(URL(string: download)!)
default:
@@ -35,22 +37,17 @@ class NotificationCenterDelegate: NSObject, NSUserNotificationCenterDelegate{
@IBOutlet @objc var menu: NSMenu!
@objc public var sharedInputManager: CIMInputManager!
let configuration = GureumConfiguration.shared
var notificationCenter = NSUserNotificationCenter.default
var notificationCenterDelegate = NotificationCenterDelegate()

struct VersionInfo {
var recent: String
var current: String
var download: String
var note: String
}
let notificationCenterDelegate = NotificationCenterDelegate()

@objc override func awakeFromNib(){
HGKeyboard.initialize()
sharedInputManager = CIMInputManager()
}

func applicationDidFinishLaunching(_ notification: Notification) {
NSUserNotificationCenter.default.delegate = notificationCenterDelegate

let notificationCenter = NSUserNotificationCenter.default
#if DEBUG
let notification = NSUserNotification()
notification.title = "디버그 빌드 알림"
@@ -61,57 +58,14 @@ class NotificationCenterDelegate: NSObject, NSUserNotificationCenterDelegate{
#else
Fabric.with([Crashlytics.self])
#endif
checkUpdate()

let updateManager = UpdateManager.shared
updateManager.notifyUpdateIfNeeded()
}

@objc func composer(server: IMKServer!, client: Any!) -> CIMComposer {
let composer: CIMComposer = GureumComposer()
return composer
}

func checkUpdate() {
guard let info = getRecentVersion() else {
return
}
guard info.recent != info.current else {
return
}
guard info.download.count > 0 else {
return
}

let notification = NSUserNotification()
notification.title = "구름 입력기 업데이트 알림"
notification.hasActionButton = true
notification.hasReplyButton = false
notification.actionButtonTitle = "업데이트"
notification.otherButtonTitle = "취소"
notification.informativeText = "최신 버전: \(info.recent) 현재 버전: \(info.current)\n\(info.note)"
notificationCenterDelegate.download = info.download

notificationCenter.deliver(notification)
}

func getRecentVersion() -> VersionInfo? {
let currentVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String
var url: URL
if currentVersion.contains("-pre") {
url = URL(string: "http://gureum.io/version-pre.txt")!
} else {
url = URL(string: "http://gureum.io/version.txt")!
}
var request = URLRequest(url: url)
request.timeoutInterval = 0.5
request.cachePolicy = .reloadIgnoringCacheData
guard let data = try? NSData(contentsOf: request, error: ()) else {
return nil
}
if data.length == 0 { // 위에서 제대로 안걸림
return nil
}
let verstring = String(data: data as Data, encoding: String.Encoding.utf8)!
var components = verstring.components(separatedBy: "::")
let version = VersionInfo(recent: components[0], current: currentVersion, download: components[1], note: components[2])
return version
}
}
@@ -0,0 +1,71 @@
//
// UpdateManager.swift
// OSX
//
// Created by Jeong YunWon on 01/01/2019.
// Copyright © 2019 youknowone.org. All rights reserved.
//
import Foundation


class UpdateManager {

public static let shared = UpdateManager()

struct VersionInfo {
var recent: String
var current: String
var download: String
var note: String
}

func requestRecentVersion() -> VersionInfo? {
let currentVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String
var url: URL
if currentVersion.contains("-pre") {
url = URL(string: "http://gureum.io/version-pre.txt")!
} else {
url = URL(string: "http://gureum.io/version.txt")!
}
var request = URLRequest(url: url)
request.timeoutInterval = 0.5
request.cachePolicy = .reloadIgnoringCacheData
guard let data = try? NSData(contentsOf: request, error: ()) else {
return nil
}
if data.length == 0 { // 위에서 제대로 안걸림
return nil
}
let verstring = String(data: data as Data, encoding: String.Encoding.utf8)!
var components = verstring.components(separatedBy: "::")
let version = VersionInfo(recent: components[0], current: currentVersion, download: components[1], note: components[2])
return version
}

func notifyUpdate(info: VersionInfo) {
let notification = NSUserNotification()
notification.title = "구름 입력기 업데이트 알림"
notification.hasActionButton = true
notification.hasReplyButton = false
notification.actionButtonTitle = "업데이트"
notification.otherButtonTitle = "취소"
notification.informativeText = "최신 버전: \(info.recent) 현재 버전: \(info.current)\n\(info.note)"
notification.userInfo = ["download": info.download]

NSUserNotificationCenter.default.deliver(notification)
}

func notifyUpdateIfNeeded() {
guard let info = requestRecentVersion() else {
return
}
guard info.recent != info.current else {
return
}
guard info.download.count > 0 else {
return
}
self.notifyUpdate(info: info)
}
}

0 comments on commit 9551956

Please sign in to comment.