Skip to content

Instantly share code, notes, and snippets.

Avatar
😷
status messages are fun

Rob Mayoff mayoff

😷
status messages are fun
View GitHub Profile
@mayoff
mayoff / README.md
Last active Aug 23, 2021
defining an extra-large widget only if on iOS 15
View README.md

You probably have a WidgetBundle type in your widget extension like this:

@main
struct MyWidgets: WidgetBundle {
    var body: some Widget {
        SmallWidget()
        MediumWidget()
        LargeWidget()
    }
@mayoff
mayoff / EqualIconWidthDomain.swift
Created Aug 12, 2021
Complete code for https://stackoverflow.com/a/68751668/77567 (EqualIconWidthDomain for SwiftUI Label views)
View EqualIconWidthDomain.swift
import SwiftUI
fileprivate struct IconWidthKey: PreferenceKey {
static var defaultValue: CGFloat? { nil }
static func reduce(value: inout CGFloat?, nextValue: () -> CGFloat?) {
switch (value, nextValue()) {
case (nil, let next): value = next
case (_, nil): break
case (.some(let current), .some(let next)): value = max(current, next)
@mayoff
mayoff / RefreshableTests.swift
Created Jul 19, 2021
for pointfree episode 153
View RefreshableTests.swift
@testable import SwiftUICaseStudies
import XCTest
class RefreshableTests: XCTestCase {
func testVanilla() async {
var k: CheckedContinuation<Void, Never>? = nil
let viewModel = PullToRefreshViewModel(
fetch: {
@mayoff
mayoff / TEA.swift
Last active Nov 13, 2020
An implementation of Point-Free's Composable Architecture, with the addition of subscriptions from The Elm Architecture (TEA). See https://github.com/pointfreeco/episode-code-samples/issues/51
View TEA.swift
#if canImport(Combine)
import Combine
import CasePaths
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
public typealias Effect<Output> = AnyPublisher<Output, Never>
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension Effect {
@mayoff
mayoff / ContentView.swift
Created Feb 26, 2020
SwiftUI gradients spanning multiple views
View ContentView.swift
import SwiftUI
struct BubbleFramesValue {
var framesForKey: [AnyHashable: [CGRect]] = [:]
var gradientFrame: CGRect? = nil
}
struct BubbleFramesKey { }
extension BubbleFramesKey: PreferenceKey {
View gist:bcb5fc06b79f8b0a0270ebf48e38d4a1
# Show the Xcode version number over the Dock icon.
defaults write com.apple.dt.Xcode DVTEnableDockIconVersionNumber -bool YES
# Or set ShowDVTDebugMenu.
# Show Index in the Report navigator.
# https://pspdfkit.com/blog/2019/how-xcode-indexing-works-and-how-to-solve-problems/
defaults write com.apple.dt.Xcode IDEIndexShowLog -bool YES
@mayoff
mayoff / ConversationView.swift
Created Feb 14, 2020
playground demonstrating SwiftUI PreferenceKey, EnvironmentKey, and GeometryReader
View ConversationView.swift
import SwiftUI
import PlaygroundSupport
struct EqualWidthKey: PreferenceKey {
static var defaultValue: CGFloat? { nil }
static func reduce(value: inout CGFloat?, nextValue: () -> CGFloat?) {
switch (value, nextValue()) {
case (_, nil): break
case (nil, let next): value = next
@mayoff
mayoff / SteppingSubscriber.swift
Created Dec 18, 2019
Custom Combine operator for stepping a publisher's signals through function
View SteppingSubscriber.swift
// See https://stackoverflow.com/q/59367202/77567
import Combine
import Foundation
extension Publisher {
func step(with stepper: @escaping (SteppingSubscriber<Output, Failure>.Event) -> ()) -> AnyCancellable {
let subscriber = SteppingSubscriber<Output, Failure>(stepper: stepper)
self.subscribe(subscriber)
return .init(subscriber)
@mayoff
mayoff / decode-dyn-uti.swift
Created Mar 14, 2019
Swift command-line script to decode dynamic UTIs
View decode-dyn-uti.swift
#!/usr/bin/xcrun swift
// http://alastairs-place.net/blog/2012/06/06/utis-are-better-than-you-think-and-heres-why/
extension String {
static let bitsForCodeUnit: [Character: [UInt8]] = {
var dict = [Character: [UInt8]]()
for (i, ch) in "abcdefghkmnpqrstuvwxyz0123456789".enumerated() {
let bits = Array((0 ..< 5).map({ UInt8((i >> $0) & 1) }).reversed())
dict[ch] = bits
@mayoff
mayoff / unowned-safety-dance.swift
Created Feb 23, 2019
test Swift unowned safety
View unowned-safety-dance.swift
class MyObject { }
var o = MyObject()
print("strong o = \(o)")
unowned let u = o
print("unowned u = \(u)")
o = MyObject()