Skip to content

Instantly share code, notes, and snippets.

View mortenbekditlevsen's full-sized avatar

Morten Bek Ditlevsen mortenbekditlevsen

View GitHub Profile
import FirebaseDatabase
import Foundation
import RxSwift
protocol ViewModelInputs {
func add(message: Message)
func update(configuration: Configuration)
}
protocol ViewModelOutputs {
protocol ResultProtocol {
associatedtype WrappedType
associatedtype ErrorType
var value: WrappedType? { get }
var error: ErrorType? { get }
}
extension Result: ResultProtocol {
typealias WrappedType = Value
typealias ErrorType = Error
public class FirebaseService {
private let rootRef: DatabaseReference
public init(ref: DatabaseReference) {
self.rootRef = ref.root
}
// MARK: Observing Paths
func observeSingleEvent<T>(at path: Path<T>) -> Single<T>
where T: Decodable {
let ref = rootRef.child(path.rendered)
@mortenbekditlevsen
mortenbekditlevsen / DatabaseQuery+rx.swift
Last active August 22, 2018 08:07
DatabaseQuery+rx.swift
extension Reactive where Base: DatabaseQuery {
func observeSingleEvent<T>(of type: DataEventType) -> Single<T> where T: Decodable {
return Single.create { single in
self.base.observeSingleEvent(of: type, with: { (result: DecodeResult<T>) in
single(result.asSingleEvent)
})
return Disposables.create()
}
}
// A small wrapper so that we prevent the user from calling collection observation with .value
public enum CollectionEventType {
case childAdded, childChanged, childRemoved
var firebaseEventType: DataEventType {
switch self {
case .childAdded:
return .childAdded
case .childChanged:
return .childChanged
case .childRemoved:
enum Root {}
enum ChatRoom {}
struct Message: Codable {
var header: String
var body: String
}
struct Configuration: Codable {
// Our actual Configuration entity
// So from the Objc.io talk, we learn about a way of representing filesystem paths that can point to either files or directories.
// Internally, these are represented as an array of path elements. Let's do that too:
public struct Path<Element> {
public struct Collection {
private var components: [String]
public func child(_ key: String) -> Path<Element> {
return append(key)
}
enum Root {}
enum ChatRooms {}
enum ChatRoom {}
enum Messages {}
struct Message: Codable {
// Our Message entity
var header: String
var body: String
}
//
// FirebaseService.swift
// SwiftyFirebase
//
// Created by Morten Bek Ditlevsen on 29/07/2018.
// Copyright © 2018 Ka-ching. All rights reserved.
//
import FirebaseDatabase
import Foundation
// So from the Objc.io talk, we learn about a way of representing filesystem paths that can point to either files or directories.
// Internally, these are represented as an array of path elements. Let's do that too:
public struct Path<Element> {
private var components: [String]
fileprivate func append<T>(_ args: String ...) -> Path<T> {
return Path<T>(components + args)
}