Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use native quick look for image and attachments, improve image display #29

Merged
merged 2 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions MoeMemos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@
191339DC291D8F15001F2B2E /* MemoryWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 191339DB291D8F15001F2B2E /* MemoryWidget.swift */; };
192DF0F328C78BAD0077E785 /* ArchivedMemosList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 192DF0F228C78BAD0077E785 /* ArchivedMemosList.swift */; };
192DF0F528C7B71D0077E785 /* LegacyShareSheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 192DF0F428C7B71D0077E785 /* LegacyShareSheet.swift */; };
1934C77A2921666000A169EC /* SafariView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1934C7792921666000A169EC /* SafariView.swift */; };
1934C77C2921670600A169EC /* Attachment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1934C77B2921670600A169EC /* Attachment.swift */; };
193AA89B28CCD34A00FF7EB6 /* Resources.swift in Sources */ = {isa = PBXBuildFile; fileRef = 193AA89A28CCD34A00FF7EB6 /* Resources.swift */; };
193AA89D28CCD38100FF7EB6 /* Resource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 193AA89C28CCD38100FF7EB6 /* Resource.swift */; };
193AA89F28CD080000FF7EB6 /* LegacyPhotoPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 193AA89E28CD080000FF7EB6 /* LegacyPhotoPicker.swift */; };
193AA8A128CD12CD00FF7EB6 /* FormData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 193AA8A028CD12CD00FF7EB6 /* FormData.swift */; };
193AA8A328CD1C9300FF7EB6 /* UIImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 193AA8A228CD1C9300FF7EB6 /* UIImage.swift */; };
193AA8A528CD9AE200FF7EB6 /* ImagePreview.swift in Sources */ = {isa = PBXBuildFile; fileRef = 193AA8A428CD9AE200FF7EB6 /* ImagePreview.swift */; };
193AA8A728CD9ED700FF7EB6 /* URL.swift in Sources */ = {isa = PBXBuildFile; fileRef = 193AA8A628CD9ED700FF7EB6 /* URL.swift */; };
193AA8A928CDB27F00FF7EB6 /* ResourceCard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 193AA8A828CDB27F00FF7EB6 /* ResourceCard.swift */; };
193DE92128C5CFBB002651E0 /* User.swift in Sources */ = {isa = PBXBuildFile; fileRef = 193DE92028C5CFBB002651E0 /* User.swift */; };
Expand All @@ -61,6 +59,7 @@
195E5D6D28C7051F00784EF5 /* Memos.swift in Sources */ = {isa = PBXBuildFile; fileRef = 195E5D6C28C7051F00784EF5 /* Memos.swift */; };
195E5D7128C70A3800784EF5 /* HeatmapStat.swift in Sources */ = {isa = PBXBuildFile; fileRef = 195E5D7028C70A3800784EF5 /* HeatmapStat.swift */; };
1973B7D72916308D003C3710 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1973B7D62916308D003C3710 /* Constants.swift */; };
197A4D3329599C300027427E /* MemoCardImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 197A4D3229599C300027427E /* MemoCardImageView.swift */; };
197C21A6293A2DCC00110EE5 /* AlertToast.swift in Sources */ = {isa = PBXBuildFile; fileRef = 195E5AD228C84E4F00CD4674 /* AlertToast.swift */; };
197D9D47292D4B160033AF33 /* ImagePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 197D9D46292D4B160033AF33 /* ImagePicker.swift */; };
1995E9CE28C46DC9004F2EDB /* Heatmap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1995E9CD28C46DC9004F2EDB /* Heatmap.swift */; };
Expand Down Expand Up @@ -159,14 +158,12 @@
191339DB291D8F15001F2B2E /* MemoryWidget.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MemoryWidget.swift; sourceTree = "<group>"; };
192DF0F228C78BAD0077E785 /* ArchivedMemosList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArchivedMemosList.swift; sourceTree = "<group>"; };
192DF0F428C7B71D0077E785 /* LegacyShareSheet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LegacyShareSheet.swift; sourceTree = "<group>"; };
1934C7792921666000A169EC /* SafariView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SafariView.swift; sourceTree = "<group>"; };
1934C77B2921670600A169EC /* Attachment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Attachment.swift; sourceTree = "<group>"; };
193AA89A28CCD34A00FF7EB6 /* Resources.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Resources.swift; sourceTree = "<group>"; };
193AA89C28CCD38100FF7EB6 /* Resource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Resource.swift; sourceTree = "<group>"; };
193AA89E28CD080000FF7EB6 /* LegacyPhotoPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LegacyPhotoPicker.swift; sourceTree = "<group>"; };
193AA8A028CD12CD00FF7EB6 /* FormData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FormData.swift; sourceTree = "<group>"; };
193AA8A228CD1C9300FF7EB6 /* UIImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIImage.swift; sourceTree = "<group>"; };
193AA8A428CD9AE200FF7EB6 /* ImagePreview.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImagePreview.swift; sourceTree = "<group>"; };
193AA8A628CD9ED700FF7EB6 /* URL.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URL.swift; sourceTree = "<group>"; };
193AA8A828CDB27F00FF7EB6 /* ResourceCard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResourceCard.swift; sourceTree = "<group>"; };
193DE92028C5CFBB002651E0 /* User.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = User.swift; sourceTree = "<group>"; };
Expand All @@ -192,6 +189,7 @@
195E5D7028C70A3800784EF5 /* HeatmapStat.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HeatmapStat.swift; sourceTree = "<group>"; };
1973B7D529160F48003C3710 /* MoeMemos.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = MoeMemos.entitlements; sourceTree = "<group>"; };
1973B7D62916308D003C3710 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
197A4D3229599C300027427E /* MemoCardImageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoCardImageView.swift; sourceTree = "<group>"; };
197D9D46292D4B160033AF33 /* ImagePicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImagePicker.swift; sourceTree = "<group>"; };
1995E9CD28C46DC9004F2EDB /* Heatmap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Heatmap.swift; sourceTree = "<group>"; };
19996E5F29118AF6001E3583 /* NavLink.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavLink.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -307,7 +305,6 @@
192DF0F428C7B71D0077E785 /* LegacyShareSheet.swift */,
19E6EF2229115A2800C998F5 /* MemosManager.swift */,
19FC1C7B290DA47C0078A7F2 /* Route.swift */,
1934C7792921666000A169EC /* SafariView.swift */,
);
path = Helpers;
sourceTree = "<group>";
Expand Down Expand Up @@ -425,9 +422,9 @@
194B273228C3973400A48675 /* ContentView.swift */,
1995E9CD28C46DC9004F2EDB /* Heatmap.swift */,
195E5D7028C70A3800784EF5 /* HeatmapStat.swift */,
193AA8A428CD9AE200FF7EB6 /* ImagePreview.swift */,
199A658028C48A6200C39994 /* Login.swift */,
19F31BFC28C3ED11000C5207 /* MemoCard.swift */,
197A4D3229599C300027427E /* MemoCardImageView.swift */,
19E1C5BF28C5191D0072CEEF /* MemoInput.swift */,
19F31BF628C3E26D000C5207 /* MemosList.swift */,
19FC1C7D290DA6E90078A7F2 /* Navigation.swift */,
Expand Down Expand Up @@ -674,14 +671,12 @@
19E1C5BE28C4F6410072CEEF /* Stats.swift in Sources */,
199A658128C48A6200C39994 /* Login.swift in Sources */,
19F31BFD28C3ED11000C5207 /* MemoCard.swift in Sources */,
193AA8A528CD9AE200FF7EB6 /* ImagePreview.swift in Sources */,
199A657F28C47D7C00C39994 /* Sidebar.swift in Sources */,
193DE92128C5CFBB002651E0 /* User.swift in Sources */,
195E5AD328C84E4F00CD4674 /* AlertToast.swift in Sources */,
19E6EF1F29113EC900C998F5 /* MemoInputViewModel.swift in Sources */,
193DE93028C65104002651E0 /* MemosViewModel.swift in Sources */,
1973B7D72916308D003C3710 /* Constants.swift in Sources */,
1934C77A2921666000A169EC /* SafariView.swift in Sources */,
197D9D47292D4B160033AF33 /* ImagePicker.swift in Sources */,
19E6EF2329115A2800C998F5 /* MemosManager.swift in Sources */,
192DF0F328C78BAD0077E785 /* ArchivedMemosList.swift in Sources */,
Expand All @@ -693,6 +688,7 @@
193DE92B28C619D9002651E0 /* HTTP.swift in Sources */,
193AA89D28CCD38100FF7EB6 /* Resource.swift in Sources */,
19E29A7D2939A94400BE9C32 /* NSItemProvider.swift in Sources */,
197A4D3329599C300027427E /* MemoCardImageView.swift in Sources */,
193AA8A928CDB27F00FF7EB6 /* ResourceCard.swift in Sources */,
195E5D6D28C7051F00784EF5 /* Memos.swift in Sources */,
1995E9CE28C46DC9004F2EDB /* Heatmap.swift in Sources */,
Expand Down
43 changes: 43 additions & 0 deletions MoeMemos/API/Memos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Foundation
import CryptoKit

