2018-08-06 14:17:04 +08:00
//
// N S U s e r N o t i f i c a t i o n C e n t e r + E x t e n s i o n . s w i f t
// C l a s h X
//
// C r e a t e d b y C Y C o n 2 0 1 8 / 8 / 6 .
2018-08-08 13:47:38 +08:00
// C o p y r i g h t © 2 0 1 8 年 y i c h e n g c h e n . A l l r i g h t s r e s e r v e d .
2018-08-06 14:17:04 +08:00
//
import Cocoa
extension NSUserNotificationCenter {
2018-08-12 00:10:42 +08:00
func post ( title : String , info : String , identifier : String ? = nil ) {
2018-08-06 14:17:04 +08:00
let notification = NSUserNotification ( )
notification . title = title
notification . informativeText = info
2018-08-12 00:10:42 +08:00
if identifier != nil {
notification . userInfo = [ " identifier " : identifier ! ]
}
self . delegate = UserNotificationCenterDelegate . shared
2018-08-06 14:17:04 +08:00
self . deliver ( notification )
}
2018-08-11 23:07:51 +08:00
func postGenerateSimpleConfigNotice ( ) {
self . post ( title : " No External-controller specified in config file! " , info : " We have replace current config with a simple config with external-controller specified! " )
}
2018-08-12 00:10:42 +08:00
func postConfigFileChangeDetectionNotice ( ) {
self . post ( title : " Config file have been changed " , info : " Tap to reload config " , identifier : " postConfigFileChangeDetectionNotice " )
2018-08-12 00:26:57 +08:00
}
func postStreamApiConnectFail ( api : String ) {
self . post ( title : " \( api ) api connect error! " , info : " Use reload config to try reconnect. " )
2018-08-12 00:10:42 +08:00
}
2018-08-19 12:57:15 +08:00
func postConfigErrorNotice ( msg : String ) {
self . post ( title : " Config loading Fail! " , info : msg )
}
2018-08-26 13:25:29 +08:00
func postImportConfigFromUrlFailNotice ( urlStr : String ) {
self . post ( title : " Import config from url fail " , info : " Unrecongized Url: \( urlStr ) " )
}
func postProxyRemarkDupNotice ( name : String ) {
self . post ( title : " Proxy Remark duplicated " , info : " Name: \( name ) " )
}
2018-08-12 00:10:42 +08:00
}
class UserNotificationCenterDelegate : NSObject , NSUserNotificationCenterDelegate {
static let shared = UserNotificationCenterDelegate ( )
func userNotificationCenter ( _ center : NSUserNotificationCenter , didActivate notification : NSUserNotification ) {
switch notification . userInfo ? [ " identifier " ] as ? String {
case " postConfigFileChangeDetectionNotice " :
NotificationCenter . default . post ( Notification ( name : kShouldUpDateConfig ) )
center . removeAllDeliveredNotifications ( )
default :
break
}
}
func userNotificationCenter ( _ center : NSUserNotificationCenter , shouldPresent notification : NSUserNotification ) -> Bool {
return true
}
2018-08-06 14:17:04 +08:00
}