Skip to content

Commit 0210a3f

Browse files
committed
Clean stuff and added decodable.
1 parent 1e28979 commit 0210a3f

11 files changed

+203
-41
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ let package = Package(
2828
)
2929
],
3030
dependencies: [
31-
.package(url: "https://github.com/firebase/firebase-ios-sdk", .upToNextMajor(from: "11.5.0")),
31+
.package(url: "https://github.com/firebase/firebase-ios-sdk", .upToNextMajor(from: "11.6.0")),
3232
.package(url: "https://github.com/google/GoogleSignIn-iOS", .upToNextMajor(from: "8.0.0")),
3333
.package(url: "https://github.com/sparrowcode/SwiftBoost", .upToNextMajor(from: "4.0.9"))
3434
],

Sources/FirewrapAuth/Error/FirewrapAuthSignInError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ public enum FirewrapAuthSignInError: Error {
55

66
case cantPresent
77
case mustConfirmViaEmail
8-
case failed
8+
case unknow
99
}

Sources/FirewrapAuth/FirewrapAuth.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ public class FirewrapAuth {
9090
AppleAuthService.signIn(on: window) { data, appleError in
9191
if let appleError {
9292
printConsole("Sign in with Apple got error: \(appleError.localizedDescription)")
93-
completion?(nil, .failed)
93+
completion?(nil, .unknow)
9494
return
9595
}
9696
guard let data else {
97-
completion?(nil, .failed)
97+
completion?(nil, .unknow)
9898
return
9999
}
100100

@@ -111,7 +111,7 @@ public class FirewrapAuth {
111111
Auth.auth().signIn(with: credential) { (authResult, firebaseError) in
112112
if let firebaseError {
113113
printConsole("Sign in with Apple complete with Firebase error: \(firebaseError.localizedDescription)")
114-
completion?(data, .failed)
114+
completion?(data, .unknow)
115115
} else {
116116
printConsole("Sign in with Apple complete successfully")
117117
completion?(data, nil)
@@ -129,17 +129,17 @@ public class FirewrapAuth {
129129
return
130130
}
131131
guard let data else {
132-
completion?(.failed)
132+
completion?(.unknow)
133133
return
134134
}
135135
let credential = GoogleAuthProvider.credential(withIDToken: data.identityToken, accessToken: data.accessToken)
136136
Auth.auth().signIn(with: credential) { (authResult, firebaseError) in
137137
if let firebaseError {
138138
printConsole("Sign in with Google complete with Firebase error: \(firebaseError.localizedDescription)")
139-
completion?(.failed)
139+
completion?(.unknow)
140140
} else {
141141
printConsole("Sign in with Google complete")
142-
completion?(.mustConfirmViaEmail) // Not exactly error, but shoud show user what need open email
142+
completion?(nil)
143143
}
144144
}
145145
}
@@ -154,7 +154,7 @@ public class FirewrapAuth {
154154
EmailAuthService.signIn(email: email, handleURL: handleURL) { emailError in
155155
if let emailError {
156156
printConsole("Sign in with Email complete with Firebase error: \(emailError.localizedDescription)")
157-
completion?(.failed)
157+
completion?(.unknow)
158158
} else {
159159
printConsole("Sign in with Email success complete")
160160
shared.completionSignInViaEmail = completion
@@ -169,13 +169,13 @@ public class FirewrapAuth {
169169
return false
170170
}
171171
guard let processingEmail = EmailAuthService.processingEmail else {
172-
completion?(.failed)
172+
completion?(.unknow)
173173
return false
174174
}
175175
Auth.auth().signIn(withEmail: processingEmail, link: url.absoluteString) { user, emailError in
176176
if let emailError {
177177
printConsole("Sign in with Email confirm action complete with Firebase error: \(emailError.localizedDescription)")
178-
completion?(.failed)
178+
completion?(.unknow)
179179
} else {
180180
printConsole("Sign in with Email confirm action complete")
181181
completion?(nil)
@@ -185,14 +185,14 @@ public class FirewrapAuth {
185185
}
186186

187187
public static func signOut(completion: @escaping (Error?)->Void) {
188-
printConsole("Sign out...")
188+
printConsole("Sign out run")
189189
do {
190190
try Auth.auth().signOut()
191191
completion(nil)
192192
} catch {
193193
completion(error)
194194
}
195-
printConsole("Sign out complete")
195+
printConsole("Sign out done")
196196
}
197197

198198
public static func revokeSignInWithApple(authorizationCode: String) {
@@ -223,7 +223,7 @@ public class FirewrapAuth {
223223
}
224224
}
225225

226-
private static func printConsole(_ text: String) {
226+
static func printConsole(_ text: String) {
227227
debug("Firewrap, Auth: " + text)
228228
}
229229

Sources/FirewrapAuth/FirewrapAuthProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public enum FirewrapAuthProvider: String, CaseIterable {
1414
return provider
1515
}
1616
}
17-
print("Firewrap: Can't get provider by web url \(url)")
17+
FirewrapAuth.printConsole("Can't get provider by web url \(url)")
1818
return nil
1919
}
2020

Sources/FirewrapAuth/Services/AppleAuthService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class AppleAuthService: NSObject, ASAuthorizationControllerDelegate, ASAuthoriza
2323
let authorizationCode = appleCredential.authorizationCode,
2424
let authorizationCodeString = String(data: authorizationCode, encoding: .utf8)
2525
else {
26-
completion(nil, FirewrapAuthSignInError.failed)
26+
completion(nil, FirewrapAuthSignInError.unknow)
2727
return
2828
}
2929

Sources/FirewrapAuth/Services/EmailAuthService.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ class EmailAuthService {
1010
actionCodeSettings.handleCodeInApp = true
1111
actionCodeSettings.setIOSBundleID(Bundle.main.bundleIdentifier!)
1212
Auth.auth().sendSignInLink(toEmail: email, actionCodeSettings: actionCodeSettings) { emailError in
13-
print(emailError.debugDescription)
13+
if let emailError {
14+
FirewrapAuth.printConsole(emailError.localizedDescription)
15+
}
1416
completion?(emailError)
1517
}
1618
}

Sources/FirewrapAuth/Services/GoogleAuthService.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ class GoogleAuthService {
77

88
public static func signIn(on controller: UIViewController, completion: ((SignInWithGoogleData?, FirewrapAuthSignInError?) -> Void)?) {
99
guard let clientID = FirebaseApp.app()?.options.clientID else {
10-
completion?(nil, .failed)
10+
completion?(nil, .unknow)
1111
return
1212
}
1313
let config = GIDConfiguration(clientID: clientID)
1414
GIDSignIn.sharedInstance.configuration = config
1515
GIDSignIn.sharedInstance.signIn(withPresenting: controller) { result, googleError in
1616
guard googleError == nil else {
17-
completion?(nil, .failed)
17+
completion?(nil, .unknow)
1818
return
1919
}
2020
guard let user = result?.user, let idToken = user.idToken?.tokenString else {
21-
completion?(nil, .failed)
21+
completion?(nil, .unknow)
2222
return
2323
}
2424
let data = SignInWithGoogleData(identityToken: idToken, accessToken: user.accessToken.tokenString)

Sources/FirewrapAuth/Views/WrapperSignInWithAppleButton.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public struct WrapperSignInWithAppleButton: View {
2121
if let data {
2222
FirewrapAuth.signInWithApple(with: data, completion: completion)
2323
} else {
24-
self.completion?(nil, error ?? .failed)
24+
self.completion?(nil, error ?? .unknow)
2525
}
2626
}
2727
case .failure(_):
28-
self.completion?(nil, .failed)
28+
self.completion?(nil, .unknow)
2929
}
3030
}
3131
}

Sources/FirewrapDatabase/FirewrapCollection.swift

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,108 @@ public class FirewrapCollection {
1111
self.path = path
1212
}
1313

14+
public func document() -> FirewrapDocument {
15+
let db = Firestore.firestore()
16+
let newDocument = db.collection(path).document()
17+
return FirewrapDocument(newDocument.path)
18+
}
19+
20+
public func document(_ path: String) -> FirewrapDocument {
21+
return FirewrapDocument(self.path + "/" + path)
22+
}
23+
1424
// MARK: - Getter
1525

16-
func getDocuments(_ source: FirewrapSource = .default, completion: @escaping (([[String : Any]]?) -> Void)) {
26+
public func getDocuments(_ source: FirewrapSource = .default, completion: @escaping ([[String : Any]]?) -> Void) {
1727
let db = Firestore.firestore()
1828
db.collection(path).getDocuments(source: source.firebaseValue) { snapshot, error in
29+
1930
guard error == nil, let documents = snapshot?.documents else {
2031
completion(nil)
2132
return
2233
}
23-
completion(documents.map({ $0.data() }))
34+
35+
let response = documents.map { $0.data() }
36+
completion(response)
37+
}
38+
}
39+
40+
public func getDocuments<T: Decodable>(as type: T.Type, source: FirewrapSource = .default, completion: @escaping ([T]?, Error?) -> Void) {
41+
let db = Firestore.firestore()
42+
43+
db.collection(path).getDocuments(source: source.firebaseValue) { (snapshot, error) in
44+
45+
guard error == nil, let snapshot else {
46+
completion(nil, error)
47+
return
48+
}
49+
50+
let models = Self.decode(as: T.self, from: snapshot)
51+
completion(models, nil)
2452
}
2553
}
2654

27-
func getDocument(_ source: FirewrapSource = .default, where field: String, equal: [Any], completion: @escaping (([[String : Any]]?) -> Void)) {
55+
public func getDocument(_ source: FirewrapSource = .default, where field: String, equal: [Any], completion: @escaping (([[String : Any]]?) -> Void)) {
2856
let db = Firestore.firestore()
2957
db.collection(path).whereField(field, in: equal).getDocuments(source: source.firebaseValue) { snapshot, error in
58+
3059
guard error == nil, let documents = snapshot?.documents else {
3160
completion(nil)
3261
return
3362
}
63+
3464
completion(documents.map({ $0.data() }))
3565
}
3666
}
3767

3868
// MARK: - Observer
3969

40-
func observe(_ handler: @escaping ([[String : Any]]?) -> Void) {
70+
public func observe<T: Decodable>(as type: T.Type, handler: @escaping ([T]?) -> Void) {
4171
self.listener?.remove()
4272
let db = Firestore.firestore()
4373
self.listener = db.collection(path).addSnapshotListener { snapshot, error in
74+
75+
guard error == nil, let snapshot else {
76+
handler(nil)
77+
return
78+
}
79+
80+
handler(Self.decode(as: T.self, from: snapshot))
81+
}
82+
}
83+
84+
public func observe(_ handler: @escaping ([[String : Any]]?) -> Void) {
85+
self.listener?.remove()
86+
let db = Firestore.firestore()
87+
self.listener = db.collection(path).addSnapshotListener { snapshot, error in
88+
4489
guard error == nil, let documents = snapshot?.documents else {
4590
handler(nil)
4691
return
4792
}
48-
handler(documents.map({ $0.data() }))
93+
94+
let response = documents.map { $0.data() }
95+
handler(response)
96+
}
97+
}
98+
99+
public func removeObserver() {
100+
self.listener?.remove()
101+
}
102+
103+
// MARK: - Private
104+
105+
private static func decode<T: Decodable>(as type: T.Type, from snapshot: QuerySnapshot) -> [T]? {
106+
var models: [T] = []
107+
for document in snapshot.documents {
108+
do {
109+
let model = try document.data(as: T.self)
110+
models.append(model)
111+
} catch {
112+
break
113+
}
49114
}
115+
return models
50116
}
51117
}
52118
#endif

0 commit comments

Comments
 (0)