Refactor(shopping): Remove unused code in ShoppingListsViewModel
This commit removes several unused functions and a class from ShoppingListsViewModel.kt to clean up the codebase. - Removed unused functions: - deleteItem - moveItemToTop - moveItemToBottom - moveItemUp - moveItemDown - Removed unused class ShoppingListItemDetails and its associated functions toShoppingListItem and isValid.
This commit is contained in:
@@ -110,8 +110,7 @@ class ShoppingListsViewModel(private val noteshopRepository: NoteshopRepository)
|
||||
private val _listDetails = MutableStateFlow(ShoppingListDetails())
|
||||
val listDetails: StateFlow<ShoppingListDetails> = _listDetails.asStateFlow()
|
||||
|
||||
private val _itemDetails = MutableStateFlow(ShoppingListItemDetails())
|
||||
val itemDetails: StateFlow<ShoppingListItemDetails> = _itemDetails.asStateFlow()
|
||||
|
||||
|
||||
fun updateListDetails(list: ShoppingList) {
|
||||
_listDetails.value = ShoppingListDetails(
|
||||
@@ -147,19 +146,9 @@ class ShoppingListsViewModel(private val noteshopRepository: NoteshopRepository)
|
||||
_listDetails.value = ShoppingListDetails()
|
||||
}
|
||||
|
||||
fun updateItemDetails(item: ShoppingListItem) {
|
||||
_itemDetails.value = ShoppingListItemDetails(
|
||||
id = item.id,
|
||||
name = item.name,
|
||||
isChecked = item.isChecked,
|
||||
listId = item.listId,
|
||||
displayOrder = item.displayOrder
|
||||
)
|
||||
}
|
||||
|
||||
fun updateItemDetails(itemDetails: ShoppingListItemDetails) {
|
||||
_itemDetails.value = itemDetails
|
||||
}
|
||||
|
||||
|
||||
|
||||
suspend fun saveShoppingListItem(item: ShoppingListItem) {
|
||||
if (item.name.isNotBlank()) {
|
||||
@@ -202,13 +191,9 @@ class ShoppingListsViewModel(private val noteshopRepository: NoteshopRepository)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun deleteItem(item: ShoppingListItem) {
|
||||
noteshopRepository.deleteShoppingListItem(item)
|
||||
}
|
||||
|
||||
fun resetItemDetails(listId: Int) {
|
||||
_itemDetails.value = ShoppingListItemDetails(listId = listId)
|
||||
}
|
||||
|
||||
|
||||
|
||||
fun getShoppingListWithItemsStream(listId: Int): Flow<ShoppingListWithItems?> {
|
||||
return noteshopRepository.getShoppingListWithItemsStream(listId).map { listWithItems ->
|
||||
@@ -244,65 +229,13 @@ class ShoppingListsViewModel(private val noteshopRepository: NoteshopRepository)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun moveItemToTop(listId: Int, itemToMove: ShoppingListItem) {
|
||||
noteshopRepository.getShoppingListWithItemsStream(listId).firstOrNull()?.let { listWithItems ->
|
||||
val currentItems = listWithItems.items.toMutableList()
|
||||
val index = currentItems.indexOfFirst { it.id == itemToMove.id }
|
||||
if (index > 0) {
|
||||
currentItems.removeAt(index)
|
||||
currentItems.add(0, itemToMove)
|
||||
val updatedItems = currentItems.mapIndexed { idx, item ->
|
||||
item.copy(displayOrder = idx)
|
||||
}
|
||||
noteshopRepository.updateShoppingListItems(updatedItems)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun moveItemToBottom(listId: Int, itemToMove: ShoppingListItem) {
|
||||
noteshopRepository.getShoppingListWithItemsStream(listId).firstOrNull()?.let { listWithItems ->
|
||||
val currentItems = listWithItems.items.toMutableList()
|
||||
val index = currentItems.indexOfFirst { it.id == itemToMove.id }
|
||||
if (index != -1 && index < currentItems.size - 1) {
|
||||
currentItems.removeAt(index)
|
||||
currentItems.add(currentItems.size, itemToMove)
|
||||
val updatedItems = currentItems.mapIndexed { idx, item ->
|
||||
item.copy(displayOrder = idx)
|
||||
}
|
||||
noteshopRepository.updateShoppingListItems(updatedItems)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun moveItemUp(listId: Int, itemToMove: ShoppingListItem) {
|
||||
noteshopRepository.getShoppingListWithItemsStream(listId).firstOrNull()?.let { listWithItems ->
|
||||
val currentItems = listWithItems.items.toMutableList()
|
||||
val index = currentItems.indexOfFirst { it.id == itemToMove.id }
|
||||
if (index > 0) {
|
||||
currentItems.removeAt(index)
|
||||
currentItems.add(index - 1, itemToMove)
|
||||
val updatedItems = currentItems.mapIndexed { idx, item ->
|
||||
item.copy(displayOrder = idx)
|
||||
}
|
||||
noteshopRepository.updateShoppingListItems(updatedItems)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun moveItemDown(listId: Int, itemToMove: ShoppingListItem) {
|
||||
noteshopRepository.getShoppingListWithItemsStream(listId).firstOrNull()?.let { listWithItems ->
|
||||
val currentItems = listWithItems.items.toMutableList()
|
||||
val index = currentItems.indexOfFirst { it.id == itemToMove.id }
|
||||
if (index != -1 && index < currentItems.size - 1) {
|
||||
currentItems.removeAt(index)
|
||||
currentItems.add(index + 1, itemToMove)
|
||||
val updatedItems = currentItems.mapIndexed { idx, item ->
|
||||
item.copy(displayOrder = idx)
|
||||
}
|
||||
noteshopRepository.updateShoppingListItems(updatedItems)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
fun moveItem(items: List<ShoppingListItem>) {
|
||||
viewModelScope.launch {
|
||||
@@ -432,23 +365,3 @@ data class ShoppingListDetails(
|
||||
return name.isNotBlank()
|
||||
}
|
||||
}
|
||||
|
||||
data class ShoppingListItemDetails(
|
||||
val id: Int = 0,
|
||||
val name: String = "",
|
||||
val isChecked: Boolean = false,
|
||||
val listId: Int = 0,
|
||||
val displayOrder: Int = 0
|
||||
) {
|
||||
fun toShoppingListItem(): ShoppingListItem = ShoppingListItem(
|
||||
id = id,
|
||||
name = name,
|
||||
isChecked = isChecked,
|
||||
listId = listId,
|
||||
displayOrder = displayOrder
|
||||
)
|
||||
|
||||
fun isValid(): Boolean {
|
||||
return name.isNotBlank() && listId != 0
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user