handle context menu linkeditem action in view model

This commit is contained in:
Satindar Dhillon 2022-12-12 13:58:34 -08:00
parent 201a736f0f
commit f7f2bce153
2 changed files with 28 additions and 8 deletions

View file

@ -8,8 +8,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.omnivore.omnivore.models.LinkedItem
import app.omnivore.omnivore.networking.Networker
import app.omnivore.omnivore.networking.search
import app.omnivore.omnivore.networking.*
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -91,11 +90,31 @@ class HomeViewModel @Inject constructor(
fun handleLinkedItemAction(itemID: String, action: LinkedItemAction) {
when (action) {
LinkedItemAction.Delete -> {
Log.d("maxx", "delete action for id: $itemID")
removeItemFromList(itemID)
viewModelScope.launch {
networker.deleteLinkedItem(itemID)
}
}
LinkedItemAction.Archive -> {
Log.d("maxx", "archive action for id: $itemID")
removeItemFromList(itemID)
viewModelScope.launch {
networker.archiveLinkedItem(itemID)
}
}
LinkedItemAction.Unarchive -> {
removeItemFromList(itemID)
viewModelScope.launch {
networker.unarchiveLinkedItem(itemID)
}
}
}
}
private fun removeItemFromList(itemID: String) {
itemsLiveData.value?.let {
val newList = it.filter { item -> item.id != itemID }
itemsLiveData.postValue(newList)
}
}
@ -112,5 +131,6 @@ class HomeViewModel @Inject constructor(
enum class LinkedItemAction {
Delete,
Archive
Archive,
Unarchive
}

View file

@ -5,7 +5,6 @@ import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Delete
import androidx.compose.material.icons.outlined.Edit
import androidx.compose.material.icons.outlined.List
import androidx.compose.material3.*
import androidx.compose.runtime.*
@ -88,9 +87,10 @@ fun LinkedItemCard(item: LinkedItem, onClickHandler: () -> Unit, actionHandler:
onDismissRequest = { isMenuExpanded = false }
) {
DropdownMenuItem(
text = { Text("Archive") },
text = { Text(if (item.isArchived) "Unarchive" else "Archive") },
onClick = {
actionHandler(LinkedItemAction.Archive)
val action = if (item.isArchived) LinkedItemAction.Unarchive else LinkedItemAction.Archive
actionHandler(action)
isMenuExpanded = false
},
leadingIcon = {