Skip to content

Instantly share code, notes, and snippets.

View danhalliday's full-sized avatar

Dan Halliday danhalliday

View GitHub Profile
@danhalliday
danhalliday / ContentView.swift
Created February 29, 2024 09:27
SwiftUINavigation Test Case
import SwiftUI
import SwiftUINavigation
import XCTestDynamicOverlay
// MARK: - Screen One
@MainActor class ScreenOne: ObservableObject {
@CasePathable
@dynamicMemberLookup
enum Destination {
@danhalliday
danhalliday / Setting.swift
Last active December 21, 2023 05:37
Sketch of a settings dependency.
import SwiftUI
import Dependencies
import AsyncExtensions
public struct Setting<Value:Sendable>: Sendable {
let id: String
let name: String
let fallback: Value
let load: @Sendable (String) -> Value?
@danhalliday
danhalliday / Analytics.swift
Created March 21, 2022 15:11
Sound Amplifier V1 Analytics Configuration
// MARK: - Audio
extension Audio.InputDidChange: TrackedAction {}
extension Audio.OutputDidChange: TrackedAction {}
extension Audio.RecordingDidStop: TrackedAction {}
extension Audio.RecordingDidStart: TrackedAction {}
extension AudioRoutePicker.DidAppear: TrackedAction {}
extension AudioRoutePicker.DidDisappear: TrackedAction {}
@danhalliday
danhalliday / BigLazyVStack.swift
Created October 22, 2020 12:28
Test case for LazyVStack scrolling behaviour with a large number of items.
struct ContentView: View {
let count = 1_000_000
var body: some View {
ScrollViewReader { reader in
VStack {
Button(action: { reader.scrollTo(100, anchor: .center) }) {
Text("Scroll to 100") // Works
}
import SwiftUI
struct TappableView: UIViewRepresentable {
let onTap: () -> Void
let onPress: (Bool) -> Void
func makeUIView(context: Context) -> UIView {
TappableUIView(onTap: onTap, onPress: onPress)
}
@danhalliday
danhalliday / TappableContentView.swift
Last active June 21, 2024 16:59
SwiftUI solution for responsive, instantly-tappable tiles in a scroll view.
import SwiftUI
/// SwiftUI implementation of a responsive-feeling scrollview with tappable tiles.
/// We use a custom UIScrollView via UIViewRepresentable and a UIView tap overlay to
/// get around current SwiftUI issues with simultaneous gesture recognition and allow
/// the tiles to respond instantly to presses while scrolling.
struct ContentView: View {
@State private var showSheet = false
@danhalliday
danhalliday / BuggyDataFlowContentView.swift
Last active April 10, 2020 13:11
SwiftUI Data Flow Buggy Example
import SwiftUI
struct Person {
let id: UUID
var name: String
var score: Float
}
class PersonStore: ObservableObject {
@Published var people: [Person] = [
@danhalliday
danhalliday / Sequence+Sum.swift
Last active February 14, 2020 10:22
Sum Swift extension, including sum by property.
extension Sequence where Element: AdditiveArithmetic {
func sum() -> Element {
reduce(.zero, +)
}
}
extension Sequence {
func sum<T: AdditiveArithmetic>(_ keyPath: KeyPath<Element, T>) -> T {
map { $0[keyPath: keyPath] }.sum()
}
@danhalliday
danhalliday / podcast.xml.builder
Created January 8, 2020 09:18
Quick Podcast Feed with Ruby’s Builder Gem
xml.instruct! :xml, version: "1.0"
rss_attributes = {
"version" => "2.0",
"xmlns:dc" => "http://purl.org/dc/elements/1.1/",
"xmlns:sy" => "http://purl.org/rss/1.0/modules/syndication/",
"xmlns:atom" => "http://www.w3.org/2005/Atom",
"xmlns:rdf" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"xmlns:content" => "http://purl.org/rss/1.0/modules/content/",
"xmlns:itunes" => "http://www.itunes.com/dtds/podcast-1.0.dtd",
@danhalliday
danhalliday / sitemap.xml.builder
Last active October 22, 2019 14:20
Quick-and-dirty sitemap for Middleman.
xml.instruct!
xml.urlset "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
sitemap.resources.select { |page| page.destination_path =~ /\.html/ && page.data.noindex != true }.each do |page|
xml.url do
modified = page.data.date ? page.data.date.to_time : Time.now
xml.loc URI.join(data.site.host, page.destination_path.chomp("index.html").chomp(".html"))
xml.lastmod modified.iso8601
xml.changefreq page.data.changefreq || "monthly"
xml.priority page.data.priority || "0.5"
end