From b113d789045c9b25ac4d686b07ae13dfe5262fdc Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Tue, 21 Jun 2022 22:05:39 -0700 Subject: [PATCH] add os conditionals to get mac app running: --- .../xcshareddata/swiftpm/Package.resolved | 9 ---- apple/OmnivoreKit/Package.swift | 4 +- .../Share/ExtensionSaveService.swift | 42 +++++++++---------- .../App/Views/LinkedItemTitleEditView.swift | 34 ++++++++------- .../Views/Registration/RegistrationView.swift | 15 +------ .../Models/CoreData/StorageProvider.swift | 1 + .../Services/Authentication/GoogleAuth.swift | 31 ++++++++++++-- .../Services/DataService/DataService.swift | 7 +++- .../OmnivoreKit/Sources/Utils/PDFUtils.swift | 12 +++++- .../Sources/Views/Article/WebAppView.swift | 9 +++- .../Sources/Views/Article/WebView.swift | 4 +- .../Views/FontSizeAdjustmentPopoverView.swift | 14 +++++-- .../Sources/Views/ShareExtensionView.swift | 42 ++++--------------- 13 files changed, 113 insertions(+), 111 deletions(-) diff --git a/apple/Omnivore.xcworkspace/xcshareddata/swiftpm/Package.resolved b/apple/Omnivore.xcworkspace/xcshareddata/swiftpm/Package.resolved index 83c1ac48d..5090a25ab 100644 --- a/apple/Omnivore.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/apple/Omnivore.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -153,15 +153,6 @@ "version" : "2.1.0" } }, - { - "identity" : "pspdfkit-sp", - "kind" : "remoteSourceControl", - "location" : "https://github.com/PSPDFKit/PSPDFKit-SP", - "state" : { - "branch" : "master", - "revision" : "0e18629c443e3f39ecfee0f600d9ef5551ecf488" - } - }, { "identity" : "sovran-swift", "kind" : "remoteSourceControl", diff --git a/apple/OmnivoreKit/Package.swift b/apple/OmnivoreKit/Package.swift index f5ef78249..58f0d3de0 100644 --- a/apple/OmnivoreKit/Package.swift +++ b/apple/OmnivoreKit/Package.swift @@ -56,7 +56,7 @@ let package = Package( var appPackageDependencies: [Target.Dependency] { var deps: [Target.Dependency] = ["Views", "Services", "Models", "Utils"] // #if canImport(UIKit) - deps.append(.product(name: "PSPDFKit", package: "PSPDFKit-SP")) +// deps.append(.product(name: "PSPDFKit", package: "PSPDFKit-SP")) // #endif return deps } @@ -70,7 +70,7 @@ var dependencies: [Package.Dependency] { .package(url: "https://github.com/google/GoogleSignIn-iOS", from: "6.2.2") ] // #if canImport(UIKit) - deps.append(.package(url: "https://github.com/PSPDFKit/PSPDFKit-SP", branch: "master")) +// deps.append(.package(url: "https://github.com/PSPDFKit/PSPDFKit-SP", branch: "master")) // #endif return deps } diff --git a/apple/OmnivoreKit/Sources/App/AppExtensions/Share/ExtensionSaveService.swift b/apple/OmnivoreKit/Sources/App/AppExtensions/Share/ExtensionSaveService.swift index 4875e0ba6..9ecd7fc0f 100644 --- a/apple/OmnivoreKit/Sources/App/AppExtensions/Share/ExtensionSaveService.swift +++ b/apple/OmnivoreKit/Sources/App/AppExtensions/Share/ExtensionSaveService.swift @@ -1,10 +1,3 @@ -// -// File.swift -// -// -// Created by Jackson Harper on 6/1/22. -// - import Foundation import Models import Services @@ -18,23 +11,25 @@ class ExtensionSaveService { self.queue = OperationQueue() } - private func queueSaveOperation( - _ pageScrape: PageScrapePayload, - shareExtensionViewModel: ShareExtensionChildViewModel - ) { - ProcessInfo().performExpiringActivity(withReason: "app.omnivore.SaveActivity") { [self] expiring in - guard !expiring else { - self.queue.cancelAllOperations() + #if os(iOS) + private func queueSaveOperation( + _ pageScrape: PageScrapePayload, + shareExtensionViewModel: ShareExtensionChildViewModel + ) { + ProcessInfo().performExpiringActivity(withReason: "app.omnivore.SaveActivity") { [self] expiring in + guard !expiring else { + self.queue.cancelAllOperations() + self.queue.waitUntilAllOperationsAreFinished() + return + } + + let operation = SaveOperation(pageScrapePayload: pageScrape, shareExtensionViewModel: shareExtensionViewModel) + + self.queue.addOperation(operation) self.queue.waitUntilAllOperationsAreFinished() - return } - - let operation = SaveOperation(pageScrapePayload: pageScrape, shareExtensionViewModel: shareExtensionViewModel) - - self.queue.addOperation(operation) - self.queue.waitUntilAllOperationsAreFinished() } - } + #endif public func save(_ extensionContext: NSExtensionContext, shareExtensionViewModel: ShareExtensionChildViewModel) { PageScraper.scrape(extensionContext: extensionContext) { [weak self] result in @@ -71,7 +66,10 @@ class ExtensionSaveService { } } } - self.queueSaveOperation(payload, shareExtensionViewModel: shareExtensionViewModel) + #if os(iOS) + // TODO: need alternative call for macos + self.queueSaveOperation(payload, shareExtensionViewModel: shareExtensionViewModel) + #endif case .failure: DispatchQueue.main.async { shareExtensionViewModel.status = .failed(error: .unknown(description: "Could not retrieve content")) diff --git a/apple/OmnivoreKit/Sources/App/Views/LinkedItemTitleEditView.swift b/apple/OmnivoreKit/Sources/App/Views/LinkedItemTitleEditView.swift index e8911c89d..4fb72af6e 100644 --- a/apple/OmnivoreKit/Sources/App/Views/LinkedItemTitleEditView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/LinkedItemTitleEditView.swift @@ -66,24 +66,26 @@ struct LinkedItemTitleEditView: View { NavigationView { editForm .navigationTitle("Edit Title and Description") + #if os(iOS) .navigationBarTitleDisplayMode(.inline) - .toolbar { - ToolbarItem(placement: .barTrailing) { - Button( - action: { - viewModel.submit(dataService: dataService, item: item) - presentationMode.wrappedValue.dismiss() - }, - label: { Text("Save").foregroundColor(.appGrayTextContrast) } - ) - } - ToolbarItem(placement: .barLeading) { - Button( - action: { presentationMode.wrappedValue.dismiss() }, - label: { Text("Cancel").foregroundColor(.appGrayTextContrast) } - ) - } + #endif + .toolbar { + ToolbarItem(placement: .barTrailing) { + Button( + action: { + viewModel.submit(dataService: dataService, item: item) + presentationMode.wrappedValue.dismiss() + }, + label: { Text("Save").foregroundColor(.appGrayTextContrast) } + ) } + ToolbarItem(placement: .barLeading) { + Button( + action: { presentationMode.wrappedValue.dismiss() }, + label: { Text("Cancel").foregroundColor(.appGrayTextContrast) } + ) + } + } } .task { viewModel.load(item: item) } } diff --git a/apple/OmnivoreKit/Sources/App/Views/Registration/RegistrationView.swift b/apple/OmnivoreKit/Sources/App/Views/Registration/RegistrationView.swift index 863ac8751..f365a467e 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Registration/RegistrationView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Registration/RegistrationView.swift @@ -59,8 +59,7 @@ import Views } func handleGoogleAuth(authenticator: Authenticator) async { - guard let presentingViewController = presentingViewController() else { return } - let googleAuthResponse = await authenticator.handleGoogleAuth(presenting: presentingViewController) + let googleAuthResponse = await authenticator.handleGoogleAuth() switch googleAuthResponse { case let .loginError(error): @@ -72,15 +71,3 @@ import Views } } } - -private func presentingViewController() -> PlatformViewController? { - #if os(iOS) - let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene - return scene?.windows - .filter(\.isKeyWindow) - .first? - .rootViewController - #elseif os(macOS) - return nil - #endif -} diff --git a/apple/OmnivoreKit/Sources/Models/CoreData/StorageProvider.swift b/apple/OmnivoreKit/Sources/Models/CoreData/StorageProvider.swift index 374770177..2ef169fc4 100644 --- a/apple/OmnivoreKit/Sources/Models/CoreData/StorageProvider.swift +++ b/apple/OmnivoreKit/Sources/Models/CoreData/StorageProvider.swift @@ -15,6 +15,7 @@ public class PersistentContainer: NSPersistentContainer { // Store the sqlite file in the app group container. // This allows shared access for app and app extensions. let appGroupID = "group.app.omnivoreapp" + // TODO: fix this for macos...it's crashing locally when using forSecurityApplicationGroupIdentifier let appGroupContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupID) let appGroupContainerURL = appGroupContainer?.appendingPathComponent("store.sqlite") container.persistentStoreDescriptions.first!.url = appGroupContainerURL diff --git a/apple/OmnivoreKit/Sources/Services/Authentication/GoogleAuth.swift b/apple/OmnivoreKit/Sources/Services/Authentication/GoogleAuth.swift index 1302aae7a..52b64c944 100644 --- a/apple/OmnivoreKit/Sources/Services/Authentication/GoogleAuth.swift +++ b/apple/OmnivoreKit/Sources/Services/Authentication/GoogleAuth.swift @@ -10,8 +10,8 @@ public enum GoogleAuthResponse { } extension Authenticator { - public func handleGoogleAuth(presenting: PlatformViewController) async -> GoogleAuthResponse { - let idToken = try? await googleSignIn(presenting: presenting) + public func handleGoogleAuth() async -> GoogleAuthResponse { + let idToken = try? await googleSignIn() guard let idToken = idToken else { return .loginError(error: .unauthorized) } do { @@ -47,9 +47,20 @@ extension Authenticator { } } - func googleSignIn(presenting: PlatformViewController) async throws -> String { - try await withCheckedThrowingContinuation { continuation in + func googleSignIn() async throws -> String { + #if os(iOS) + let presenting = presentingViewController() + #else + let presenting = await NSApplication.shared.mainWindow + #endif + + guard let presenting = presenting else { + throw LoginError.unknown + } + return try await withCheckedThrowingContinuation { continuation in + let clientID = "\(AppKeys.sharedInstance?.iosClientGoogleId ?? "").apps.googleusercontent.com" + GIDSignIn.sharedInstance.signIn( with: GIDConfiguration(clientID: clientID), presenting: presenting @@ -71,3 +82,15 @@ extension Authenticator { } } } + +private func presentingViewController() -> PlatformViewController? { + #if os(iOS) + let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene + return scene?.windows + .filter(\.isKeyWindow) + .first? + .rootViewController + #elseif os(macOS) + return nil + #endif +} diff --git a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift index 0eb0d41ca..078a6ffea 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift @@ -4,9 +4,14 @@ import Foundation import Models import OSLog import QuickLookThumbnailing -import UIKit import Utils +#if os(iOS) + import UIKit +#else + import AppKit +#endif + let logger = Logger(subsystem: "app.omnivore", category: "data-service") public final class DataService: ObservableObject { diff --git a/apple/OmnivoreKit/Sources/Utils/PDFUtils.swift b/apple/OmnivoreKit/Sources/Utils/PDFUtils.swift index 074ec2c03..b722edc3f 100644 --- a/apple/OmnivoreKit/Sources/Utils/PDFUtils.swift +++ b/apple/OmnivoreKit/Sources/Utils/PDFUtils.swift @@ -1,7 +1,11 @@ import CoreImage import Foundation import QuickLookThumbnailing -import UIKit +#if os(iOS) + import UIKit +#else + import AppKit +#endif public enum PDFUtils { public static func copyToLocal(url: URL) throws -> String { @@ -64,7 +68,11 @@ public enum PDFUtils { public static func createThumbnailFor(inputUrl: URL) async throws -> URL? { let size = CGSize(width: 80, height: 80) - let scale = await UIScreen.main.scale + #if os(iOS) + let scale = await UIScreen.main.scale + #else + let scale = NSScreen.main?.backingScaleFactor ?? 1 + #endif let outputUrl = thumbnailUrl(localUrl: inputUrl) // Create the thumbnail request. diff --git a/apple/OmnivoreKit/Sources/Views/Article/WebAppView.swift b/apple/OmnivoreKit/Sources/Views/Article/WebAppView.swift index 4dfc776cd..5baa10fce 100644 --- a/apple/OmnivoreKit/Sources/Views/Article/WebAppView.swift +++ b/apple/OmnivoreKit/Sources/Views/Article/WebAppView.swift @@ -168,14 +168,19 @@ public enum WebViewManager { context.coordinator.needsReload = false } + if annotationSaveTransactionID != context.coordinator.lastSavedAnnotationID { + context.coordinator.lastSavedAnnotationID = annotationSaveTransactionID + (webView as? WebView)?.dispatchEvent(.saveAnnotation(annotation: annotation)) + } + if sendIncreaseFontSignal { sendIncreaseFontSignal = false - (webView as? WebView)?.increaseFontSize() + (webView as? WebView)?.updateFontSize() } if sendDecreaseFontSignal { sendDecreaseFontSignal = false - (webView as? WebView)?.decreaseFontSize() + (webView as? WebView)?.updateFontSize() } } } diff --git a/apple/OmnivoreKit/Sources/Views/Article/WebView.swift b/apple/OmnivoreKit/Sources/Views/Article/WebView.swift index 62e6ebd76..3828ea872 100644 --- a/apple/OmnivoreKit/Sources/Views/Article/WebView.swift +++ b/apple/OmnivoreKit/Sources/Views/Article/WebView.swift @@ -86,9 +86,9 @@ public final class WebView: WKWebView { super.viewDidChangeEffectiveAppearance() switch effectiveAppearance.bestMatch(from: [.aqua, .darkAqua]) { case .some(.darkAqua): - dispatchEvent("switchToDarkMode") + dispatchEvent(.updateColorMode(isDark: true)) default: - dispatchEvent("switchToLightMode") + dispatchEvent(.updateColorMode(isDark: false)) } } #endif diff --git a/apple/OmnivoreKit/Sources/Views/FontSizeAdjustmentPopoverView.swift b/apple/OmnivoreKit/Sources/Views/FontSizeAdjustmentPopoverView.swift index 5746cd9c1..b1fbe1b82 100644 --- a/apple/OmnivoreKit/Sources/Views/FontSizeAdjustmentPopoverView.swift +++ b/apple/OmnivoreKit/Sources/Views/FontSizeAdjustmentPopoverView.swift @@ -81,7 +81,9 @@ public struct WebPreferencesPopoverView: View { } } .listStyle(.plain) - .navigationBarTitleDisplayMode(.inline) + #if os(iOS) + .navigationBarTitleDisplayMode(.inline) + #endif .navigationTitle("Reader Font") } @@ -148,9 +150,11 @@ public struct WebPreferencesPopoverView: View { } .padding() .navigationTitle("Reader Preferences") - .navigationBarTitleDisplayMode(.inline) + #if os(iOS) + .navigationBarTitleDisplayMode(.inline) + #endif .toolbar { - ToolbarItem(placement: .navigationBarTrailing) { + ToolbarItem(placement: .barTrailing) { Button( action: dismissAction, label: { Text("Done").foregroundColor(.appGrayTextContrast).padding() } @@ -158,7 +162,9 @@ public struct WebPreferencesPopoverView: View { } } } - .navigationViewStyle(.stack) + #if os(iOS) + .navigationViewStyle(.stack) + #endif .accentColor(.appGrayTextContrast) } } diff --git a/apple/OmnivoreKit/Sources/Views/ShareExtensionView.swift b/apple/OmnivoreKit/Sources/Views/ShareExtensionView.swift index bf5186771..fb8d32fb7 100644 --- a/apple/OmnivoreKit/Sources/Views/ShareExtensionView.swift +++ b/apple/OmnivoreKit/Sources/Views/ShareExtensionView.swift @@ -37,36 +37,6 @@ public enum ShareExtensionStatus { } } -struct CornerRadiusStyle: ViewModifier { - var radius: CGFloat - var corners: UIRectCorner - - struct CornerRadiusShape: Shape { - var radius = CGFloat.infinity - var corners = UIRectCorner.allCorners - - func path(in rect: CGRect) -> Path { - let path = UIBezierPath( - roundedRect: rect, - byRoundingCorners: corners, - cornerRadii: CGSize(width: radius, height: radius) - ) - return Path(path.cgPath) - } - } - - func body(content: Content) -> some View { - content - .clipShape(CornerRadiusShape(radius: radius, corners: corners)) - } -} - -extension View { - func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View { - ModifiedContent(content: self, modifier: CornerRadiusStyle(radius: radius, corners: corners)) - } -} - private extension SaveArticleError { var displayMessage: String { switch self { @@ -197,9 +167,15 @@ public struct ShareExtensionChildView: View { } private func localImage(from url: URL) -> Image? { - if let data = try? Data(contentsOf: url), let img = UIImage(data: data) { - return Image(uiImage: img) - } + #if os(iOS) + if let data = try? Data(contentsOf: url), let img = UIImage(data: data) { + return Image(uiImage: img) + } + #else + if let data = try? Data(contentsOf: url), let img = NSImage(data: data) { + return Image(nsImage: img) + } + #endif return nil }