From f57d39ec3bcf781f275fe457b5159e79949c6afc Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Thu, 11 Jul 2024 17:53:01 +0200 Subject: [PATCH] Extract state from AddFeedDialog --- .../com/readrops/app/feeds/FeedScreenModel.kt | 10 +++-- .../java/com/readrops/app/feeds/FeedState.kt | 5 ++- .../java/com/readrops/app/feeds/FeedTab.kt | 11 +++-- .../app/feeds/dialogs/AddFeedDialog.kt | 41 ++++++++----------- .../app/feeds/dialogs/UpdateFeedDialog.kt | 12 +++--- 5 files changed, 41 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/com/readrops/app/feeds/FeedScreenModel.kt b/app/src/main/java/com/readrops/app/feeds/FeedScreenModel.kt index 579703a6..bb2cf641 100644 --- a/app/src/main/java/com/readrops/app/feeds/FeedScreenModel.kt +++ b/app/src/main/java/com/readrops/app/feeds/FeedScreenModel.kt @@ -222,7 +222,11 @@ class FeedScreenModel( } fun setAddFeedDialogSelectedAccount(account: Account) { - _addFeedDialogState.update { it.copy(selectedAccount = account) } + _addFeedDialogState.update { it.copy(selectedAccount = account, isAccountDropDownExpanded = false) } + } + + fun setAccountDropDownExpanded(isExpanded: Boolean) { + _addFeedDialogState.update { it.copy(isAccountDropDownExpanded = isExpanded) } } fun addFeedDialogValidate() { @@ -319,9 +323,9 @@ class FeedScreenModel( //region Update feed - fun setAccountDropDownState(isExpanded: Boolean) { + fun setFolderDropDownState(isExpanded: Boolean) { _updateFeedDialogState.update { - it.copy(isAccountDropDownExpanded = isExpanded) + it.copy(isFolderDropDownExpanded = isExpanded) } } diff --git a/app/src/main/java/com/readrops/app/feeds/FeedState.kt b/app/src/main/java/com/readrops/app/feeds/FeedState.kt index a78dfc4f..74a2f4f7 100644 --- a/app/src/main/java/com/readrops/app/feeds/FeedState.kt +++ b/app/src/main/java/com/readrops/app/feeds/FeedState.kt @@ -35,7 +35,8 @@ data class AddFeedDialogState( val accounts: List = listOf(), val error: TextFieldError? = null, val exception: Exception? = null, - val isLoading: Boolean = false + val isLoading: Boolean = false, + val isAccountDropDownExpanded: Boolean = false ) { val isError: Boolean get() = error != null } @@ -49,7 +50,7 @@ data class UpdateFeedDialogState( val feedUrlError: TextFieldError? = null, val selectedFolder: Folder? = null, val folders: List = listOf(), - val isAccountDropDownExpanded: Boolean = false, + val isFolderDropDownExpanded: Boolean = false, val isFeedUrlReadOnly: Boolean = true, val exception: Exception? = null, val isLoading: Boolean = false diff --git a/app/src/main/java/com/readrops/app/feeds/FeedTab.kt b/app/src/main/java/com/readrops/app/feeds/FeedTab.kt index 1467c14e..64056853 100644 --- a/app/src/main/java/com/readrops/app/feeds/FeedTab.kt +++ b/app/src/main/java/com/readrops/app/feeds/FeedTab.kt @@ -231,15 +231,18 @@ object FeedTab : Tab { private fun FeedDialogs(state: FeedState, screenModel: FeedScreenModel) { val uriHandler = LocalUriHandler.current + val addFeedDialogState by screenModel.addFeedDialogState.collectAsStateWithLifecycle() val folderState by screenModel.folderState.collectAsStateWithLifecycle() when (val dialog = state.dialog) { is DialogState.AddFeed -> { AddFeedDialog( - screenModel = screenModel, - onDismiss = { - screenModel.closeDialog(DialogState.AddFeed) - }, + state = addFeedDialogState, + onValueChange = { screenModel.setAddFeedDialogURL(it) }, + onExpandChange = { screenModel.setAccountDropDownExpanded(it) }, + onAccountClick = { screenModel.setAddFeedDialogSelectedAccount(it) }, + onValidate = { screenModel.addFeedDialogValidate() }, + onDismiss = { screenModel.closeDialog(DialogState.AddFeed) }, ) } diff --git a/app/src/main/java/com/readrops/app/feeds/dialogs/AddFeedDialog.kt b/app/src/main/java/com/readrops/app/feeds/dialogs/AddFeedDialog.kt index 0a74e94a..f9128494 100644 --- a/app/src/main/java/com/readrops/app/feeds/dialogs/AddFeedDialog.kt +++ b/app/src/main/java/com/readrops/app/feeds/dialogs/AddFeedDialog.kt @@ -14,37 +14,33 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp -import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.readrops.app.R import com.readrops.app.account.selection.adaptiveIconPainterResource -import com.readrops.app.feeds.FeedScreenModel +import com.readrops.app.feeds.AddFeedDialogState import com.readrops.app.util.ErrorMessage import com.readrops.app.util.components.LoadingTextButton import com.readrops.app.util.components.dialog.BaseDialog import com.readrops.app.util.theme.LargeSpacer import com.readrops.app.util.theme.MediumSpacer import com.readrops.app.util.theme.ShortSpacer +import com.readrops.db.entities.account.Account @OptIn(ExperimentalMaterial3Api::class) @Composable fun AddFeedDialog( - screenModel: FeedScreenModel, - onDismiss: () -> Unit, + state: AddFeedDialogState, + onValueChange: (String) -> Unit, + onExpandChange: (Boolean) -> Unit, + onAccountClick: (Account) -> Unit, + onValidate: () -> Unit, + onDismiss: () -> Unit ) { - val state by screenModel.addFeedDialogState.collectAsStateWithLifecycle() - - var isExpanded by remember { mutableStateOf(false) } - BaseDialog( title = stringResource(R.string.add_feed_item), icon = painterResource(id = R.drawable.ic_rss_feed_grey), @@ -53,12 +49,12 @@ fun AddFeedDialog( OutlinedTextField( value = state.url, label = { Text(text = stringResource(id = R.string.url)) }, - onValueChange = { screenModel.setAddFeedDialogURL(it) }, + onValueChange = { onValueChange(it) }, singleLine = true, trailingIcon = { if (state.url.isNotEmpty()) { IconButton( - onClick = { screenModel.setAddFeedDialogURL("") } + onClick = { onValueChange("") } ) { Icon( imageVector = Icons.Default.Clear, @@ -74,19 +70,18 @@ fun AddFeedDialog( ShortSpacer() ExposedDropdownMenuBox( - expanded = isExpanded, - onExpandedChange = { isExpanded = !isExpanded } + expanded = state.isAccountDropDownExpanded, + onExpandedChange = { onExpandChange(!state.isAccountDropDownExpanded) } ) { ExposedDropdownMenu( - expanded = isExpanded, - onDismissRequest = { isExpanded = false } + expanded = state.isAccountDropDownExpanded, + onDismissRequest = { onExpandChange(false) } ) { for (account in state.accounts) { DropdownMenuItem( text = { Text(text = account.accountName!!) }, onClick = { - isExpanded = false - screenModel.setAddFeedDialogSelectedAccount(account) + onAccountClick(account) }, leadingIcon = { Image( @@ -106,7 +101,7 @@ fun AddFeedDialog( readOnly = true, onValueChange = {}, trailingIcon = { - ExposedDropdownMenuDefaults.TrailingIcon(expanded = isExpanded) + ExposedDropdownMenuDefaults.TrailingIcon(expanded = state.isAccountDropDownExpanded) }, leadingIcon = { Image( @@ -125,7 +120,7 @@ fun AddFeedDialog( MediumSpacer() Text( - text = ErrorMessage.get(state.exception!!, LocalContext.current), + text = ErrorMessage.get(state.exception, LocalContext.current), color = MaterialTheme.colorScheme.error, textAlign = TextAlign.Center ) @@ -136,7 +131,7 @@ fun AddFeedDialog( LoadingTextButton( text = stringResource(id = R.string.validate), isLoading = state.isLoading, - onClick = { screenModel.addFeedDialogValidate() }, + onClick = onValidate, ) } } diff --git a/app/src/main/java/com/readrops/app/feeds/dialogs/UpdateFeedDialog.kt b/app/src/main/java/com/readrops/app/feeds/dialogs/UpdateFeedDialog.kt index aa042d7e..e2dc7d3e 100644 --- a/app/src/main/java/com/readrops/app/feeds/dialogs/UpdateFeedDialog.kt +++ b/app/src/main/java/com/readrops/app/feeds/dialogs/UpdateFeedDialog.kt @@ -78,19 +78,19 @@ fun UpdateFeedDialog( MediumSpacer() ExposedDropdownMenuBox( - expanded = state.isAccountDropDownExpanded && state.hasFolders, - onExpandedChange = { viewModel.setAccountDropDownState(state.isAccountDropDownExpanded.not()) } + expanded = state.isFolderDropDownExpanded && state.hasFolders, + onExpandedChange = { viewModel.setFolderDropDownState(state.isFolderDropDownExpanded.not()) } ) { ExposedDropdownMenu( - expanded = state.isAccountDropDownExpanded && state.hasFolders, - onDismissRequest = { viewModel.setAccountDropDownState(false) } + expanded = state.isFolderDropDownExpanded && state.hasFolders, + onDismissRequest = { viewModel.setFolderDropDownState(false) } ) { for (folder in state.folders) { DropdownMenuItem( text = { Text(text = folder.name!!) }, onClick = { viewModel.setSelectedFolder(folder) - viewModel.setAccountDropDownState(false) + viewModel.setFolderDropDownState(false) }, leadingIcon = { Icon( @@ -108,7 +108,7 @@ fun UpdateFeedDialog( enabled = state.hasFolders, onValueChange = {}, trailingIcon = { - ExposedDropdownMenuDefaults.TrailingIcon(expanded = state.isAccountDropDownExpanded) + ExposedDropdownMenuDefaults.TrailingIcon(expanded = state.isFolderDropDownExpanded) }, leadingIcon = { if (state.selectedFolder != null) {