Swipe Action Modifier working inconsistently on Swift Data Model

I’ve been trying to create a Workout tracking app and been coming across a weird bug that appears in my app.

When using a “Mark as Favourite” .swipeActions modifier in a ForEach and on a NavigationLink, the workout is inconsistently marked as a “Favourite”.

Here is the relevant WorkoutView code (within “var body: some View”) :

Workouts View

List {
            ForEach(searchedWorkouts, id: .id) { workout in
                NavigationLink {
                    WorkoutView(workout: workout, selectedTab: selectedTab, workoutTypes: workoutTypes, darkMode: darkMode)
                } label: {
                    WorkoutsViewHStack(workout: workout, darkMode: darkMode)
                }
                .modifier(SwipeActionsAndAlertsModifiers(workout: workout, showingDeleteAlert: $showingDeleteAlert, showDeleted: $showDeleted, editingWorkout: $editingWorkout, markAsFavouriteAlert: $markAsFavouriteAlert, unmarkAsFavouriteAlert: $unmarkAsFavouriteAlert))
                
                .fullScreenCover(isPresented: $editingWorkout) {
                    EditWorkoutView(workout: workout, workoutTypes: workoutTypes, selectedTab: $selectedTab, showingCompleted: $showingCompleted, showingLapsed: $showingLapsed, showingUpcoming: $showingUpcoming, darkMode: darkMode)
                }

                
            } 
        } 
        
        .searchable(text: $searchQuery, prompt: "Search by keyword or date")
    
        .overlay {
            if searchedWorkouts.isEmpty && searchQuery.isEmpty == false  {
                ContentUnavailableView {
                    Label("No results", systemImage: "magnifyingglass")
                        .padding()
                    
                    Text("Check the spelling or try a new search.")
                        .font(.subheadline)
                }
                
            } else if searchedWorkouts.isEmpty {
                ContentUnavailableView {
                    Label(filterType == "All" ? "No workouts found": "(filterType) workouts weren't found :( ", systemImage: "note")
                        .padding()
                } actions: {
                    Button {
                        showingAddWorkout.toggle()
                    } label: {
                        Label("Add a new workout", systemImage: "plus.square")
                    }
                }
            }
        }
        
        
    }

and here is the SwipeActionsAndAlertsModifiers file:

SwipeActionsAndAlertsModifiers modifier

struct SwipeActionsAndAlertsModifiers: ViewModifier {
    
    @Environment(.modelContext) var modelContext
    @Bindable var workout: Workout
    
    // For Deleting
    @Binding var showingDeleteAlert: Bool
    @Binding var showDeleted: Bool
    
    // For Editing
    @Binding var editingWorkout: Bool
    
    // For Favouriting
    @Binding var markAsFavouriteAlert: Bool
    @Binding var unmarkAsFavouriteAlert: Bool
    
    func body(content: Content) -> some View {
        content
            // Deleting
            .swipeActions(edge: .trailing, allowsFullSwipe: false) {
                Button {
                    showingDeleteAlert.toggle()
                } label: {
                    Image("icons8-delete-darkmode")
                        .resizable()
                        .frame(width: 28, height: 28)
                        .scaledToFit()
                        .tint(.red)
                }
            }
            
            .alert("Delete Workout", isPresented: $showingDeleteAlert) {
                Button("Delete", role: .destructive) {
                    modelContext.delete(workout)
                    showDeleted.toggle()
                }
                Button("Cancel", role: .cancel) {}
            } message: {
                Text("Are you sure you want to delete this workout permanently?")
            }
            
            // Favourited + Mark as Completed
            .swipeActions(edge: .leading, allowsFullSwipe: false) {
                
                Button {
                    if workout.favourites == false {
                        markAsFavouriteAlert.toggle()
                    } else {
                        unmarkAsFavouriteAlert.toggle()
                    }
                    
                } label: {
                    workout.favourites == false ?
                    
                    Image(systemName: "heart.fill")
                        .resizable()
                        .frame(width: 5, height: 5)
                        .scaledToFit()
                        .tint(.yellow) :
                    
                    Image(systemName: "heart.slash.fill") // padding of 60%
                        .resizable()
                        .frame(width: 5, height: 5)
                        .scaledToFit()
                        .tint(.yellow)
                    
                }
            }
            
            .alert("Mark as Favourite", isPresented: $markAsFavouriteAlert) {
                Button("Mark as Favourite", action: { workout.favourites = true })
                Button("Cancel", role: .cancel) {}
            } message: {
                Text("Mark this workout as a Favourite?")
            }
            
            .alert("Unmark as Favourite", isPresented: $unmarkAsFavouriteAlert) {
                Button("Unmark as Favourite", action: { workout.favourites = false })
                Button("Cancel", role: .cancel) {}
            } message: {
                Text("Unmark this workout as a Favourite?")
            }
        
    }
}

If any SwiftUI developer could let me know what the issue is / has the proficiency to debug it it would be much appreciated!! Let me know if more context explanation is required too. (The delete swipe action and alert works perfectly but the favouriting one doesn’t, even when it is supposed(?) to be as simple as workout.favourites = true or false)

The above is what I have tried but to no avail so far!

New contributor

Gabriel Tang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

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