article

Wednesday, February 16, 2022

SwiftUI Firebase Search Bar - Integrating searchable

SwiftUI Firebase Search Bar - Integrating searchable

https://console.firebase.google.com/

ContentView.swift
 
//
//  ContentView.swift
//  DevSwiftUI
//
//  Created by Cairocoders
//

import SwiftUI
import Firebase

struct ContentView: View {
    
    @State var search = ""
    
    @ObservedObject var data = getData()
    
    var body: some View {
       
        NavigationView{
            List {
 
                ForEach(self.data.datas.filter{(self.search.isEmpty ? true : $0.title.localizedCaseInsensitiveContains(self.search))}, id: \.id) { rs in
                    
                    NavigationLink(destination: Detail(data: rs)) {
                        Text(rs.title)
                    }
                }
            }
            .navigationBarTitle("Search Movie")
            .searchable(text: self.$search)
            {
                ForEach(data.datas, id:\.id) { info in
                    HStack {
                        Text(info.title)
                            .searchCompletion(info.title)
                    }
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


struct Detail : View {
    
    var data : dataType
    
    var body : some View{
        VStack {
            Text(data.title)
                .font(.title)
                .fontWeight(.bold)
            Text(data.description)
        }.padding()
    }
}
Model.swift
 
//
//  Model.swift
//  DevSwiftUI
//
//  Created by Cairocoders
//
import SwiftUI
import Firebase

class getData : ObservableObject{
    
    @Published var datas = [dataType]()
    
    init() {
        
        let db = Firestore.firestore()
        
        db.collection("movielist").getDocuments { (snap, err) in
            
            if err != nil{
                
                print((err?.localizedDescription)!)
                return
            }
            
            for i in snap!.documents{
                
                let id = i.documentID
                let title = i.get("title") as! String
                let description = i.get("description") as! String
                
                self.datas.append(dataType(id: id, title: title, description: description))
            }
        }
    }
}

struct dataType : Identifiable {
    
    var id : String
    var title : String
    var description : String
}
 
//
//  DevSwiftUIApp.swift
//  DevSwiftUI
//
//  Created by Cairocoders
//
  
import SwiftUI
import Firebase
  
@main
struct DevSwiftUIApp: App {
      
    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
       
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
   
class AppDelegate: NSObject,UIApplicationDelegate{
       
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
           
        FirebaseApp.configure()
        return true
    }
}

Related Post