Skip to content

Commit

Permalink
[fix] fixes last modified ordering.
Browse files Browse the repository at this point in the history
  • Loading branch information
coderPaddyS committed Mar 14, 2024
1 parent ed65ec6 commit 1f5611c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL("DROP TABLE notes;");
database.execSQL("ALTER TABLE notes_new RENAME TO notes");
database.execSQL(
"CREATE TRIGGER [UpdateLastModified] AFTER UPDATE ON notes FOR EACH ROW WHEN NEW.last_modified = OLD.last_modified " +
"CREATE TRIGGER [UpdateLastModified] AFTER UPDATE ON notes FOR EACH ROW WHEN NEW.last_modified = OLD.last_modified AND NEW.custom_order = OLD.custom_order " +
"BEGIN " +
"UPDATE notes SET last_modified = DateTime('now') WHERE _id=NEW._id; " +
"END;"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ data class Note(
var type: Int,
var category: Int,
var in_trash: Int = 0,
@ColumnInfo(defaultValue = "CURRENT_TIMESTAMP")
var last_modified: String,
var custom_order: Int
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
lateinit var adapter: NoteAdapter
private val searchView: SearchView by lazy { findViewById(R.id.searchViewFilter) }
private lateinit var fab: MainFABFragment
private var skipNextNoteFlow = false

// A launcher to receive and react to a NoteActivity returning a category
// The category is used to set the selectecCategory
Expand Down Expand Up @@ -143,7 +144,12 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
recyclerView.adapter = adapter

lifecycleScope.launch {
mainActivityViewModel.activeNotes.collect { notes -> adapter.setNotes(notes) }
mainActivityViewModel.activeNotes.collect { notes ->
if (!skipNextNoteFlow) {
adapter.setNotes(notes)
}
skipNextNoteFlow = false
}
}

val ith = ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP or ItemTouchHelper.DOWN, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
Expand All @@ -156,6 +162,8 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
adapter.notes[from].custom_order = adapter.notes[to].custom_order
adapter.notes[to].custom_order = temp
Collections.swap(adapter.notes, from, to)
skipNextNoteFlow = true
mainActivityViewModel.updateAll(listOf(adapter.notes[from], adapter.notes[to]))

adapter.notifyItemMoved(to, from)

Expand Down Expand Up @@ -342,7 +350,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte

override fun onPause() {
// Save all changed orders if activity is paused
mainActivityViewModel.updateAll(adapter.notes)
// mainActivityViewModel.updateAll(adapter.notes)
super.onPause()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ class MainActivityViewModel(application: Application) : AndroidViewModel(applica
}

fun insert(note: Note) {
viewModelScope.launch(Dispatchers.Default) {
viewModelScope.launch(Dispatchers.IO) {
repository.noteDao().insert(note)
}
}

fun update(note: Note) {
viewModelScope.launch(Dispatchers.Default) {
viewModelScope.launch(Dispatchers.IO) {
repository.noteDao().update(note)
}
}
Expand Down

0 comments on commit 1f5611c

Please sign in to comment.