ReadKeep/readeck/UI/Bookmarks/BookmarksViewModel.swift
Ilyas Hallak cd265730d3 feat: Add bookmark actions (archive, favorite, delete)
- Add PATCH API endpoint for updating bookmarks with toggle functions
- Add DELETE API endpoint for permanent bookmark deletion
- Implement UpdateBookmarkUseCase with convenience methods for common actions
- Implement DeleteBookmarkUseCase for permanent bookmark removal
- Create BookmarkUpdateRequest domain model with builder pattern
- Extend BookmarkCardView with action menu and confirmation dialog
- Add context-sensitive actions based on current bookmark state
- Implement optimistic updates in BookmarksViewModel
- Add error handling and recovery for failed operations
- Enhance UI with badges, progress indicators, and action buttons
2025-06-11 22:31:43 +02:00

88 lines
2.5 KiB
Swift

import Foundation
@Observable
class BookmarksViewModel {
private let getBooksmarksUseCase = DefaultUseCaseFactory.shared.makeGetBookmarksUseCase()
private let updateBookmarkUseCase = DefaultUseCaseFactory.shared.makeUpdateBookmarkUseCase()
private let deleteBookmarkUseCase = DefaultUseCaseFactory.shared.makeDeleteBookmarkUseCase()
var bookmarks: [Bookmark] = []
var isLoading = false
var errorMessage: String?
var currentState: BookmarkState = .unread
init() {
}
@MainActor
func loadBookmarks(state: BookmarkState = .unread) async {
isLoading = true
errorMessage = nil
currentState = state
do {
bookmarks = try await getBooksmarksUseCase.execute(state: state)
} catch {
errorMessage = "Fehler beim Laden der Bookmarks"
bookmarks = []
}
isLoading = false
}
@MainActor
func refreshBookmarks() async {
await loadBookmarks(state: currentState)
}
@MainActor
func toggleArchive(bookmark: Bookmark) async {
do {
try await updateBookmarkUseCase.toggleArchive(
bookmarkId: bookmark.id,
isArchived: !bookmark.isArchived
)
// Liste aktualisieren
await loadBookmarks(state: currentState)
} catch {
errorMessage = "Fehler beim Archivieren des Bookmarks"
}
}
@MainActor
func toggleFavorite(bookmark: Bookmark) async {
do {
try await updateBookmarkUseCase.toggleFavorite(
bookmarkId: bookmark.id,
isMarked: !bookmark.isMarked
)
// Liste aktualisieren
await loadBookmarks(state: currentState)
} catch {
errorMessage = "Fehler beim Markieren des Bookmarks"
}
}
@MainActor
func deleteBookmark(bookmark: Bookmark) async {
do {
// Echtes Löschen über API statt nur als gelöscht markieren
try await deleteBookmarkUseCase.execute(bookmarkId: bookmark.id)
// Lokal aus der Liste entfernen (optimistische Update)
bookmarks.removeAll { $0.id == bookmark.id }
} catch {
errorMessage = "Fehler beim Löschen des Bookmarks"
// Bei Fehler die Liste neu laden, um konsistenten Zustand zu haben
await loadBookmarks(state: currentState)
}
}
}