-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91ca3a2
commit 641c4a5
Showing
3 changed files
with
24 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 18 additions & 4 deletions
22
app/src/main/java/com/example/plainolnotes4/MainViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,29 @@ | ||
package com.example.plainolnotes4 | ||
|
||
import android.app.Application | ||
import androidx.lifecycle.AndroidViewModel | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.example.plainolnotes4.data.AppDatabase | ||
import com.example.plainolnotes4.data.NoteEntity | ||
import com.example.plainolnotes4.data.SampleDataProvider | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
|
||
class MainViewModel : ViewModel() { | ||
class MainViewModel(app: Application) : AndroidViewModel(app) { | ||
|
||
val notesList = MutableLiveData<List<NoteEntity>>() | ||
private val database = AppDatabase.getInstance(app) | ||
val notesList = database?.noteDao()?.getAll() | ||
|
||
init { | ||
notesList.value = SampleDataProvider.getNotes() | ||
fun addSampleData() { | ||
viewModelScope.launch { | ||
withContext(Dispatchers.IO) { | ||
val sampleNotes = SampleDataProvider.getNotes() | ||
database?.noteDao()?.insertAll(sampleNotes) | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters