-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathViewModel.kt
32 lines (28 loc) · 950 Bytes
/
ViewModel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.promise
@OptIn(DelicateCoroutinesApi::class)
class ShoppingListViewModel {
var shoppingList by mutableStateOf(listOf<ShoppingListItem>())
private set
fun fetchShoppingList() {
GlobalScope.promise {
shoppingList = getShoppingList()
}
}
fun pushShoppingListItem(shoppingListItem: ShoppingListItem) {
GlobalScope.promise {
addShoppingListItem(shoppingListItem)
shoppingList += shoppingListItem
}
}
fun removeShoppingListItem(shoppingListItem: ShoppingListItem) {
GlobalScope.promise {
deleteShoppingListItem(shoppingListItem)
shoppingList -= shoppingListItem
}
}
}