/* See the LICENSE.txt file for this sample’s licensing information. Abstract: A SwiftUI view that shows a scrollable full-size image. */ import SwiftUI import CoreData import CloudKit struct FullImageView: View { @Binding var activeSheet: ActiveSheet? var photo: Photo private let fetchRequest: FetchRequest private var ratings: FetchedResults { return fetchRequest.wrappedValue } init(activeSheet: Binding, photo: Photo) { self._activeSheet = activeSheet self.photo = photo let nsFetchRequest = Rating.fetchRequest() nsFetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Rating.value, ascending: true)] nsFetchRequest.predicate = NSPredicate(format: "photo = %@", photo) fetchRequest = FetchRequest(fetchRequest: nsFetchRequest, animation: .default) } private var photoImage: UIImage? { let photoData = photo.photoData?.data return photoData != nil ? UIImage(data: photoData!) : nil } var body: some View { NavigationStack { HStack (alignment: .top) { VStack (alignment: .leading){ if let share = PersistenceController.shared.existingShare(photo: photo) { ScrollView(.horizontal) { VStack (alignment: .leading) { Text("A share exists: \(share.title)") Text("ckName = \(photo.ckRecordName ?? "")") .font(.footnote) if let ckZoneR = photo.ckRecordZoneNameReverse { if ckZoneR == "zone.cloudkit.coredata.apple.com" { Text("ckZoneR = \(ckZoneR)") .font(.footnote) .foregroundColor(.gray) } else { Text("ckZoneR = \(ckZoneR)") .font(.footnote) .foregroundColor(.green) } } else { Text("ckZoneR = ") .font(.footnote) .foregroundColor(.red) } } } } else { Text("NO share exists") } if let image = photoImage { Image(uiImage: image) .resizable() .frame(width: 150, height: 150) } else { Text("The full size image is probably not downloaded from CloudKit.").padding() } Button(action: { addRandomRating() }) { Text("ADD a random ratings") .foregroundColor(.accentColor) } Button(action: { addRandomRatingQueue() }) { Text("ADD a random ratings Queued") .foregroundColor(.accentColor) } Button(action: { addRandomRatingWaitingQueue() }) { Text("ADD a random ratings Waiting + Queued") .foregroundColor(.accentColor) } ScrollView(.horizontal) { VStack(alignment:.leading) { Text("Ratings (#=\(ratings.count)):") ForEach(Array(ratings.enumerated()), id: \.element) { index, rating in if let ckZoneR = rating.ckRecordZoneNameReverse { if ckZoneR == "zone.cloudkit.coredata.apple.com" { HStack { Text("\(rating.value): ckZoneR =") .font(.footnote) Text("\(ckZoneR)") .font(.footnote) Text(" ;ckName = \(rating.ckRecordName ?? "")") .font(.footnote) } } else { HStack { Text("\(rating.value): ckZoneR =") .font(.footnote) Text("\(ckZoneR)") .font(.footnote) .foregroundColor(.green) Text("; ckName = \(rating.ckRecordName ?? "")") .font(.footnote) } } } else { HStack { Text("\(rating.value): ckZoneR =") .font(.footnote) Text("") .font(.footnote) .foregroundColor(.red) Text("; ckName = \(rating.ckRecordName ?? "")") .font(.footnote) } } } } } } Spacer() } .toolbar { ToolbarItem(placement: .dismiss) { Button("Dismiss") { activeSheet = nil } } } .listStyle(.plain) .navigationTitle("Full Size Photo") Spacer() } .frame(idealWidth: Layout.sheetIdealWidth, idealHeight: Layout.sheetIdealHeight) } private func addRandomRatingWaitingQueue() { let ratingValue = Int16.random(in: 1...5) DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { withAnimation { PersistenceController.shared.addRating(value: Int16(ratingValue), relateTo: photo) } } } private func addRandomRatingQueue() { let ratingValue = Int16.random(in: 1...5) DispatchQueue.main.async { withAnimation { PersistenceController.shared.addRating(value: Int16(ratingValue), relateTo: photo) } } } private func addRandomRating() { withAnimation { let ratingValue = Int16.random(in: 1...5) PersistenceController.shared.addRating(value: ratingValue, relateTo: photo) } } } extension NSManagedObject { var ckRecordName: String? { let returnValue = PersistenceController.shared.persistentContainer.recordID(for: self.objectID)?.recordName #if DEBUG print("\(#function) @ NSManagedObject-extension: \(returnValue ?? "nil")") #endif return returnValue } var ckRecordZoneNameReverse: String? { let returnValue = PersistenceController.shared.persistentContainer.recordID(for: self.objectID)?.zoneID.zoneName if let inputString = returnValue { let reversedString = inputString.split(separator: ".").reversed().joined(separator: ".") #if DEBUG print("\(#function) @ NSManagedObject-extension: \(reversedString)") #endif return reversedString } #if DEBUG print("\(#function) @ NSManagedObject-extension: nil") #endif return nil } }