Skip to content

Commit

Permalink
If the upload completes while the application is not in the foregroun…
Browse files Browse the repository at this point in the history
…d, notify the user with a local notification
  • Loading branch information
joshmcarthur committed Nov 12, 2021
1 parent 5f7d73e commit 935be94
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Droplet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Droplet/Info.plist;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
INFOPLIST_KEY_NSHumanReadableCopyright = "";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down Expand Up @@ -481,6 +482,7 @@
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Droplet/Info.plist;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
INFOPLIST_KEY_NSHumanReadableCopyright = "";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down
21 changes: 20 additions & 1 deletion Droplet/DropletDropDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import SwiftUI
import SotoS3
import AVFoundation
import UserNotifications

struct DropletDropDelegate : DropDelegate {
@AppStorage("awsAccessKeyId") var awsAccessKeyId = ""
Expand All @@ -23,6 +24,9 @@ struct DropletDropDelegate : DropDelegate {


func performDrop(info: DropInfo) -> Bool {
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.requestAuthorization(options: [.alert]) { granted, error in }

let awsClient = AWSClient(
credentialProvider: .static(accessKeyId: awsAccessKeyId, secretAccessKey: awsSecretAccessKey),
httpClientProvider: .createNew
Expand All @@ -34,10 +38,11 @@ struct DropletDropDelegate : DropDelegate {
if let urlData = urlData as? Data {
self.active = true
self.fileUrl = NSURL(absoluteURLWithDataRepresentation: urlData, relativeTo: nil) as URL
let key = NSUUID().uuidString
let request = S3.CreateMultipartUploadRequest(bucket: awsBucketName,
contentDisposition: "inline",
contentType: self.fileUrl!.mimeType(),
key: NSUUID().uuidString)
key: key)
let multipartUploadRequest = s3.multipartUpload(
request,
partSize: 5*1024*1024,
Expand Down Expand Up @@ -74,6 +79,20 @@ struct DropletDropDelegate : DropDelegate {
let pasteboard = NSPasteboard.general
pasteboard.clearContents()
pasteboard.setString(self.signedUrl!.absoluteString, forType: .string)

notificationCenter.getNotificationSettings { settings in
guard ((settings.authorizationStatus == .authorized) ||
(settings.authorizationStatus == .provisional)) && settings.alertSetting == .enabled
else { return }

let content = UNMutableNotificationContent()
content.title = "Upload finished"
content.body = "Presigned URL has been copied to the clipboard"

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: key, content: content, trigger: trigger)
notificationCenter.add(request);
}
try! awsClient.syncShutdown()
self.active = false
}
Expand Down

0 comments on commit 935be94

Please # to comment.