Skip to content

Commit 7646e1b

Browse files
author
Thomas Taschauer
committed
improve UX for editing, update core
1 parent 0031f11 commit 7646e1b

6 files changed

Lines changed: 88 additions & 94 deletions

File tree

OpenDocument.core

OpenDocumentReader.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@
930930
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
931931
CLANG_ENABLE_MODULES = YES;
932932
CODE_SIGN_STYLE = Automatic;
933-
CURRENT_PROJECT_VERSION = 13;
933+
CURRENT_PROJECT_VERSION = 14;
934934
DEVELOPMENT_TEAM = 5LS6X97G6J;
935935
HEADER_SEARCH_PATHS = (
936936
"$(inherited)",
@@ -943,7 +943,7 @@
943943
"@executable_path/Frameworks",
944944
);
945945
LIBRARY_SEARCH_PATHS = "$(inherited)";
946-
MARKETING_VERSION = 1.9;
946+
MARKETING_VERSION = 1.10;
947947
PRODUCT_BUNDLE_IDENTIFIER = at.tomtasche.reader;
948948
PRODUCT_NAME = "$(TARGET_NAME)";
949949
SWIFT_OBJC_BRIDGING_HEADER = OpenDocumentReader/BridgingHeader.h;
@@ -960,7 +960,7 @@
960960
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
961961
CLANG_ENABLE_MODULES = YES;
962962
CODE_SIGN_STYLE = Automatic;
963-
CURRENT_PROJECT_VERSION = 13;
963+
CURRENT_PROJECT_VERSION = 14;
964964
DEVELOPMENT_TEAM = 5LS6X97G6J;
965965
HEADER_SEARCH_PATHS = (
966966
"$(inherited)",
@@ -973,7 +973,7 @@
973973
"@executable_path/Frameworks",
974974
);
975975
LIBRARY_SEARCH_PATHS = "$(inherited)";
976-
MARKETING_VERSION = 1.9;
976+
MARKETING_VERSION = 1.10;
977977
PRODUCT_BUNDLE_IDENTIFIER = at.tomtasche.reader;
978978
PRODUCT_NAME = "$(TARGET_NAME)";
979979
SWIFT_OBJC_BRIDGING_HEADER = OpenDocumentReader/BridgingHeader.h;

OpenDocumentReader/CoreWrapper.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
@property NSArray *pageNames;
1717
@property NSNumber *errorCode;
1818

19-
- (void)close;
20-
2119
- (bool)translate:(NSString *)inputPath into:(NSString *)outputPath at:(NSNumber *)page with:(NSString *)password editable:(bool)editable;
2220
- (bool)backTranslate:(NSString *)inputPath into:(NSString *)outputPath;
2321
@end

OpenDocumentReader/CoreWrapper.mm

Lines changed: 62 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -15,89 +15,85 @@
1515
#include "FileMeta.h"
1616

1717
@implementation CoreWrapper {
18-
odr::TranslationHelper *translator;
19-
}
20-
21-
- (void)close {
22-
if (translator != nullptr) {
23-
delete translator;
24-
translator = nullptr;
25-
}
18+
odr::TranslationHelper translator;
19+
bool initialized;
2620
}
2721

