Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to initialize this "WakeUpDate" date element so that the default displayed value is 10:00 AM. This date picker is HourandMinute only and is being stored in userdefaults.

I tried to init the date element but it is not building. With this error: Cannot assign value of type 'State<Date>' to type 'Published<Date>'

UserData: Currently, the following init does not build

import Foundation
import SwiftUI

class UserData: ObservableObject {

    init() {
        _wakeUpTime = State<Date>(initialValue: Calendar.current.date(DateComponents(Hour: 10)) ?? Date())
    }
    
@Published var wakeUpTime: Date = UserDefaults.standard.object(forKey: "wakeUpTime") as? Date ?? Date() {
        didSet {
            UserDefaults.standard.set(self.wakeUpTime, forKey: "wakeUpTime")
        }
    }
}

SettingsDetail: Where the DatePicker is being selected:

struct SettingsDetailView: View {

@ObservedObject var userData: UserData

var body: some View {
         Form{
                
                DatePicker("Select a new time", selection: $userData.wakeUpTime, displayedComponents: .hourAndMinute)
              }
       }
}

MainSettings: Where the selected DatePicker Date is being displayed:

import SwiftUI
import UserNotifications

struct SettingsView: View {

    @ObservedObject var userData = UserData()
        
    static var dateFormatter: DateFormatter = {
            let formatter = DateFormatter()
            formatter.dateFormat = "hh:mm a"
            return formatter
        }()

    var body: some View {
        
        NavigationView {
            VStack (alignment: .center, spacing: 0) {
                Form{
                    Section(header: Text("NOTIFICATION SETTINGS")) {
                        HStack {
                            Text("Current Notification Time")
                            .foregroundColor(Color("MainText"))
                            
                            Spacer()
                            
                            Text("(self.userData.wakeUpTime, formatter: SettingsView.self.dateFormatter)")
                        }
                    }
                }
            }
        }
    }
}

EDIT I tried initializing UserData like this, but now when I pick a new time with the date picker and quit the app, the new time is gone and 5PM (the init time) is displayed again.

import Foundation
import SwiftUI

class UserData: ObservableObject {

    init() {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat =  "HH:mm"

            if let date = dateFormatter.date(from:"17:00") {
                wakeUpTime = date
            }
    }
    
@Published var wakeUpTime: Date = UserDefaults.standard.object(forKey: "wakeUpTime") as? Date ?? Date() {
        didSet {
            UserDefaults.standard.set(self.wakeUpTime, forKey: "wakeUpTime")
        }
    }
}

How can I run init only on the first launch, and be removed once the selected time has been picked with the datepicker?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
495 views
Welcome To Ask or Share your Answers For Others

1 Answer

I figured it out by doing this:

class UserData: ObservableObject {
    
    @AppStorage("userdatahaslaunched") var userdatahaslaunched = false

    init() {
            
        if !userdatahaslaunched {
            let dateFormatter = DateFormatter()
                dateFormatter.dateFormat =  "HH:mm"

                if let date = dateFormatter.date(from:"10:00") {
                    wakeUpTime = date
                }
                userdatahaslaunched = true
            }
        
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...