From 1affb7069ad4a5e833ae52e3be6a645e588c5069 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Tue, 22 Sep 2020 13:40:56 -0700 Subject: [PATCH] (Hopefully) fix an issue with accidentally overwriting files with blank content upon --- app/build.gradle | 4 ++-- .../simplemarkdown/view/fragment/MainFragment.kt | 12 +++++++++++- .../simplemarkdown/viewmodel/MarkdownViewModel.kt | 4 ++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index e04aba7..92a8899 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -40,8 +40,8 @@ android { applicationId "com.wbrawner.simplemarkdown" minSdkVersion 23 targetSdkVersion 30 - versionCode 29 - versionName "0.8.7" + versionCode 30 + versionName "0.8.8" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" buildConfigField "boolean", "ENABLE_CUSTOM_CSS", "false" } diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MainFragment.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MainFragment.kt index b0ea4cf..6665b61 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MainFragment.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MainFragment.kt @@ -16,6 +16,7 @@ import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat +import androidx.core.content.edit import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.lifecycle.Observer @@ -228,6 +229,10 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba Toast.makeText(it, R.string.file_load_error, Toast.LENGTH_SHORT) .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() { if (viewModel.originalMarkdown.value == viewModel.markdownUpdates.value) { viewModel.reset("Untitled.md") + PreferenceManager.getDefaultSharedPreferences(requireContext()).edit { + remove(getString(R.string.pref_key_autosave_uri)) + } return } val context = context ?: return @@ -257,6 +265,9 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba .setMessage(R.string.prompt_save_changes) .setNegativeButton(R.string.action_discard) { _, _ -> viewModel.reset("Untitled.md") + PreferenceManager.getDefaultSharedPreferences(requireContext()).edit { + remove(getString(R.string.pref_key_autosave_uri)) + } } .setPositiveButton(R.string.action_save) { _, _ -> requestFileOp(REQUEST_SAVE_FILE) @@ -319,7 +330,6 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba // Request codes const val REQUEST_OPEN_FILE = 1 const val REQUEST_SAVE_FILE = 2 - const val REQUEST_DARK_MODE = 4 const val KEY_AUTOSAVE = "autosave" } } diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/viewmodel/MarkdownViewModel.kt b/app/src/main/java/com/wbrawner/simplemarkdown/viewmodel/MarkdownViewModel.kt index 36fbbb6..f1a2d6c 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/viewmodel/MarkdownViewModel.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/viewmodel/MarkdownViewModel.kt @@ -28,6 +28,10 @@ class MarkdownViewModel : ViewModel() { val fileInput = FileInputStream(it.fileDescriptor) val fileName = uri.getName(context) 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) markdownUpdates.postValue(content) this@MarkdownViewModel.fileName.postValue(fileName)