From 0e7cdfa2b5e7707a814e302228a82f4ae8587c5b Mon Sep 17 00:00:00 2001 From: William Brawner Date: Mon, 5 Oct 2020 20:43:56 -0700 Subject: [PATCH] Fix an issue with deleting changes upon device rotation --- .../simplemarkdown/utility/ErrorHandler.kt | 1 + .../view/fragment/EditFragment.kt | 9 ++++++--- .../view/fragment/MainFragment.kt | 2 +- .../viewmodel/MarkdownViewModel.kt | 18 +++++++++++++++--- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/app/src/free/java/com/wbrawner/simplemarkdown/utility/ErrorHandler.kt b/app/src/free/java/com/wbrawner/simplemarkdown/utility/ErrorHandler.kt index 7bc5eda..3894dec 100644 --- a/app/src/free/java/com/wbrawner/simplemarkdown/utility/ErrorHandler.kt +++ b/app/src/free/java/com/wbrawner/simplemarkdown/utility/ErrorHandler.kt @@ -1,5 +1,6 @@ package com.wbrawner.simplemarkdown.utility +import android.util.Log import kotlin.reflect.KProperty class errorHandlerImpl { diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/EditFragment.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/EditFragment.kt index b0ce8f3..c5ac0b4 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/EditFragment.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/EditFragment.kt @@ -95,15 +95,18 @@ class EditFragment : Fragment(), ViewPagerPage, CoroutineScope { false } markdownEditor?.setText(viewModel.markdownUpdates.value) - viewModel.originalMarkdown.observe(viewLifecycleOwner, Observer { - markdownEditor?.setText(it) + viewModel.editorActions.observe(viewLifecycleOwner, Observer { + if (it.consumed.getAndSet(true)) return@Observer + if (it is MarkdownViewModel.EditorAction.Load) { + markdownEditor?.setText(it.markdown) + } }) launch { val enableReadability = withContext(Dispatchers.IO) { context?.let { PreferenceManager.getDefaultSharedPreferences(it) .getBoolean(getString(R.string.readability_enabled), false) - }?: false + } ?: false } if (enableReadability) { if (readabilityWatcher == null) { 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 6665b61..ed21617 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 @@ -252,7 +252,7 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba } private fun promptSaveOrDiscardChanges() { - if (viewModel.originalMarkdown.value == viewModel.markdownUpdates.value) { + if (viewModel.shouldPromptSave()) { viewModel.reset("Untitled.md") PreferenceManager.getDefaultSharedPreferences(requireContext()).edit { remove(getString(R.string.pref_key_autosave_uri)) 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 f1a2d6c..f90aad0 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/viewmodel/MarkdownViewModel.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/viewmodel/MarkdownViewModel.kt @@ -9,15 +9,18 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import java.io.FileInputStream import java.io.Reader +import java.util.concurrent.atomic.AtomicBoolean class MarkdownViewModel : ViewModel() { val fileName = MutableLiveData("Untitled.md") val markdownUpdates = MutableLiveData() - val originalMarkdown = MutableLiveData() + val editorActions = MutableLiveData() val uri = MutableLiveData() + private val isDirty = AtomicBoolean(false) fun updateMarkdown(markdown: String?) { this.markdownUpdates.postValue(markdown ?: "") + isDirty.set(true) } suspend fun load(context: Context, uri: Uri?): Boolean { @@ -32,7 +35,8 @@ class MarkdownViewModel : ViewModel() { // If we don't get anything back, then we can assume that reading the file failed return@withContext false } - originalMarkdown.postValue(content) + isDirty.set(false) + editorActions.postValue(EditorAction.Load(content)) markdownUpdates.postValue(content) this@MarkdownViewModel.fileName.postValue(fileName) this@MarkdownViewModel.uri.postValue(uri) @@ -66,7 +70,15 @@ class MarkdownViewModel : ViewModel() { fun reset(untitledFileName: String) { fileName.postValue(untitledFileName) - originalMarkdown.postValue("") markdownUpdates.postValue("") + isDirty.set(false) + } + + fun shouldPromptSave() = isDirty.get() + + sealed class EditorAction { + val consumed = AtomicBoolean(false) + + data class Load(val markdown: String) : EditorAction() } }