Skip to content

Commit

Permalink
Merge pull request #820 from kiwix/814-export-of-a-pdf-fails
Browse files Browse the repository at this point in the history
Fix share/export content depending on it's type
  • Loading branch information
kelson42 committed Jun 19, 2024
2 parents 6e90d9e + ea2239d commit 1e3f9ce
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
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
17 changes: 15 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,27 @@ final class BrowserViewModel: NSObject, ObservableObject,
}
}

/// Get the webpage in a binary format
/// - Returns: PDF of the current page (if text type) or binary data of the content
func pdfData() async -> Data? {
if metaData?.isTextType == true {
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 +281,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

0 comments on commit 1e3f9ce

Please sign in to comment.