Skip to content

Instantly share code, notes, and snippets.

View mortenbekditlevsen's full-sized avatar

Morten Bek Ditlevsen mortenbekditlevsen

View GitHub Profile
enum Root {}
enum ChatRooms {}
enum ChatRoom {}
enum Messages {}
struct Message: Codable {
// Our Message entity
var header: String
var body: String
}
// 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 ChatRoom {}
struct Message: Codable {
var header: String
var body: String
}
struct Configuration: Codable {
// Our actual Configuration entity
// 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:
@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()
}
}
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)
protocol ResultProtocol {
associatedtype WrappedType
associatedtype ErrorType
var value: WrappedType? { get }
var error: ErrorType? { get }
}
extension Result: ResultProtocol {
typealias WrappedType = Value
typealias ErrorType = Error
import FirebaseDatabase
import Foundation
import RxSwift
protocol ViewModelInputs {
func add(message: Message)
func update(configuration: Configuration)
}
protocol ViewModelOutputs {
@mortenbekditlevsen
mortenbekditlevsen / PixelBasedLayoutConstraint.swift
Created April 6, 2019 12:03
Any constants in the constraint will be interpreted as pixels instead of points
public class PixelBasedLayoutConstraint: NSLayoutConstraint {
override public func awakeFromNib() {
super.awakeFromNib()
constant /= UIScreen.main.scale
}
}
import SwiftUI
struct TextFormatting: ExpressibleByStringLiteral, ExpressibleByStringInterpolation {
struct StringInterpolation: StringInterpolationProtocol {
var output: Text = Text(verbatim: "")
init(literalCapacity: Int, interpolationCount: Int) {
// TODO
}