Skip to content

Commit 734cb44

Browse files
author
Thomas Taschauer
committed
use latest core, including "robust editing"
1 parent 57190a7 commit 734cb44

5 files changed

Lines changed: 35 additions & 46 deletions

File tree

OpenDocument.core

OpenDocumentReader/CoreWrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
@property NSNumber *errorCode;
1818

1919
- (bool)translate:(NSString *)inputPath into:(NSString *)outputPath at:(NSNumber *)page with:(NSString *)password editable:(bool)editable;
20-
- (bool)backTranslate:(NSString *)inputPath into:(NSString *)outputPath;
20+
- (bool)backTranslate:(NSString *)diff into:(NSString *)outputPath;
2121
@end
2222

2323
#endif /* CoreWrapper_h */

OpenDocumentReader/CoreWrapper.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ - (bool)translate:(NSString *)inputPath into:(NSString *)outputPath at:(NSNumber
7878
}
7979
}
8080

81-
- (bool)backTranslate:(NSString *)inputPath into:(NSString *)outputPath {
81+
- (bool)backTranslate:(NSString *)diff into:(NSString *)outputPath {
8282
@synchronized(self) {
8383
try {
8484
_errorCode = 0;
8585

86-
bool translated = translator.backTranslate([inputPath cStringUsingEncoding:NSUTF8StringEncoding], [outputPath cStringUsingEncoding:NSUTF8StringEncoding]);
86+
bool translated = translator.backTranslate([diff cStringUsingEncoding:NSUTF8StringEncoding], [outputPath cStringUsingEncoding:NSUTF8StringEncoding]);
8787
if (!translated) {
8888
_errorCode = @(-4);
8989
return false;

OpenDocumentReader/Document.swift

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class Document: UIDocument {
4848
public var edit = false {
4949
didSet {
5050
parse()
51-
updateChangeCount(.done)
5251
}
5352
}
5453

@@ -117,32 +116,25 @@ class Document: UIDocument {
117116
delegate?.documentLoadingCompleted(self)
118117
}
119118

120-
override func contents(forType typeName: String) throws -> Any {
121-
saveGroup.enter()
122-
123-
var tempPath = URL(fileURLWithPath: NSTemporaryDirectory())
124-
tempPath.appendPathComponent("tempSave.html")
119+
override func writeContents(_ contents: Any, to url: URL, for saveOperation: UIDocument.SaveOperation, originalContentsURL: URL?) throws {
120+
var diff = ""
125121

126-
webview?.evaluateJavaScript("document.documentElement.outerHTML", completionHandler: { (value: Any!, error: Error!) -> Void in
127-
if error != nil {
128-
Crashlytics.sharedInstance().recordError(error)
129-
Crashlytics.sharedInstance().throwException()
122+
DispatchQueue.main.sync {
123+
self.saveGroup.enter()
130124

131-
return
132-
}
133-
134-
let content = value as? String
135-
let data = content?.data(using: .utf8)
136-
try? data?.write(to: tempPath, options: .atomic)
137-
138-
self.saveGroup.leave()
139-
})
125+
webview?.evaluateJavaScript("generateDiff()", completionHandler: { (value: Any!, error: Error!) -> Void in
126+
if error != nil {
127+
Crashlytics.sharedInstance().recordError(error)
128+
Crashlytics.sharedInstance().throwException()
140129

141-
return tempPath as Any
142-
}
143-
144-
override func writeContents(_ contents: Any, to url: URL, for saveOperation: UIDocument.SaveOperation, originalContentsURL: URL?) throws {
145-
let tempPath = contents as! URL
130+
return
131+
}
132+
133+
diff = (value as? String)!
134+
135+
self.saveGroup.leave()
136+
})
137+
}
146138

147139
let waitResult = saveGroup.wait(timeout: .now() + 30)
148140
if (waitResult != DispatchTimeoutResult.success) {
@@ -152,7 +144,7 @@ class Document: UIDocument {
152144
// running on main-thread to make sure CoreWrapper is always called from the same thread
153145
// TODO: run on background thread instead?
154146
DispatchQueue.main.sync {
155-
coreWrapper.backTranslate(tempPath.path, into: url.path)
147+
coreWrapper.backTranslate(diff, into: url.path)
156148
}
157149

158150
let errorCode = coreWrapper.errorCode != nil ? coreWrapper.errorCode.intValue : 0

OpenDocumentReader/DocumentViewController.swift

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class DocumentViewController: UIViewController, DocumentDelegate {
114114
return
115115
}
116116

117-
if doc.hasUnsavedChanges {
117+
if doc.edit {
118118
let alert = UIAlertController(title: "You have unsaved changes", message: "Save them now?", preferredStyle: .alert)
119119
alert.addAction(UIAlertAction(title: "No", style: .destructive, handler: { (_) in
120120
Analytics.logEvent("alert_unsaved_changes_no", parameters: nil)
@@ -125,7 +125,8 @@ class DocumentViewController: UIViewController, DocumentDelegate {
125125
alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (_) in
126126
Analytics.logEvent("alert_unsaved_changes_yes", parameters: nil)
127127

128-
self.closeCurrentDocument() //doc.close calls autosave
128+
self.saveContent()
129+
self.closeCurrentDocument()
129130
}))
130131

131132
self.present(alert, animated: true, completion: nil)
@@ -143,15 +144,8 @@ class DocumentViewController: UIViewController, DocumentDelegate {
143144
return
144145
}
145146

146-
doc.close { (success) in
147-
if (!success) {
148-
self.showToast(controller: self, message: "Error while saving", seconds: 1.5, color: .red) {
149-
self.dismiss(animated: true, completion: nil)
150-
}
151-
} else {
152-
self.dismiss(animated: true, completion: nil)
153-
}
154-
}
147+
doc.close()
148+
self.dismiss(animated: true, completion: nil)
155149
}
156150

157151
@IBAction func showMenu(_ sender: Any) {
@@ -163,7 +157,7 @@ class DocumentViewController: UIViewController, DocumentDelegate {
163157
}))
164158
}
165159

166-
if document?.hasUnsavedChanges ?? false {
160+
if document?.edit ?? false {
167161
alert.addAction(UIAlertAction(title: "Save", style: .default, handler: { (_) in
168162
self.saveContent()
169163
}))
@@ -210,14 +204,17 @@ class DocumentViewController: UIViewController, DocumentDelegate {
210204
}
211205

212206
doc.save(to: doc.fileURL, for: .forOverwriting) { success in
207+
let message: String
208+
let color: UIColor
213209
if success {
214-
self.showToast(controller: self, message: "Successfully saved", seconds: 1.5, color: .green)
215-
216-
// makes sure UI stays in edit-mode
217-
self.document?.updateChangeCount(.done)
210+
message = "Successfully saved"
211+
color = .green
218212
} else {
219-
self.showToast(controller: self, message: "Error while saving", seconds: 1.5, color: .red)
213+
message = "Error while saving"
214+
color = .red
220215
}
216+
217+
self.showToast(controller: self, message: message, seconds: 1.5, color: color)
221218
}
222219
}
223220

0 commit comments

Comments
 (0)