feat: added list overview and detail view
This commit is contained in:
44
MindDump/ViewModels/ListStore.swift
Normal file
44
MindDump/ViewModels/ListStore.swift
Normal file
@@ -0,0 +1,44 @@
|
||||
import Foundation
|
||||
import Observation
|
||||
|
||||
@Observable
|
||||
class ListStore {
|
||||
var lists: [TodoList]
|
||||
|
||||
init() {
|
||||
self.lists = [TodoList(name: "Inbox", isInbox: true)]
|
||||
}
|
||||
|
||||
func addList(name: String) {
|
||||
let list = TodoList(name: name)
|
||||
lists.append(list)
|
||||
}
|
||||
|
||||
func deleteList(_ list: TodoList) {
|
||||
guard !list.isInbox else { return }
|
||||
lists.removeAll { $0.id == list.id }
|
||||
}
|
||||
|
||||
func addItem(to listID: UUID, title: String) {
|
||||
guard let index = lists.firstIndex(where: { $0.id == listID }) else { return }
|
||||
let item = TodoItem(title: title)
|
||||
lists[index].items.append(item)
|
||||
}
|
||||
|
||||
func deleteItem(_ itemID: UUID, from listID: UUID) {
|
||||
guard let listIndex = lists.firstIndex(where: { $0.id == listID }) else { return }
|
||||
lists[listIndex].items.removeAll { $0.id == itemID }
|
||||
}
|
||||
|
||||
func updateItem(_ itemID: UUID, in listID: UUID, title: String) {
|
||||
guard let listIndex = lists.firstIndex(where: { $0.id == listID }),
|
||||
let itemIndex = lists[listIndex].items.firstIndex(where: { $0.id == itemID }) else { return }
|
||||
lists[listIndex].items[itemIndex].title = title
|
||||
}
|
||||
|
||||
func toggleItemCompleted(_ itemID: UUID, in listID: UUID) {
|
||||
guard let listIndex = lists.firstIndex(where: { $0.id == listID }),
|
||||
let itemIndex = lists[listIndex].items.firstIndex(where: { $0.id == itemID }) else { return }
|
||||
lists[listIndex].items[itemIndex].isCompleted.toggle()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user