resolve merge conflict (web prefs popover)

This commit is contained in:
Satindar Dhillon
2022-06-03 17:25:07 -07:00
parent 043cb25c32
commit 25a8aebe4d
6 changed files with 112 additions and 71 deletions

View File

@ -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"))
}

View File

@ -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)
}
}
}
}

View File

@ -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)
}
}
}
}

View File

@ -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") {

View File

@ -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
}

View File

@ -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)