@@ -15,9 +15,11 @@ import android.webkit.WebView
1515import android.webkit.WebViewClient
1616import android.widget.ImageView
1717import android.widget.TextView
18+ import com.simplemobiletools.commons.dialogs.ConfirmationAdvancedDialog
1819import com.simplemobiletools.commons.extensions.*
1920import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
2021import com.simplemobiletools.commons.helpers.REAL_FILE_PATH
22+ import com.simplemobiletools.commons.helpers.SAVE_DISCARD_PROMPT_INTERVAL
2123import com.simplemobiletools.commons.helpers.ensureBackgroundThread
2224import com.simplemobiletools.commons.views.MyEditText
2325import com.simplemobiletools.filemanager.pro.R
@@ -31,10 +33,12 @@ import java.io.OutputStream
3133
3234class ReadTextActivity : SimpleActivity () {
3335 private val SELECT_SAVE_FILE_INTENT = 1
36+ private val SELECT_SAVE_FILE_AND_EXIT_INTENT = 2
3437
3538 private var filePath = " "
3639 private var originalText = " "
3740 private var searchIndex = 0
41+ private var lastSavePromptTS = 0L
3842 private var searchMatches = emptyList<Int >()
3943 private var isSearchActive = false
4044
@@ -83,15 +87,30 @@ class ReadTextActivity : SimpleActivity() {
8387 super .onActivityResult(requestCode, resultCode, resultData)
8488 if (requestCode == SELECT_SAVE_FILE_INTENT && resultCode == Activity .RESULT_OK && resultData != null && resultData.data != null ) {
8589 val outputStream = contentResolver.openOutputStream(resultData.data!! )
86- saveTextContent(outputStream)
90+ saveTextContent(outputStream, shouldExitAfterSaving = requestCode == SELECT_SAVE_FILE_AND_EXIT_INTENT ,
91+ shouldOverwriteOriginalText = getRealPathFromURI(intent.data!! ) == filePath)
8792 }
8893 }
8994
9095 override fun onBackPressed () {
91- if (isSearchActive) {
92- closeSearch()
93- } else {
94- super .onBackPressed()
96+ val hasUnsavedChanges = originalText != read_text_view.text.toString()
97+ when {
98+ isSearchActive -> {
99+ closeSearch()
100+ }
101+ hasUnsavedChanges && System .currentTimeMillis() - lastSavePromptTS > SAVE_DISCARD_PROMPT_INTERVAL -> {
102+ lastSavePromptTS = System .currentTimeMillis()
103+ ConfirmationAdvancedDialog (this , " " , R .string.save_before_closing, R .string.save, R .string.discard) {
104+ if (it) {
105+ saveText(shouldExitAfterSaving = true )
106+ } else {
107+ super .onBackPressed()
108+ }
109+ }
110+ }
111+ else -> {
112+ super .onBackPressed()
113+ }
95114 }
96115 }
97116
@@ -108,7 +127,7 @@ class ReadTextActivity : SimpleActivity() {
108127 }, 250 )
109128 }
110129
111- private fun saveText () {
130+ private fun saveText (shouldExitAfterSaving : Boolean = false ) {
112131 if (filePath.isEmpty()) {
113132 filePath = getRealPathFromURI(intent.data!! ) ? : " "
114133 }
@@ -120,7 +139,12 @@ class ReadTextActivity : SimpleActivity() {
120139 putExtra(Intent .EXTRA_TITLE , filename)
121140 addCategory(Intent .CATEGORY_OPENABLE )
122141
123- startActivityForResult(this , SELECT_SAVE_FILE_INTENT )
142+ val requestCode = if (shouldExitAfterSaving) {
143+ SELECT_SAVE_FILE_AND_EXIT_INTENT
144+ } else {
145+ SELECT_SAVE_FILE_INTENT
146+ }
147+ startActivityForResult(this , requestCode)
124148 }
125149 }
126150 } else {
@@ -129,19 +153,28 @@ class ReadTextActivity : SimpleActivity() {
129153 if (it) {
130154 val file = File (path)
131155 getFileOutputStream(file.toFileDirItem(this ), true ) {
132- saveTextContent(it)
156+ saveTextContent(it, shouldExitAfterSaving, shouldOverwriteOriginalText = path == filePath )
133157 }
134158 }
135159 }
136160 }
137161 }
138162 }
139163
140- private fun saveTextContent (outputStream : OutputStream ? ) {
164+ private fun saveTextContent (outputStream : OutputStream ? , shouldExitAfterSaving : Boolean , shouldOverwriteOriginalText : Boolean ) {
141165 if (outputStream != null ) {
142- outputStream.bufferedWriter().use { it.write(read_text_view.text.toString()) }
166+ val currentText = read_text_view.text.toString()
167+ outputStream.bufferedWriter().use { it.write(currentText) }
143168 toast(R .string.file_saved)
144169 hideKeyboard()
170+
171+ if (shouldOverwriteOriginalText) {
172+ originalText = currentText
173+ }
174+
175+ if (shouldExitAfterSaving) {
176+ super .onBackPressed()
177+ }
145178 } else {
146179 toast(R .string.unknown_error_occurred)
147180 }
0 commit comments