| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,92 @@ |
| 1 |
+/* |
|
| 2 |
+ * Copyright (c) 2023 Samson Achiaga |
|
| 3 |
+ * |
|
| 4 |
+ * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 5 |
+ * you may not use this file except in compliance with the License. |
|
| 6 |
+ * You may obtain a copy of the License at |
|
| 7 |
+ * |
|
| 8 |
+ * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 9 |
+ * |
|
| 10 |
+ * Unless required by applicable law or agreed to in writing, software |
|
| 11 |
+ * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 12 |
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 13 |
+ * See the License for the specific language governing permissions and |
|
| 14 |
+ * limitations under the License. |
|
| 15 |
+ */ |
|
| 16 |
+ |
|
| 17 |
+package com.certified.audionote.ui |
|
| 18 |
+ |
|
| 19 |
+import androidx.lifecycle.ViewModel |
|
| 20 |
+import androidx.lifecycle.viewModelScope |
|
| 21 |
+import com.certified.audionote.model.Note |
|
| 22 |
+import com.certified.audionote.repository.Repository |
|
| 23 |
+import com.certified.audionote.utils.ReminderAvailableState |
|
| 24 |
+import com.certified.audionote.utils.ReminderCompletionState |
|
| 25 |
+import com.certified.audionote.utils.UIState |
|
| 26 |
+import dagger.hilt.android.lifecycle.HiltViewModel |
|
| 27 |
+import kotlinx.coroutines.Dispatchers |
|
| 28 |
+import kotlinx.coroutines.flow.MutableStateFlow |
|
| 29 |
+import kotlinx.coroutines.flow.asStateFlow |
|
| 30 |
+import kotlinx.coroutines.launch |
|
| 31 |
+import javax.inject.Inject |
|
| 32 |
+ |
|
| 33 |
+@HiltViewModel |
|
| 34 |
+class NotesViewModel @Inject constructor(private val repository: Repository) : ViewModel() {
|
|
| 35 |
+ |
|
| 36 |
+ val _reminderAvailableState = MutableStateFlow(ReminderAvailableState.NO_REMINDER) |
|
| 37 |
+ val reminderAvailableState = _reminderAvailableState.asStateFlow() |
|
| 38 |
+ |
|
| 39 |
+ val _reminderCompletionState = MutableStateFlow(ReminderCompletionState.ONGOING) |
|
| 40 |
+ val reminderCompletionState = _reminderCompletionState.asStateFlow() |
|
| 41 |
+ |
|
| 42 |
+ private val _uiState = MutableStateFlow(UIState.LOADING) |
|
| 43 |
+ val uiState = _uiState.asStateFlow() |
|
| 44 |
+ |
|
| 45 |
+ private val _notes = MutableStateFlow<List<Note>?>(null) |
|
| 46 |
+ val notes = _notes.asStateFlow() |
|
| 47 |
+ |
|
| 48 |
+ private val _note = MutableStateFlow<Note?>(null) |
|
| 49 |
+ val note = _note.asStateFlow() |
|
| 50 |
+ |
|
| 51 |
+ init {
|
|
| 52 |
+ getNotes() |
|
| 53 |
+ } |
|
| 54 |
+ |
|
| 55 |
+ private fun getNotes() {
|
|
| 56 |
+ viewModelScope.launch {
|
|
| 57 |
+ repository.allNotes.collect {
|
|
| 58 |
+ _notes.value = it |
|
| 59 |
+ if (it.isNullOrEmpty()) |
|
| 60 |
+ _uiState.value = UIState.EMPTY |
|
| 61 |
+ else |
|
| 62 |
+ _uiState.value = UIState.HAS_DATA |
|
| 63 |
+ } |
|
| 64 |
+ } |
|
| 65 |
+ } |
|
| 66 |
+ |
|
| 67 |
+ fun insertNote(note: Note) {
|
|
| 68 |
+ viewModelScope.launch(Dispatchers.IO) {
|
|
| 69 |
+ repository.insertNote(note) |
|
| 70 |
+ } |
|
| 71 |
+ } |
|
| 72 |
+ |
|
| 73 |
+ fun updateNote(note: Note) {
|
|
| 74 |
+ viewModelScope.launch(Dispatchers.IO) {
|
|
| 75 |
+ repository.updateNote(note) |
|
| 76 |
+ } |
|
| 77 |
+ } |
|
| 78 |
+ |
|
| 79 |
+ fun deleteNote(note: Note) {
|
|
| 80 |
+ viewModelScope.launch(Dispatchers.IO) {
|
|
| 81 |
+ repository.deleteNote(note) |
|
| 82 |
+ } |
|
| 83 |
+ } |
|
| 84 |
+ |
|
| 85 |
+ fun getNote(noteId: Int) {
|
|
| 86 |
+ viewModelScope.launch(Dispatchers.IO) {
|
|
| 87 |
+ repository.getNote(noteId).collect {
|
|
| 88 |
+ _note.value = it |
|
| 89 |
+ } |
|
| 90 |
+ } |
|
| 91 |
+ } |
|
| 92 |
+} |
|
| 0 | 93 |
\ No newline at end of file |