Skip to content

Commit

Permalink
fix: concurrency stuff once more
Browse files Browse the repository at this point in the history
  • Loading branch information
castdrian committed Dec 3, 2024
1 parent 9a5934d commit e305f5d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 49 deletions.
96 changes: 49 additions & 47 deletions ishare/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -122,67 +121,70 @@ 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()
}
}
}
}
}
}
}
}

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
6 changes: 6 additions & 0 deletions ishare/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
},
"Capture Window:" : {

},
"Check for Updates" : {

},
"Clear All" : {

Expand All @@ -66,6 +69,9 @@
},
"Delete Uploader" : {

},
"Donate" : {

},
"Drag and drop .iscu files here or click to select" : {

Expand Down
4 changes: 2 additions & 2 deletions ishare/Views/MainMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ 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)
}
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 {
Expand Down

0 comments on commit e305f5d

Please sign in to comment.