Below is a view within my Task management app, and I am trying to make my picker display a certain color when it is chosen. My question is how do I save the Ints used for the priority and color picker within Core data? I can't figure out how to convert the int value into int32, and it can't start as an Int32 object because then it wouldn't be able to be used as the selector for the background color of each picker. PLEASE HELP!
struct NewTask: View {
#Environment(\.dismiss) var dismiss
// MARK: Task Values
#State var taskTitle: String = ""
#State var taskDescription: String = ""
#State var taskDate: Date = Date()
var colors = ["Teal","Yellow","Pink"]
#State var selectedColor: Int = 0
var colorsColors = ["logoTeal","logoYellow","logoPink"]
var selectedColorInt: String {
get {
return ("\(selectedColor)")
}
}
var priorities = ["Urgent","Slightly Urgent","Not Urgent"]
#State var selectedPriority: Int = 0
var priorityColors = ["priorityRed","priorityYellow","priorityGreen"]
// MARK: Core Data Context
#Environment(\.managedObjectContext) var context
#EnvironmentObject var taskModel: TaskViewModel
var body: some View {
NavigationView{
List{
Section {
TextField("Go to work", text: $taskTitle)
Picker("Priority", selection: $selectedPriority) {
ForEach(0..<priorities.count){
Text(priorities[$0])
}
.padding(5)
.foregroundColor(.white)
.background(priorityColors[selectedPriority])
.cornerRadius(5)
}
Picker("Theme", selection: $selectedColor) {
ForEach(0..<colors.count){
Text(colors[$0])
}
.padding(5)
.foregroundColor(.black)
.background(colorColors[selectedColor])
.cornerRadius(5)
}
} header: {
Text("Task Information")
}
Section {
TextEditor(text: $taskDescription)
} header: {
Text("Task Description")
}
// Disabling Date for Edit Mode
if taskModel.editTask == nil{
Section {
DatePicker("", selection: $taskDate)
.datePickerStyle(.graphical)
.labelsHidden()
} header: {
Text("Task Date")
}
}
}
.listStyle(.insetGrouped)
.navigationTitle("Add New Task")
.navigationBarTitleDisplayMode(.inline)
// MARK: Disbaling Dismiss on Swipe
.interactiveDismissDisabled()
// MARK: Action Buttons
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button("Save"){
if let task = taskModel.editTask{
task.taskTitle = taskTitle
task.taskDescription = taskDescription
}
else{
let task = Task(context: context)
task.taskTitle = taskTitle
task.taskDescription = taskDescription
task.taskDate = taskDate
/*task.selectedColor = selectedColor*/
/*task.selectedPriority = selectedPriority*/
}
// Saving
try? context.save()
// Dismissing View
dismiss()
}
.disabled(taskTitle == "" || taskDescription == "")
}
ToolbarItem(placement: .navigationBarLeading) {
Button("Cancel"){
dismiss()
}
}
}
// Loading Task data if from Edit
.onAppear {
if let task = taskModel.editTask{
taskTitle = task.taskTitle ?? ""
taskDescription = task.taskDescription ?? ""
}
}
}
}
}
Here's the approach I would take.
First, as priority will only ever be one of three values, express it as an enum with an Int32 raw value, and with the text description and colours as properties of the enum:
enum Priority: Int32, CaseIterable {
case urgent = 0
case slightlyUrgent = 1
case notUrgent = 2
var description: String {
switch self {
case .urgent: return "Urgent"
case .slightlyUrgent: return "Slightly Urgent"
case .notUrgent: return "Not Urgent"
}
}
var colorName: String {
switch self {
case .urgent: return "PriorityRed"
case .slightlyUrgent: return "PriorityYellow"
case .notUrgent: return "PriorityGreen"
}
}
var color: Color { Color(colorName) }
}
In the form, this keeps your code a bit cleaner, and allows you to use semantic values over 0 when initialising your state variable:
#State private var selectedPriority: Priority = .urgent
Picker("Priority", selection: $selectedPriority) {
ForEach(Priority.allCases, id: \.self) { priority in
Text(priority.description)
.padding(5)
.foregroundColor(.black)
.background(priority.color)
.cornerRadius(5)
}
}
Then, add an extension to your CoreData model that gives it a Priority property, adding a getter and setter so that it uses the stored column:
extension Task {
var priorityEnum: Priority {
get { Priority(rawValue: priority) ?? .urgent }
set { priority = newValue.rawValue }
}
}
Then when it comes to save the form details into the Core Data managed object, you can set task.priorityEnum to the picker's value, and the custom property will make sure that the correct Int32 value is stored.
Likewise, when loading the edit form's state variables from the task, referencing task.priorityEnum will get you a Priority value initialized with the integer that's stored in the database.
Related
I'm currently trying to change the data the picker will display based on the value in the series text field. I'm not getting the picker to show up, I'm not getting any errors but I'm getting this warning "Non-constant range: not an integer range" for both the ForEach lines below.
struct ConveyorTracks: View {
#State private var series = ""
#State private var selectedMaterial = 0
#State private var selectedWidth = 0
#State private var positRack = false
let materials8500 = ["HP", "LF", "Steel"]
let widths8500 = ["3.25", "4", "6"]
let materials882 = ["HP", "LF", "PS", "PSX"]
let widths882 = ["3.25", "4.5", "6","7.5", "10", "12"]
var materials: [String] {
if series == "8500" {
return materials8500
} else if series == "882" {
return materials882
} else {
return []
}
}
var widths: [String] {
if series == "8500" {
return widths8500
} else if series == "882" {
return widths882
} else {
return []
}
}
var body: some View {
VStack(alignment: .leading) {
HStack {
Text("Series:")
TextField("Enter series", text: $series)
}.padding()
HStack {
Text("Material:")
Picker("Materials", selection: $selectedMaterial) {
ForEach(materials.indices) { index in
Text(self.materials[index])
}
}.pickerStyle(SegmentedPickerStyle())
}.padding()
HStack {
Text("Width:")
Picker("Widths", selection: $selectedWidth) {
ForEach(widths.indices) { index in
Text(self.widths[index])
}
}.pickerStyle(SegmentedPickerStyle())
}.padding()
HStack {
Text("Positive Rack:")
Toggle("", isOn: $positRack)
}.padding()
}
}
}
struct ConveyorTrack_Previews: PreviewProvider {
static var previews: some View {
ConveyorTracks()
}
}
I would like the pickers to change based on which value is input in the series text field, for both materials and width.
Perhaps pickers isn't the best choice, I am open to any suggestions.
Thanks!
ForEach(materials.indices)
Needs to be
ForEach(materials.indices, id: \.self)
Because you are not using a compile-time constant in ForEach.
In general for fixed selections like this your code can be much simpler if you make everything enums, and make the enums Identifiable. This simplified example only shows one set of materials but you could return an array of applicable materials depending on the selected series (which could also be an enum?)
enum Material: Identifiable, CaseIterable {
case hp, lf, steel
var id: Material { self }
var title: String {
... return an appropriate title
}
}
#State var material: Material
...
Picker("Material", selection: $material) {
ForEach(Material.allCases) {
Text($0.title)
}
}
I have an array that contains the data that is displayed on a list. When the user hits "new", a sheet pops up to allow the user to enter a new item to the list.
I just added a swipe option to edit this item and I wanted to reuse the same sheet to edit the item's text. But I'm having problems understanding how to check whether a specific item was selected (by UUID?) to pass to the sheet, or it's a new item.
Code:
let dateFormatter = DateFormatter()
struct NoteItem: Codable, Hashable, Identifiable {
let id: UUID
var text: String
var date = Date()
var dateText: String {
dateFormatter.dateFormat = "EEEE, MMM d yyyy, h:mm a"
return dateFormatter.string(from: date)
}
var tags: [String] = []
}
struct ContentView: View {
#EnvironmentObject private var data: DataModel
#State private var selectedItemId: UUID?
#State var searchText: String = ""
#State private var sheetIsShowing = false
NavigationView {
List(filteredNotes) { note in
VStack(alignment: .leading) {
//....
// not relevant code
}
.swipeActions(allowsFullSwipe: false) {
Button(action: {
selectedItemId = note.id
self.sheetIsShowing = true
} ) {
Label("Edit", systemImage: "pencil")
}
}
}
.toolbar {
// new item
Button(action: {
self.sheetIsShowing = true
}) {
Image(systemName: "square.and.pencil")
}
}
.sheet(isPresented: $sheetIsShowing) {
if self.selectedItemId == NULL { // <-- this is giving me an error
let Note = NoteItem(id: UUID(), text: "New Note", date: Date(), tags: [])
SheetView(isVisible: self.$sheetIsShowing, note: Note)
} else {
let index = data.notes.firstIndex(of: selectedItemId)
SheetView(isVisible: self.$sheetIsShowing, note: data.notes[index])
}
}
}
}
My rationale was to check whether self.selectedItemId == NULL was null or not, if not then pass that element to the sheet to be edited, if yes, the as it as a new element.
What am I doing wrong? And if there is a standard way to pass information to the sheet based on whether there is an item select or not, could you show me?
Thanks!
From this post, you can do like this in your case:
struct SheetForNewAndEdit: View {
#EnvironmentObject private var data: DataModel
#State var searchText: String = ""
// The selected row
#State var selectedNote: NoteItem? = nil
#State private var sheetNewNote = false
// for test :
#State private var filteredNotes: [NoteItem] = [
NoteItem(id: UUID(), text: "111"),
NoteItem(id: UUID(), text: "222")];
var body: some View {
NavigationView {
List(filteredNotes) { note in
VStack(alignment: .leading) {
//....
// not relevant code
Text(note.text)
}
.swipeActions(allowsFullSwipe: false) {
Button(action: {
// the action select the note to display
selectedNote = note
} ) {
Label("Edit", systemImage: "pencil")
}
}
}
// sheet is displayed depending on selected note
.sheet(item: $selectedNote, content: {
note in
SheetView(note: note)
})
// moved tool bar one level (inside navigation view)
.toolbar {
// Toolbar item to have toolbar
ToolbarItemGroup(placement: .navigationBarTrailing) {
ZStack {
Button(action: {
// change bool value
self.sheetNewNote.toggle()
}) {
Image(systemName: "square.and.pencil")
}
}
}
}
.sheet(isPresented: $sheetNewNote) {
let Note = NoteItem(id: UUID(), text: "New Note", date: Date(), tags: [])
SheetView(note: Note)
}
}
}
}
Note : SheetView does not need any more a boolean, but you can add one if you orefer
Swift uses nil, not null, so the compiler is complaining when you are comparing selected items to null. However, you will have another issue. Your selectedItemId is optional, so you can't just use it in your else clause to make your note. You are better off using an if let to unwrap it. Change it to:
.sheet(isPresented: $sheetIsShowing) {
if let selectedItemId = selectedItemId,
let index = data.notes.firstIndex(where: { $0.id == selectedItemId }) {
SheetView(isVisible: self.$sheetIsShowing, note: data.notes[index])
} else {
let note = NoteItem(id: UUID(), text: "New Note", date: Date(), tags: [])
SheetView(isVisible: self.$sheetIsShowing, note: note)
}
}
edit:
I realized that you were attempting to use two optionals without unwrapping them, so I changed this to an if let to make sure both are safely unwrapped.
I have 3 views. Content View, TrainingView and TrainingList View. I want to list exercises from Core Data but also I want to make some changes without changing data.
In ContentView; I am trying to fetch data with CoreData
struct ContentView: View {
// MARK: - PROPERTY
#FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \Training.timestamp, ascending: false)],
animation: .default)
private var trainings: FetchedResults<Training>
#State private var showingAddProgram: Bool = false
// FETCHING DATA
// MARK: - FUNCTION
// MARK: - BODY
var body: some View {
NavigationView {
Group {
VStack {
HStack {
Text("Your Programs")
Spacer()
Button(action: {
self.showingAddProgram.toggle()
}) {
Image(systemName: "plus")
}
.sheet(isPresented: $showingAddProgram) {
AddProgramView()
}
} //: HSTACK
.padding()
List {
ForEach(trainings) { training in
TrainingListView(training: training)
}
} //: LIST
Spacer()
} //: VSTACK
} //: GROUP
.navigationTitle("Good Morning")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
print("test")
}) {
Image(systemName: "key")
}
}
} //: TOOLBAR
.onAppear() {
}
} //: NAVIGATION
}
private func showId(training: Training) {
guard let id = training.id else { return }
print(id)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}
In TrainingView; I am getting exercises as a array list and I am pushing into to TrainingListView.
import SwiftUI
struct TrainingView: View {
#Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
#State var training: Training
#State var exercises: [Exercise]
#State var tempExercises: [Exercise] = [Exercise]()
#State var timeRemaining = 0
#State var timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
#State var isTimerOn = false
var body: some View {
VStack {
HStack {
Text("\(training.name ?? "")")
Spacer()
Button(action: {
presentationMode.wrappedValue.dismiss()
}) {
Text("Finish")
}
}
.padding()
ZStack {
Circle()
.fill(Color.blue)
.frame(width: 250, height: 250)
Circle()
.fill(Color.white)
.frame(width: 240, height: 240)
Text("\(timeRemaining)s")
.font(.system(size: 100))
.fontWeight(.ultraLight)
.onReceive(timer) { _ in
if isTimerOn {
if timeRemaining > 0 {
timeRemaining -= 1
} else {
isTimerOn.toggle()
stopTimer()
removeExercise()
}
}
}
}
Button(action: {
startResting()
}) {
if isTimerOn {
Text("CANCEL")
} else {
Text("GIVE A BREAK")
}
}
Spacer()
ExerciseListView(exercises: $tempExercises)
}
.navigationBarHidden(true)
.onAppear() {
updateBigTimer()
}
}
private func startResting() {
tempExercises = exercises
if let currentExercise: Exercise = tempExercises.first {
timeRemaining = Int(currentExercise.rest)
startTimer()
isTimerOn.toggle()
}
}
private func removeExercise() {
if let currentExercise: Exercise = tempExercises.first {
if Int(currentExercise.rep) == 1 {
let index = tempExercises.firstIndex(of: currentExercise) ?? 0
tempExercises.remove(at: index)
} else if Int(currentExercise.rep) > 1 {
currentExercise.rep -= 1
let index = tempExercises.firstIndex(of: currentExercise) ?? 0
tempExercises.remove(at: index)
tempExercises.insert(currentExercise, at: index)
}
updateBigTimer()
}
}
private func updateBigTimer() {
timeRemaining = Int(tempExercises.first?.rest ?? 0)
}
private func stopTimer() {
timer.upstream.connect().cancel()
}
private func startTimer() {
timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
}
}
struct TrainingView_Previews: PreviewProvider {
static var previews: some View {
TrainingView(training: Training(), exercises: [Exercise]())
}
}
In TrainingListView; I am listing all exercises.
struct TrainingListView: View {
#ObservedObject var training: Training
#Environment(\.managedObjectContext) private var managedObjectContext
var body: some View {
NavigationLink(destination: TrainingView(training: training, exercises: training.exercises?.toArray() ?? [Exercise]())) {
HStack {
Text("\(training.name ?? "")")
Text("\(training.exercises?.count ?? 0) exercises")
}
}
}
}
Also, I am adding video: https://twitter.com/huseyiniyibas/status/1388571724346793986
What I want to do is, when user taps any Training Exercises List should refreshed. It should be x5 again like in the beginning.
I had a hard time understanding your question but I guess I got the idea.
My understanding is this:
You want to store the rep count in the Core Data. (Under Training > Exercises)
You want to count down the reps one by one as the user completes the exercise.
But you don't want to change the original rep count stored in the Core Data.
I didn't run your code since I didn't want to recreate all the models and Core Data files. I guess I've spotted the problem. Here I'll explain how you can solve it:
The Core Data models are classes (reference types). When you pass around the classes (as you do in your code) and change their properties, you change the original data. In your case, you don't want that.
(Btw, being a reference type is a very useful and powerful property of classes. Structs and enums are value types, i.e. they are copied when passed around. The original data is unchanged.)
You have several options to solve your problem:
Just generate a different struct (something like ExerciseDisplay) from Exercise, and pass ExerciseDisplay to TrainingView.
You can write an extension to Exercise and "copy" the model before passing it to TrainingView. For this you'll need to implement the NSCopying protocol.
extension Exercise: NSCopying {
func copy(with zone: NSZone? = nil) -> Any {
return Exercise(...)
}
}
But before doing this I guess you'll need to change the Codegen to Manual/None of your entry in your .xcdatamodeld file. This is needed when you want to create the attributes manually. I'm not exactly sure how you can implement NSCopying for a CoreDate model, but it's certainly doable.
The first approach is easier but kinda ugly. The second is more versatile and elegant, but it's also more advanced. Just try the first approach first and move to the second once you feel confident.
Update:
This is briefly how you can implement the 1st approach:
struct ExerciseDisplay: Identifiable, Equatable {
public let id = UUID()
public let name: String
public var rep: Int
public let rest: Int
}
struct TrainingView: View {
// Other properties and states etc.
let training: Training
#State var exercises: [ExerciseDisplay] = []
init(training: Training) {
self.training = training
}
var body: some View {
VStack {
// Views
}
.onAppear() {
let stored: [Exercise] = training.exercises?.toArray() ?? []
self.exercises = stored.map { ExerciseDisplay(name: $0.name ?? "", rep: Int($0.rep), rest: Int($0.rest)) }
}
}
}
I have a SwiftUI app with SwiftUI App lifecycle that includes a master-detail type
list driven from CoreData. I have the standard list in ContentView and NavigationLinks
to the DetailView. I pass a Core Data entity object to the Detailview.
My struggle is setting-up bindings to TextFields in the DetailView for data entry
and for editing. I tried to create an initializer which I could not make work. I have
only been able to make it work with the following. Assigning the initial values
inside the body does not seem like the best way to do this, though it does work.
Since the Core Data entities are ObservableObjects I thought I should be able to
directly access and update bound variables, but I could not find any way to reference
a binding to Core Data in a ForEach loop.
Is there a way to do this that is more appropriate than my code below?
Simplified Example:
struct DetailView: View {
var thing: Thing
var count: Int
#State var localName: String = ""
#State private var localComment: String = ""
#State private var localDate: Date = Date()
//this does not work - cannot assign String? to State<String>
// init(t: Thing) {
// self._localName = t.name
// self._localComment = t.comment
// self._localDate = Date()
// }
var body: some View {
//this is the question - is this safe?
DispatchQueue.main.async {
self.localName = self.thing.name ?? "no name"
self.localComment = self.thing.comment ?? "No Comment"
self.localDate = self.thing.date ?? Date()
}
return VStack {
Text("\(thing.count)")
.font(.title)
Text(thing.name ?? "no what?")
TextField("name", text: $localName)
Text(thing.comment ?? "no comment?")
TextField("comment", text: $localComment)
Text("\(thing.date ?? Date())")
//TextField("date", text: $localDate)
}.padding()
}
}
And for completeness, the ContentView:
struct ContentView: View {
#Environment(\.managedObjectContext) private var viewContext
#FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Thing.date, ascending: false)])
private var things : FetchedResults<Thing>
#State private var count: Int = 0
#State private var coverDeletedDetail = false
var body: some View {
NavigationView {
List {
ForEach(things) { thing in
NavigationLink(destination: DetailView(thing: thing, count: self.count + 1)) {
HStack {
Image(systemName: "gear")
.resizable()
.frame(width: 40, height: 40)
.onTapGesture(count: 1, perform: {
updateThing(thing)
})
Text(thing.name ?? "untitled")
Text("\(thing.count)")
}
}
}
.onDelete(perform: deleteThings)
if UIDevice.current.userInterfaceIdiom == .pad {
NavigationLink(destination: WelcomeView(), isActive: self.$coverDeletedDetail) {
Text("")
}
}
}
.navigationTitle("Thing List")
.navigationBarItems(trailing: Button("Add Task") {
addThing()
})
}
}
private func updateThing(_ thing: FetchedResults<Thing>.Element) {
withAnimation {
thing.name = "Updated Name"
thing.comment = "Updated Comment"
saveContext()
}
}
private func deleteThings(offsets: IndexSet) {
withAnimation {
offsets.map { things[$0] }.forEach(viewContext.delete)
saveContext()
self.coverDeletedDetail = true
}
}
private func addThing() {
withAnimation {
let newThing = Thing(context: viewContext)
newThing.name = "New Thing"
newThing.comment = "New Comment"
newThing.date = Date()
newThing.count = Int64(self.count + 1)
self.count = self.count + 1
saveContext()
}
}
func saveContext() {
do {
try viewContext.save()
} catch {
print(error)
}
}
}
And Core Data:
extension Thing {
#nonobjc public class func fetchRequest() -> NSFetchRequest<Thing> {
return NSFetchRequest<Thing>(entityName: "Thing")
}
#NSManaged public var comment: String?
#NSManaged public var count: Int64
#NSManaged public var date: Date?
#NSManaged public var name: String?
}
extension Thing : Identifiable {
}
Any guidance would be appreciated. Xcode 12.2 iOS 14.2
You already mentioned it. CoreData works great with SwiftUI.
Just make your Thing as ObservableObject
#ObservedObject var thing: Thing
and then you can pass values from thing as Binding. This works in ForEach aswell
TextField("name", text: $thing.localName)
For others - note that I had to use the Binding extension above since NSManagedObjects are optionals. Thus as davidev stated:
TextField("name", text: Binding($thing.name, "no name"))
And ObservedObject, not Observable
I have a CoreData object Day which I am editing within this view, when in "editing" mode the card allows for a Picker view and then a TextField to allow user input. These all work as expected, however when I tap "done" which runs the saveEditToDay() function which is just saving those into the Day variable. This crashes with Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1cffc77e0) and gives the hint Modifying state during view update, this will cause undefined behavior.. I am not clear what state I modifying in this case?
Here is the view:
import SwiftUI
struct EditCard: View {
#Binding var day: Day?
var timeOfDay: Time
#State var isEditing: Bool = false
#State var selectedRating = 0
#State var description = ""
var ratings = Rating.getAllRatings()
// MARK: - Body
var body: some View {
VStack(alignment: .leading) {
HStack {
Text(timeOfDay.rawValue).font(.headline)
Spacer()
Button(action: {
if self.isEditing { self.saveEditToDay() }
self.isEditing.toggle()
}, label: { self.isEditing ? Text("Done") : Text("Edit") })
.foregroundColor(Color("PrimaryColour"))
}
Divider()
VStack(alignment: .leading) {
Text("RATING")
.font(.caption)
.foregroundColor(Color(UIColor.systemGray))
if isEditing {
Picker(selection: $selectedRating, label: Text("")) {
ForEach(0 ..< ratings.count) {
Text(self.ratings[$0]).tag([$0])
}
}
} else {
Text(day!.getRating(for: timeOfDay))
}
}.padding(.trailing)
VStack(alignment: .leading) {
Text("REASON")
.font(.caption)
.foregroundColor(Color(UIColor.systemGray))
if isEditing {
TextField(description, text: $description)
} else {
Text(description)
.multilineTextAlignment(.leading)
.lineLimit(nil)
}
}
}
.padding()
.background(Color(UIColor.secondarySystemGroupedBackground))
.cornerRadius(10)
.onAppear { self.setSelected() }
.onDisappear{ self.saveEditToDay(); print("Toodles") }
}
// MARK: - Functions
func setSelected() {
guard let day = day else { return }
self.selectedRating = ratings.firstIndex(of: day.getRating(for: timeOfDay))!
self.description = day.getDescription(for: timeOfDay)
}
func saveEditToDay() {
guard let day = day else { return }
day.setRating(rating: ratings[selectedRating], for: timeOfDay)
day.setDescription(description: description, for: timeOfDay)
}
}
Here at the Extensions to the NSManagedObject that i'm calling:
func setRating(rating: String, for time: Time) {
switch time {
case .morning: self.morning = rating;
case .afternoon: self.afternoon = rating;
case .evening: self.evening = rating;
}
}
func setDescription(description: String, for time: Time) {
switch time {
case .morning: self.morningDescription = description;
case .afternoon: self.afternoonDescription = description;
case .evening: self.eveningDescription = description;
}
}
You are calling setSelected() in onApear(), which modifies two of your variables marked as #State: selectedRating and description. This means that as the view gets drawn or redrawn you are changing the variables that define what is being drawn halfway through the process.
onApear() is a good place to trigger a background refresh, but not modify any #State variables instantly.
You probably should write an initialiser that takes binding to a Day and Time sets the other two variables just like you do in setSelected().