SHARE
TWEET

Untitled

a guest Jan 20th, 2017 111 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import UIKit
  2. import Firebase
  3.  
  4. struct Reference {
  5.    
  6.     struct FBRef {
  7.        
  8.         static var allUsers = FIRDatabase.database().reference().child("users")
  9.        
  10.         static var allJobs = FIRDatabase.database().reference().child("jobs")
  11.        
  12.         static var allChats = FIRDatabase.database().reference().child("chatHistory")
  13.        
  14.         static var storage = FIRStorage.storage().reference(forURL: "gs://stitchedapp.appspot.com")
  15.        
  16.         // MARK: user management
  17.         static func register(withEmail email: String, password: String, completion: @escaping (_ user: FIRUser?, _ error: Error?) -> Swift.Void) {
  18.             FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in
  19.                 completion(user, error)
  20.             })
  21.         }
  22.        
  23.        
  24.         static func login(withEmail email: String, password: String, completion: @escaping (_ user: FIRUser?, _ error: Error?) -> Swift.Void) {
  25.             FIRAuth.auth()?.signIn(withEmail: email, password: password, completion: { (user, error) in
  26.                 completion(user, error)
  27.             })
  28.         }
  29.        
  30.        
  31.         static func updateUser(with usr: User, completion:  @escaping (_ ref: FIRDatabaseReference?, _ error: Error?) -> Swift.Void) {
  32.             let date = NSDate().timeIntervalSince1970
  33.             let loc = String(format:"%f", usr.location.latitude) + "," + String(format:"%f", usr.location.longitude)
  34.             let user = ["name": usr.name,
  35.                         "email": usr.email,
  36.                         "phone": usr.phoneNumber,
  37.                         "avatar_image": usr.avatar,
  38.                         "role": usr.role,
  39.                         "created_date": date,
  40.                         "ranking": usr.ranking,
  41.                         "follower": "\(usr.follower)",
  42.                         "network": "\(usr.network)",
  43.                         "location": loc,
  44.                         "verified": String(usr.isVerified),
  45.                         "followers": [Any]()] as [String : Any]
  46.             let record = [usr.id: user]
  47.             Reference.FBRef.allUsers.updateChildValues(record) { (error, ref) in
  48.                 completion(ref, error)
  49.             }
  50.         }
  51.        
  52.        
  53.         static func logout() {
  54.             do {
  55.                 try FIRAuth.auth()?.signOut()
  56.             } catch let error {
  57.                 print("sign out with \(error)")
  58.             }
  59.         }
  60.        
  61.        
  62.         // MARK: avatar management
  63.         static func storageForAvatar(userID: String) -> FIRStorageReference {
  64.             return Reference.FBRef.storage.child("userdata").child("user-" + userID).child("avatar.jpg")
  65.         }
  66.        
  67.        
  68.         static func uploadAvatarFile(withID: String, imgData: Data, completion: @escaping (_ data: FIRStorageMetadata?, _ error: Error?) -> Swift.Void) {
  69.             let storageURL = Reference.FBRef.storageForAvatar(userID: withID)
  70.             let metaData = FIRStorageMetadata()
  71.             metaData.contentType = "image/jpg"
  72.            
  73.             storageURL.put(imgData, metadata: metaData, completion: { (data, error) in
  74.                 completion(data, error)
  75.             })
  76.         }
  77.        
  78.        
  79.         // MARK: job management
  80.         static func loadPostedJob(Of user: User?, forUser: User?, completion: @escaping (_ jobs: [PostedJob]) -> Swift.Void) {
  81.             Reference.FBRef.allJobs.observe(.value, with: { (snap) in
  82.                 guard (snap.value as AnyObject).classForCoder != NSNull.classForCoder() else { return }
  83.                
  84.                 let allItems = snap.value as! [String: AnyObject]!
  85.                
  86.                 var jobs = [PostedJob]()
  87.                
  88.                 for key in (allItems?.keys)! {
  89.                     let each = allItems?[key] as! [String: AnyObject]!
  90.                    
  91.                     var aJob = PostedJob()
  92.                     aJob.id = key
  93.                     aJob.title = each?["title"] as! String?
  94.                     aJob.description = each?["description"] as! String?
  95.                     aJob.category = JobCategory(s: (each?["category"] as! String?)!)
  96.                     aJob.deliveryTime = JobDeliverTime(s: (each?["deliverTime"] as! String?)!)
  97.                     aJob.price = each?["price"] as! String?
  98.                     aJob.attachType = JobAttachType(s: (each?["attachType"] as! String?)!)
  99.                     aJob.attachURL = each?["attachURL"] as! String?
  100.                     aJob.clientID = each?["owner"] as! String?
  101.                    
  102.                     if each?["bids"] != nil {
  103.                         let bids = each?["bids"] as! [String: AnyObject]!
  104.                         aJob.bids = bids
  105.                     }
  106.                    
  107.                     if user?.id == each?["owner"] as! String? || (user == nil && forUser == nil) {
  108.                         jobs.append(aJob)
  109.                        
  110.                     } else if forUser != nil && aJob.bids?[(forUser?.id)!] != nil {
  111.                         jobs.append(aJob)
  112.                     }
  113.                 }
  114.                
  115.                 completion(jobs)
  116.             })
  117.         }
  118.        
  119.         static func storageForJobAttach(jobID: String) -> FIRStorageReference {
  120.             return Reference.FBRef.storage.child("jobattach").child(jobID)
  121.         }
  122.        
  123.        
  124.         static func uploadJobAttach(withID: String, data: Data, type: JobAttachType, completion: @escaping (_ data: FIRStorageMetadata?, _ error: Error?) -> Swift.Void) {
  125.             let storageURL = Reference.FBRef.storageForJobAttach(jobID: withID).child("attach." + type.rawValue)
  126.             let metaData = FIRStorageMetadata()
  127.             metaData.contentType = (type == .image ? "image/jpg" : "video/quicktime")
  128.            
  129.             storageURL.put(data, metadata: metaData) { (data, error) in
  130.                 completion(data, error)
  131.             }
  132.         }
  133.        
  134.        
  135.         static func uploadJob(withJob: Job, attachURL: String, completion:  @escaping (_ ref: FIRDatabaseReference?, _ error: Error?) -> Swift.Void) {
  136.             let date = NSDate().timeIntervalSince1970
  137.             let job = ["title": withJob.title!,
  138.                        "description": withJob.description!,
  139.                        "category": withJob.category.rawValue,
  140.                        "deliverTime": withJob.deliveryTime.rawValue,
  141.                        "price": withJob.price!,
  142.                        "owner": withJob.clientID!,
  143.                        "attachType": withJob.attachment.type.rawValue,
  144.                        "attachURL": attachURL,
  145.                        "created_date": date] as [String: Any]
  146.             let record = [withJob.id! : job]
  147.            
  148.             Reference.FBRef.allJobs.updateChildValues(record) { (error, ref) in
  149.                 completion(ref, error)
  150.             }
  151.         }
  152.        
  153.        
  154.         // MARK: bid to job
  155.         static func bidTo(Job job: PostedJob, user: User, propsal: String, completion:  @escaping (_ ref: FIRDatabaseReference?, _ error: Error?) -> Swift.Void) {
  156.             let date = NSDate().timeIntervalSince1970
  157.             let bid = ["created_date": date,
  158.                        "proposal": propsal] as [String: Any]
  159.             let record = [user.id: bid]
  160.             Reference.FBRef.allJobs.child(job.id!).child("bids").updateChildValues(record) { (error, ref) in
  161.                 completion(ref, error)
  162.             }
  163.         }
  164.        
  165.     }
  166.    
  167. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top