Apologies for a somewhat complex code snippet, I tried to strip away as many red herrings as possible.
Expected Behaviour:
I can also fix the circle behaviour by copying everything from ExtractedView and implementing it in ContentView.
Am I doing something wrong? or is there something I can do differently to make my code work? (I would prefer to not copy everything from extractedView into contentView as my app requires n ExtractedViews and I'd rather not duplicate that code.
thanks in advance,
Mike
Expected Behaviour:
tap red circle
it animates to green
status text updates
circle returns to red
tap red circle
it animates to green
status text updates
circle does NOT return to red
I can also fix the circle behaviour by copying everything from ExtractedView and implementing it in ContentView.
Am I doing something wrong? or is there something I can do differently to make my code work? (I would prefer to not copy everything from extractedView into contentView as my app requires n ExtractedViews and I'd rather not duplicate that code.
thanks in advance,
Mike
Code Block class SubModel: ObservableObject { @Published var isActing: Bool = false func startActing() { isActing = true } } class Model: ObservableObject { @Published var status: Int = 0 // var status: Int = 0 let leftModel = SubModel() func startActingLeft() { leftModel.startActing() status += 3 } } struct ContentView: View { @ObservedObject var model: Model = Model() var body: some View { VStack { ExtractedView(model: model.leftModel) .onTapGesture { self.model.startActingLeft() } Text(String(model.status)) } } } struct ExtractedView: View { var model: SubModel @State var fillColor = Color.red private var actSubject = PassthroughSubject<Void, Never>() private var afterAct: AnyPublisher<Void, Never> init(model: SubModel) { self.model = model self.afterAct = actSubject .debounce(for: .milliseconds(1100), scheduler: DispatchQueue.main) .eraseToAnyPublisher() } var body: some View { ZStack { Circle() .fill(fillColor) .frame(width: 150, height: 150) } .onReceive(model.$isActing, perform: { if $0 { withAnimation(Animation.easeInOut(duration: 0.8), { self.fillColor = Color.green }) self.actSubject.send() } else { self.fillColor = Color.red } }) .onReceive(afterAct) { self.model.isActing = false } } }