@@ -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,50 @@ 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 them now? " , preferredStyle: . alert)
119+ alert. addAction ( UIAlertAction ( title: " No " , style: . destructive, handler: { ( _) in
120+ Analytics . logEvent ( " alert_unsaved_changes_no " , parameters: nil )
121+
122+ self . discardChanges ( )
123+ self . closeCurrentDocument ( )
124+ } ) )
125+ alert. addAction ( UIAlertAction ( title: " Yes " , style: . default, handler: { ( _) in
126+ Analytics . logEvent ( " alert_unsaved_changes_yes " , parameters: nil )
127+
128+ self . closeCurrentDocument ( ) //doc.close calls autosave
129+ } ) )
130+
131+ self . present ( alert, animated: true , completion: nil )
132+
133+ Analytics . logEvent ( " show_alert_unsaved_changes " , parameters: nil )
134+ } else {
135+ closeCurrentDocument ( )
136+ }
137+ }
138+
139+ func closeCurrentDocument( ) {
140+ guard let doc = document else {
141+ Crashlytics . sharedInstance ( ) . throwException ( )
142+
143+ return
144+ }
145+
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+ }
122155 }
123156
124157 @IBAction func showMenu( _ sender: Any ) {
@@ -129,6 +162,16 @@ class DocumentViewController: UIViewController, DocumentDelegate {
129162 self . editDocument ( )
130163 } ) )
131164 }
165+
166+ if document? . hasUnsavedChanges ?? false {
167+ alert. addAction ( UIAlertAction ( title: " Save " , style: . default, handler: { ( _) in
168+ self . saveContent ( )
169+ } ) )
170+
171+ alert. addAction ( UIAlertAction ( title: " Discard changes " , style: . default, handler: { ( _) in
172+ self . discardChanges ( )
173+ } ) )
174+ }
132175
133176 alert. addAction ( UIAlertAction ( title: " Fullscreen " , style: . default, handler: { ( _) in
134177 self . toggleFullscreen ( )
@@ -145,6 +188,50 @@ class DocumentViewController: UIViewController, DocumentDelegate {
145188 self . present ( alert, animated: true , completion: nil )
146189 }
147190
191+ func discardChanges( ) {
192+ Analytics . logEvent ( " menu_discard_changes " , parameters: nil )
193+
194+ guard let doc = document else {
195+ Crashlytics . sharedInstance ( ) . throwException ( )
196+
197+ return
198+ }
199+
200+ doc. revert ( toContentsOf: doc. fileURL, completionHandler: nil )
201+ }
202+
203+ func saveContent( ) {
204+ Analytics . logEvent ( " menu_save_content " , parameters: nil )
205+
206+ guard let doc = document else {
207+ Crashlytics . sharedInstance ( ) . throwException ( )
208+
209+ return
210+ }
211+
212+ doc. save ( to: doc. fileURL, for: . forOverwriting) { success in
213+ if success {
214+ self . showToast ( controller: self , message: " Successfully saved " , seconds: 1.5 , color: . green)
215+ } else {
216+ self . showToast ( controller: self , message: " Error while saving " , seconds: 1.5 , color: . red)
217+ }
218+ }
219+ }
220+
221+ func showToast( controller: UIViewController , message : String , seconds: Double , color: UIColor ? = . gray, completion: ( ( ) -> Void ) ? = nil ) {
222+ let alert = UIAlertController ( title: nil , message: message, preferredStyle: . actionSheet)
223+ alert. view. backgroundColor = color
224+ alert. view. layer. cornerRadius = 15
225+
226+ controller. present ( alert, animated: true )
227+
228+ DispatchQueue . main. asyncAfter ( deadline: DispatchTime . now ( ) + seconds) {
229+ alert. dismiss ( animated: true )
230+
231+ completion ? ( )
232+ }
233+ }
234+
148235 func editDocument( ) {
149236 Analytics . logEvent ( " menu_edit " , parameters: nil )
150237
0 commit comments