@@ -6,7 +6,6 @@ package at.bitfire.davdroid.ui.setup
6
6
7
7
import android.accounts.Account
8
8
import android.content.Context
9
- import androidx.compose.runtime.derivedStateOf
10
9
import androidx.compose.runtime.getValue
11
10
import androidx.compose.runtime.mutableStateOf
12
11
import androidx.compose.runtime.setValue
@@ -24,9 +23,12 @@ import dagger.hilt.android.lifecycle.HiltViewModel
24
23
import dagger.hilt.android.qualifiers.ApplicationContext
25
24
import kotlinx.coroutines.Dispatchers
26
25
import kotlinx.coroutines.Job
26
+ import kotlinx.coroutines.flow.MutableStateFlow
27
27
import kotlinx.coroutines.flow.SharingStarted
28
+ import kotlinx.coroutines.flow.combine
28
29
import kotlinx.coroutines.flow.map
29
30
import kotlinx.coroutines.flow.stateIn
31
+ import kotlinx.coroutines.flow.update
30
32
import kotlinx.coroutines.launch
31
33
import kotlinx.coroutines.runInterruptible
32
34
import kotlinx.coroutines.withContext
@@ -246,66 +248,75 @@ class LoginScreenModel @AssistedInject constructor(
246
248
else
247
249
null
248
250
}
249
- .stateIn(viewModelScope, SharingStarted .Eagerly , null )
250
251
251
252
// backing field that is combined with dynamic content for the resulting UI State
252
- private var _accountDetailsUiState by mutableStateOf(AccountDetailsUiState ())
253
- val accountDetailsUiState by derivedStateOf {
254
- val method = forcedGroupMethod.value
255
-
253
+ private var _accountDetailsUiState = MutableStateFlow (AccountDetailsUiState ())
254
+ val accountDetailsUiState = combine(_accountDetailsUiState , forcedGroupMethod) { uiState, method ->
256
255
// set group type to read-only if group method is forced
257
- var combinedState = _accountDetailsUiState .copy(groupMethodReadOnly = method != null )
256
+ var combinedState = uiState .copy(groupMethodReadOnly = method != null )
258
257
259
258
// apply forced group method, if applicable
260
259
if (method != null )
261
260
combinedState = combinedState.copy(groupMethod = method)
262
261
263
262
combinedState
264
- }
263
+ }.stateIn(viewModelScope, SharingStarted . Lazily , _accountDetailsUiState .value)
265
264
266
265
fun updateAccountName (accountName : String ) {
267
- _accountDetailsUiState = _accountDetailsUiState .copy(
268
- accountName = accountName,
269
- accountNameExists = accountRepository.exists(accountName)
270
- )
266
+ _accountDetailsUiState .update { currentState ->
267
+ currentState.copy(
268
+ accountName = accountName,
269
+ accountNameExists = accountRepository.exists(accountName)
270
+ )
271
+ }
271
272
}
272
273
273
274
fun updateAccountNameAndEmails (accountName : String , emails : Set <String >) {
274
- _accountDetailsUiState = _accountDetailsUiState .copy(
275
- accountName = accountName,
276
- accountNameExists = accountRepository.exists(accountName),
277
- suggestedAccountNames = emails
278
- )
275
+ _accountDetailsUiState .update { currentState ->
276
+ currentState.copy(
277
+ accountName = accountName,
278
+ accountNameExists = accountRepository.exists(accountName),
279
+ suggestedAccountNames = emails
280
+ )
281
+ }
279
282
}
280
283
281
284
fun updateGroupMethod (groupMethod : GroupMethod ) {
282
- _accountDetailsUiState = _accountDetailsUiState .copy(groupMethod = groupMethod)
285
+ _accountDetailsUiState .update { currentState ->
286
+ currentState.copy(groupMethod = groupMethod)
287
+ }
283
288
}
284
289
285
290
fun resetCouldNotCreateAccount () {
286
- _accountDetailsUiState = _accountDetailsUiState .copy(couldNotCreateAccount = false )
291
+ _accountDetailsUiState .update { currentState ->
292
+ currentState.copy(couldNotCreateAccount = false )
293
+ }
287
294
}
288
295
289
296
fun createAccount () {
290
- _accountDetailsUiState = _accountDetailsUiState .copy(creatingAccount = true )
297
+ _accountDetailsUiState .update { currentState ->
298
+ currentState.copy(creatingAccount = true )
299
+ }
300
+
291
301
viewModelScope.launch {
292
302
val account = withContext(Dispatchers .Default ) {
293
303
accountRepository.create(
294
- accountDetailsUiState.accountName,
304
+ accountDetailsUiState.value. accountName,
295
305
loginInfo.credentials,
296
306
foundConfig!! ,
297
- accountDetailsUiState.groupMethod
307
+ accountDetailsUiState.value. groupMethod
298
308
)
299
309
}
300
310
301
- _accountDetailsUiState =
311
+ _accountDetailsUiState .update { currentState ->
302
312
if (account != null )
303
- accountDetailsUiState .copy(createdAccount = account)
313
+ currentState .copy(createdAccount = account)
304
314
else
305
- accountDetailsUiState .copy(
315
+ currentState .copy(
306
316
creatingAccount = false ,
307
317
couldNotCreateAccount = true
308
318
)
319
+ }
309
320
}
310
321
}
311
322
0 commit comments