Post

Replies

Boosts

Views

Activity

Reply to What is the best and simple way to link a SwiftUI button action to a View Model?
This import SwiftUI import Combine struct ContentView: View {     @State private var isPresented = false var body: some View {     Button("Show Modal with full screen") {         self.isPresented.toggle()     }     .fullScreenCover(isPresented: $isPresented, content: VideoList.init)     } } struct VideoList: View {      @Environment(\.presentationMode) var presentationMode      @ObservedObject private(set) var viewModel: ViewModel      @State private var isRefreshing = false var body: some View {     NavigationView {         List(viewModel.videos.sorted { $0.id > $1.id}, id: \.id) { video in             NavigationLink(             destination: VideoDetails(viewModel: VideoDetails.ViewModel(video: video))) {                 VideoRow(video: video)             }         }         .onPullToRefresh(isRefreshing: $isRefreshing, perform: {             self.viewModel.fetchVideos()         })         .onReceive(viewModel.$videos, perform: { _ in             self.isRefreshing = false         })         .navigationBarTitle(viewModel.navigationBarTitle)     }     .onAppear(perform: viewModel.fetchVideos)     .frame(maxWidth: .infinity, maxHeight: .infinity)     .background(Color.red)     .edgesIgnoringSafeArea(.all)     .onTapGesture {         presentationMode.wrappedValue.dismiss()     } } } #if DEBUG struct VideoList_Previews: PreviewProvider {     static var previews: some View {         NavigationView {             VideoList(viewModel: .init())         }     } } #endif creates this error on line 19: Cannot convert value of type '(Environment&lt;Binding<PresentationMode&gt;>, VideoList.ViewModel) -> VideoList' to expected argument type '() -> VideoList'
Aug ’20
Reply to What is the best and simple way to link a SwiftUI button action to a View Model?
trying to implement it like this: import SwiftUI import Combine struct SwiftUIView: View {     @State var showSheetView = false     var body: some View {         NavigationView {             Text("Content")             .navigationBarTitle("SwiftUI Tutorials")             .navigationBarItems(trailing:                 Button(action: {                     self.showSheetView.toggle()                 }) {                     Image(systemName: "bell.circle.fill")                         .font(Font.system(.title))                 }             )         }.sheet(isPresented: $showSheetView) {             SheetView(showSheetView: self.$showSheetView)         }     } } struct SheetView: View {     @Binding var showSheetView: Bool     @ObservedObject private(set) var viewModel: ViewModel     @State private var isRefreshing = false          var body: some View {         NavigationView {             List(viewModel.videos.sorted { $0.id > $1.id}, id: \.id) { video in                 NavigationLink(                 destination: VideoDetails(viewModel: VideoDetails.ViewModel(video: video))) {                     VideoRow(video: video)                 }             }             .onPullToRefresh(isRefreshing: $isRefreshing, perform: {                 self.viewModel.fetchVideos()             })             .onReceive(viewModel.$videos, perform: { _ in                 self.isRefreshing = false             })             .navigationBarTitle(viewModel.navigationBarTitle)         }         .onAppear(perform: viewModel.fetchVideos)         .sheet(isPresented: $showSheetView) {                     SheetView(showSheetView: self.$showSheetView)                 }     } } leads to: Cannot find type 'ViewModel' in scope &amp; The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions; on line 35 and 38.
Aug ’20