Skip to content

Instantly share code, notes, and snippets.

@sergdort
sergdort / Iff.swift
Created Jun 16, 2019
Iff and Some operators for #SwiftUI. Inspired on some stuff we use in [Bento](). It lets you chain modifiers instead of doing "if - else" dance 🚀
View Iff.swift
extension View {
func iff(_ condition: Bool, _ modifier: (Self) -> AnyView) -> AnyView {
if condition {
return modifier(self).eraseToAnyView()
}
return eraseToAnyView()
}
func some<Value>(_ optional: Value?, modifier: (Value, Self) -> AnyView) -> some View {
guard let value = optional else {
View iOS Astronauts CoC.md

iOS Astronauts Code of Conduct

iOS Astronauts is dedicated to providing a harassment-free community for everyone, regardless of sex, gender identity or expression, sexual orientation, disability, physical appearance, age, body size, race, nationality, or religious beliefs. We do not tolerate harassment of community members in any form. Participants violating these rules may be sanctioned or expelled from the community at the discretion of the organizers.

Harassment includes offensive verbal or written comments related to sex, gender identity or expression, sexual orientation, disability, physical appearance, age, body size, race, nationality, or religious beliefs, deliberate intimidation, threats, stalking, following, harassing photography or recording, sustained disruption of talks or other events, inappropriate physical contact, and unwelcome sexual attention. Sexual language and imagery is not appropriate for any iOS Astronauts event or communication channel. Community members asked to stop any harass

View Observable_Refactored.swift
public class Observable<Element>: ObservableType {
public typealias E = Element
private let _subscribeHandler: (Observer<Element>) -> Disposable
public init(_ subscribtionClosure: @escaping (Observer<Element>) -> Disposable) {
_subscribeHandler = subscribtionClosure
}
public func subscribe<O : ObserverType>(observer: O) -> Disposable where O.E == E {
let sink = Sink(forvard: observer, subscribtionHandler: _subscribeHandler)
View Observable.swift
public class Observable<Element>: ObservableType {
public typealias E = Element
private let _subscribeHandler: (Observer<Element>) -> Disposable
public init(_ subscribtionClosure: @escaping (Observer<Element>) -> Disposable) {
_subscribeHandler = subscribtionClosure
}
public func subscribe<O : ObserverType>(observer: O) -> Disposable where O.E == E {
let composite = CompositeDisposable()
View Observer.swift
public final class Observer<E>: ObserverType {
private let _handler: (Event<E>) -> Void
public init(handler: @escaping (Event<E>) -> Void) {
_handler = handler
}
public func on(event: Event<E>) {
_handler(event)
}
View Disposables.swift
public final class AnonimousDisposable: Disposable {
private let _disposeHandler: () -> Void
public init(_ disposeClosure: @escaping () -> Void) {
_disposeHandler = disposeClosure
}
public func dispose() {
_disposeHandler()
}
View Disposable.swift
public protocol Disposable {
func dispose()
}
View ObserverType.swift
public protocol ObserverType {
associatedtype E
func on(event: Event<E>)
}
public enum Event<T> {
case next(T)
case error(Error)
case completed
View Map.swift
extension ObservableType {
public func map<U>(_ transform: @escaping (E) throws -> U) -> Observable<U> {
return Observable<U> { observer in
return self.subscribe(observer: Observer { (event) in
switch event {
case .next(let element):
do {
try observer.on(event: .next(transform(element)))
} catch {
observer.on(event: .error(error))
View ObservableTest.swift
let observable = Observable<Int> { (observer) -> Disposable in
observer.on(event: .next(1))
observer.on(event: .next(2))
observer.on(event: .completed)
return AnonimousDisposable {
print("disposed")
}
}
observable.subscribe {