article

Sunday, September 19, 2021

SwiftUI Firebase Fetching data from Firestore with SDWebImageSwiftUI and Details View

SwiftUI Firebase Fetching data from Firestore with SDWebImageSwiftUI and Details View

Login to fireabase and create database https://console.firebase.google.com/
SDWebImage https://github.com/SDWebImage/SDWebImageSwiftUI

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
//
//  ContentView.swift
//  DevSwiftUI
//
//  Created by Cairocoders
//
 
import SwiftUI
import Firebase
import SDWebImageSwiftUI
 
struct ContentView: View {
     
    var body: some View {
        Main()
    }
}
 
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
 
struct Main : View {
   
  @ObservedObject var obser = observer()
  var body : some View{
       
        NavigationView {
            List(obser.users) { i in
                NavigationLink(destination : Details(userItem: i)) {
                    HStack {
                        AnimatedImage(url: URL(string: i.image)).resizable().frame(width: 60, height: 60).clipShape(Circle()).shadow(radius: 20)
                          
                        Text(i.name).padding(.leading, 10)
                    }
                }
            }.navigationBarTitle("Users")
        }
  }
}
datatypes.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//
//  datatypes.swift
//  DevSwiftUI
//
//  Created by Cairocoders
//
 
import Foundation
import SwiftUI
 
struct datatype : Identifiable {
   
  var id : String
  var name : String
  var image : String
  var age : String
  var status : String
}
observer.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
//
//  observer.swift
//  DevSwiftUI
//
//  Created by Cairocoders
//
 
import SwiftUI
import Firebase
 
class observer : ObservableObject{
   
  @Published var users = [datatype]()
     
    init() {
         
        let db = Firestore.firestore()
         
        db.collection("users").getDocuments { (snap, err) in
             
            if err != nil{
                 
                print((err?.localizedDescription)!)
                return
            }
             
            for i in snap!.documents{
                 
                let name = i.get("name") as! String
                let age = i.get("age") as! String
                let image = i.get("image") as! String
                let id = i.documentID
                let status = i.get("status") as! String
                 
                 
                if status == ""{
                     
                self.users.append(datatype(id: id, name: name, image: image, age: age, status: status))
                     
                }
 
            }
        }
    }
}
Details.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
//
//  Details.swift
//  DevSwiftUI
//
//  Created by Cairocoders
//
 
import SwiftUI
import SDWebImageSwiftUI
 
struct Details: View {
     
    let userItem : datatype
     
    var body: some View {
        GeometryReader{geo in
            //Text(userItem.name)
            VStack {
                Text("Username : \(userItem.name)")
                    .font(.title2)
                  
                AnimatedImage(url: URL(string: userItem.image))
                    .resizable().frame(height: geo.size.height - 100)
                    .padding(.horizontal, 15)
                    .cornerRadius(20)
            }
        }
    }
}
DevSwiftUIApp.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
//
//  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