Skip to content

Instantly share code, notes, and snippets.

View lukeredpath's full-sized avatar
🏠
Working from home

Luke Redpath lukeredpath

🏠
Working from home
View GitHub Profile
@lukeredpath
lukeredpath / SharedStateDemoApp.swift
Last active March 30, 2024 02:30
Demo of TCA shared state using default-providing key
//
// SharedStateDemoApp.swift
// SharedStateDemo
//
// Created by Luke Redpath on 29/03/2024.
//
import ComposableArchitecture
import SwiftUI
//
// TCAStoreCachingApp.swift
// TCAStoreCaching
//
// Created by Luke Redpath on 11/03/2024.
//
import ComposableArchitecture
import SwiftUI
@lukeredpath
lukeredpath / ExampleClass.m
Created June 30, 2011 22:18
Macro for creating your "shared instance" using GCD
@implementation MySharedThing
+ (id)sharedInstance
{
DEFINE_SHARED_INSTANCE_USING_BLOCK(^{
return [[self alloc] init];
});
}
@end
@lukeredpath
lukeredpath / BindingEqualityTests.swift
Last active July 6, 2023 16:40
BindingEquality bug in TCA 0.55.0
import ComposableArchitecture
import XCTest
struct BindingTestFeature: ReducerProtocol {
struct State: Equatable {
@BindingState
var value: Int = 0
}
enum Action: Equatable, BindableAction {
@lukeredpath
lukeredpath / StoreScope.swift
Created June 16, 2023 19:15
Ergonomic store scoping
struct StoreScope<State, Action, ChildState, ChildAction> {
typealias Scope<_State, _Action> = StoreScope<State, Action, _State, _Action>
typealias ScopeOf<R: ReducerProtocol> = StoreScope<State, Action, R.State, R.Action>
typealias PresentationScope<_State, _Action> = StoreScope<State, Action, PresentationState<_State>, PresentationAction<_Action>>
typealias PresentationScopeOf<R: ReducerProtocol> = StoreScope<State, Action, PresentationState<R.State>, PresentationAction<R.Action>>
var toChildState: (State) -> ChildState
var fromChildAction: (ChildAction) -> Action
}
@lukeredpath
lukeredpath / LICENSE
Last active June 7, 2023 13:02
A wrapper for raw representable values when decoding them from JSON
Copyright 2023 Luke Redpath
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHE
@lukeredpath
lukeredpath / ExampleDomain.swift
Last active January 31, 2023 16:18
An enum equivalent of IfLetStore for The Composable Architecture
enum AppState: Equatable {
case featureOne(FeatureState)
case featureTwo(FeatureState)
case featureThree(FeatureState)
}
enum AppAction: Equatable {
case featureOne(FeatureAction)
case featureTwo(FeatureAction)
case featureThree(FeatureAction)
@lukeredpath
lukeredpath / PerceptualImage.swift
Created September 13, 2022 10:15
Perceptual image snapshot strategy
// Taken from https://github.com/pointfreeco/swift-snapshot-testing/pull/628/files
import Foundation
import SwiftUI
@testable import SnapshotTesting
#if os(iOS) || os(tvOS)
import CoreImage.CIFilterBuiltins
import UIKit
@lukeredpath
lukeredpath / capture.rb
Created August 5, 2014 15:31
Rspec mock argument capture
# just an example of what it could be
argument = capture('label')
widget = double
# argument acts like an argument matcher that always matches but stores a reference to the real value
allow(widget).to receive(:do_something).with(argument)
# contrived, I know this could be done using the instance_of argument matcher
expect(argument.value).to be_instance_of(Something)
@lukeredpath
lukeredpath / Validations.swift
Last active October 16, 2021 17:57
A little experiment with functional Rails-style validators built on top of pointfreeco/Validated
import UIKit
import Validated
extension Validated {
func mapErrors<LocalError>(_ transform: (Error) -> LocalError) -> Validated<Value, LocalError> {
switch self {
case let .valid(value):
return .valid(value)
case let .invalid(errors):
return .invalid(errors.map(transform))