ios – How do I move CoreData into UICalendarview

0
1


I am attempting to make use of my CoreData entity ‘Merchandise’ to point out decorations on a calendar (utilizing UIcalendarview in iOS16). I’ve my view known as CalView calling the CalendarHelper struct (the place the UICalendarView is coded and setup). It’s exhibiting decorations on daily at the moment, however I wish to solely present decorations on the times the place there’s a CoreData entry, after which allow navigation if you click on on these days to the element view. The issue is that I can not work out get the CoreData entity into the CalendarHelper and UICalendarView to make use of it. It is at the moment giving me an error stating that I can not move a FetchedResult in to an anticipated ObservedResult. However after I change all of them to FetchedResult on that perform, it offers me an error saying it is anticipating a kind of Merchandise.

NOTE: I posted this over right here as properly however I believe I tousled that publish by deleting the unique after I up to date it. https://stackoverflow.com/questions/75138925/pass-coredata-into-uicalendarview-to-show-decorations-and-click-on-date-for-deta

CALVIEW:

struct CalView: View {
    
    var physique: some View {
        ZStack {
            VStack {            
                HStack {
                   CalendarHelper(interval: DateInterval(begin: .distantPast, finish: .distantFuture))
                }
            }
        }
    }
}

CALENDARHELPER:

struct CalendarHelper: UIViewRepresentable {
    
    @Surroundings(.managedObjectContext) non-public var viewContext
    @FetchRequest(
        sortDescriptors: SleepSort.default.descriptors,
            animation: .default)
        non-public var gadgets: FetchedResults<Merchandise>
    
    let interval: DateInterval
    
    func makeUIView(context: Context) -> UICalendarView {
        let calView = UICalendarView()
        calView.delegate = context.coordinator
        calView.calendar = Calendar(identifier: .gregorian)
        calView.availableDateRange = interval
        calView.fontDesign = .rounded
        let dateSelection = UICalendarSelectionSingleDate(delegate: context.coordinator)
        calView.selectionBehavior = dateSelection
        calView.wantsDateDecorations = true
        return calView
    }
    func makeCoordinator() -> Coordinator {
        Coordinator(dad or mum: self, gadgets: gadgets)
        }
    
    func updateUIView(_ uiView: UICalendarView, context: Context) {

    }
    
    class Coordinator: NSObject, UICalendarViewDelegate, UICalendarSelectionSingleDateDelegate {
            var dad or mum: CalendarHelper
            @ObservedObject var gadgets: Merchandise
        
            init(dad or mum: CalendarHelper, gadgets: ObservedObject<Merchandise>) {
                self.dad or mum = dad or mum
                self._items = gadgets
            }
            
            @MainActor
            func calendarView(_ calendarView: UICalendarView, decorationFor dateComponents: DateComponents) -> UICalendarView.Ornament? {
                let font = UIFont.systemFont(ofSize: 10)
                let configuration = UIImage.SymbolConfiguration(font: font)
                let picture = UIImage(systemName: "star.fill", withConfiguration: configuration)?.withRenderingMode(.alwaysOriginal)
                return .picture(picture)
            }
            
            func dateSelection(_ choice: UICalendarSelectionSingleDate,
                               didSelectDate dateComponents: DateComponents?) {
            }
            
            func dateSelection(_ choice: UICalendarSelectionSingleDate,
                               canSelectDate dateComponents: DateComponents?) -> Bool {
                return true
            }
            
        }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here