How to Automatically Update a View When a Variable Changes in Swift

I have a Flutter Application that regularly sends me “messages” with Data. In my ViewModel I have the code to decode the data and in my ContentView I make an @State private var viewModel = ViewModel, and pass variables from that viewModel Instance to my other Views through the NavigationLink in my ContentView. The ChallengeTabView (the simplified version that doesn’t work is below) accepts the currentChallenge and every time it gets changed I want the view to update.

struct ChallengeTabView: View {
    let currentChallenge: CoreChallengeData
    @State private var timer: Timer?
    @State private var counter = 0
    @State private var isRunning = false
    @State private var showCancelAlert = false
    private var editableButtons: Bool {
        return currentChallenge.state.type != .DelayedChallengeState || currentChallenge.state.type != .NoActiveChallengeState
    }
    private var challengeIsActive: Bool {
        currentChallenge.state.type != .NoActiveChallengeState
    }
    @State private var message = "No Message"
    @State private var conter = 0
    
    
    var body: some View {
        NavigationStack{
            Text(message)
            TabView{
                if challengeIsActive{
                    VStack(alignment: .leading){
                        Text("(DateUtilities.formatSecondsToString(duration: currentChallenge.state.data.duration - counter))")
                            .font(.largeTitle)
                        HStack{
                            ForEach(0..<currentChallenge.state.data.maxLives, id: .self){ life in
                                if life > currentChallenge.state.data.currentLives - 1{
                                    Image(systemName: "heart.slash")
                                        .resizable()
                                        .scaledToFit()
                                        .foregroundStyle(.tertiary)
                                } else{
                                    Image(systemName: "heart.fill")
                                        .resizable()
                                        .scaledToFit()
                                        .foregroundStyle(.red)
                                }
                            }
                        }
                        .frame(height: 30)
                    }
                }
                else {
                    Text("Keine Aktive Challenge")
                }
            }
            .navigationTitle("Challenge")
            .navigationBarTitleDisplayMode(.inline)
        }
    }
}

So if I get a new message the view gets updated, and for example the currentLives var of my struct gets updated in the View. But I also want to make a switch for one of the other Variables in my Struct, that is an Enum. Depending on the type I want to get different behavior. I tried putting that switch on .onAppear oder .task but neither of that worked, because I always had to navigate back to the content view and click on the NavLink again to have the code in the onAppear executed. So how can this switch be executed every time the currentChallenge variable gets changed/replaced.

My struct:

enum StateType: String, Codable{
    case  NoActiveChallengeState, DelayedChallengeState, PausedChallengeState, ActiveChallengeState, BelowThresholdState
}

struct CoreChallengeData: Codable{
    let event: Event
    let state: CurrentState
}

struct CurrentState: Codable {
    let data: ChallengeInformation
    let type: StateType
    
}
struct Event: Codable {
    let type: String
}

struct ChallengeInformation: Codable {
    let currentLives: Int
    let duration: Int
    let maxLives: Int
    let reward: Int
    let since: String
}

My ContentView:

struct ContentView: View {
    @State private var viewModel: WatchViewModel = WatchViewModel()
    @State private var vibrates = false
    @State private var showBatterySheet = false
    var body: some View {
        NavigationStack{
            NavigationLink("Challenge View"){
                ChallengeTabView(currentChallenge: viewModel.challengeMessage)
            }
            ScoreTabView(liveScores: viewModel.liveScores)
                .accessibilityLabel("Start")
                .onAppear{
                    //viewModel.sendDataMessage(for: .syncRequest)
                }
                .sheet(isPresented: $showBatterySheet) {
                    VStack{
                        Text("Batterie:")
                        Text("99%")
                            .font(.title)
                            .foregroundStyle(.green)
                    }
                }
        }
    
    }
}

I tried onAppear and .task but both didn’t work unless I navigated back to the ContentView and then clicked on the NavLink again.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật