skip to Main Content

I’m trying to allow the user to select what time they want a notification to go off through time selection on a datePicker. I have a datePicker set to only display the time as follows:

DatePicker("First Workout Notification Time", selection: $firstNotificationTime, displayedComponents: .hourAndMinute)

I want the firstNotificationTime @State var to use my @AppStorage time vars which are set as defaults on this page above the "var body: some View" as:

@AppStorage("startNoficationHour") var startNoficationHour: Int = 9
@AppStorage("startNoficationMin") var startNoficationMin: Int = 30

When the view loads (i.e. .onAppear(){}) I want the datePicker’s $firstNotificationTime to get set to the startNoficationHour and startNoficationMin

my view is set up as follows:

var body: some View {
        VStack {
DatePicker("First Workout Notification Time", selection: $firstNotificationTime, displayedComponents: .hourAndMinute)
}
}
.onAppear(){
// set datePicker's fisrtNotificationTime to startNotificationHour and startNotificationMin
let components = DateComponents(calendar: Calendar.current, timeZone: TimeZone.current, hour:startNoficationHour, minute: startNoficationMin)
firstNotificationTime = components.date!        
                        print("start hour is (startNoficationHour) and start minute is (startNoficationMin)")
}
.onDisappear(){
// sets startNotificationHour and startNotificationMin to datePicker
                        let components = Calendar.current.dateComponents([.hour, .minute], from: firstNotificationTime)
                        startNoficationHour = components.hour!
                        startNoficationMin = components.minute!
                        print("start hour is set to (startNoficationHour) and start minute is set to (startNoficationMin)")
}

2

Answers


  1. Chosen as BEST ANSWER

    ok so all I had to do was set a component variable to the hour and minute and place that in the onAppear

    let components = DateComponents(calendar: Calendar.current, timeZone: TimeZone.current, hour:startNoficationHour, minute: startNoficationMin)
    firstNotificationTime = components.date!
    

    I updated my question above with the answer which was based on the comment from Tushar Sharma above that linked to this Set default date on DatePicker in SwiftUI?


  2. You are doing so much thing. There is easy solution. Use a shadow property.

    Date has a method that convert the time into a double value. So, you can store the double value into @AppStorage and use it to update the date.

    Here is an example,

    Create a @State date variable,

    @State var reminderTime = Date()

    Create a show property save the date.

    @AppStorage("reminderTime") var reminderTimeShadow: Double = 0

    Now in the DatePicker,

     DatePicker("", selection: Binding(
                                get: {reminderTime}, set: {
                                    reminderTimeShadow = $0.timeIntervalSince1970
                                    reminderTime = $0
                                    setNotification()
                                }), displayedComponents: .hourAndMinute)   
                        .onAppear {
                            reminderTime = Date(timeIntervalSince1970: reminderTimeShadow)
                        }
    

    Here, you use the explicit binding to get and set the value and when setting the value of reminderTime, you also set the value reminderTimeShadow and save it by @AppStorage and use this in .onAppear by using reminderTime = Date(timeIntervalSince1970: reminderTimeShadow) thus update reminderTime and the view.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search