Skip to content

Instantly share code, notes, and snippets.

@liaujianjie
Last active July 3, 2018 10:51
Show Gist options
  • Save liaujianjie/9b46a95f0b6341953ad1d34e228725a1 to your computer and use it in GitHub Desktop.
Save liaujianjie/9b46a95f0b6341953ad1d34e228725a1 to your computer and use it in GitHub Desktop.
Swift extension to bridge between Eureka datatypes and Firebase datatypes
import Foundation
import Eureka // https://github.com/xmartlabs/Eureka
public extension Form {
public func valuesForFirebase(includeHidden: Bool = false) -> [String: Any?] {
let rows = includeHidden ? self.allRows : self.rows
return rows.filter({ $0.tag != nil })
.reduce([:], { (dictionary, row) -> [String: Any?] in
var dictionary = dictionary
dictionary[row.tag!] = row.firebaseValue
return dictionary
})
}
}
public extension Dictionary {
func valuesForEureka(forForm form: Form) -> [String: Any?] {
return self.reduce([:], { (dictionary, tuple) -> [String: Any?] in
var dictionary = dictionary
let row = form.rowBy(tag: tuple.key as! String)
if row is SwitchRow || row is CheckRow {
let typedValue = tuple.value as! Int
dictionary[tuple.key as! String] = (typedValue == 1) ? true : false
} else if row is DateRow || row is TimeRow || row is DateTimeRow {
let typedValue = tuple.value as! TimeInterval
dictionary[tuple.key as! String] = Date(timeIntervalSince1970: typedValue)
} else {
dictionary[tuple.key as! String] = tuple.value
}
return dictionary
})
}
}
private extension BaseRow {
var firebaseValue: Any? {
get {
if self is SwitchRow || self is CheckRow {
return (self.baseValue as! Bool) ? 1:0
} else if self is DateRow || self is TimeRow || self is DateTimeRow {
return (self.baseValue as! Date).timeIntervalSince1970
} else {
return self.baseValue
}
}
}
}
@jtaytshungman
Copy link

If I try to upload to firebase via ImageRow, how should I go about doing that?

@liaujianjie
Copy link
Author

@jtaytshungman So sorry I missed your message! I didn't get any notification. Anyway, I'm afraid I've been out of touch with iOS dev and the Firebase SDK for far too long and I won't be able to provide much help with this.

You can add a new if statement for the Dictionary and BaseRow extension to map ImageRows to whatever Firebase is using to contain images.

@SigmundRakes
Copy link

SigmundRakes commented Jul 3, 2018

Hey @liaujianjie, does this convert Date to String? If so, how does one call/ use this extension you made? Can you please give us an example of a dictionary maybe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment