Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Delete Note issue #161

Merged
merged 3 commits into from
Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions app/src/main/java/thecodemonks/org/nottzapp/db/NotesDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@

package thecodemonks.org.nottzapp.db

import androidx.room.*
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import kotlinx.coroutines.flow.Flow
import thecodemonks.org.nottzapp.model.Notes

Expand All @@ -48,7 +52,7 @@ interface NotesDao {
@Query("SELECT * FROM notes")
fun getNotes(): Flow<List<Notes>>

// delete notes from db
@Delete
suspend fun deleteNotes(notes: Notes)
// delete notes by id
@Query("DELETE FROM notes where id=:id")
suspend fun deleteNote(id: Int)
}
4 changes: 2 additions & 2 deletions app/src/main/java/thecodemonks/org/nottzapp/repo/NotesRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ class NotesRepo @Inject constructor(private val db: NotesDatabase) {
// get saved notes
fun getSavedNotes() = db.getNotesDao().getNotes()

// delete article
suspend fun deleteNotes(notes: Notes) = db.getNotesDao().deleteNotes(notes)
// delete note by ID
suspend fun deleteNote(id: Int) = db.getNotesDao().deleteNote(id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class NotesDetailsFragment : Fragment(R.layout.fragment_notes_details) {
return when (item.itemId) {

R.id.action_delete -> {
viewModel.deleteNotes(args.notes.id, args.notes.title, args.notes.description)
viewModel.deleteNoteByID(args.notes.id)
findNavController().navigateUp()
true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@
package thecodemonks.org.nottzapp.ui.notes

import android.os.Bundle
import android.view.*
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatDelegate
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
Expand Down Expand Up @@ -136,10 +141,8 @@ class NotesFragment : Fragment(R.layout.notes_fragment) {
// get item position & delete notes
val position = viewHolder.adapterPosition
val notes = notesAdapter.differ.currentList[position]
viewModel.deleteNotes(
notes.id,
notes.title,
notes.description
viewModel.deleteNoteByID(
notes.id
)
Snackbar.make(
binding.root,
Expand Down Expand Up @@ -173,6 +176,7 @@ class NotesFragment : Fragment(R.layout.notes_fragment) {

private fun showEmptyState() {
binding.emptyStateLayout.show()
notesAdapter.differ.submitList(emptyList())
}

private fun onNotesLoaded(notes: List<Notes>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class NotesViewModel @Inject internal constructor(
notesRepo.insert(notes)
}

// save notes
// update notes
fun updateNotes(id: Int, taskName: String, taskDesc: String) = viewModelScope.launch {
val notes = Notes(
id = id,
Expand All @@ -101,13 +101,8 @@ class NotesViewModel @Inject internal constructor(
}
}

// delete notes
fun deleteNotes(taskID: Int, taskName: String, taskDesc: String) = viewModelScope.launch {
val notes = Notes(
id = taskID,
title = taskName,
description = taskDesc
)
notesRepo.deleteNotes(notes)
// delete note by ID
fun deleteNoteByID(id: Int) = viewModelScope.launch {
notesRepo.deleteNote(id)
}
}