From 935be94d7b42f92950b875603f3c7e53513cd4a0 Mon Sep 17 00:00:00 2001
From: Josh McArthur <joshua.mcarthur@gmail.com>
Date: Fri, 12 Nov 2021 14:14:27 +1300
Subject: [PATCH] If the upload completes while the application is not in the
 foreground, notify the user with a local notification

---
 Droplet.xcodeproj/project.pbxproj |  2 ++
 Droplet/DropletDropDelegate.swift | 21 ++++++++++++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/Droplet.xcodeproj/project.pbxproj b/Droplet.xcodeproj/project.pbxproj
index ae57ee7..77cdd23 100644
--- a/Droplet.xcodeproj/project.pbxproj
+++ b/Droplet.xcodeproj/project.pbxproj
@@ -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)",
@@ -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)",
diff --git a/Droplet/DropletDropDelegate.swift b/Droplet/DropletDropDelegate.swift
index 63279d6..f7b2e07 100644
--- a/Droplet/DropletDropDelegate.swift
+++ b/Droplet/DropletDropDelegate.swift
@@ -8,6 +8,7 @@
 import SwiftUI
 import SotoS3
 import AVFoundation
+import UserNotifications
 
 struct DropletDropDelegate : DropDelegate {
     @AppStorage("awsAccessKeyId") var awsAccessKeyId = ""
@@ -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
@@ -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,
@@ -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
                                             }