From 9ea466610b3941155e864e5d6b5479a5156f1170 Mon Sep 17 00:00:00 2001 From: John Costa Date: Sat, 3 May 2025 11:14:57 +0100 Subject: [PATCH] fix --- .../apple/Haystack/ShareViewController.swift | 164 +++++++++++++----- 1 file changed, 116 insertions(+), 48 deletions(-) diff --git a/frontend/src-tauri/gen/apple/Haystack/ShareViewController.swift b/frontend/src-tauri/gen/apple/Haystack/ShareViewController.swift index c102aef..7ed03e4 100644 --- a/frontend/src-tauri/gen/apple/Haystack/ShareViewController.swift +++ b/frontend/src-tauri/gen/apple/Haystack/ShareViewController.swift @@ -1,8 +1,8 @@ // -// ShareViewController.swift -// Haystack +//  ShareViewController.swift +//  Haystack // -// Created by Rio Keefe on 03/05/2025. +//  Created by Rio Keefe on 03/05/2025. // import UIKit @@ -16,88 +16,156 @@ class ShareViewController: SLComposeServiceViewController { let uploadURL = URL(string: "http://192.168.1.199:3040/image/")! var bearerToken: String? + // Store the item provider to access it later in didSelectPost + private var imageItemProvider: NSItemProvider? + private var extractedImageName: String = "image" // Default name override func viewDidLoad() { super.viewDidLoad() - // Load the bearer token from the App Group + // Load the bearer token from the App Group in viewDidLoad + // This is okay as reading from UserDefaults is fast if let sharedDefaults = UserDefaults(suiteName: appGroupName) { bearerToken = sharedDefaults.string(forKey: tokenKey) print("Retrieved bearer token: \(bearerToken ?? "nil")") } else { print("Error accessing App Group UserDefaults.") + // Optionally inform the user or disable posting if token is crucial + // self.isContentValid() could check if bearerToken is nil } + // Store the item provider, but don't load the data synchronously yet if let item = extensionContext?.inputItems.first as? NSExtensionItem, let provider = item.attachments?.first as? NSItemProvider { if provider.hasItemConformingToTypeIdentifier(kUTTypeImage as String) { - let semaphore = DispatchSemaphore(value: 0) - var rawImageData: Data? - var extractedName = "image.png" // Default name + self.imageItemProvider = provider + // Attempt to get a suggested name early if available + extractedImageName = provider.suggestedName ?? "image" + if let dotRange = extractedImageName.range(of: ".", options: .backwards) { + extractedImageName = String(extractedImageName[.. Bool { + // Content is valid only if we have an item provider for an image AND a bearer token + return imageItemProvider != nil && bearerToken != nil + } + + override func didSelectPost() { + // This method is called when the user taps the "Post" button. + // Start the asynchronous operation here. + + guard let provider = imageItemProvider else { + print("Error: No image item provider found when posting.") + // Inform the user or log an error + extensionContext!.completeRequest(returningItems: [], completionHandler: nil) return } + guard let token = bearerToken else { + print("Error: Bearer token is missing when posting.") + // Inform the user or log an error + extensionContext!.completeRequest(returningItems: [], completionHandler: nil) + return + } + + // Load the image data asynchronously + provider.loadItem(forTypeIdentifier: kUTTypeImage as String, options: nil) { [weak self] (item, error) in + guard let self = self else { return } + + if let error = error { + print("Error loading image data for upload: \(error.localizedDescription)") + // Inform the user about the failure + self.extensionContext!.cancelRequest(withError: error) + return + } + + var rawImageData: Data? + var finalImageName = self.extractedImageName // Use the name extracted earlier + + if let url = item as? URL, let data = try? Data(contentsOf: url) { + rawImageData = data + // Refine the name extraction here if necessary, though doing it in viewDidLoad is also an option + finalImageName = url.lastPathComponent + if let dotRange = finalImageName.range(of: ".", options: .backwards) { + finalImageName = String(finalImageName[.. Bool { - return true // Always valid for now - } - - override func didSelectPost() { - // The upload happens in uploadRawData after the data is loaded - } - override func configurationItems() -> [Any]! { + // You can add items here if you want to allow the user to enter additional info + // e.g., a text field for a caption. + // This example only handles image upload, so no config items are needed. return [] } }