Post

Replies

Boosts

Views

Activity

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 & 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
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 Simple way to load json remotely from server?
Posting the solution I tinkered around with until it actually worked... feel free to add extra refinements for future developers Your content view import AVKit struct ContentView: View {     @State private var wolData = [Main]()          var body: some View {                  NavigationView{List(wolData, id: \.id) { item in             NavigationLink(destination: CountryDetail(country: item)) {                                  HStack() {                     Text(item.name)                         .font(.headline)                     Text(item.date)                         .font(.footnote)                     if #available(iOS 15.0, *) {                         AsyncImage(url: URL(string: item.thumbnail))                         { image in                             image                                 .resizable()                                 .scaledToFill()                         } placeholder: {                             Color.purple.opacity(0.1)                         }                         .frame(width: 20, height: 20)                     } else {                         // Fallback on earlier versions                     }                 }                              }                      }.onAppear(perform: loadData)}              }           } extension ContentView {     func loadData() {                  guard let url = URL(string: "https://wolvideos.firebaseapp.com/Simple.json") else {             return         }                  let request = URLRequest(url: url)         URLSession.shared.dataTask(with: request) { data, response, error in                          if let data = data {                 if let response_obj = try? JSONDecoder().decode([Main].self, from: data) {                                          DispatchQueue.main.async {                         self.wolData = response_obj                     }                 }             }                      }.resume()     } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView()     } } your struct file struct Main: Decodable {     var id: Int     var name: String     var interactive: String     var thumbnail: String     var date: String     var videolink: String     var sharelink: String } your detail file ```import SwiftUI import AVKit struct CountryDetail: View {     var country: Main          var body: some View {                  VStack(alignment: .leading, spacing: 10) {             if #available(iOS 14.0, *) {                 VideoPlayer(player: AVPlayer(url:  URL(string: "https://bit.ly/swswift")!))             } else {                 // Fallback on earlier versions             }         }     } }
Jun ’22
Reply to Tab bar color does not respect in more view.
.tint(.white)         .onAppear {           UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation") // Forcing the rotation to portrait           AppDelegate.orientationLock = .portrait // And making sure it stays that way       }.onDisappear {           AppDelegate.orientationLock = .all // Unlocking the rotation when leaving the view       }         .onAppear {                     let appearance = UITabBarAppearance()                     appearance.backgroundEffect = UIBlurEffect(style: .systemUltraThinMaterial)                     appearance.backgroundColor = UIColor(Color.black.opacity(1.0))                                          // Use this appearance when scrolling behind the TabView:                     UITabBar.appearance().standardAppearance = appearance                     // Use this appearance when scrolled all the way up:                     UITabBar.appearance().scrollEdgeAppearance = appearance         }     } }
Jul ’22