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
  • Loading branch information
virunarala committed Sep 3, 2022
1 parent d9c1f45 commit 935db3c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
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"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.NoteX">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Note>
suspend fun getNotes(): Flow<List<Note>>

@Delete
fun deleteNote(note: Note)
suspend fun deleteNote(note: Note): Note
}
Original file line number Diff line number Diff line change
@@ -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<List<Note>> = noteDao.getNotes()
}
46 changes: 46 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 @@ -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
Expand All @@ -14,4 +23,41 @@ class NotesViewModel @Inject constructor(
init {
Log.i(TAG, "${this.javaClass.name} initialized")
}

lateinit var notesFlow: StateFlow<List<Note>>



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)
}
}

}

0 comments on commit 935db3c

Please sign in to comment.