0

I have a managed object, Treatment, in a one-to-many relationship with another managed object TreatmentTime.

I am presenting the different TreatmentTime objects associated with a given Treatment in a ForEach and I want to be able to animate adding and deleting TreatmentTime's. I've tried a whole bunch of things but none of it has worked.

/// this is being presented within another view
struct TreatmentView_Manual: View {
    @ObservedObject var treatment: Treatment
    var body: some View {
        ForEach(Array(zip(Array(treatment.treatmentTimes as? Set ?? Set<TreatmentTime>()).sorted { $0.time ?? Date() < $1.time ?? Date() }, treatment.wrappedTreatmentTimes.indices)), id: \.0) { (treatmentTime, index) in
            Treatment_DatePicker(treatmentTime: treatmentTime, index: index)
        }
        HStack {
            Button("Add another") {
               addTreatment()
            }
            
            Spacer()
            
            if treatment.numberOfAdmins > 1 {
                Button("Remove last"){
                    removeTreatment()
                }.foregroundColor(.red)
            }
        }
    }
    func addTreatment() {
        treatment.addToTreatmentTimes(TreatmentTime(context: treatment.managedObjectContext!))
    }
    
    func removeTreatment(_ treatmentTime: TreatmentTime) {
        // function not yet written
    }
}

struct Treatment_DatePicker: View {
    @ObservedObject var treatmentTime: TreatmentTime
    let index: Int
    var body: some View {
        DatePicker("Dose #\(index + 1)", selection: $treatmentTime.time ?? Date())
    }
}

Any advice would be greatly appreciated. Additionally, if you notice anything else pertaining to my code that can be improved I would love to hear it as well.

pakobongbong
  • 123
  • 1
  • 9
  • 2
    This needs a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). You will need to change out your `ObservableObject` for something more mundane. Also, what do you want to animate, and how, exactly. Lastly, as an unrelated comment, why are you using an `ObservableObject` for a `Core Data` managed object, instead of an `@FetchRequest`? – Yrb Nov 26 '21 at 13:45
  • @Yrb Thanks for the reply. I'll work on producing one. I am trying to animate removing and adding the `DatePicker`'s that are being presented in the `ForEach` when the number of `TreatmentTime`'s associated with a `Treatment` changes. The reason I used an `ObservableObject` instead of a `FetchRequest` was so that I could read and write the managed object property `time` in the `DatePicker`. These objects are also associated with a child context and haven't yet been committed to the persistent store. It's entirely possible I have a gross misunderstand here, however. – pakobongbong Nov 26 '21 at 14:28
  • 1
    In your MRE, please show what you have tried. Are you just looking for a default animation? As to the read and write of the `time` attribute, you can that from the managed object class itself. A good example for doing that can be found in [Stanford's CS193P lectures.](https://cs193p.sites.stanford.edu) – Yrb Nov 26 '21 at 15:34
  • 2
    Off topic but why not add a method in an extension to Treatment that returns an array of TreatmentTime, it would make the code easier to read – Joakim Danielson Nov 26 '21 at 15:56
  • @Yrb thanks a lot. I will check that resource out – pakobongbong Nov 26 '21 at 17:14
  • @JoakimDanielson thanks for that advice. The code is really unreadable I know, I'll try and use more extensions going forward. – pakobongbong Nov 26 '21 at 17:15

0 Answers0