Skip to content

Commit 73f87b1

Browse files
committed
Explicit backing fields
1 parent 76c51ee commit 73f87b1

17 files changed

Lines changed: 181 additions & 189 deletions

File tree

core/presentation/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ kotlin {
6767
wasmJsTest.dependencies {
6868
}
6969
}
70-
compilerOptions.freeCompilerArgs.add("-Xexpect-actual-classes")
70+
compilerOptions.freeCompilerArgs.addAll("-Xexpect-actual-classes", "-Xexplicit-backing-fields")
7171
}
7272

7373
dependencies {

core/presentation/src/commonMain/kotlin/com/softartdev/notedelight/presentation/files/FilesViewModel.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@ import kotlinx.coroutines.launch
1212

1313
class FilesViewModel(private val fileRepo: FileRepo) : ViewModel() {
1414
private val logger = Logger.withTag(this@FilesViewModel::class.simpleName.toString())
15-
private val mutableStateFlow: MutableStateFlow<FilesResult> = MutableStateFlow(
16-
value = FilesResult.Loading
17-
)
18-
val resultStateFlow: StateFlow<FilesResult> = mutableStateFlow
15+
16+
val resultStateFlow: StateFlow<FilesResult>
17+
field: MutableStateFlow<FilesResult> = MutableStateFlow(value = FilesResult.Loading)
18+
1919
var job: Job? = null
2020

2121
fun updateFiles() {
2222
job?.cancel()
2323
job = viewModelScope.launch {
2424
fileRepo.fileListFlow
2525
.map(FilesResult::Success)
26-
.collect(mutableStateFlow::emit)
26+
.collect(resultStateFlow::emit)
2727
}
2828
try {
2929
fileRepo.goToStartPath()
3030
} catch (e: Throwable) {
3131
logger.e(e) { "Error goToStartPath" }
32-
mutableStateFlow.value = FilesResult.Error(e.message)
32+
resultStateFlow.value = FilesResult.Error(e.message)
3333
}
3434
}
3535

@@ -38,7 +38,7 @@ class FilesViewModel(private val fileRepo: FileRepo) : ViewModel() {
3838
fileRepo.goTo(fileName)
3939
} catch (e: Throwable) {
4040
logger.e(e) { "Error onItemClicked: $fileName" }
41-
mutableStateFlow.value = FilesResult.Error(e.message)
41+
resultStateFlow.value = FilesResult.Error(e.message)
4242
}
4343
}
4444
}

