Created
August 22, 2018 08:42
-
-
Save mortenbekditlevsen/f749ff2022a8d319c643231efda965f8 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 FirebaseDatabase | |
import Foundation | |
import RxSwift | |
protocol ViewModelInputs { | |
func add(message: Message) | |
func update(configuration: Configuration) | |
} | |
protocol ViewModelOutputs { | |
var newMessages: Observable<Message> { get } | |
var configuration: Observable<Configuration> { get } | |
} | |
protocol ViewModelType { | |
var inputs: ViewModelInputs { get } | |
var outputs: ViewModelOutputs { get } | |
} | |
class ViewModel: ViewModelType, ViewModelOutputs, ViewModelInputs { | |
var inputs: ViewModelInputs { return self } | |
var outputs: ViewModelOutputs { return self } | |
private let configurationPath = Path().configuration | |
private let firechatPath = Path().chatroom("firechat").messages | |
lazy var newMessages: Observable<Message> = s.observe(eventType: .childAdded, at: firechatPath).filtered() | |
lazy var configuration: Observable<Configuration> = s.observe(at: configurationPath).filtered() | |
func add(message: Message) { | |
try? s.addValue(at: firechatPath, value: message) | |
} | |
func update(configuration: Configuration) { | |
try? s.setValue(at: configurationPath, value: configuration) | |
} | |
private let s: FirebaseService | |
init(service: FirebaseService) { | |
self.s = service | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment