Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
Repository and ViewModel implementation for all notes screen. Issue #16
Browse files Browse the repository at this point in the history
… (#61)
  • Loading branch information
virunarala authored Oct 15, 2022
1 parent 4f6b1fe commit e832b3e
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 7 deletions.
28 changes: 21 additions & 7 deletions app/src/main/java/com/hadiyarajesh/notex/database/dao/NoteDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ interface NoteDao {
updateFolderModificationProperty(parentFolderId)
}

/**
* This method will update the provided Note and also modify the updatedOn field of the containing Folder(if the note is in a folder)
*/
@Transaction
suspend fun updateNote(note: Note, parentFolderId: Long?) {
insertOrUpdate(note)
parentFolderId?.let {
updateFolderModificationProperty(folderId = parentFolderId, updatedOn = note.updatedOn)
}
}

@InternalUseOnly
@Query("UPDATE Note SET parentFolderId = :parentFolderId WHERE noteId = :noteId")
suspend fun attachNoteToFolder(noteId: Long, parentFolderId: Long)
Expand All @@ -29,7 +40,7 @@ interface NoteDao {
suspend fun updateFolderModificationProperty(folderId: Long, updatedOn: Instant = Instant.now())

@Query("SELECT * FROM Note WHERE noteId = :noteId")
fun getById(noteId: Long): Note
suspend fun getById(noteId: Long): Note

/**
* This method wil only return non-deleted notes.
Expand All @@ -43,30 +54,33 @@ interface NoteDao {
* This method wil return ALL notes, including deleted (archived) notes.
*/
@Query("SELECT * FROM Note ORDER BY noteId DESC")
fun getAllByDesc(): PagingSource<Int, Note>
suspend fun getAllByDesc(): PagingSource<Int, Note>

/**
* Mark a note as deleted (archived)
*/
@Query("UPDATE Note SET archived = 1 WHERE noteId = :noteId")
fun markAsArchived(noteId: Long)
suspend fun markAsArchived(noteId: Long)

@Query("UPDATE Note SET archived = 0 WHERE noteId = :noteId")
suspend fun markAsUnarchived(noteId: Long)

@Delete
fun delete(note: Note)
suspend fun delete(note: Note): Int

@Query("DELETE FROM Note WHERE noteId = :noteId")
fun deleteById(noteId: Long)
suspend fun deleteById(noteId: Long): Int

/**
* Permanently delete all notes that are already archived.
*/
@Query("DELETE FROM Note WHERE archived = 1")
fun deleteAllArchivedNotes()
suspend fun deleteAllArchivedNotes(): Int

/**
* WARNING: USE WITH CAUTION
* Permanently delete all notes
*/
@Query("DELETE FROM Note")
fun deleteAll()
suspend fun deleteAll(): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,36 @@ class NotesRepository @Inject constructor(
) {
noteDao.getAllUnArchivedByDesc()
}.flow

suspend fun getNote(noteId: Long): Note = noteDao.getById(noteId)

/**
* Inserts the provided Note into the database.
*/
suspend fun addNote(note: Note) = noteDao.insertOrUpdate(note)

/**
* Updates the provided Note in the database.
*/
suspend fun updateNote(note: Note,parentFolderId: Long?) = noteDao.updateNote(note, parentFolderId)



suspend fun deleteNote(note: Note): Int = noteDao.delete(note)

suspend fun deleteNote(noteId: Long): Int = noteDao.deleteById(noteId)

suspend fun deleteArchived(): Int = noteDao.deleteAllArchivedNotes()

suspend fun deleteAllNotes(): Int = noteDao.deleteAll()



suspend fun archive(noteId: Long) = noteDao.markAsArchived(noteId)

suspend fun unarchive(noteId: Long) = noteDao.markAsUnarchived(noteId)



suspend fun addToFolder(noteId: Long, folderId: Long) = noteDao.addToFolder(noteId,folderId)
}
67 changes: 67 additions & 0 deletions app/src/main/java/com/hadiyarajesh/notex/ui/note/NotesViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,90 @@ import com.hadiyarajesh.notex.database.entity.Note
import com.hadiyarajesh.notex.repository.notes.NotesRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class NotesViewModel @Inject constructor(
private val notesRepository: NotesRepository
) : ViewModel() {

// private val _notesFlow = MutableStateFlow<PagingData<Note>>(PagingData.empty())
// val notesFlow: StateFlow<PagingData<Note>>
// get() = _notesFlow

// Get all notes as soon as collector collect this flow
val notes: Flow<PagingData<Note>> =
notesRepository
.getAllNotes()
.cachedIn(viewModelScope)

private var _numberOfNotesDeleted: Int = 0
val numberOfNotesDeleted: Int
get() = _numberOfNotesDeleted

private var _note = MutableStateFlow<Note?>(null)
val note: StateFlow<Note?>
get() = _note

fun createNote(
title: String?,
content: String?
) = viewModelScope.launch {
notesRepository.createNote(title = title, content = content)
}

fun deleteNote(note: Note) {
viewModelScope.launch {
_numberOfNotesDeleted = notesRepository.deleteNote(note)
}
}

fun deleteNote(noteId: Long) {
viewModelScope.launch {
_numberOfNotesDeleted = notesRepository.deleteNote(noteId)
}
}

fun deleteArchivedNotes() {
viewModelScope.launch {
_numberOfNotesDeleted = notesRepository.deleteArchived()
}
}

/**
* Deletes all Notes
*/
fun deleteAll() {
viewModelScope.launch {
_numberOfNotesDeleted = notesRepository.deleteAllNotes()
}
}

fun archive(noteId: Long) {
viewModelScope.launch {
notesRepository.archive(noteId)
}
}

fun unarchive(noteId: Long) {
viewModelScope.launch {
notesRepository.unarchive(noteId)
}
}


fun getNote(noteId: Long) {
viewModelScope.launch {
_note.value = notesRepository.getNote(noteId)
}
}

fun addToFolder(noteId: Long,folderId: Long) {
viewModelScope.launch {
notesRepository.addToFolder(noteId,folderId)
}
}
}

0 comments on commit e832b3e

Please sign in to comment.