-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #729 from kiwix/180-export-and-share-article-featu…
…re-request 180 export and share article feature request
- Loading branch information
Showing
9 changed files
with
179 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// This file is part of Kiwix for iOS & macOS. | ||
// | ||
// Kiwix is free software; you can redistribute it and/or modify it | ||
// under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation; either version 3 of the License, or | ||
// any later version. | ||
// | ||
// Kiwix is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Kiwix; If not, see https://www.gnu.org/licenses/. | ||
|
||
import SwiftUI | ||
|
||
#if os(iOS) | ||
struct ActivityViewController: UIViewControllerRepresentable { | ||
|
||
@Environment(\.dismiss) var dismissAction | ||
var activityItems: [Any] | ||
|
||
func makeUIViewController( | ||
context: UIViewControllerRepresentableContext<ActivityViewController> | ||
) -> UIActivityViewController { | ||
let controller = UIActivityViewController(activityItems: activityItems, applicationActivities: nil) | ||
controller.modalPresentationStyle = .pageSheet | ||
controller.completionWithItemsHandler = { (_, _, _, _) in | ||
self.dismissAction() | ||
} | ||
return controller | ||
} | ||
|
||
func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) { | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// This file is part of Kiwix for iOS & macOS. | ||
// | ||
// Kiwix is free software; you can redistribute it and/or modify it | ||
// under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation; either version 3 of the License, or | ||
// any later version. | ||
// | ||
// Kiwix is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Kiwix; If not, see https://www.gnu.org/licenses/. | ||
|
||
import SwiftUI | ||
|
||
struct ShareButton: View { | ||
|
||
@EnvironmentObject private var browser: BrowserViewModel | ||
|
||
private func dataAndName() async -> (Data, String)? { | ||
guard let browserURLName = browser.webView.url?.lastPathComponent else { | ||
return nil | ||
} | ||
guard let pdfData = try? await browser.webView.pdf() else { | ||
return nil | ||
} | ||
return (pdfData, browserURLName) | ||
} | ||
|
||
private func tempFileURL() async -> URL? { | ||
guard let (pdfData, browserURLName) = await dataAndName() else { return nil } | ||
return PDFHandler.tempFileFrom(pdfData: pdfData, fileName: browserURLName) | ||
} | ||
|
||
var body: some View { | ||
Button { | ||
Task { | ||
#if os(iOS) | ||
guard let (pdfData, browserURLName) = await dataAndName() else { return } | ||
NotificationCenter.sharePDF(pdfData, fileName: browserURLName) | ||
#else | ||
guard let url = await tempFileURL() else { return } | ||
NSSharingServicePicker(items: [url]).show(relativeTo: .null, of: browser.webView, preferredEdge: .minY) | ||
#endif | ||
} | ||
} label: { | ||
Label { | ||
Text("Share".localized) | ||
} icon: { | ||
Image(systemName: "square.and.arrow.up") | ||
} | ||
}.disabled(browser.zimFileName.isEmpty) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// This file is part of Kiwix for iOS & macOS. | ||
// | ||
// Kiwix is free software; you can redistribute it and/or modify it | ||
// under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation; either version 3 of the License, or | ||
// any later version. | ||
// | ||
// Kiwix is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Kiwix; If not, see https://www.gnu.org/licenses/. | ||
|
||
import SwiftUI | ||
|
||
enum PDFHandler { | ||
static func tempFileFrom(pdfData: Data, fileName: String) -> URL? { | ||
guard let tempFileName = fileName.split(separator: ".").first?.appending(".pdf") else { | ||
return nil | ||
} | ||
let tempFileURL = URL(temporaryFileWithName: tempFileName) | ||
guard (try? pdfData.write(to: tempFileURL)) != nil else { | ||
return nil | ||
} | ||
return tempFileURL | ||
} | ||
} | ||
|
||
#if os(iOS) | ||
/// On receiving pdf content and a file name, it gives the ability to share it | ||
struct SharePDFHandler: ViewModifier { | ||
|
||
private let sharePDF = NotificationCenter.default.publisher(for: .sharePDF) | ||
@State private var temporaryURL: URL? | ||
|
||
func body(content: Content) -> some View { | ||
content.onReceive(sharePDF) { notification in | ||
|
||
guard let userInfo = notification.userInfo, | ||
let pdfData = userInfo["data"] as? Data, | ||
let fileName = userInfo["fileName"] as? String, | ||
let tempURL = PDFHandler.tempFileFrom(pdfData: pdfData, fileName: fileName) else { | ||
temporaryURL = nil | ||
return | ||
} | ||
temporaryURL = tempURL | ||
} | ||
.sheet( | ||
isPresented: Binding<Bool>.constant($temporaryURL.wrappedValue != nil), | ||
onDismiss: { | ||
if let temporaryURL { | ||
try? FileManager.default.removeItem(at: temporaryURL) | ||
} | ||
temporaryURL = nil | ||
}, content: { | ||
ActivityViewController(activityItems: [temporaryURL].compactMap { $0 }) | ||
} | ||
) | ||
} | ||
} | ||
#endif |