From 914c77b3da2b7105f02926da3b57c6b61d942c3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20L=C3=A4nge?= Date: Mon, 16 Sep 2024 13:06:13 +0200 Subject: [PATCH] Save notes on main thread if saved manually - Fixes race condition in some edge cases --- .../ui/notes/BaseNoteActivity.kt | 22 ++++++++++++++++--- .../ui/notes/CreateEditNoteViewModel.kt | 5 +++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/BaseNoteActivity.kt b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/BaseNoteActivity.kt index fee3abb1..8e9e76ca 100644 --- a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/BaseNoteActivity.kt +++ b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/BaseNoteActivity.kt @@ -36,7 +36,14 @@ import android.view.MotionEvent import android.view.View import android.view.WindowManager import android.view.inputmethod.InputMethodManager -import android.widget.* +import android.widget.ArrayAdapter +import android.widget.AutoCompleteTextView +import android.widget.DatePicker +import android.widget.EditText +import android.widget.PopupMenu +import android.widget.TextView +import android.widget.TimePicker +import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity @@ -47,6 +54,7 @@ import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.lifecycleScope import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking import org.secuso.privacyfriendlynotes.R import org.secuso.privacyfriendlynotes.preference.PreferenceKeys import org.secuso.privacyfriendlynotes.room.DbContract @@ -59,7 +67,8 @@ import org.secuso.privacyfriendlynotes.ui.helper.NotificationHelper.removeNotifi import org.secuso.privacyfriendlynotes.ui.helper.NotificationHelper.showAlertScheduledToast import org.secuso.privacyfriendlynotes.ui.manageCategories.ManageCategoriesActivity import java.io.OutputStream -import java.util.* +import java.util.Calendar +import java.util.Date /** * A abstract note. @@ -464,7 +473,14 @@ abstract class BaseNoteActivity(noteType: Int) : AppCompatActivity(), View.OnCli } if (isLoadedNote) { note._id = id - createEditNoteViewModel.update(note) + if (showNotSaved) { + //Wait for job to complete + runBlocking { + createEditNoteViewModel.update(note).join() + } + } else { + createEditNoteViewModel.update(note) + } Toast.makeText(applicationContext, R.string.toast_updated, Toast.LENGTH_SHORT).show() } else { id = createEditNoteViewModel.insert(note) diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/CreateEditNoteViewModel.kt b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/CreateEditNoteViewModel.kt index e091a453..115953e9 100644 --- a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/CreateEditNoteViewModel.kt +++ b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/CreateEditNoteViewModel.kt @@ -21,6 +21,7 @@ import androidx.lifecycle.MediatorLiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -111,8 +112,8 @@ class CreateEditNoteViewModel(application: Application) : AndroidViewModel(appli return id } - fun update(note: Note) { - viewModelScope.launch(Dispatchers.Default) { + fun update(note: Note): Job { + return viewModelScope.launch(Dispatchers.Default) { database.noteDao().update(note) } }