Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
🏔🏃‍♂️

Rui Peres RuiAAPeres

💭
🏔🏃‍♂️
View GitHub Profile
@RuiAAPeres
RuiAAPeres / Assertion+ReactiveSwift.swift
Last active Aug 5, 2020
Unit Testing ReactiveSwift streams with Assertions (idea by @andersio)
View Assertion+ReactiveSwift.swift
import ReactiveSwift
import XCTest
private let counterFailure: (Int, Int) -> String = { count, maxCount in
return "Counter is currently at \(count) which higher than the max \(maxCount)"
}
private let timeoutFailure = "Expectation timeout"
private let infoTemplate: (String, String, Int) -> String = { fileName, functionName, lineNumber in
@RuiAAPeres
RuiAAPeres / reactiveswift+grdb.swift
Last active May 20, 2020
ReactiveSwift extensions for GRDB
View reactiveswift+grdb.swift
/// This heavily based on the work done at RxGRDB
import GRDB
import ReactiveSwift
extension DatabasePool: ReactiveExtensionsProvider {}
extension DatabaseQueue: ReactiveExtensionsProvider {}
extension ValueObservation: ReactiveExtensionsProvider {}
extension DatabaseRegionObservation: ReactiveExtensionsProvider {}
View notifiers.swift
import Foundation
@propertyWrapper struct Notifier<Value> {
private let identifier: String
var wrappedValue: Value? {
didSet {
NotificationCenter.default.post(name: Notification.Name(identifier), object: wrappedValue)
}
}
View function.swift
public typealias CompletionHandler = Function<Void, Void>
public typealias SideEffect<Input> = Function<Input, Void>
public struct Function<Input, Output>: Identifiable {
public let id: String
internal let f: (Input) -> Output
public init(
id: String,
View Injection.md
/// |                    World                 |
/// |------------------------------------------|
/// | Module A | Module B | Module C | Module D|
  1. World is a module
  2. World is aware of all modules.
  3. Modules aren't aware of World.
@RuiAAPeres
RuiAAPeres / opaqueEquivalent.swift
Last active Feb 18, 2020
Extension to provide an equivalent opaque UIColor to a UIColor with alpha channel. So: f(r,g,b,a) == (r,g,b,1)
View opaqueEquivalent.swift
import UIKit
public extension UIColor {
var components: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
@RuiAAPeres
RuiAAPeres / covariance_contravariance.swift
Last active Sep 16, 2019
Covariance and Contravariance in Swift
View covariance_contravariance.swift
import UIKit
// Based on https://www.stephanboyer.com/post/132/what-are-covariance-and-contravariance
// > Denotes "a subtype of"
// UIButton > UIView > UIResponder > NSObject
//
// e.g. `UIButton` is a subtype of `UIView`
//
// This means that any function that takes a `UIView`, can receive a `UIButton`:
//
@RuiAAPeres
RuiAAPeres / SwiftUIBindsWithReactiveSwift.swift
Last active Aug 7, 2020
Couple of methods to bridge ReactiveSwift with SwiftUI
View SwiftUIBindsWithReactiveSwift.swift
import Combine
import ReactiveSwift
import SwiftUI
class AnySubscription: Subscription {
private let cancelable: Cancellable
init(cancelable: Cancellable) {
self.cancelable = cancelable
View SwiftUI Property Delegates.swift
/// `ObjectBinding` used as a way to create a `Binding` from a `BindableObject`. In this case the ViewModel
/// complies with `BindableObject` which is then translated into a `Binding` which is what `Toggle` is expecting
/// NOTE: Since it's a `DynamicViewProperty`, after its value is changed, the body will be updated.
class ViewModel: BindableObject {
let didChange = PassthroughSubject<ViewModel, Never>()
var isEnabled = false {
didSet {
didChange.send(self)
View Flow_example.swift
public extension UIViewController {
var modalFlow: Flow {
return ModalFlow(self)
}
var navigationFlow: Flow {
guard let navigationController = self.navigationController else { return modalFlow }
return NavigationFlow(navigationController)
}
}
You can’t perform that action at this time.