From e305f5d017a574e645d9d4068f31d6d5c43a312f Mon Sep 17 00:00:00 2001 From: Adrian Castro <22133246+castdrian@users.noreply.github.com> Date: Tue, 3 Dec 2024 14:28:32 +0100 Subject: [PATCH] fix: concurrency stuff once more --- ishare/App.swift | 96 +++++++++++++++++---------------- ishare/Localizable.xcstrings | 6 +++ ishare/Views/MainMenuView.swift | 4 +- 3 files changed, 57 insertions(+), 49 deletions(-) diff --git a/ishare/App.swift b/ishare/App.swift index a1508dd..c253712 100644 --- a/ishare/App.swift +++ b/ishare/App.swift @@ -45,7 +45,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, SPUUpdaterDelegate { static var shared: AppDelegate { sharedInstance } var recordGif = false - var screenRecorder: ScreenRecorder! + var screenRecorder: ScreenRecorder? var updaterController: SPUStandardUpdaterController! func application(_: NSApplication, open urls: [URL]) { @@ -88,8 +88,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, SPUUpdaterDelegate { func applicationDidFinishLaunching(_: Notification) { NSLog("Application finished launching") - AppDelegate.shared = self - + Task { NSLog("Initializing screen recorder") screenRecorder = ScreenRecorder() @@ -122,33 +121,35 @@ class AppDelegate: NSObject, NSApplicationDelegate, SPUUpdaterDelegate { } } #else - class AppDelegate: NSObject, NSApplicationDelegate { - @MainActor private(set) static var shared: AppDelegate! = nil - var recordGif = false - var screenRecorder: ScreenRecorder! - - func application(_: NSApplication, open urls: [URL]) { - if urls.first!.isFileURL { - importIscu(urls.first!) - } +@MainActor +class AppDelegate: NSObject, NSApplicationDelegate { + private static let sharedInstance = AppDelegate() + static var shared: AppDelegate { sharedInstance } + + var recordGif = false + var screenRecorder: ScreenRecorder? - if let url = urls.first { - let path = url.host - let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems + func application(_: NSApplication, open urls: [URL]) { + if urls.first!.isFileURL { + importIscu(urls.first!) + } - if path == "upload" { - if let fileItem = queryItems?.first(where: { $0.name == "file" }) { - if let encodedFileURLString = fileItem.value, let decodedFileURLString = encodedFileURLString.removingPercentEncoding, let fileURL = URL(string: decodedFileURLString) { - print("Received file URL: \(fileURL.absoluteString)") + if let url = urls.first { + let path = url.host + let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems + + if path == "upload" { + if let fileItem = queryItems?.first(where: { $0.name == "file" }) { + if let encodedFileURLString = fileItem.value, let decodedFileURLString = encodedFileURLString.removingPercentEncoding, let fileURL = URL(string: decodedFileURLString) { + print("Received file URL: \(fileURL.absoluteString)") - @Default(.uploadType) var uploadType - let localFileURL = fileURL + @Default(.uploadType) var uploadType + let localFileURL = fileURL - uploadFile(fileURL: fileURL, uploadType: uploadType) { - Task { @MainActor in - showToast(fileURL: localFileURL) { - NSSound.beep() - } + uploadFile(fileURL: fileURL, uploadType: uploadType) { + Task { @MainActor in + showToast(fileURL: localFileURL) { + NSSound.beep() } } } @@ -156,33 +157,34 @@ class AppDelegate: NSObject, NSApplicationDelegate, SPUUpdaterDelegate { } } } + } - func applicationDidFinishLaunching(_: Notification) { - AppDelegate.shared = self - - Task { - screenRecorder = ScreenRecorder() - } + func applicationDidFinishLaunching(_: Notification) { + NSLog("Application finished launching") + + Task { + screenRecorder = ScreenRecorder() } + } - @MainActor - func stopRecording() { - let wasRecordingGif = recordGif - let recorder = screenRecorder - - Task { - recorder?.stop { result in - Task { @MainActor in - switch result { - case let .success(url): - print("Recording stopped successfully. URL: \(url)") - postRecordingTasks(url, wasRecordingGif) - case let .failure(error): - print("Error while stopping recording: \(error.localizedDescription)") - } + @MainActor + func stopRecording() { + let wasRecordingGif = recordGif + let recorder = screenRecorder + + Task { + recorder?.stop { result in + Task { @MainActor in + switch result { + case let .success(url): + print("Recording stopped successfully. URL: \(url)") + postRecordingTasks(url, wasRecordingGif) + case let .failure(error): + print("Error while stopping recording: \(error.localizedDescription)") } } } } } +} #endif diff --git a/ishare/Localizable.xcstrings b/ishare/Localizable.xcstrings index 25b123d..f19ddda 100644 --- a/ishare/Localizable.xcstrings +++ b/ishare/Localizable.xcstrings @@ -42,6 +42,9 @@ }, "Capture Window:" : { + }, + "Check for Updates" : { + }, "Clear All" : { @@ -66,6 +69,9 @@ }, "Delete Uploader" : { + }, + "Donate" : { + }, "Drag and drop .iscu files here or click to select" : { diff --git a/ishare/Views/MainMenuView.swift b/ishare/Views/MainMenuView.swift index 6277347..a09626b 100644 --- a/ishare/Views/MainMenuView.swift +++ b/ishare/Views/MainMenuView.swift @@ -118,7 +118,7 @@ struct MainMenuView: View { label: { Image(systemName: "menubar.dock.rectangle.badge.record") Text("Record") - }.globalKeyboardShortcut(.recordScreen).disabled(AppDelegate.shared?.screenRecorder?.isRunning ?? false) + }.globalKeyboardShortcut(.recordScreen).disabled(AppDelegate.shared.screenRecorder?.isRunning ?? false) Button { recordScreen(gif: true) @@ -126,7 +126,7 @@ struct MainMenuView: View { label: { Image(systemName: "photo.stack") Text("Record GIF") - }.globalKeyboardShortcut(.recordGif).disabled(AppDelegate.shared?.screenRecorder?.isRunning ?? false) + }.globalKeyboardShortcut(.recordGif).disabled(AppDelegate.shared.screenRecorder?.isRunning ?? false) } VStack { Menu {