It appears that while ContextMenu was deprecated, .contextMenu was not, so I am using the correct option. However, there does not appear to be a way to change the color of actions at this time.
Post
Replies
Boosts
Views
Activity
I had to add a dateAdded property and sort by that.
This is reliably how I get the issue to happen. I create 3 albums, open one of the albums to view its content (empty), go back out to the album collection view, and then if I try to add another album it crashes with that error.
I have added dateAdded to Photo and sorted by the date added, and still get the same error.
struct AlbumCollectionView: View {
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(entity: Photo.entity(),
sortDescriptors: [
NSSortDescriptor(keyPath: \Photo.dateAdded, ascending: true)
]
) var photos: FetchedResults<Photo>
@FetchRequest(entity: Album.entity(),
sortDescriptors: [
NSSortDescriptor(keyPath: \Album.name, ascending: true)
]
) var albums: FetchedResults<Album>
@State private var showingImagePicker = false
@State private var showingPopover = false
@State private var inputImage: UIImage?
@State private var albumNameInput: String = ""
let columns = [
GridItem(.adaptive(minimum: 150))
]
var body: some View {
ScrollView {
LazyVGrid(columns: columns, spacing: 20) {
ForEach(albums) { album in
NavigationLink(destination: AlbumView(album: album)) {
VStack {
Image(data: album.albumCoverImageData, placeholder: "No Image")
.resizable()
.aspectRatio(1, contentMode: .fill)
.contextMenu(menuItems: {
Button(action: {
deleteAlbum(selectedAlbum: album)
}) {
Label("Remove", systemImage: "trash")
}
})
Text(album.name ?? "")
}
}
}
}
.padding()
.navigationBarTitle(Text("Albums"))
.navigationBarItems(trailing:
Menu {
Button(action: {
self.showingPopover = true
}) {
Label("Add Album", systemImage: "photo.on.rectangle.angled")
}
Button(action: {
self.showingImagePicker = true
}) {
Label("Add Photo", systemImage: "photo")
}
} label: {
Image(systemName: "plus")
.popover(isPresented: $showingPopover, arrowEdge: .trailing ,content: {
VStack {
Text("New Album")
TextField("Enter Album Name",text: $albumNameInput)
.textFieldStyle(RoundedBorderTextFieldStyle())
Button("Submit"){
addAlbum(name: albumNameInput, albumCover: UIImage(named: "Image 1")!)
albumNameInput = ""
showingPopover = false
}
}
.padding()
})
}
)
.sheet(isPresented: $showingImagePicker, onDismiss: loadImage) {
ImagePicker(image: self.$inputImage)
}
}
}
func addAlbum(name: String, albumCover: UIImage) {
let newAlbum = Album(context: managedObjectContext)
newAlbum.id = UUID()
newAlbum.name = name
newAlbum.passwordProtected = false
newAlbum.albumCoverImageData = albumCover.jpegData(compressionQuality: 1.0)
saveContext()
}
func deleteAlbum(selectedAlbum: Album) {
for album in albums {
if selectedAlbum == album {
self.managedObjectContext.delete(album)
}
}
saveContext()
}
func loadImage() {
guard let inputImage = inputImage else { return }
addPhoto(image: inputImage)
}
func addPhoto(image: UIImage) {
let newPhoto = Photo(context: managedObjectContext)
newPhoto.id = UUID()
newPhoto.imageData = image.jpegData(compressionQuality: 1.0)!
newPhoto.dateAdded = Date()
print(newPhoto.dateAdded)
saveContext()
}
func saveContext() {
do {
try managedObjectContext.save()
} catch {
print("Error saving managed object context: \(error)")
}
}
}
When trying to add that to the line, I receive Thread 1: Fatal error: Could not extract a String from KeyPath Swift.KeyPath<LockIt.Photo, Swift.Optional<Swift.String>>. I originally had just photos in the application, with that NSSortDescriptor and it seemed to work fine before. The issue was introduced when I attempted to add albums to the application.
Yes, they say to use menu instead. With a long press gesture
I'd like to make sure trackpads are supported to be a good platform citizen. Would this work for right click with a mouse on iPadOS or would I need to roll out support for that by myself?