@@ -34,11 +34,11 @@ class NoteViewModel(
3434 private val coroutineDispatchers : CoroutineDispatchers ,
3535) : ViewModel() {
3636 private val logger = Logger .withTag(this @NoteViewModel::class .simpleName.toString())
37- private val mutableStateFlow: MutableStateFlow <NoteResult > = MutableStateFlow (
38- value = NoteResult (checkSaveChangeChannel = adaptiveInteractor.checkSaveChangeChannel)
39- )
40- val stateFlow: StateFlow <NoteResult > = mutableStateFlow
4137
38+ val stateFlow: StateFlow <NoteResult >
39+ field = MutableStateFlow (
40+ value = NoteResult (checkSaveChangeChannel = adaptiveInteractor.checkSaveChangeChannel)
41+ )
4242 private var noteId: Long
4343 set(value) { adaptiveInteractor.selectedNoteIdStateFlow.value = value }
4444 get() = requireNotNull(adaptiveInteractor.selectedNoteIdStateFlow.value)
@@ -51,7 +51,7 @@ class NoteViewModel(
5151 adaptiveInteractor.selectedNoteIdStateFlow.collect { selectedNoteId: Long? ->
5252 logger.d { " Collected note id = $selectedNoteId " }
5353 when (selectedNoteId) {
54- null -> mutableStateFlow .update { result -> result.copy(note = null ) }
54+ null -> stateFlow .update { result -> result.copy(note = null ) }
5555 else -> createOrLoadNote()
5656 }
5757 }
@@ -72,7 +72,7 @@ class NoteViewModel(
7272 }
7373
7474 private fun createNote () = viewModelScope.launch {
75- mutableStateFlow .update(NoteResult ::showLoading)
75+ stateFlow .update(NoteResult ::showLoading)
7676 try {
7777 val id: Long = withContext(coroutineDispatchers.io) {
7878 createNoteUseCase()
@@ -82,29 +82,29 @@ class NoteViewModel(
8282 } catch (e: Throwable ) {
8383 handleError(e) { " Error creating note" }
8484 } finally {
85- mutableStateFlow .update(NoteResult ::hideLoading)
85+ stateFlow .update(NoteResult ::hideLoading)
8686 }
8787 }
8888
8989 private fun loadNote () = viewModelScope.launch {
90- mutableStateFlow .update(NoteResult ::showLoading)
90+ stateFlow .update(NoteResult ::showLoading)
9191 try {
9292 val note = withContext(coroutineDispatchers.io) {
9393 noteDAO.load(noteId)
9494 }
9595 logger.d { " Loaded note with id = $noteId " }
96- mutableStateFlow .update { result -> result.copy(note = note) }
96+ stateFlow .update { result -> result.copy(note = note) }
9797 } catch (e: Throwable ) {
9898 handleError(e) { " Error loading note" }
9999 } finally {
100- mutableStateFlow .update(NoteResult ::hideLoading)
100+ stateFlow .update(NoteResult ::hideLoading)
101101 }
102102 }
103103
104104 private fun saveNote (text : String ) = viewModelScope.launch {
105- mutableStateFlow .update(NoteResult ::showLoading)
105+ stateFlow .update(NoteResult ::showLoading)
106106 try {
107- var title: String? = mutableStateFlow .value.note?.title
107+ var title: String? = stateFlow .value.note?.title
108108 if (title.isNullOrEmpty() && text.isEmpty()) {
109109 snackbarInteractor.showMessage(SnackbarMessage .Resource (SnackbarTextResource .EMPTY ))
110110 } else {
@@ -113,7 +113,7 @@ class NoteViewModel(
113113 saveNoteUseCase(noteId, title, text)
114114 }
115115 logger.d { " Saved note with id=$noteId " }
116- mutableStateFlow .update { result: NoteResult ->
116+ stateFlow .update { result: NoteResult ->
117117 result.copy(note = result.note?.copy(title = title, text = text))
118118 }
119119 snackbarInteractor.showMessage(SnackbarMessage .Resource (
@@ -124,27 +124,27 @@ class NoteViewModel(
124124 } catch (e: Throwable ) {
125125 handleError(e) { " Error saving note" }
126126 } finally {
127- mutableStateFlow .update(NoteResult ::hideLoading)
127+ stateFlow .update(NoteResult ::hideLoading)
128128 }
129129 }
130130
131131 private fun editTitle () = viewModelScope.launch {
132- mutableStateFlow .update(NoteResult ::showLoading)
132+ stateFlow .update(NoteResult ::showLoading)
133133 try {
134134 subscribeToEditTitle()
135135 router.navigate(route = AppNavGraph .EditTitleDialog (noteId = noteId))
136136 } catch (e: Throwable ) {
137137 handleError(e) { " Error navigating to edit title dialog" }
138138 } finally {
139- mutableStateFlow .update(NoteResult ::hideLoading)
139+ stateFlow .update(NoteResult ::hideLoading)
140140 }
141141 }
142142
143143 private fun checkSaveChange (text : String ) = viewModelScope.launch {
144- mutableStateFlow .update(NoteResult ::showLoading)
144+ stateFlow .update(NoteResult ::showLoading)
145145 try {
146146 val title: String = createTitleIfNeed(text)
147- mutableStateFlow .update { result: NoteResult ->
147+ stateFlow .update { result: NoteResult ->
148148 result.copy(note = result.note?.copy(title = title, text = text))
149149 }
150150 val changed: Boolean = isChanged(noteId, title, text)
@@ -157,12 +157,12 @@ class NoteViewModel(
157157 } catch (e: Throwable ) {
158158 handleError(e) { " Error checking save changes" }
159159 } finally {
160- mutableStateFlow .update(NoteResult ::hideLoading)
160+ stateFlow .update(NoteResult ::hideLoading)
161161 }
162162 }
163163
164164 private fun showSaveChangesDialog (text : String ) = viewModelScope.launch {
165- mutableStateFlow .update(NoteResult ::showLoading)
165+ stateFlow .update(NoteResult ::showLoading)
166166 try {
167167 router.navigate(route = AppNavGraph .SaveChangesDialog )
168168 logger.d { " Subscribe to save note dialog channel" }
@@ -177,12 +177,12 @@ class NoteViewModel(
177177 } catch (e: Throwable ) {
178178 handleError(e) { " Error subscribing to save note dialog channel" }
179179 } finally {
180- mutableStateFlow .update(NoteResult ::hideLoading)
180+ stateFlow .update(NoteResult ::hideLoading)
181181 }
182182 }
183183
184184 private fun saveNoteAndNavBack (text : String ) = viewModelScope.launch {
185- mutableStateFlow .update(NoteResult ::showLoading)
185+ stateFlow .update(NoteResult ::showLoading)
186186 try {
187187 val title: String = createTitleIfNeed(text)
188188 saveNoteUseCase(noteId, title, text)
@@ -191,12 +191,12 @@ class NoteViewModel(
191191 } catch (e: Throwable ) {
192192 handleError(e) { " Error saving note and navigating back" }
193193 } finally {
194- mutableStateFlow .update(NoteResult ::hideLoading)
194+ stateFlow .update(NoteResult ::hideLoading)
195195 }
196196 }
197197
198198 private fun doNotSaveAndNavBack () = viewModelScope.launch {
199- mutableStateFlow .update(NoteResult ::showLoading)
199+ stateFlow .update(NoteResult ::showLoading)
200200 try {
201201 val noteIsEmpty: Boolean = isEmpty(noteId)
202202 if (noteIsEmpty) {
@@ -208,12 +208,12 @@ class NoteViewModel(
208208 } catch (e: Throwable ) {
209209 handleError(e) { " Error not saving note and navigating back" }
210210 } finally {
211- mutableStateFlow .update(NoteResult ::hideLoading)
211+ stateFlow .update(NoteResult ::hideLoading)
212212 }
213213 }
214214
215215 private fun subscribeToDeleteNote () = viewModelScope.launch {
216- mutableStateFlow .update(NoteResult ::showLoading)
216+ stateFlow .update(NoteResult ::showLoading)
217217 try {
218218 router.navigate(route = AppNavGraph .DeleteNoteDialog )
219219 val doDelete: Boolean = withContext(coroutineDispatchers.io) {
@@ -228,7 +228,7 @@ class NoteViewModel(
228228 } catch (e: Throwable ) {
229229 handleError(e) { " Error subscribing to delete note dialog channel" }
230230 } finally {
231- mutableStateFlow .update(NoteResult ::hideLoading)
231+ stateFlow .update(NoteResult ::hideLoading)
232232 }
233233 }
234234
@@ -247,25 +247,25 @@ class NoteViewModel(
247247 }
248248
249249 private fun subscribeToEditTitle () = viewModelScope.launch {
250- mutableStateFlow .update(NoteResult ::showLoading)
250+ stateFlow .update(NoteResult ::showLoading)
251251 try {
252252 val title: String? = withContext(coroutineDispatchers.io) {
253253 UpdateTitleUseCase .dialogChannel.receive()
254254 }
255255 if (title.isNullOrEmpty()) return @launch
256256
257- mutableStateFlow .update { result: NoteResult ->
257+ stateFlow .update { result: NoteResult ->
258258 val updatedNote = result.note?.copy(title = title)
259259 result.copy(note = updatedNote)
260260 }
261261 } catch (e: Throwable ) {
262262 handleError(e) { " Error subscribing to edit title dialog channel" }
263263 } finally {
264- mutableStateFlow .update(NoteResult ::hideLoading)
264+ stateFlow .update(NoteResult ::hideLoading)
265265 }
266266 }
267267
268- private fun createTitleIfNeed (text : String ): String = mutableStateFlow .value.note?.title
268+ private fun createTitleIfNeed (text : String ): String = stateFlow .value.note?.title
269269 ?.takeIf (String ::isNotEmpty)
270270 ? : createTitle(text)
271271
@@ -301,7 +301,7 @@ class NoteViewModel(
301301 }
302302
303303 @VisibleForTesting
304- fun resetResultState (noteId : Long = 0L) = mutableStateFlow .update { noteResult ->
304+ fun resetResultState (noteId : Long = 0L) = stateFlow .update { noteResult ->
305305 this @NoteViewModel.noteId = noteId
306306 return @update noteResult.copy(loading = false , note = null )
307307 }
0 commit comments