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
 
//
//  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
 
//
//  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
 
//
//  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
 
//
//  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
 
//
//  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