Implemented ability to delete annotations via swipe-to-delete gesture in the annotations list view. Added close button with X icon to dismiss the annotations sheet. Changes: - Added DeleteAnnotationUseCase with repository integration - Extended API with DELETE endpoint for annotations - Implemented swipe-to-delete in AnnotationsListView - Added error handling and optimistic UI updates - Updated toolbar with close button (X icon)
43 lines
1.3 KiB
Swift
43 lines
1.3 KiB
Swift
import Foundation
|
|
|
|
@Observable
|
|
class AnnotationsListViewModel {
|
|
private let getAnnotationsUseCase: PGetBookmarkAnnotationsUseCase
|
|
private let deleteAnnotationUseCase: PDeleteAnnotationUseCase
|
|
|
|
var annotations: [Annotation] = []
|
|
var isLoading = false
|
|
var errorMessage: String?
|
|
var showErrorAlert = false
|
|
|
|
init(_ factory: UseCaseFactory = DefaultUseCaseFactory.shared) {
|
|
self.getAnnotationsUseCase = factory.makeGetBookmarkAnnotationsUseCase()
|
|
self.deleteAnnotationUseCase = factory.makeDeleteAnnotationUseCase()
|
|
}
|
|
|
|
@MainActor
|
|
func loadAnnotations(for bookmarkId: String) async {
|
|
isLoading = true
|
|
errorMessage = nil
|
|
defer { isLoading = false }
|
|
|
|
do {
|
|
annotations = try await getAnnotationsUseCase.execute(bookmarkId: bookmarkId)
|
|
} catch {
|
|
errorMessage = "Failed to load annotations"
|
|
showErrorAlert = true
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
func deleteAnnotation(bookmarkId: String, annotationId: String) async {
|
|
do {
|
|
try await deleteAnnotationUseCase.execute(bookmarkId: bookmarkId, annotationId: annotationId)
|
|
annotations.removeAll { $0.id == annotationId }
|
|
} catch {
|
|
errorMessage = "Failed to delete annotation"
|
|
showErrorAlert = true
|
|
}
|
|
}
|
|
}
|