private let cookieStorage = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: groupContainerIdentifier)

Expand Down Expand Up @@ -86,4 +87,46 @@ class Memos {
func deleteResource(id: Int) async throws -> MemosDeleteResource.Output {
return try await MemosDeleteResource.request(self, data: nil, param: id)
}

func url(for resource: Resource) -> URL {
// to be compatible with future Memos release with resource visibility
var url = host.appendingPathComponent(resource.path())
if let openId = openId, !openId.isEmpty {
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)!
var queryItems = components.queryItems ?? []
queryItems.append(URLQueryItem(name: "openId", value: openId))
components.queryItems = queryItems
url = components.url!
}
return url
}

func download(url: URL) async throws -> URL {
guard let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupContainerIdentifier) else { throw MemosError.unknown }

let hash = SHA256.hash(data: url.absoluteString.data(using: .utf8)!)
let hex = hash.map { String(format: "%02X", $0) }[0...10].joined()

let downloadDestination = containerURL.appendingPathComponent("Library/Caches")
.appendingPathComponent(hex).appendingPathExtension(url.pathExtension)

try FileManager.default.createDirectory(at: downloadDestination.deletingLastPathComponent(), withIntermediateDirectories: true)

do {
if try downloadDestination.checkResourceIsReachable() {
return downloadDestination
}
} catch {}

