diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index afb7c03..62a5302 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,7 +12,7 @@ android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" - android:label="@string/app_name" + android:label="@string/app_name_debug" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.NoteX" @@ -20,7 +20,6 @@ diff --git a/app/src/main/java/com/hadiyarajesh/notex/database/dao/NoteDao.kt b/app/src/main/java/com/hadiyarajesh/notex/database/dao/NoteDao.kt index 8ccc755..5e24a23 100644 --- a/app/src/main/java/com/hadiyarajesh/notex/database/dao/NoteDao.kt +++ b/app/src/main/java/com/hadiyarajesh/notex/database/dao/NoteDao.kt @@ -7,11 +7,11 @@ import kotlinx.coroutines.flow.Flow @Dao interface NoteDao { @Insert(onConflict = OnConflictStrategy.REPLACE) - fun insertOrUpdate(note: Note) + suspend fun insertOrUpdate(note: Note): Long @Query("SELECT * FROM Note") - fun getNote(): Flow + suspend fun getNotes(): Flow> @Delete - fun deleteNote(note: Note) + suspend fun deleteNote(note: Note): Note } diff --git a/app/src/main/java/com/hadiyarajesh/notex/repository/NotesRepository.kt b/app/src/main/java/com/hadiyarajesh/notex/repository/NotesRepository.kt index 54f5ab8..9e80021 100644 --- a/app/src/main/java/com/hadiyarajesh/notex/repository/NotesRepository.kt +++ b/app/src/main/java/com/hadiyarajesh/notex/repository/NotesRepository.kt @@ -1,8 +1,20 @@ package com.hadiyarajesh.notex.repository +import com.hadiyarajesh.notex.database.dao.NoteDao +import com.hadiyarajesh.notex.database.entity.Note +import kotlinx.coroutines.flow.Flow import javax.inject.Inject import javax.inject.Singleton @Singleton -class NotesRepository @Inject constructor() { +class NotesRepository @Inject constructor(private val noteDao: NoteDao) { + + + suspend fun add(note: Note): Long = noteDao.insertOrUpdate(note) + + suspend fun update(note: Note): Long = noteDao.insertOrUpdate(note) + + suspend fun remove(note: Note): Note = noteDao.deleteNote(note) + + suspend fun allNotes(): Flow> = noteDao.getNotes() } diff --git a/app/src/main/java/com/hadiyarajesh/notex/ui/note/NotesViewModel.kt b/app/src/main/java/com/hadiyarajesh/notex/ui/note/NotesViewModel.kt index f5c496c..61e84a5 100644 --- a/app/src/main/java/com/hadiyarajesh/notex/ui/note/NotesViewModel.kt +++ b/app/src/main/java/com/hadiyarajesh/notex/ui/note/NotesViewModel.kt @@ -2,9 +2,18 @@ package com.hadiyarajesh.notex.ui.note import android.util.Log import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.hadiyarajesh.notex.database.entity.Note import com.hadiyarajesh.notex.repository.NotesRepository import com.hadiyarajesh.notex.utility.TAG import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.SharingStarted.Companion.WhileSubscribed +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.stateIn +import kotlinx.coroutines.launch import javax.inject.Inject @HiltViewModel @@ -14,4 +23,41 @@ class NotesViewModel @Inject constructor( init { Log.i(TAG, "${this.javaClass.name} initialized") } + + lateinit var notesFlow: StateFlow> + + + + init { + getAllNotes() + } + + private fun getAllNotes() { + viewModelScope.launch { + notesFlow = notesRepository.allNotes().stateIn( + scope = viewModelScope, + started = WhileSubscribed(5000), + initialValue = emptyList() + ) + } + } + + fun add(note: Note) { + viewModelScope.launch { + notesRepository.add(note) + } + } + + fun update(note: Note) { + viewModelScope.launch { + notesRepository.update(note) + } + } + + fun remove(note: Note) { + viewModelScope.launch { + notesRepository.remove(note) + } + } + }