Skip to content

Commit

Permalink
Merge branch 'feature/history-url' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
gee1k committed Jun 26, 2019
2 parents 77f83f9 + c352931 commit bae5b48
Show file tree
Hide file tree
Showing 15 changed files with 278 additions and 59 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

6 changes: 6 additions & 0 deletions uPic.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
1647474522B66E3400F9575D /* Util.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1647474422B66E3400F9575D /* Util.swift */; };
164C3A0022B2AA6C003ADE39 /* Notifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 164C39FF22B2AA6C003ADE39 /* Notifier.swift */; };
164C3A0222B2AB22003ADE39 /* ConfigNotifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 164C3A0122B2AB22003ADE39 /* ConfigNotifier.swift */; };
165D908622C333740096FF38 /* PasteboardType+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 165D908522C333740096FF38 /* PasteboardType+Extension.swift */; };
1660FCBA22C11C2300372950 /* TencentHostConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1660FCB922C11BA000372950 /* TencentHostConfig.swift */; };
1660FCBB22C11C7200372950 /* TencentUploader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1660FCB722C11BA000372950 /* TencentUploader.swift */; };
1660FCBC22C11C7500372950 /* TencentUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1660FCB822C11BA000372950 /* TencentUtil.swift */; };
Expand Down Expand Up @@ -107,6 +108,7 @@
1647474422B66E3400F9575D /* Util.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Util.swift; sourceTree = "<group>"; };
164C39FF22B2AA6C003ADE39 /* Notifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notifier.swift; sourceTree = "<group>"; };
164C3A0122B2AB22003ADE39 /* ConfigNotifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigNotifier.swift; sourceTree = "<group>"; };
165D908522C333740096FF38 /* PasteboardType+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PasteboardType+Extension.swift"; sourceTree = "<group>"; };
1660FCB622C11BA000372950 /* TencentRegion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TencentRegion.swift; sourceTree = "<group>"; };
1660FCB722C11BA000372950 /* TencentUploader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TencentUploader.swift; sourceTree = "<group>"; };
1660FCB822C11BA000372950 /* TencentUtil.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TencentUtil.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -212,6 +214,7 @@
163632F222B2751D00805E7F /* BoolType.swift */,
164745EF22B65FE900F9575D /* String+Extension.swift */,
1647474222B66ACE00F9575D /* Data+Extension.swift */,
165D908522C333740096FF38 /* PasteboardType+Extension.swift */,
);
path = Basic;
sourceTree = "<group>";
Expand Down Expand Up @@ -582,6 +585,7 @@
166B4A5322B9CB4D001288ED /* UpYunConfigView.swift in Sources */,
166B4A5722B9D118001288ED /* PreferencesNotifier.swift in Sources */,
1690E7EB22BF2D9400FC81F8 /* QiniuUploader.swift in Sources */,
165D908622C333740096FF38 /* PasteboardType+Extension.swift in Sources */,
16A6DC5822AA375700813706 /* AppDelegate.swift in Sources */,
1602ED9722ADEFB200AA8638 /* BaseUploader.swift in Sources */,
163632F322B2751D00805E7F /* BoolType.swift in Sources */,
Expand Down Expand Up @@ -816,6 +820,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.12;
PRODUCT_BUNDLE_IDENTIFIER = com.svend.uPic;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "";
Expand All @@ -839,6 +844,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.12;
PRODUCT_BUNDLE_IDENTIFIER = com.svend.uPic;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "";
Expand Down
17 changes: 11 additions & 6 deletions uPic/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ extension AppDelegate {
if (pasteboardType == NSPasteboard.PasteboardType.png) {
let imgData = NSPasteboard.general.data(forType: NSPasteboard.PasteboardType.png)
self.uploadFile(nil, data: imgData!)
} else if (pasteboardType == NSPasteboard.PasteboardType.fileURL) {
} else if (pasteboardType == NSPasteboard.PasteboardType.backwardsCompatibleFileURL) {

let filePath = NSPasteboard.general.string(forType: NSPasteboard.PasteboardType.fileURL)!
let filePath = NSPasteboard.general.string(forType: NSPasteboard.PasteboardType.backwardsCompatibleFileURL)!
let url = URL(string: filePath)!

let fileManager = FileManager.default
Expand Down Expand Up @@ -175,6 +175,11 @@ extension AppDelegate {
///
func uploadCompleted(url: String) {
self.setStatusBarIcon(isIndicator: false)
let outputUrl = self.copyUrl(url: url)
NotificationExt.sendUploadSuccessfulNotification(body: outputUrl)
}

func copyUrl(url: String) -> String {
var outputUrl = ""
let outputFormat = Defaults[.ouputFormat]
switch outputFormat {
Expand All @@ -186,13 +191,14 @@ extension AppDelegate {
break
default:
outputUrl = url

}

NSPasteboard.general.clearContents()
NSPasteboard.general.declareTypes([.string], owner: nil)
NSPasteboard.general.setString(outputUrl, forType: .string)
NotificationExt.sendUploadSuccessfulNotification(body: outputUrl)

return outputUrl
}

///
Expand Down Expand Up @@ -253,4 +259,3 @@ extension AppDelegate: NSWindowDelegate, NSDraggingDestination {
}

}

6 changes: 6 additions & 0 deletions uPic/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@
<outlet property="checkUpdateMenuItem" destination="yai-4F-Btp" id="klG-hy-GGO"/>
<outlet property="guideMenuItem" destination="O8o-6e-fZn" id="3FX-T4-S1c"/>
<outlet property="helpMenuItem" destination="2Jw-ET-3eH" id="Ubq-wz-L3N"/>
<outlet property="historyMenuItem" destination="EhO-Zw-XXj" id="okQ-0q-kK5"/>
<outlet property="hostMenuItem" destination="d02-bT-1Lv" id="Ah0-J0-Z4M"/>
<outlet property="ouputFormatMenuItem" destination="1BX-LF-q51" id="H54-M9-CBq"/>
<outlet property="preferenceMenuItem" destination="VW7-Z2-hXR" id="Mhm-Tb-F4W"/>
Expand Down Expand Up @@ -744,6 +745,11 @@
</items>
</menu>
</menuItem>
<menuItem isSeparatorItem="YES" id="bfM-jE-V7Q"/>
<menuItem title="上传历史" id="EhO-Zw-XXj">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="上传历史" id="MjM-mX-Ci5"/>
</menuItem>
<menuItem isSeparatorItem="YES" id="9go-zg-sPT"/>
<menuItem title="偏好设置" id="VW7-Z2-hXR">
<modifierMask key="keyEquivalentModifierMask"/>
Expand Down
8 changes: 0 additions & 8 deletions uPic/Basic/Data+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,4 @@ extension Data {
return String(data: self, encoding: .utf8)!
}

init<T>(from value: T) {
var value = value
self.init(buffer: UnsafeBufferPointer(start: &value, count: 1))
}

func to<T>(type: T.Type) -> T {
return self.withUnsafeBytes { $0.pointee }
}
}
25 changes: 25 additions & 0 deletions uPic/Basic/PasteboardType+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// PasteboardType+Extension.swift
// uPic
//
// Created by Svend Jin on 2019/6/26.
// Copyright © 2019 Svend Jin. All rights reserved.
//

import Cocoa


extension NSPasteboard.PasteboardType {
// MARK: 剪切板扩展,让 10.13 以前的版本也支持 FileUrl 类型

static let backwardsCompatibleFileURL: NSPasteboard.PasteboardType = {

if #available(OSX 10.13, *) {
return NSPasteboard.PasteboardType.fileURL
} else {
return NSPasteboard.PasteboardType(kUTTypeFileURL as String)
}

} ()

}
130 changes: 98 additions & 32 deletions uPic/Extensions/NotificationExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,12 @@ import UserNotifications

class NotificationExt: NSObject {
static let shared = NotificationExt()

// MARK: 本地通知扩展


func sendNotification(title: String, subTitle: String, body: String) -> Void {
let content = UNMutableNotificationContent()
content.title = title
content.subtitle = subTitle
content.body = body

content.sound = UNNotificationSound.default
content.categoryIdentifier = "U_PIC"
content.userInfo = ["url": body]

let request = UNNotificationRequest(identifier: "U_PIC_REQUEST",
content: content,
trigger: nil)


let category = UNNotificationCategory(identifier: "U_PIC_CATEGORY",
actions: [],
intentIdentifiers: [],
hiddenPreviewsBodyPlaceholder: "",
options: .customDismissAction)

let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.delegate = self
notificationCenter.setNotificationCategories([category])
notificationCenter.add(request) { (error) in
if error != nil {
// Handle any errors.
}
if #available(OSX 10.14, *) {
self.sendNotificationByNew(title: title, subTitle: subTitle, body: body)
} else {
self.sendNotificationByOld(title: title, subTitle: subTitle, body: body)
}
}

Expand All @@ -59,6 +34,14 @@ class NotificationExt: NSObject {

NotificationExt.shared.sendNotification(title: NSLocalizedString("upload.notification.success.title", comment: "上传成功通知标题"), subTitle: NSLocalizedString("upload.notification.success.subtitle", comment: "上传成功通知副标题"), body: body!)
}

///
/// 发送拷贝成功通知
///
static func sendCopySuccessfulNotification(body: String? = "") {

NotificationExt.shared.sendNotification(title: "", subTitle: NSLocalizedString("upload.notification.success.subtitle", comment: "上传成功通知副标题"), body: body!)
}


///
Expand All @@ -79,16 +62,52 @@ class NotificationExt: NSObject {

}

@available(OSX 10.14, *)
extension NotificationExt: UNUserNotificationCenterDelegate {


// MARK: Version Target >= 10.14

func sendNotificationByNew(title: String, subTitle: String, body: String) -> Void {
let content = UNMutableNotificationContent()
content.title = title
content.subtitle = subTitle
content.body = body

content.sound = UNNotificationSound.default
content.categoryIdentifier = "NOTIFICATION_U_PIC"
content.userInfo = ["body": body]

let request = UNNotificationRequest(identifier: "U_PIC_REQUEST",
content: content,
trigger: nil)


let category = UNNotificationCategory(identifier: "U_PIC_CATEGORY",
actions: [],
intentIdentifiers: [],
hiddenPreviewsBodyPlaceholder: "",
options: .customDismissAction)

let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.delegate = self
notificationCenter.setNotificationCategories([category])
notificationCenter.add(request) { (error) in
if error != nil {
// Handle any errors.
}
}

}

// 用户点击弹窗后的回调
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo

if let url = userInfo["url"] {
if let body = userInfo["body"] {
NSPasteboard.general.clearContents()
NSPasteboard.general.declareTypes([.string], owner: nil)
NSPasteboard.general.setString(url as! String, forType: .string)
NSPasteboard.general.setString(body as! String, forType: .string)
}

completionHandler()
Expand All @@ -99,3 +118,50 @@ extension NotificationExt: UNUserNotificationCenterDelegate {
completionHandler([.alert, .sound])
}
}

extension NotificationExt: NSUserNotificationCenterDelegate {

// MARK: Version Target < 10.14

func sendNotificationByOld(title: String, subTitle: String, body: String) -> Void {

let userNotification = NSUserNotification()

userNotification.title = title
userNotification.subtitle = subTitle
userNotification.informativeText = body

userNotification.identifier = "OLD_NOTIFICATION_U_PIC"
userNotification.userInfo = ["body": "body"]

userNotification.soundName = NSUserNotificationDefaultSoundName

NSUserNotificationCenter.default.delegate = self
NSUserNotificationCenter.default.removeAllDeliveredNotifications()
NSUserNotificationCenter.default.deliver(userNotification)

}

// 当 App 在前台时是否弹出通知
func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
return true
}

// 推送消息后的回调
func userNotificationCenter(_ center: NSUserNotificationCenter, didDeliver notification: NSUserNotification) {
print("\(Date(timeIntervalSinceNow: 0)) -> 消息已经推送")
}

// 用户点击了通知后的回调
func userNotificationCenter(_ center: NSUserNotificationCenter, didActivate notification: NSUserNotification) {

if notification.activationType == .contentsClicked {
if let userInfo = notification.userInfo, let body = userInfo["body"] {
NSPasteboard.general.clearContents()
NSPasteboard.general.declareTypes([.string], owner: nil)
NSPasteboard.general.setString(body as! String, forType: .string)
}
}
}

}
Loading

0 comments on commit bae5b48

Please sign in to comment.