|
1 | | -// |
2 | | -// Document.swift |
3 | | -// OpenDocument Reader |
4 | | -// |
5 | | -// Created by Thomas Taschauer on 06.02.19. |
6 | | -// Copyright © 2019 Thomas Taschauer. All rights reserved. |
7 | | -// |
| 1 | +/* |
| 2 | +See LICENSE folder for this sample’s licensing information. |
| 3 | + |
| 4 | +Abstract: |
| 5 | +A document that manages UTF8 text files. |
| 6 | +*/ |
8 | 7 |
|
9 | 8 | import UIKit |
10 | 9 |
|
| 10 | +protocol DocumentDelegate: class { |
| 11 | + func documentUpdateContent(_ doc: Document) |
| 12 | + func documentEncrypted(_ doc: Document) |
| 13 | + func documentLoadingError(_ doc: Document) |
| 14 | + func documentLoadingStarted(_ doc: Document) |
| 15 | + func documentLoadingCompleted(_ doc: Document) |
| 16 | + func documentPagesChanged(_ doc: Document) |
| 17 | +} |
| 18 | + |
11 | 19 | class Document: UIDocument { |
12 | 20 |
|
13 | | - override func contents(forType typeName: String) throws -> Any { |
14 | | - // Encode your document with an instance of NSData or NSFileWrapper |
15 | | - return Data() |
| 21 | + public var result: URL? |
| 22 | + public var pageNames: [String]? |
| 23 | + |
| 24 | + public weak var delegate: DocumentDelegate? |
| 25 | + public var loadProgress = Progress(totalUnitCount: 5) |
| 26 | + |
| 27 | + private var page: Int = 0 |
| 28 | + private var password: String? |
| 29 | + |
| 30 | + private var wasPageCountAnnounced = false |
| 31 | + |
| 32 | + override init(fileURL url: URL) { |
| 33 | + super.init(fileURL: url) |
16 | 34 | } |
17 | 35 |
|
18 | 36 | override func load(fromContents contents: Any, ofType typeName: String?) throws { |
19 | | - // Load your document from contents |
| 37 | + parse() |
20 | 38 | } |
21 | | -} |
| 39 | + |
| 40 | + func parse() { |
| 41 | + delegate?.documentLoadingStarted(self) |
| 42 | + |
| 43 | + loadProgress.completedUnitCount = 2 |
| 44 | + loadProgress.resume() |
| 45 | + |
| 46 | + result = nil |
| 47 | + delegate?.documentUpdateContent(self) |
22 | 48 |
|
| 49 | + var tempPath = URL(fileURLWithPath: NSTemporaryDirectory()) |
| 50 | + tempPath.appendPathComponent("temp.html") |
| 51 | + |
| 52 | + let core = CoreWrapper() |
| 53 | + core.translate(fileURL.path, into: tempPath.path, at: NSNumber(value: page), with: password) |
| 54 | + |
| 55 | + let errorCode = core.errorCode != nil ? core.errorCode.intValue : 0 |
| 56 | + |
| 57 | + if (errorCode == -2) { |
| 58 | + // delay because ViewController might not be visible yet |
| 59 | + DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: { |
| 60 | + self.delegate?.documentEncrypted(self) |
| 61 | + }) |
| 62 | + |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + if (errorCode < 0) { |
| 67 | + delegate?.documentLoadingError(self) |
| 68 | + |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + loadProgress.completedUnitCount = loadProgress.totalUnitCount |
| 73 | + |
| 74 | + var pageNames = [String]() |
| 75 | + for object in core.pageNames { |
| 76 | + if let pageName = object as? String { |
| 77 | + pageNames.append(pageName) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + self.pageNames = pageNames |
| 82 | + result = tempPath |
| 83 | + |
| 84 | + delegate?.documentUpdateContent(self) |
| 85 | + |
| 86 | + if (!wasPageCountAnnounced) { |
| 87 | + delegate?.documentPagesChanged(self) |
| 88 | + |
| 89 | + wasPageCountAnnounced = true |
| 90 | + } |
| 91 | + |
| 92 | + delegate?.documentLoadingCompleted(self) |
| 93 | + } |
| 94 | + |
| 95 | + func setPassword(password: String) { |
| 96 | + self.password = password |
| 97 | + |
| 98 | + parse() |
| 99 | + } |
| 100 | + |
| 101 | + func setPage(page: Int) { |
| 102 | + self.page = page |
| 103 | + |
| 104 | + parse() |
| 105 | + } |
| 106 | +} |
0 commit comments