cupertino-sample-code/coredata-sharing-core-data-objects-between-icloud-users/SwiftUI/PhotoGridItemView.swift
Mihaela Mihaljevic 37fd4fb24f Initial commit: 606 Apple sample code projects
MIT License - Apple Developer sample code
Downloaded via Cupertino (https://github.com/mihaelamj/cupertino)
2025-12-03 00:19:12 +01:00

55 lines
1.7 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
See the LICENSE.txt file for this samples licensing information.
Abstract:
A SwiftUI view that manages a grid item.
*/
import SwiftUI
import CoreData
struct PhotoGridItemView: View {
@ObservedObject var photo: Photo
var itemSize: CGSize
@State private var isHovering = false
private let persistenceController = PersistenceController.shared
var body: some View {
ZStack(alignment: .topTrailing) {
/**
Show the thumbnail image, or a placeholder if the thumbnail data doesn't exist.
*/
if let data = photo.thumbnail?.data, let thumbnail = UIImage(data: data) {
Image(uiImage: thumbnail)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: itemSize.width, height: itemSize.height)
} else {
Image(systemName: "questionmark.square.dashed")
.font(.system(size: 30))
.frame(width: itemSize.width, height: itemSize.height)
}
topLeftButton()
}
.frame(width: itemSize.width, height: itemSize.height)
.background(Color.gridItemBackground)
#if os(macOS)
.overlay(
RoundedRectangle(cornerRadius: 2)
.stroke(.blue, lineWidth: isHovering ? 2 : 0)
)
.onHover { hovering in
isHovering = hovering
}
#endif
}
@ViewBuilder
private func topLeftButton() -> some View {
if persistenceController.sharedPersistentStore.contains(manageObject: photo) {
Image(systemName: "person.2.circle")
.foregroundColor(.green)
}
}
}