Created
April 8, 2019 15:16
-
-
Save ashour/e2be7be7fdda0ffdbc9233339bf14f95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Firebase | |
class Product | |
{ | |
typealias OnProductsFetched = (_ products: [Product]) -> Void | |
typealias Listener = ListenerRegistration | |
var name: String | |
var store: String | |
var discount: String | |
var priceAfterDiscount: String | |
var priceBeforeDiscount: String | |
var expires: String | |
var imageUrl: String | |
static func fetchFeed(onSuccess: @escaping OnProductsFetched) | |
{ | |
baseQuery().getDocuments | |
{ | |
(querySnapshot, error) in | |
if let error = error | |
{ | |
print("Error getting documents: \(error)") | |
return | |
} | |
onSuccess(fromDB(documents: querySnapshot!.documents)) | |
} | |
} | |
static func listenToFeed(onChange: @escaping OnProductsFetched) | |
-> ListenerRegistration | |
{ | |
return baseQuery().addSnapshotListener | |
{ | |
(querySnapshot, error) in | |
guard let documents = querySnapshot?.documents else | |
{ | |
print("Error fetching documents: \(error!)") | |
return | |
} | |
onChange(fromDB(documents: documents)) | |
} | |
} | |
fileprivate static var collection: CollectionReference | |
{ | |
get | |
{ | |
return DB.instance.collection("product-feed") | |
} | |
} | |
fileprivate static func baseQuery() -> Query { | |
return collection.order(by: "expires") | |
} | |
fileprivate static func fromDB(documents: [QueryDocumentSnapshot]) | |
-> [Product] | |
{ | |
return documents.map { fromDB(data: $0.data()) } | |
} | |
fileprivate static func fromDB(data: [String: Any]) -> Product | |
{ | |
return Product( | |
name: DB.convert(data, "name") ?? "", | |
store: DB.convert(data, "store") ?? "", | |
discount: DB.convert(data, "discount") ?? "", | |
priceAfterDiscount: centsToString( | |
DB.convert(data, "priceAfterDiscountInCents") ?? 0), | |
priceBeforeDiscount: centsToString( | |
DB.convert(data, "priceBeforeDiscountInCents") ?? 0), | |
expires: humanizeDate(date: DB.timestampToDate(data, "expires")), | |
imageUrl: DB.convert(data, "imageUrl") ?? "" | |
) | |
} | |
init( name: String, | |
store: String, | |
discount: String, | |
priceAfterDiscount: String, | |
priceBeforeDiscount: String, | |
expires: String, | |
imageUrl: String) | |
{ | |
self.name = name | |
self.store = store | |
self.discount = discount | |
self.priceAfterDiscount = priceAfterDiscount | |
self.priceBeforeDiscount = priceBeforeDiscount | |
self.expires = expires | |
self.imageUrl = imageUrl | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment