Improve some naming in feed package

This commit is contained in:
Shinokuni 2024-02-11 18:24:49 +01:00
parent f0ac2de8e4
commit e176cdbdb1
4 changed files with 34 additions and 57 deletions

View File

@ -56,7 +56,7 @@ data class UpdateFeedDialogState(
get() = accountType != null && !accountType.accountConfig!!.isFeedUrlEditable
}
data class AddUpdateFolderState(
data class FolderState(
val folder: Folder = Folder(),
val nameError: TextFieldError? = null,
) {

View File

@ -33,8 +33,8 @@ import cafe.adriel.voyager.navigator.tab.Tab
import cafe.adriel.voyager.navigator.tab.TabOptions
import com.readrops.app.compose.R
import com.readrops.app.compose.feeds.dialogs.AddFeedDialog
import com.readrops.app.compose.feeds.dialogs.AddUpdateFolderDialog
import com.readrops.app.compose.feeds.dialogs.FeedModalBottomSheet
import com.readrops.app.compose.feeds.dialogs.FolderDialog
import com.readrops.app.compose.feeds.dialogs.UpdateFeedDialog
import com.readrops.app.compose.util.components.Placeholder
import com.readrops.app.compose.util.components.TwoChoicesDialog
@ -116,14 +116,14 @@ object FeedTab : Tab {
}
DialogState.AddFolder -> {
AddUpdateFolderDialog(
FolderDialog(
viewModel = viewModel,
onDismiss = {
viewModel.closeDialog()
viewModel.resetAddFolderState()
viewModel.resetFolderState()
},
onValidate = {
viewModel.addFolderValidate()
viewModel.folderValidate()
}
)
}
@ -144,15 +144,15 @@ object FeedTab : Tab {
}
is DialogState.UpdateFolder -> {
AddUpdateFolderDialog(
FolderDialog(
updateFolder = true,
viewModel = viewModel,
onDismiss = {
viewModel.closeDialog()
viewModel.resetAddFolderState()
viewModel.resetFolderState()
},
onValidate = {
viewModel.updateFolderValidate()
viewModel.folderValidate(updateFolder = true)
}
)
}

View File

@ -12,6 +12,7 @@ import com.readrops.db.entities.Feed
import com.readrops.db.entities.Folder
import com.readrops.db.entities.account.Account
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
@ -21,6 +22,7 @@ import kotlinx.coroutines.launch
import org.koin.core.component.KoinComponent
import org.koin.core.component.get
@OptIn(ExperimentalCoroutinesApi::class)
class FeedViewModel(
database: Database,
private val getFoldersWithFeeds: GetFoldersWithFeeds,
@ -36,8 +38,8 @@ class FeedViewModel(
private val _updateFeedDialogState = MutableStateFlow(UpdateFeedDialogState())
val updateFeedDialogState = _updateFeedDialogState.asStateFlow()
private val _addFolderState = MutableStateFlow(AddUpdateFolderState())
val addFolderState = _addFolderState.asStateFlow()
private val _folderState = MutableStateFlow(FolderState())
val folderState = _folderState.asStateFlow()
init {
viewModelScope.launch(context = Dispatchers.IO) {
@ -101,7 +103,7 @@ class FeedViewModel(
}
if (state is DialogState.UpdateFolder) {
_addFolderState.update {
_folderState.update {
it.copy(
folder = state.folder
)
@ -135,11 +137,7 @@ class FeedViewModel(
}
fun setAddFeedDialogSelectedAccount(account: Account) {
_addFeedDialogState.update {
it.copy(
selectedAccount = account
)
}
_addFeedDialogState.update { it.copy(selectedAccount = account) }
}
fun addFeedDialogValidate() {
@ -267,64 +265,43 @@ class FeedViewModel(
// update feed
// add folder
// add/update folder
fun setFolderName(name: String) = _addFolderState.update {
fun setFolderName(name: String) = _folderState.update {
it.copy(
folder = it.folder.copy(name = name),
nameError = null,
)
}
fun addFolderValidate() {
val name = _addFolderState.value.name.orEmpty()
fun resetFolderState() = _folderState.update {
it.copy(
folder = Folder(),
nameError = null,
)
}
fun folderValidate(updateFolder: Boolean = false) {
val name = _folderState.value.name.orEmpty()
if (name.isEmpty()) {
_addFolderState.update { it.copy(nameError = TextFieldError.EmptyField) }
_folderState.update { it.copy(nameError = TextFieldError.EmptyField) }
return
}
viewModelScope.launch(Dispatchers.IO) {
repository?.addFolder(_addFolderState.value.folder.apply { accountId = currentAccount!!.id })
closeDialog()
resetAddFolderState()
}
}
fun resetAddFolderState() {
_addFolderState.update {
it.copy(
folder = Folder(),
nameError = null,
)
}
}
// add folder
// update folder
fun updateFolderValidate() {
val name = _addFolderState.value.name.orEmpty()
if (name.isEmpty()) {
_addFolderState.update {
it.copy(nameError = TextFieldError.EmptyField)
if (updateFolder) {
repository?.updateFolder(_folderState.value.folder)
} else {
repository?.addFolder(_folderState.value.folder.apply { accountId = currentAccount!!.id })
}
return
}
viewModelScope.launch(Dispatchers.IO) {
repository?.updateFolder(_addFolderState.value.folder)
closeDialog()
resetAddFolderState()
resetFolderState()
}
}
// update folder
// add/update folder
}

View File

@ -15,13 +15,13 @@ import com.readrops.app.compose.feeds.FeedViewModel
import com.readrops.app.compose.util.components.BaseDialog
@Composable
fun AddUpdateFolderDialog(
fun FolderDialog(
updateFolder: Boolean = false,
viewModel: FeedViewModel,
onDismiss: () -> Unit,
onValidate: () -> Unit
) {
val state by viewModel.addFolderState.collectAsStateWithLifecycle()
val state by viewModel.folderState.collectAsStateWithLifecycle()
BaseDialog(
title = if (updateFolder) "Update Folder" else "Add Folder",