fix: save state when rotating screen (#111)

* Fixed Text File Editor losing unsaved changes when rotating screen

* style: replace state variable with constant

---------

Co-authored-by: Naveen Singh <36371707+naveensingh@users.noreply.github.com>
Co-authored-by: Naveen Singh <snaveen935@gmail.com>
This commit is contained in:
Burgerpower01
2025-05-21 08:15:09 +02:00
committed by GitHub
parent 5dae7f82f5
commit 97dcf30b47

View File

@@ -32,6 +32,7 @@ class ReadTextActivity : SimpleActivity() {
companion object {
private const val SELECT_SAVE_FILE_INTENT = 1
private const val SELECT_SAVE_FILE_AND_EXIT_INTENT = 2
private const val KEY_UNSAVED_TEXT = "KEY_UNSAVED_TEXT"
}
private val binding by viewBinding(ActivityReadTextBinding::inflate)
@@ -85,7 +86,7 @@ class ReadTextActivity : SimpleActivity() {
binding.readTextView.onGlobalLayout {
ensureBackgroundThread {
checkIntent(uri)
checkIntent(uri, savedInstanceState)
}
}
@@ -97,6 +98,13 @@ class ReadTextActivity : SimpleActivity() {
setupToolbar(binding.readTextToolbar, NavigationIcon.Arrow)
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
if (originalText != binding.readTextView.text.toString()) {
outState.putString(KEY_UNSAVED_TEXT, binding.readTextView.text.toString())
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
if (requestCode == SELECT_SAVE_FILE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) {
@@ -241,7 +249,7 @@ class ReadTextActivity : SimpleActivity() {
}
}
private fun checkIntent(uri: Uri) {
private fun checkIntent(uri: Uri, savedInstanceState: Bundle?) {
originalText = if (uri.scheme == "file") {
filePath = uri.path!!
val file = File(filePath)
@@ -270,7 +278,13 @@ class ReadTextActivity : SimpleActivity() {
}
runOnUiThread {
binding.readTextView.setText(originalText)
var textToSet = originalText
if (savedInstanceState != null) {
textToSet = savedInstanceState.getString(KEY_UNSAVED_TEXT, originalText)
}
binding.readTextView.setText(textToSet)
if (originalText.isNotEmpty()) {
hideKeyboard()
} else {