Last active
July 3, 2018 10:51
-
-
Save liaujianjie/9b46a95f0b6341953ad1d34e228725a1 to your computer and use it in GitHub Desktop.
Swift extension to bridge between Eureka datatypes and Firebase datatypes
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 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 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 ImageRow
s to whatever Firebase is using to contain images.
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
If I try to upload to firebase via ImageRow, how should I go about doing that?