2822
- (bool)translate:(NSString *)inputPath into:(NSString *)outputPath at:(NSNumber *)page with:(NSString *)password editable:(bool)editable {
29-
try {
30-
_errorCode = 0;
31-
32-
if (translator == nullptr) {
33-
translator = new odr::TranslationHelper();
34-
35-
bool opened = translator->openOpenDocument([inputPath cStringUsingEncoding:NSUTF8StringEncoding]);
36-
if (!opened) {
37-
_errorCode = @(-1);
38-
return false;
23+
@synchronized(self) {
24+
try {
25+
_errorCode = 0;
26+
27+
if (!initialized) {
28+
bool opened = translator.openOpenDocument([inputPath cStringUsingEncoding:NSUTF8StringEncoding]);
29+
if (!opened) {
30+
_errorCode = @(-1);
31+
return false;
32+
}
33+
34+
const auto meta = translator.getMeta();
35+
36+
bool decrypted = !meta->encrypted;
37+
if (password != nil) {
38+
decrypted = translator.decrypt([password cStringUsingEncoding:NSUTF8StringEncoding]);
39+
}
40+
41+
if (!decrypted) {
42+
_errorCode = @(-2);
43+
return false;
44+
}
45+
46+
NSMutableArray *pageNames = [[NSMutableArray alloc] init];
47+
if (meta->type == odr::FileType::OPENDOCUMENT_TEXT) {
48+
[pageNames addObject:@"Text document"];
49+
} else {
50+
for (auto page = meta->entries.begin(); page != meta->entries.end(); page++) {
51+
auto pageName = page->name;
52+
53+
[pageNames addObject:[NSString stringWithCString:pageName.c_str() encoding:[NSString defaultCStringEncoding]]];
54+
}
55+
}
56+
_pageNames = pageNames;
57+
58+
initialized = true;
3959
}
4060

41-
const auto meta = translator->getMeta();
61+
odr::TranslationConfig config = {};
62+
config.editable = editable;
63+
config.entryOffset = page.intValue;
64+
config.entryCount = 1;
4265

43-
bool decrypted = !meta->encrypted;
44-
if (password != nil) {
45-
decrypted = translator->decrypt([password cStringUsingEncoding:NSUTF8StringEncoding]);
46-
}
47-
48-
if (!decrypted) {
49-
[self close];
50-
51-
_errorCode = @(-2);
66+
bool translated = translator.translate([outputPath cStringUsingEncoding:NSUTF8StringEncoding], config);
67+
if (!translated) {
68+
_errorCode = @(-4);
5269
return false;
5370
}
54-
55-
NSMutableArray *pageNames = [[NSMutableArray alloc] init];
56-
if (meta->type == odr::FileType::OPENDOCUMENT_TEXT) {
57-
[pageNames addObject:@"Text document"];
58-
} else {
59-
for (auto page = meta->entries.begin(); page != meta->entries.end(); page++) {
60-
auto pageName = page->name;
61-
62-
[pageNames addObject:[NSString stringWithCString:pageName.c_str() encoding:[NSString defaultCStringEncoding]]];
63-
}
64-
}
65-
_pageNames = pageNames;
66-
}
67-
68-
odr::TranslationConfig config = {};
69-
config.editable = editable;
70-
config.entryOffset = page.intValue;
71-
config.entryCount = 1;
72-
73-
bool translated = translator->translate([outputPath cStringUsingEncoding:NSUTF8StringEncoding], config);
74-
if (!translated) {
75-
_errorCode = @(-4);
71+
} catch (...) {
72+
_errorCode = @(-3);
7673
return false;
7774
}
78-
} catch (...) {
79-
_errorCode = @(-3);
80-
return false;
75+
76+
return true;
8177
}
82-
83-
return true;
8478
}
8579

8680
- (bool)backTranslate:(NSString *)inputPath into:(NSString *)outputPath {
87-
try {
88-
_errorCode = 0;
89-
90-
bool translated = translator->backTranslate([inputPath cStringUsingEncoding:NSUTF8StringEncoding], [outputPath cStringUsingEncoding:NSUTF8StringEncoding]);
91-
if (!translated) {
92-
_errorCode = @(-4);
81+
@synchronized(self) {
82+
try {
83+
_errorCode = 0;
84+
85+
bool translated = translator.backTranslate([inputPath cStringUsingEncoding:NSUTF8StringEncoding], [outputPath cStringUsingEncoding:NSUTF8StringEncoding]);
86+
if (!translated) {
87+
_errorCode = @(-4);
88+
return false;
89+
}
90+
} catch (...) {
91+
_errorCode = @(-3);
9392
return false;
9493
}
95-
} catch (...) {
96-
_errorCode = @(-3);
97-
return false;
94+
95+
return true;
9896
}
99-
100-
return true;
10197
}
10298

10399
@end

OpenDocumentReader/Document.swift

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ protocol DocumentDelegate: class {
1818
func documentPagesChanged(_ doc: Document)
1919
}
2020

21+
enum DocumentError: Error {
22+
case getHtml
23+
case backTranslate
24+
}
25+
26+
2127
class Document: UIDocument {
2228

2329
public var result: URL?
@@ -72,7 +78,6 @@ class Document: UIDocument {
7278
tempPath.appendPathComponent("temp.html")
7379

7480
coreWrapper.translate(fileURL.path, into: tempPath.path, at: page as NSNumber, with: password, editable: edit)
75-
7681
let errorCode = coreWrapper.errorCode != nil ? coreWrapper.errorCode.intValue : 0
7782

7883
if (errorCode == -2) {
@@ -139,23 +144,23 @@ class Document: UIDocument {
139144
override func writeContents(_ contents: Any, to url: URL, for saveOperation: UIDocument.SaveOperation, originalContentsURL: URL?) throws {
140145
let tempPath = contents as! URL
141146

142-
// TODO: set timeout
143-
saveGroup.wait()
147+
let waitResult = saveGroup.wait(timeout: .now() + 30)
148+
if (waitResult != DispatchTimeoutResult.success) {
149+
throw DocumentError.getHtml
150+
}
144151

145-
coreWrapper.backTranslate(tempPath.path, into: url.path)
152+
// running on main-thread to make sure CoreWrapper is always called from the same thread
153+
// TODO: run on background thread instead?
154+
DispatchQueue.main.sync {
155+
coreWrapper.backTranslate(tempPath.path, into: url.path)
156+
}
146157

147158
let errorCode = coreWrapper.errorCode != nil ? coreWrapper.errorCode.intValue : 0
148-
// TODO: how to propagate error here?
149-
print(errorCode)
150-
}
151-
152-
override func close(completionHandler: ((Bool) -> Void)? = nil) {
153-
super.close(completionHandler: { (success) in
154-
// do AFTER writeContents has completed
155-
self.coreWrapper.close()
159+
if (errorCode < 0) {
160+
print(errorCode)
156161

157-
// TODO: call completionHandler
158-
})
162+
throw DocumentError.backTranslate
163+
}
159164
}
160165
}
161166

OpenDocumentReader/DocumentViewController.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,9 @@ class DocumentViewController: UIViewController, DocumentDelegate {
7777
}
7878

7979
doc.close { (success) in
80-
guard success else {
80+
if (!success) {
8181
Crashlytics.sharedInstance().throwException()
82-
83-
return
8482
}
85-
86-
// TODO: handle save error here?
8783
}
8884
}
8985

@@ -129,7 +125,7 @@ class DocumentViewController: UIViewController, DocumentDelegate {
129125
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
130126

131127
if (document?.isOdf ?? false && !(document?.edit ?? false)) {
132-
alert.addAction(UIAlertAction(title: "Edit", style: .default, handler: { (_) in
128+
alert.addAction(UIAlertAction(title: "Edit (EXPERIMENTAL)", style: .default, handler: { (_) in
133129
self.editDocument()
134130
}))
135131
}
@@ -230,7 +226,6 @@ class DocumentViewController: UIViewController, DocumentDelegate {
230226
Analytics.logEvent(
231227
"load_error",
232228
parameters: [
233-
// TODO: check name on android
234229
"code": errorCode,
235230
AnalyticsParameterItemName: doc.shortenedDocumentUrl,
236231
AnalyticsParameterContentType: fileType

0 commit comments

Comments
 (0)