Skip to content

Commit e2fbe73

Browse files
committed
Save,discard added to menu. Alerts if file was saved or not.
1 parent e150ac6 commit e2fbe73

2 files changed

Lines changed: 93 additions & 12 deletions

File tree

OpenDocumentReader/DocumentViewController.swift

Lines changed: 90 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,8 @@ class DocumentViewController: UIViewController, DocumentDelegate {
6969

7070
override func viewDidDisappear(_ animated: Bool) {
7171
super.viewDidDisappear(animated)
72-
73-
guard let doc = document else {
74-
Crashlytics.sharedInstance().throwException()
7572

76-
return
77-
}
78-
79-
doc.close { (success) in
80-
if (!success) {
81-
Crashlytics.sharedInstance().throwException()
82-
}
83-
}
73+
closeCurrentDocument()
8474
}
8575

8676
@objc func segmentSelected(sender:ScrollableSegmentedControl) {
@@ -118,7 +108,46 @@ class DocumentViewController: UIViewController, DocumentDelegate {
118108
}
119109

120110
@IBAction func returnToDocuments(_ sender: Any) {
121-
dismiss(animated: true, completion: nil)
111+
guard let doc = document else {
112+
Crashlytics.sharedInstance().throwException()
113+
114+
return
115+
}
116+
117+
if doc.hasUnsavedChanges {
118+
let alert = UIAlertController(title: "You have unsaved changes", message: "Save it?", preferredStyle: .alert)
119+
alert.addAction(UIAlertAction(title: "No", style: .destructive, handler: { (_) in
120+
self.discardChanges()
121+
self.closeCurrentDocument()
122+
}))
123+
alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (_) in
124+
self.closeCurrentDocument() //doc.close calls autosave
125+
}))
126+
127+
self.present(alert, animated: true, completion: nil)
128+
} else {
129+
closeCurrentDocument()
130+
}
131+
132+
}
133+
134+
func closeCurrentDocument() {
135+
guard let doc = document else {
136+
Crashlytics.sharedInstance().throwException()
137+
138+
return
139+
}
140+
141+
doc.close { (success) in
142+
if (!success) {
143+
self.showToast(controller: self, message: "Error while saving", seconds: 1, color: .red) {
144+
self.dismiss(animated: true, completion: nil)
145+
}
146+
} else {
147+
self.dismiss(animated: true, completion: nil)
148+
}
149+
150+
}
122151
}
123152

124153
@IBAction func showMenu(_ sender: Any) {
@@ -129,6 +158,16 @@ class DocumentViewController: UIViewController, DocumentDelegate {
129158
self.editDocument()
130159
}))
131160
}
161+
162+
if document?.hasUnsavedChanges ?? false {
163+
alert.addAction(UIAlertAction(title: "Save", style: .default, handler: { (_) in
164+
self.saveContent()
165+
}))
166+
167+
alert.addAction(UIAlertAction(title: "Discard changes", style: .default, handler: { (_) in
168+
self.discardChanges()
169+
}))
170+
}
132171

133172
alert.addAction(UIAlertAction(title: "Fullscreen", style: .default, handler: { (_) in
134173
self.toggleFullscreen()
@@ -145,6 +184,45 @@ class DocumentViewController: UIViewController, DocumentDelegate {
145184
self.present(alert, animated: true, completion: nil)
146185
}
147186

187+
func discardChanges() {
188+
guard let doc = document else {
189+
Crashlytics.sharedInstance().throwException()
190+
191+
return
192+
}
193+
194+
doc.revert(toContentsOf: doc.fileURL, completionHandler: nil)
195+
}
196+
197+
func saveContent() {
198+
if let URL = document?.fileURL {
199+
document?.save(to: URL, for: .forOverwriting) { success in
200+
if success {
201+
self.showToast(controller: self, message: "Successfully saved", seconds: 1, color: .green)
202+
} else {
203+
self.showToast(controller: self, message: "Error while saving", seconds: 1, color: .red)
204+
}
205+
206+
}
207+
208+
}
209+
}
210+
211+
func showToast(controller: UIViewController, message : String, seconds: Double, color: UIColor? = .gray, completion: (() -> Void)? = nil) {
212+
let alert = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)
213+
alert.view.backgroundColor = color
214+
alert.view.layer.cornerRadius = 15
215+
216+
controller.present(alert, animated: true)
217+
218+
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + seconds) {
219+
alert.dismiss(animated: true)
220+
221+
completion?()
222+
}
223+
224+
}
225+
148226
func editDocument() {
149227
Analytics.logEvent("menu_edit", parameters: nil)
150228

OpenDocumentReader/Main.storyboard

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,7 @@
107107
<point key="canvasLocation" x="1028.985507246377" y="652.90178571428567"/>
108108
</scene>
109109
</scenes>
110+
<resources>
111+
<image name="ellipsis" catalog="system" width="64" height="18"/>
112+
</resources>
110113
</document>

0 commit comments

Comments
 (0)