Skip to content

Instantly share code, notes, and snippets.

View pteasima's full-sized avatar

Petr Sima pteasima

  • simapps s.r.o.
  • Prague, Czech Republic
  • X @pteasima
View GitHub Profile
@pteasima
pteasima / BottomUpTransactions.swift
Last active June 19, 2019 23:08
BottomUpTransactions
// WIP PoC of passing animated Binding transactions from a View to the Store.
// This works, but is probably a dead end since it will be better to have the Store decide on animations and impose them top-down
// I still havent played with interactive gestures tho, so this could come in handy someday
import SwiftUI
import Combine
final class P {
let store: Store<ExampleApp.State, ExampleApp.Action> = ObjectBinding(initialValue: ExampleApp.theStore)
func dispatch(_ action: ExampleApp.Action, transaction: Transaction? = nil) {
@pteasima
pteasima / AutoupdatingStore.swift
Created June 19, 2019 19:54
Autoupdating Store
import SwiftUI
import Combine
final class Store<State>: BindableObject {
let didChange = PassthroughSubject<(), Never>()
init(state: State) {
self.state = state
}
var state: State {
didSet {
@pteasima
pteasima / BindingObjectAutoDidChange.swift
Last active June 19, 2019 18:23
BindingObject auto didChange
import SwiftUI
import Combine
@propertyWrapper struct MyObjectBinding<BindableObjectType: BindableObject> : DynamicViewProperty where BindableObjectType.PublisherType == PassthroughSubject<(), Never> {
init(initialValue: BindableObjectType) {
delegateValue = Wrapper(bindableObject: initialValue)
}
@dynamicMemberLookup struct Wrapper {
var bindableObject: BindableObjectType
@pteasima
pteasima / OnScroll.swift
Last active December 16, 2020 17:35
SwiftUI onScroll
import SwiftUI
import Combine
struct OnScroll: ViewModifier {
@Binding var offset: CGFloat
//we can have a version with a closure instead of the binding, but that triggers an infinite loop if content depends on the same Store
// var onOffset: (CGFloat) -> ()
func body(content: Content) -> some View {
return VStack {
@pteasima
pteasima / GlobalSwiftUIBindings.swift
Last active June 11, 2019 02:42
Experiments with current state of SwiftUI beta. Second state property broke it so Ill probably end here.
import SwiftUI
extension Binding {
init(global keyPath: KeyPath<Bindings, Binding>) {
self = bindings[keyPath: keyPath]
}
}
struct Bindings {
@Binding var anotherOne: Bool
@pteasima
pteasima / HKTs.swift
Last active January 17, 2019 07:29
HKTs
protocol Functor {
associatedtype Tag
associatedtype E //Element or _Element clashes on Dictionary :(
}
struct Functoring<F: Functor> {
let _map: (F, (F.E) -> Any) -> Any
func map<FB: Functor>(_ functor: F, transform: (F.E) -> FB.E) -> FB where F.Tag == FB.Tag {
return _map(functor, transform) as! FB
}
}
@pteasima
pteasima / AutoDiff.swift
Last active September 25, 2018 16:56
experimental example of a reactive µframework with automatic differentiation
import Foundation
private func get(keyPaths: [AnyKeyPath], from object: Any) -> Any {
return keyPaths.reduce(object) { acc, kp in acc[keyPath: kp] }
}
extension Collection where Element: Equatable { //might not work for unordered collections
func hasPrefix(_ prefix: Self) -> Bool {
guard let firstFromPrefix = prefix.first else { return true }
guard let firstFromSelf = self.first else { return false }
return firstFromPrefix == firstFromSelf && dropFirst().hasPrefix(prefix.dropFirst())
import ReactiveSwift
import enum Result.Result
import Alamofire
typealias Never = NoError
//This file alone wont build, its just for reading ;)
struct Tagged<Tag, RawValue> {
var rawValue: RawValue
@pteasima
pteasima / tagless-map-problem.swift
Last active January 7, 2018 20:43
tagless-map-problem
protocol Effect {
associatedtype Action
static var none: Self { get }
static func batch(_ effects: [Self]) -> Self
// ! What should we return? Effect has associated types. Effect<B>, Self<B> are not possible.
// Feels like a usecase for type-erasure but didnt get me anywhere (+ I feel like a type-erased AnyEffect defeats the purpose of tagless)
//func map<B>(_ transform: @escaping (Action) -> B) -> ???
// this works but is too general, we want to keep OtherEffect fixed to the same implementation as Self