core/presentation/src/commonMain/kotlin/com/softartdev/notedelight/presentation/main/MainViewModel.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ class MainViewModel(
2525
private val coroutineDispatchers: CoroutineDispatchers,
2626
) : ViewModel() {
2727
private val logger = Logger.withTag(this@MainViewModel::class.simpleName.toString())
28-
private val mutableStateFlow: MutableStateFlow<NoteListResult> = MutableStateFlow(
29-
value = NoteListResult.Loading
30-
)
31-
val stateFlow: StateFlow<NoteListResult> = mutableStateFlow
28+
29+
val stateFlow: StateFlow<NoteListResult>
30+
field: MutableStateFlow<NoteListResult> = MutableStateFlow(value = NoteListResult.Loading)
3231

3332
private var job: Job? = null
3433

@@ -70,18 +69,18 @@ class MainViewModel(
7069
fun updateNotes() {
7170
job?.cancel()
7271
try {
73-
mutableStateFlow.value = NoteListResult.Loading
72+
stateFlow.value = NoteListResult.Loading
7473
val pagingDataFlow: Flow<PagingData<Note>> = safeRepo.noteDAO.pagingDataFlow
7574
.cachedIn(viewModelScope)
76-
mutableStateFlow.value = NoteListResult.Success(result = pagingDataFlow, selectedId = null)
75+
stateFlow.value = NoteListResult.Success(result = pagingDataFlow, selectedId = null)
7776
} catch (throwable: Throwable) {
7877
handleError("Error loading notes", throwable)
7978
}
8079
job = viewModelScope.launch {
8180
adaptiveInteractor.selectedNoteIdStateFlow.collect { selectedId: Long? ->
82-
val currentState = mutableStateFlow.value
81+
val currentState = stateFlow.value
8382
if (currentState is NoteListResult.Success) {
84-
mutableStateFlow.value = currentState.copy(selectedId = selectedId)
83+
stateFlow.value = currentState.copy(selectedId = selectedId)
8584
}
8685
}
8786
}
@@ -92,7 +91,7 @@ class MainViewModel(
9291
if (isDbError(throwable)) viewModelScope.launch(coroutineDispatchers.main) {
9392
router.navigateClearingBackStack(AppNavGraph.Splash)
9493
}
95-
mutableStateFlow.value = NoteListResult.Error(throwable.message)
94+
stateFlow.value = NoteListResult.Error(throwable.message)
9695
}
9796

9897
private fun isDbError(throwable: Throwable): Boolean {

core/presentation/src/commonMain/kotlin/com/softartdev/notedelight/presentation/note/NoteViewModel.kt

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

core/presentation/src/commonMain/kotlin/com/softartdev/notedelight/presentation/settings/LanguageViewModel.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ import com.softartdev.notedelight.model.LanguageEnum
66
import com.softartdev.notedelight.navigation.Router
77
import kotlinx.coroutines.flow.MutableStateFlow
88
import kotlinx.coroutines.flow.StateFlow
9-
import kotlinx.coroutines.flow.asStateFlow
109

1110
class LanguageViewModel(
1211
private val router: Router,
1312
private val localeInteractor: LocaleInteractor,
1413
) : ViewModel() {
15-
private val mutableStateFlow = MutableStateFlow(localeInteractor.languageEnum)
16-
val selectedLanguage: StateFlow<LanguageEnum> = mutableStateFlow.asStateFlow()
17-
14+
val selectedLanguage: StateFlow<LanguageEnum>
15+
field = MutableStateFlow(localeInteractor.languageEnum)
16+
1817
fun selectLanguage(language: LanguageEnum) {
1918
localeInteractor.languageEnum = language
20-
mutableStateFlow.value = language
19+
selectedLanguage.value = language
2120
}
22-
21+
2322
fun dismiss() = router.popBackStack()
2423
}

core/presentation/src/commonMain/kotlin/com/softartdev/notedelight/presentation/settings/SettingsCategoriesViewModel.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ class SettingsCategoriesViewModel(
1616
private val router: Router,
1717
private val adaptiveInteractor: AdaptiveInteractor,
1818
) : ViewModel() {
19-
private val mutableStateFlow: MutableStateFlow<SettingsCategoriesResult> = MutableStateFlow(
20-
value = SettingsCategoriesResult()
21-
)
22-
val stateFlow: StateFlow<SettingsCategoriesResult> = mutableStateFlow
19+
20+
val stateFlow: StateFlow<SettingsCategoriesResult>
21+
field = MutableStateFlow(value = SettingsCategoriesResult())
2322

2423
private var job: Job? = null
2524

@@ -40,19 +39,19 @@ class SettingsCategoriesViewModel(
4039
}
4140

4241
private fun refresh() = viewModelScope.launch {
43-
mutableStateFlow.update(SettingsCategoriesResult::showLoading)
42+
stateFlow.update(SettingsCategoriesResult::showLoading)
4443
startCollectingSelection()
45-
mutableStateFlow.update { result ->
44+
stateFlow.update { result ->
4645
result.copy(selectedCategoryId = adaptiveInteractor.selectedSettingsCategoryIdStateFlow.value)
4746
}
48-
mutableStateFlow.update(SettingsCategoriesResult::hideLoading)
47+
stateFlow.update(SettingsCategoriesResult::hideLoading)
4948
}
5049

5150
private fun startCollectingSelection() {
5251
job?.cancel()
5352
job = viewModelScope.launch {
5453
adaptiveInteractor.selectedSettingsCategoryIdStateFlow.collect { selectedId: Long? ->
55-
mutableStateFlow.update { result -> result.copy(selectedCategoryId = selectedId) }
54+
stateFlow.update { result -> result.copy(selectedCategoryId = selectedId) }
5655
}
5756
}
5857
}

0 commit comments

Comments
 (0)