https://console.firebase.google.com/
ContentView.swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | // // 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() } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | // // 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 } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | // // 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 } } |