Learn Courses My Dashboard

Cannot find 'withAnimation ' in scope

Good evening. I am trying to add code to save and sort favorites in a List. Unfortunately I can’t figure out the error I get withAnimation. I believe it has to do with explicit operations but I am new to this and can’t figure it out. Thanks for any help or guidance to continue my studies.

Error: cannot find ‘withAnimation’ in scope.

import Foundation

extension MyFlightView {
    final class MyFlightViewModel: ObservableObject {
        @Published var items = [Title]()
        @Published var showingFavs = false
        @Published var savedItems: Set<Int> = [1]
        
        var filteredItems: [Title] {
            if showingFavs {
                return items.filter { savedItems.contains($0.ids)}
            } else {
                return items
            }
        }
        
        private var db = Database()
        
        init() {
            //self.savedItems = db.load()
            self.items = items
        }
        
        func sortFavs() {
          withAnimation() {
           showingFavs.toggle()
           }
        }
        
        func contains(_ item: Title) -> Bool {
            savedItems.contains(item.ids)
        }
        
        func toggleFav(item: Title) {
            if contains(item) {
                savedItems.remove(item.ids)
            } else {
                savedItems.insert(item.ids)
            }
            db.save(items: savedItems)
        }
    }
}
import SwiftUI

struct MyFlightView: View {
    //reference the view model
    @ObservedObject var model = TitleModel()
    
    @StateObject private var vm = MyFlightViewModel()
    
    var body: some View {
        
        VStack {
            Button("Toggle Favorites", action: vm.sortFavs)
                .padding()
        }
        
    NavigationView {
            List {
                
                ForEach(model.titles) { item in
                     
                        NavigationLink(destination: DetailView(detail: item),
                                       label: {
                            
                            HStack(spacing: 20.0) {
                                Image(item.image1)
                                    .resizable()
                                    .scaledToFill()
                                    .frame (width: 50, height: 50, alignment: .center)
                                    .clipped()
                                    .cornerRadius(5)
                                Text(item.title)
                                
                                Spacer()
                                
                                Image(systemName: vm.contains(item) ?
                                      "star.fill" : "star")
                                .foregroundColor(.yellow)
                                .onTapGesture {
                                    vm.toggleFav(item: item)
                                }

                            }
                            .cornerRadius(10)
                        })
              
                }
                .onMove(perform: move)
                
            }
            .toolbar {
                EditButton()
                    Text("Move")
            }
        
            .navigationBarTitle("MyFlight")
        }
    }
    
    func move(from source:IndexSet, to destination: Int) {
        (model.titles).move(fromOffsets: source, toOffset: destination)
     }
    
}

struct MyFlightView_Previews: PreviewProvider {
    static var previews: some View {
        MyFlightView()
    }
}

You will have to import SwiftUI in your file where you have your extension defined.

Thank you sir!

Good morning Chris. Thanks for the help on the addition of the ‘import SwiftUI’. That fixed the error however the “Toggle Favorites” Button still does not work. My intent is to only List the identified title favorites or every title when it is toggled. I think it has something to do with the function sortFavs which calls the withAnimation. I have watched a few tutorials outside of your site. Is there anything on your site that I can study to help me out? I have attached the file. The Toggle button is in ‘MyFlightView’. Thanks for your continued support in my studies. After this issue is resolved I can move forward with finalizing the UI and content.

Enjoy your day,
Shawn

https://drive.google.com/file/d/1qjvmHju5UbeJvlkTnMg4msFxl5VAn8Yi/view?usp=sharing

@Ready7

Hi Shawn,

The file on Google Drive is restricted access requiring authentication. Probably best to PM me with the code if you don’t want to make it public.