From 25a8aebe4dc2accf464da52651255048000f00b0 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Fri, 3 Jun 2022 17:25:07 -0700 Subject: [PATCH] resolve merge conflict (web prefs popover) --- .../Share/ExtensionSaveService.swift | 13 +++- .../Services/DataService/DataService.swift | 56 ++--------------- .../OmnivoreKit/Sources/Utils/PDFUtils.swift | 62 +++++++++++++++++++ .../Views/FontSizeAdjustmentPopoverView.swift | 13 +--- .../Sources/Views/FormSheetWrapper.swift | 4 +- .../Sources/Views/ShareExtensionView.swift | 35 +++++++++-- 6 files changed, 112 insertions(+), 71 deletions(-) create mode 100644 apple/OmnivoreKit/Sources/Utils/PDFUtils.swift diff --git a/apple/OmnivoreKit/Sources/App/AppExtensions/Share/ExtensionSaveService.swift b/apple/OmnivoreKit/Sources/App/AppExtensions/Share/ExtensionSaveService.swift index e527cae64..c3c7faf3c 100644 --- a/apple/OmnivoreKit/Sources/App/AppExtensions/Share/ExtensionSaveService.swift +++ b/apple/OmnivoreKit/Sources/App/AppExtensions/Share/ExtensionSaveService.swift @@ -8,6 +8,7 @@ import Foundation import Models import Services +import Utils import Views class ExtensionSaveService { @@ -56,13 +57,19 @@ class ExtensionSaveService { url.path = "/favicon.ico" shareExtensionViewModel.iconURL = url.url?.absoluteString } - case let .pdf(localUrl: _): - shareExtensionViewModel.title = payload.url + case let .pdf(localUrl: localUrl): shareExtensionViewModel.url = hostname + shareExtensionViewModel.title = PDFUtils.titleFromPdfFile(localUrl.absoluteString) + Task { + let localThumbnail = try await PDFUtils.createThumbnailFor(inputUrl: localUrl) + DispatchQueue.main.async { + shareExtensionViewModel.iconURL = localThumbnail?.absoluteString + } + } } } self.queueSaveOperation(payload, requestId: requestId, shareExtensionViewModel: shareExtensionViewModel) - case let .failure(error): + case let .failure: DispatchQueue.main.async { shareExtensionViewModel.status = .failed(error: .unknown(description: "Could not retrieve content")) } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift index b2db01cab..123faa838 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift @@ -147,18 +147,15 @@ public final class DataService: ObservableObject { case let .pdf(localUrl): linkedItem.contentReader = "PDF" linkedItem.localPdfURL = localUrl.absoluteString - linkedItem.title = self.titleFromPdfFile(pageScrape.url) - -// TODO: Attempt to set thumbnail from PDF data -// let thumbnailUrl = DataService.thumbnailUrl(localUrl: localUrl) -// self.createThumbnailFor(inputUrl: localUrl, at: thumbnailUrl) -// linkedItem.imageURLString = thumbnailUrl.absoluteString + linkedItem.title = PDFUtils.titleFromPdfFile(pageScrape.url) +// let thumbnailUrl = PDFUtils.thumbnailUrl(localUrl: localUrl) +// linkedItem.imageURLString = await PDFUtils.createThumbnailFor(inputUrl: localUrl, at: thumbnailUrl) case let .html(html: html, title: title, iconURL: iconURL): linkedItem.contentReader = "WEB" linkedItem.originalHtml = html linkedItem.imageURLString = iconURL - linkedItem.title = title ?? self.titleFromPdfFile(pageScrape.url) + linkedItem.title = title ?? PDFUtils.titleFromPdfFile(pageScrape.url) case .none: print("SAVING URL", linkedItem.unwrappedPageURLString) linkedItem.contentReader = "WEB" @@ -175,49 +172,4 @@ public final class DataService: ObservableObject { } } } - - func titleFromPdfFile(_ urlStr: String) -> String { - let url = URL(string: urlStr) - if let url = url { - return url.lastPathComponent - } - return urlStr - } - - func titleFromUrl(_ urlStr: String) -> String { - let url = URL(string: urlStr) - if let url = url { - return url.lastPathComponent - } - return urlStr - } - - func thumbnailUrl(localUrl: URL) -> URL { - var thumbnailUrl = localUrl - thumbnailUrl.appendPathExtension(".pdf") - return thumbnailUrl - } - - // TODO: we can try to use this to create PDF thumbnails locally - func createThumbnailFor(inputUrl: URL, at outputUrl: URL) { - let size = CGSize(width: 80, height: 80) - let scale = UIScreen.main.scale - - // Create the thumbnail request. - let request = - QLThumbnailGenerator.Request( - fileAt: inputUrl, - size: size, - scale: scale, - representationTypes: .all - ) - - // Retrieve the singleton instance of the thumbnail generator and generate the thumbnails. - let generator = QLThumbnailGenerator.shared - generator.saveBestRepresentation(for: request, to: outputUrl, contentType: UTType.jpeg.identifier) { error in - if let error = error { - print(error.localizedDescription) - } - } - } } diff --git a/apple/OmnivoreKit/Sources/Utils/PDFUtils.swift b/apple/OmnivoreKit/Sources/Utils/PDFUtils.swift new file mode 100644 index 000000000..87f483918 --- /dev/null +++ b/apple/OmnivoreKit/Sources/Utils/PDFUtils.swift @@ -0,0 +1,62 @@ +// +// PDFUtils.swift +// +// +// Created by Jackson Harper on 6/3/22. +// + +import CoreImage +import Foundation +import QuickLookThumbnailing +import UIKit + +public enum PDFUtils { + public static func titleFromPdfFile(_ urlStr: String) -> String { + let url = URL(string: urlStr) + if let url = url { + return url.lastPathComponent + } + return urlStr + } + + public static func titleFromUrl(_ urlStr: String) -> String { + let url = URL(string: urlStr) + if let url = url { + return url.lastPathComponent + } + return urlStr + } + + public static func thumbnailUrl(localUrl: URL) -> URL { + var thumbnailUrl = localUrl + thumbnailUrl.appendPathExtension(".jpg") + return thumbnailUrl + } + + public static func createThumbnailFor(inputUrl: URL) async throws -> URL? { + let size = CGSize(width: 80, height: 80) + let scale = await UIScreen.main.scale + let outputUrl = thumbnailUrl(localUrl: inputUrl) + + // Create the thumbnail request. + let request = + QLThumbnailGenerator.Request( + fileAt: inputUrl, + size: size, + scale: scale, + representationTypes: .all + ) + + // Retrieve the singleton instance of the thumbnail generator and generate the thumbnails. + let generator = QLThumbnailGenerator.shared + return try await withCheckedThrowingContinuation { continuation in + generator.saveBestRepresentation(for: request, to: outputUrl, contentType: UTType.jpeg.identifier) { error in + if let error = error { + continuation.resume(throwing: error) + return + } + continuation.resume(returning: outputUrl) + } + } + } +} diff --git a/apple/OmnivoreKit/Sources/Views/FontSizeAdjustmentPopoverView.swift b/apple/OmnivoreKit/Sources/Views/FontSizeAdjustmentPopoverView.swift index 663d03178..e27e03035 100644 --- a/apple/OmnivoreKit/Sources/Views/FontSizeAdjustmentPopoverView.swift +++ b/apple/OmnivoreKit/Sources/Views/FontSizeAdjustmentPopoverView.swift @@ -43,16 +43,9 @@ public struct WebPreferencesPopoverView: View { public var body: some View { VStack(alignment: .center) { - ZStack { - Text("Preferences").font(.appTitleThree) - HStack { - Spacer() - Button( - action: dismissAction, - label: { Image(systemName: "xmark").foregroundColor(.appGrayTextContrast) } - ) - } - } + Text("Reader Preferences") + .foregroundColor(.appGrayText) + .font(Font.system(size: 17, weight: .semibold)) List { Section("Sizing") { diff --git a/apple/OmnivoreKit/Sources/Views/FormSheetWrapper.swift b/apple/OmnivoreKit/Sources/Views/FormSheetWrapper.swift index 42de57bf2..dfd0776bb 100644 --- a/apple/OmnivoreKit/Sources/Views/FormSheetWrapper.swift +++ b/apple/OmnivoreKit/Sources/Views/FormSheetWrapper.swift @@ -33,8 +33,8 @@ import SwiftUI if controller.traitCollection.userInterfaceIdiom == .phone { if let sheet = controller.sheetPresentationController { - sheet.preferredCornerRadius = 16 - sheet.prefersGrabberVisible = false + // sheet.preferredCornerRadius = 32 + sheet.prefersGrabberVisible = true sheet.detents = [.medium()] sheet.widthFollowsPreferredContentSizeWhenEdgeAttached = true } diff --git a/apple/OmnivoreKit/Sources/Views/ShareExtensionView.swift b/apple/OmnivoreKit/Sources/Views/ShareExtensionView.swift index 591850b07..23ef80ecc 100644 --- a/apple/OmnivoreKit/Sources/Views/ShareExtensionView.swift +++ b/apple/OmnivoreKit/Sources/Views/ShareExtensionView.swift @@ -187,12 +187,37 @@ public struct ShareExtensionChildView: View { } } + private func localImage(from: URL) -> Image? { + do { + if let data = try? Data(contentsOf: from), let img = UIImage(data: data) { + return Image(uiImage: img) + } + } catch { + return nil + } + return nil + } + public var previewCard: some View { HStack { if let iconURLStr = viewModel.iconURL, let iconURL = URL(string: iconURLStr) { - AsyncLoadingImage(url: iconURL) { imageStatus in - if case let AsyncImageStatus.loaded(image) = imageStatus { - image + if !iconURL.isFileURL { + AsyncLoadingImage(url: iconURL) { imageStatus in + if case let AsyncImageStatus.loaded(image) = imageStatus { + image + .resizable() + .aspectRatio(contentMode: .fill) + .frame(width: 61, height: 61) + .clipped() + } else { + Color.appButtonBackground + .aspectRatio(contentMode: .fill) + .frame(width: 61, height: 61) + } + } + } else { + if let localImage = localImage(from: iconURL) { + localImage .resizable() .aspectRatio(contentMode: .fill) .frame(width: 61, height: 61) @@ -204,9 +229,11 @@ public struct ShareExtensionChildView: View { } } } else { - EmptyView() + Color.appButtonBackground + .aspectRatio(contentMode: .fill) .frame(width: 61, height: 61) } + VStack(alignment: .leading) { Text(viewModel.title ?? "") .lineLimit(1)