let (tmpURL, response) = try await session.download(for: URLRequest(url: url))
guard let response = response as? HTTPURLResponse else {
throw MemosError.unknown
}
if response.statusCode < 200 || response.statusCode >= 300 {
throw MemosError.invalidStatusCode(response.statusCode, url.absoluteString)
}

try FileManager.default.moveItem(at: tmpURL, to: downloadDestination)
return downloadDestination
}
}
21 changes: 0 additions & 21 deletions MoeMemos/Helpers/SafariView.swift

This file was deleted.

25 changes: 19 additions & 6 deletions MoeMemos/Views/Attachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,25 @@ struct Attachment: View {
let resource: Resource
@State var showingAttachment = false
@EnvironmentObject private var memosManager: MemosManager
@State private var downloadedURL: URL?
@State private var downloadError: Error?
@State private var showingErrorToast = false
@State private var downloading = false

var body: some View {
Button {
showingAttachment = true
Task {
do {
downloading = true
if let memos = memosManager.memos {
downloadedURL = try await memos.download(url: memos.url(for: resource))
}
} catch {
showingErrorToast = true
downloadError = error
}
downloading = false
}
} label: {
HStack {
Image(systemName: "paperclip")
Expand All @@ -23,11 +38,9 @@ struct Attachment: View {
}
.buttonStyle(BorderlessButtonStyle())
.padding([.top, .bottom], 5)
.sheet(isPresented: $showingAttachment) {
if let hostURL = memosManager.hostURL {
SafariView(url: hostURL.appendingPathComponent(resource.path()))
}
}
.toast(isPresenting: $showingErrorToast, alertType: .systemImage("xmark.circle", downloadError?.localizedDescription))
.toast(isPresenting: $downloading, alertType: .loading)
.quickLookPreview($downloadedURL)
}
}

Expand Down
75 changes: 0 additions & 75 deletions MoeMemos/Views/ImagePreview.swift

This file was deleted.

Loading