(Hopefully) fix an issue with accidentally overwriting files with blank content upon

This commit is contained in:
William Brawner 2020-09-22 13:40:56 -07:00
parent 0ff5ccdbd6
commit 1affb7069a
3 changed files with 17 additions and 3 deletions

View file

@ -40,8 +40,8 @@ android {
applicationId "com.wbrawner.simplemarkdown" applicationId "com.wbrawner.simplemarkdown"
minSdkVersion 23 minSdkVersion 23
targetSdkVersion 30 targetSdkVersion 30
versionCode 29 versionCode 30
versionName "0.8.7" versionName "0.8.8"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildConfigField "boolean", "ENABLE_CUSTOM_CSS", "false" buildConfigField "boolean", "ENABLE_CUSTOM_CSS", "false"
} }

View file

@ -16,6 +16,7 @@ import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
@ -228,6 +229,10 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba
Toast.makeText(it, R.string.file_load_error, Toast.LENGTH_SHORT) Toast.makeText(it, R.string.file_load_error, Toast.LENGTH_SHORT)
.show() .show()
} }
} else {
PreferenceManager.getDefaultSharedPreferences(requireContext()).edit {
putString(getString(R.string.pref_key_autosave_uri), data.data.toString())
}
} }
} }
} }
@ -249,6 +254,9 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba
private fun promptSaveOrDiscardChanges() { private fun promptSaveOrDiscardChanges() {
if (viewModel.originalMarkdown.value == viewModel.markdownUpdates.value) { if (viewModel.originalMarkdown.value == viewModel.markdownUpdates.value) {
viewModel.reset("Untitled.md") viewModel.reset("Untitled.md")
PreferenceManager.getDefaultSharedPreferences(requireContext()).edit {
remove(getString(R.string.pref_key_autosave_uri))
}
return return
} }
val context = context ?: return val context = context ?: return
@ -257,6 +265,9 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba
.setMessage(R.string.prompt_save_changes) .setMessage(R.string.prompt_save_changes)
.setNegativeButton(R.string.action_discard) { _, _ -> .setNegativeButton(R.string.action_discard) { _, _ ->
viewModel.reset("Untitled.md") viewModel.reset("Untitled.md")
PreferenceManager.getDefaultSharedPreferences(requireContext()).edit {
remove(getString(R.string.pref_key_autosave_uri))
}
} }
.setPositiveButton(R.string.action_save) { _, _ -> .setPositiveButton(R.string.action_save) { _, _ ->
requestFileOp(REQUEST_SAVE_FILE) requestFileOp(REQUEST_SAVE_FILE)
@ -319,7 +330,6 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba
// Request codes // Request codes
const val REQUEST_OPEN_FILE = 1 const val REQUEST_OPEN_FILE = 1
const val REQUEST_SAVE_FILE = 2 const val REQUEST_SAVE_FILE = 2
const val REQUEST_DARK_MODE = 4
const val KEY_AUTOSAVE = "autosave" const val KEY_AUTOSAVE = "autosave"
} }
} }

View file

@ -28,6 +28,10 @@ class MarkdownViewModel : ViewModel() {
val fileInput = FileInputStream(it.fileDescriptor) val fileInput = FileInputStream(it.fileDescriptor)
val fileName = uri.getName(context) val fileName = uri.getName(context)
val content = fileInput.reader().use(Reader::readText) val content = fileInput.reader().use(Reader::readText)
if (content.isBlank()) {
// If we don't get anything back, then we can assume that reading the file failed
return@withContext false
}
originalMarkdown.postValue(content) originalMarkdown.postValue(content)
markdownUpdates.postValue(content) markdownUpdates.postValue(content)
this@MarkdownViewModel.fileName.postValue(fileName) this@MarkdownViewModel.fileName.postValue(fileName)