-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkingServices.swift
126 lines (80 loc) · 3.51 KB
/
NetworkingServices.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// NetworkingServices.swift
// GOT
//
// Created by Kenneth Okereke on 3/31/17.
// Copyright © 2017 Mexonis. All rights reserved.
//
import Foundation
import Firebase
struct NetworkingServices {
var databaseRef: FIRDatabaseReference! {
return FIRDatabase.database().reference()
}
var storageRef: FIRStorageReference! {
return FIRStorage.storage().reference()
}
func fetchPostUserInfo(forUserID: String, completion: @escaping (User?)->()){
//let currentUser = FIRAuth.auth()!.currentUser!
let userRef = databaseRef.child("user").child(forUserID).child("credentials")
userRef.observeSingleEvent(of: .value, with: { (currentUser) in
let user: User = User(snapshot: currentUser)
completion(user)
}) { (error) in
print(error.localizedDescription)
}
}
func uploadVideoToFirebase(postId: String, videoURL: NSURL, completion: @escaping (URL)->()){
let postImagePath = "postImages/\(postId)video.mov"
let postImageRef = storageRef.child(postImagePath)
postImageRef.putFile(videoURL as URL, metadata: nil) { (metadata, error) in
if let error = error {
print(error.localizedDescription)
}else {
if let postVideoURL = metadata?.downloadURL() {
completion(postVideoURL)
}
}
}
}
func uploadImageToFirebase(postId: String, imageData: Data, completion: @escaping (URL)->()){
let postImagePath = "postImages/\(postId)image.jpg"
let postImageRef = storageRef.child(postImagePath)
let postImageMetadata = FIRStorageMetadata()
postImageMetadata.contentType = "image/jpeg"
postImageRef.put(imageData, metadata: postImageMetadata) { (newPostImageMD, error) in
if let error = error {
print(error.localizedDescription)
}else {
if let postImageURL = newPostImageMD?.downloadURL() {
completion(postImageURL)
}
}
}
}
func savePostToDB(post: Post, completed: @escaping ()->Void){
let postRef = databaseRef.child("Gotposts").childByAutoId()
postRef.setValue(post.toAnyObject()) { (error, ref) in
if let error = error {
print(error.localizedDescription)
}else {
let alertView = SCLAlertView()
_ = alertView.showSuccess("Success", subTitle: "Post saved successfuly", closeButtonTitle: "Done", duration: 4, colorStyle: UIColor(colorWithHexValue: 0x3D5B94), colorTextButton: UIColor.white)
completed()
}
}
}
func fetchAllPosts(completion: @escaping ([Post])->()){
let postsRef = databaseRef.child("Gotposts")
postsRef.observe(.value, with: { (Gotposts) in
var resultArray = [Post]()
for post in Gotposts.children {
let post = Post(snapshot: post as! FIRDataSnapshot)
resultArray.append(post)
}
completion(resultArray)
}) { (error) in
print(error.localizedDescription)
}
}
}