Skip to content

Instantly share code, notes, and snippets.

View billibala's full-sized avatar

Bill So billibala

View GitHub Profile
@SeanMcTex
SeanMcTex / FilteredTextField.md
Created May 8, 2020 20:05
A pitfall to avoid when filtering SwiftUI TextFields

Today’s very hard-won lesson: be careful how you do TextField content filtering! We were filtering input to a text field (making sure the field only accepted numbers, etc.) like so:

@Binding var value: String
TextField( placeholder, text: $value )
    .onReceive( Just( value ), perform: { 
		self.value = self.contentType.filterCharacters( oldValue: $0 ) 
	})
@andymatuschak
andymatuschak / States-v3.md
Last active June 12, 2024 04:17
A composable pattern for pure state machines with effects (draft v3)

A composable pattern for pure state machines with effects

State machines are everywhere in interactive systems, but they're rarely defined clearly and explicitly. Given some big blob of code including implicit state machines, which transitions are possible and under what conditions? What effects take place on what transitions?

There are existing design patterns for state machines, but all the patterns I've seen complect side effects with the structure of the state machine itself. Instances of these patterns are difficult to test without mocking, and they end up with more dependencies. Worse, the classic patterns compose poorly: hierarchical state machines are typically not straightforward extensions. The functional programming world has solutions, but they don't transpose neatly enough to be broadly usable in mainstream languages.

Here I present a composable pattern for pure state machiness with effects,

@Daemon-Devarshi
Daemon-Devarshi / PasteboardWatcher.swift
Last active October 6, 2023 14:15
A class which performs polling to determine the newly copied url of desired kind in an external app
//
// PasteboardWatcher.swift
// PasteboardWatcher
//
// Created by Devarshi Kulshreshtha on 6/19/15.PasteboardWatcher
// Copyright © 2015 Devarshi Kulshreshtha. All rights reserved.
//
import Cocoa