Skip to content

Commit

Permalink
Merge pull request #14 from OutSystems/fix/RMET-911/open-document-cal…
Browse files Browse the repository at this point in the history
…lback

RMET-912 :: fix: iOS success callback
  • Loading branch information
CarlsCorrea authored Aug 3, 2021
2 parents 65fe175 + a919c2e commit 7db1cce
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## 2021-07-30
- Fix: Implemented a completion handler to indentify when the preview document is dismissed. [RMET-740](https://outsystemsrd.atlassian.net/browse/RMET-912)
## 2021-07-13
- Migrating package upload to newer Saucelabs API [RMET-761](https://outsystemsrd.atlassian.net/browse/RMET-761)
## 2021-07-06
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.outsystems.plugins.fileviewer",
"version": "1.0.1",
"version": "1.0.2",
"description": "OutSystems Cordova Plugin to provide the functionality of a file viewer to mobile applications.",
"keywords": [
"ecosystem:cordova",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<plugin id="com.outsystems.plugins.fileviewer" version="1.0.1" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<plugin id="com.outsystems.plugins.fileviewer" version="1.0.2" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name>OSFileViewer</name>
<description>OutSystems Cordova Plugin to provide the functionality of a file viewer to mobile applications.</description>
<author>OutSystems Inc</author>
Expand Down
22 changes: 20 additions & 2 deletions src/ios/FileViewerOpenDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ class FileViewerOpenDocument: NSObject {
})
}

func openDocumentFromLocalPath(filePath:URL) throws {
self.documentInteractionController = UIDocumentInteractionController.init()
func openDocumentFromLocalPath(filePath:URL, completion: @escaping () -> Void) throws {
self.documentInteractionController = DocumentInteractor() {
completion()
}
self.documentInteractionController?.delegate = self
self.documentInteractionController?.url = filePath.standardized
self.documentInteractionController?.presentPreview(animated: true)
Expand All @@ -50,10 +52,26 @@ extension FileViewerOpenDocument: UIDocumentInteractionControllerDelegate {
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self.viewController!
}

}

extension URL {
var uti: String {
return (try? self.resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier ?? "public.data"
}
}


class DocumentInteractor: UIDocumentInteractionController {

var previewFinished: (() -> Void)

init(presentationFinished: @escaping () -> Void) {
self.previewFinished = presentationFinished
}

deinit {
self.previewFinished()
}

}
29 changes: 19 additions & 10 deletions src/ios/FileViewerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,25 @@ class FileViewerPlugin {
var fileViewerOpenDocument: FileViewerOpenDocument?
var rootViewController:UIViewController?

func openDocumentFromLocalPath(filePath:String) throws {

func openDocumentFromLocalPath(filePath:String, completion: @escaping (() -> Void )) throws {
guard !filePath.isEmpty else { throw FileViewerErrors.invalidEmptyURL }

if let file = URL.init(string: filePath) {
guard FileManager.default.fileExists(atPath: file.path) else { throw FileViewerErrors.fileDoesNotExist }

if let viewController = rootViewController {
let fileViewerOpenDocument = FileViewerOpenDocument(viewController: viewController)
try fileViewerOpenDocument.openDocumentFromLocalPath(filePath: file)
let urlString = filePath.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

if let url = urlString {
if let file = URL.init(string: url) {
guard FileManager.default.fileExists(atPath: file.path) else { throw FileViewerErrors.fileDoesNotExist }

if let viewController = rootViewController {
let fileViewerOpenDocument = FileViewerOpenDocument(viewController: viewController)
try fileViewerOpenDocument.openDocumentFromLocalPath(filePath: file) {
completion()
}
}
}
}

}

func openDocumentFromUrl(url:String, completion: @escaping (_ inner: ErrorCompletionHandler) -> Void) {
Expand Down Expand Up @@ -99,14 +106,16 @@ class FileViewerPlugin {
}
}

func openDocumentFromResources(fileName: String, ext: String) throws {
func openDocumentFromResources(fileName: String, ext: String, completion: @escaping (() -> Void )) throws {
guard !fileName.isEmpty && !ext.isEmpty else { throw FileViewerErrors.emptyFileName }
guard let filePath = Bundle.main.path(forResource: fileName, ofType: ext, inDirectory: "www/resources") else { throw FileViewerErrors.fileDoesNotExist }
let url:URL = URL.init(fileURLWithPath: filePath)
guard FileManager.default.fileExists(atPath: url.path) else { throw FileViewerErrors.fileDoesNotExist }
if let viewController = rootViewController {
let fileViewerOpenDocument = FileViewerOpenDocument(viewController: viewController)
try fileViewerOpenDocument.openDocumentFromLocalPath(filePath: url)
try fileViewerOpenDocument.openDocumentFromLocalPath(filePath: url) {
completion()
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/ios/FileViewerPreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class FileViewerPreview {

lazy var previewItem = NSURL()
weak var viewController: UIViewController?
var presentationFinished: (() -> Void)?

init(viewController: UIViewController) {
self.viewController = viewController
Expand Down
8 changes: 6 additions & 2 deletions src/ios/OSFileViewer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,23 @@ class OSFileViewer : CDVPlugin {
callbackId = command.callbackId
let filePath = command.arguments[0] as? String ?? ""
do {
try plugin.openDocumentFromLocalPath(filePath: filePath)
try plugin.openDocumentFromLocalPath(filePath: filePath, completion: sendResultOK)
} catch {
sendResult(result: "", error: error.localizedDescription)
}
}

func sendResultOK() {
sendResult(result: "", error: "")
}

@objc(openDocumentFromResources:)
func openDocumentFromResources(command: CDVInvokedUrlCommand) {
callbackId = command.callbackId
let fileName = command.arguments[0] as? String ?? ""
let fileExtension = command.arguments[1] as? String ?? ""
do {
try plugin.openDocumentFromResources(fileName: fileName, ext: fileExtension)
try plugin.openDocumentFromResources(fileName: fileName, ext: fileExtension, completion: sendResultOK)
} catch {
sendResult(result: "", error: error.localizedDescription)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class OSFileViewerOpenDocumentTests: XCTestCase {
func test_When_emptyUrlPassed_Expect_invalidEmptyURL() throws {
let fileViewerPlugin = FileViewerPlugin()
do {
try fileViewerPlugin.openDocumentFromLocalPath(filePath: "")
try fileViewerPlugin.openDocumentFromLocalPath(filePath: "") {}
XCTFail("Did not throw error")
} catch {
XCTAssertEqual(error as? FileViewerErrors, .invalidEmptyURL)
Expand All @@ -23,7 +23,7 @@ class OSFileViewerOpenDocumentTests: XCTestCase {
func test_When_invalidPathPassed_Expect_fileDoesNotExist() throws {
let fileViewerPlugin = FileViewerPlugin()
do {
try fileViewerPlugin.openDocumentFromLocalPath(filePath: "aaaaaaaaaaaaaa")
try fileViewerPlugin.openDocumentFromLocalPath(filePath: "aaaaaaaaaaaaaa") {}
XCTFail("Did not throw error")
} catch {
XCTAssertEqual(error as? FileViewerErrors, .fileDoesNotExist)
Expand Down

0 comments on commit 7db1cce

Please sign in to comment.