Skip to content

Commit

Permalink
Merge pull request #174 from SecUSo/fix/save-note-race-condition
Browse files Browse the repository at this point in the history
Fix race condition in some edge cases
  • Loading branch information
coderPaddyS authored Sep 17, 2024
2 parents 3455ead + 914c77b commit 49dfbfa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 49dfbfa

Please sign in to comment.