UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Get Reminders List and update View in the right way

Forums > SwiftUI

@Kdkwn  

Hello, I want that the user can choose a list from the Reminders app where some should be saved.

Now, I get the Reminder-Lists like this:

func fetchReminderLists() -> [EKCalendar] {
        let reminderLists = eventStore.calendars(for: .reminder)
        var listFromUser: [EKCalendar] = []

        for list in reminderLists {
            listFromUser.append(list)
        }

        return listFromUser
    }

In my SwiftUI View, I have a Picker where the Reminder-List should be displayed:


 @AppStorage("selected_reminder_list_id") var selectedReminderListId: String = ""
    @State private var reminderList: [EKCalendar] = []

Picker(selection: $selectedReminderListId) {
                                    if selectedReminderListId == "" {
                                        Text("No list selected")
                                        ForEach(reminderList, id: \.self) { reminderList in
                                            Text(reminderList.title)
                                                .tag(reminderList.calendarIdentifier)
                                        }
                                    } else {
                                        ForEach(reminderList, id: \.self) { reminderList in
                                            Text(reminderList.title)
                                                .tag(reminderList.calendarIdentifier)
                                        }
                                    }
                                } label: {
                                    HStack {
                                        Image(systemName: "list.bullet")
                                            .foregroundColor(Color.blue)
                                            .padding(.trailing, 4)

                                        Text("Reminder-Lists")
                                            .bold()
                                    }
                                }
.onAppear {
                    reminderList = ReminderManager.shared.fetchReminderLists()
                    var found = false

                    for i in 1...reminderList.count {
                        if reminderList[i - 1].calendarIdentifier == selectedReminderListId {
                            found = true
                            break
                        }
                    }

                    if !found {
                        selectedReminderListId = ""
                    }
                }                       

Now my question is, how can I listen to changes in the Reminders-App for example when a lists get deleted or added. Also is there a better solution for the Picker?

Thank you for your help!

3      

Have you read this page in the documentation?

2      

@Kdkwn  

Thanks, you also know how to respond to list changes, not just create or delete. The problem is that the view with this observer only updates for a newly created list in the reminders app or a deleted one, not for an edited one.

2      

You are observing for EKEventStoreChanged? Per the docs: "This notification is posted whenever changes are made to the Calendar database, including adding, removing, and changing events or reminders." (emphasis added)

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.