Prevent autosave when manual save is in progress
This commit is contained in:
parent
260c49d8d5
commit
c2be2274e8
2 changed files with 11 additions and 4 deletions
|
@ -156,8 +156,8 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onPause() {
|
override fun onStop() {
|
||||||
super.onPause()
|
super.onStop()
|
||||||
val context = context?.applicationContext ?: return
|
val context = context?.applicationContext ?: return
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
viewModel.autosave(context, PreferenceManager.getDefaultSharedPreferences(context))
|
viewModel.autosave(context, PreferenceManager.getDefaultSharedPreferences(context))
|
||||||
|
|
|
@ -8,6 +8,8 @@ import androidx.lifecycle.ViewModel
|
||||||
import com.wbrawner.simplemarkdown.utility.getName
|
import com.wbrawner.simplemarkdown.utility.getName
|
||||||
import com.wbrawner.simplemarkdown.view.fragment.MainFragment
|
import com.wbrawner.simplemarkdown.view.fragment.MainFragment
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
@ -23,6 +25,7 @@ class MarkdownViewModel(val timber: Timber.Tree = Timber.asTree()) : ViewModel()
|
||||||
val editorActions = MutableLiveData<EditorAction>()
|
val editorActions = MutableLiveData<EditorAction>()
|
||||||
val uri = MutableLiveData<Uri?>()
|
val uri = MutableLiveData<Uri?>()
|
||||||
private val isDirty = AtomicBoolean(false)
|
private val isDirty = AtomicBoolean(false)
|
||||||
|
private val saveMutex = Mutex()
|
||||||
|
|
||||||
fun updateMarkdown(markdown: String?) {
|
fun updateMarkdown(markdown: String?) {
|
||||||
this.markdownUpdates.postValue(markdown ?: "")
|
this.markdownUpdates.postValue(markdown ?: "")
|
||||||
|
@ -45,13 +48,13 @@ class MarkdownViewModel(val timber: Timber.Tree = Timber.asTree()) : ViewModel()
|
||||||
timber.i("Ignoring load for empty file $fileName from $fileInput")
|
timber.i("Ignoring load for empty file $fileName from $fileInput")
|
||||||
return@withContext false
|
return@withContext false
|
||||||
}
|
}
|
||||||
isDirty.set(false)
|
|
||||||
editorActions.postValue(EditorAction.Load(content))
|
editorActions.postValue(EditorAction.Load(content))
|
||||||
markdownUpdates.postValue(content)
|
markdownUpdates.postValue(content)
|
||||||
this@MarkdownViewModel.fileName.postValue(fileName)
|
this@MarkdownViewModel.fileName.postValue(fileName)
|
||||||
this@MarkdownViewModel.uri.postValue(uri)
|
this@MarkdownViewModel.uri.postValue(uri)
|
||||||
timber.i("Loaded file $fileName from $fileInput")
|
timber.i("Loaded file $fileName from $fileInput")
|
||||||
timber.v("File contents:\n$content")
|
timber.v("File contents:\n$content")
|
||||||
|
isDirty.set(false)
|
||||||
true
|
true
|
||||||
} ?: run {
|
} ?: run {
|
||||||
timber.w("Open file descriptor returned null for uri: $uri")
|
timber.w("Open file descriptor returned null for uri: $uri")
|
||||||
|
@ -64,7 +67,7 @@ class MarkdownViewModel(val timber: Timber.Tree = Timber.asTree()) : ViewModel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun save(context: Context, givenUri: Uri? = null): Boolean {
|
suspend fun save(context: Context, givenUri: Uri? = null): Boolean = saveMutex.withLock {
|
||||||
val uri = givenUri?.let {
|
val uri = givenUri?.let {
|
||||||
timber.i("Saving file with given uri: $it")
|
timber.i("Saving file with given uri: $it")
|
||||||
it
|
it
|
||||||
|
@ -100,6 +103,10 @@ class MarkdownViewModel(val timber: Timber.Tree = Timber.asTree()) : ViewModel()
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun autosave(context: Context, sharedPrefs: SharedPreferences) {
|
suspend fun autosave(context: Context, sharedPrefs: SharedPreferences) {
|
||||||
|
if (saveMutex.isLocked) {
|
||||||
|
timber.i("Ignoring autosave since manual save is already in progress")
|
||||||
|
return
|
||||||
|
}
|
||||||
val isAutoSaveEnabled = sharedPrefs.getBoolean(MainFragment.KEY_AUTOSAVE, true)
|
val isAutoSaveEnabled = sharedPrefs.getBoolean(MainFragment.KEY_AUTOSAVE, true)
|
||||||
timber.d("Autosave called. isEnabled? $isAutoSaveEnabled")
|
timber.d("Autosave called. isEnabled? $isAutoSaveEnabled")
|
||||||
if (!isDirty.get() || !isAutoSaveEnabled) {
|
if (!isDirty.get() || !isAutoSaveEnabled) {
|
||||||
|
|
Loading…
Reference in a new issue