// // ContentView.swift // Swipe That Pic // // Created by Amine Bou on 26/07/2024. // import SwiftUI import SwiftData import Photos struct ContentView: View { @Environment(\.modelContext) private var modelContext @EnvironmentObject var photoLibraryService: PhotosService @Query private var items: [Item] var body: some View { NavigationSplitView { VStack { PhotoThumbnailView(asset: $photoLibraryService.one) Spacer() HStack { Button(role: .destructive, action: { PHPhotoLibrary.shared().performChanges({ PHAssetChangeRequest.deleteAssets([photoLibraryService.one as Any] as NSArray) }, completionHandler: { success, error in if success { photoLibraryService.fetchNotKeptPhotos() } else { }}) }) { Label("Supprimer", systemImage: "minus.circle.fill") }.buttonStyle(.borderedProminent) Spacer() Button(action: { let newItem = Item(localIdentfier: photoLibraryService.one!.localIdentifier) modelContext.insert(newItem) photoLibraryService.fetchNotKeptPhotos() }) { Label("Garder", systemImage: "plus.circle.fill") }.buttonStyle(.borderedProminent) }.disabled(photoLibraryService.one == nil) } .toolbar { if (items.count > 0) { ToolbarItem { NavigationLink(destination: IgnoredPictures()) { Label("Handle kept", systemImage: "tray.and.arrow.up.fill") } } } } } detail: { Text("Select an item") }.onAppear(perform: { photoLibraryService.fetchNotKeptPhotos() }) } } #Preview { ContentView() .modelContainer(for: Item.self, inMemory: true) }