@@ -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
0 commit comments