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

Fix share/export content depending on it's type #820

Merged
merged 5 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions Model/Entities/Entities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ struct URLContentMetaData {
var isMediaType: Bool {
mime.hasPrefix("video/") || mime.hasPrefix("audio/")
}

var isTextType: Bool {
mime.hasPrefix("text/")
}
}

struct URLContent {
Expand Down
15 changes: 13 additions & 2 deletions ViewModel/BrowserViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ final class BrowserViewModel: NSObject, ObservableObject,
}
}
@Published var externalURL: URL?
private var metaData: URLContentMetaData?

private(set) var tabID: NSManagedObjectID? {
didSet {
Expand Down Expand Up @@ -158,15 +159,25 @@ final class BrowserViewModel: NSObject, ObservableObject,
}
}

func pdfData() async -> Data? {
if metaData?.isTextType == true {
kelson42 marked this conversation as resolved.
Show resolved Hide resolved
return try? await webView.pdf()
} else if let url = await webView.url {
return ZimFileService.shared.getURLContent(url: url)?.data
}
return nil
}

private func didUpdate(title: String, url: URL) {
let zimFile: ZimFile? = {
guard let zimFileID = UUID(uuidString: url.host ?? "") else { return nil }
return try? Database.viewContext.fetch(ZimFile.fetchRequest(fileID: zimFileID)).first
}()

metaData = ZimFileService.shared.getContentMetaData(url: url)
// update view model
if title.isEmpty {
articleTitle = ZimFileService.shared.getContentMetaData(url: url)?.zimTitle ?? ""
articleTitle = metaData?.zimTitle ?? ""
} else {
articleTitle = title
}
Expand Down Expand Up @@ -268,7 +279,7 @@ final class BrowserViewModel: NSObject, ObservableObject,

// MARK: - WKNavigationDelegate

// swiftlint:disable:next function_body_length
// swiftlint:disable:next function_body_length cyclomatic_complexity
func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
Expand Down
2 changes: 1 addition & 1 deletion Views/Buttons/ShareButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct ShareButton: View {
guard let browserURLName = browser.webView.url?.lastPathComponent else {
return nil
}
guard let pdfData = try? await browser.webView.pdf() else {
guard let pdfData = await browser.pdfData() else {
return nil
}
return (pdfData, browserURLName)
Expand Down