Skip to content

Instantly share code, notes, and snippets.

View hannesoid's full-sized avatar

Hannes Oud hannesoid

View GitHub Profile
@hannesoid
hannesoid / InputAccessoryHostingController.swift
Last active November 19, 2024 16:33
Use a SwiftUI View as inputAccessoryView, dynamically adjusting to height changes
// (c) Hannes Oud @hannesoid
/// Hosts a SwiftUI view for use as an`inputAccessoryView`
///
/// - Implements a subclass of `UIInputViewController`, this allows setting it as `UITextView.inputAccessoryViewController`
/// - Has as a `.view` a `UIView` subclass that provides a `.intrinsicContentSize` which returns the height of a SwiftUI view
/// - Has a `UIHostingViewController` subclass as a child view controller. Invalidates the `.view`'s `intrinsicContentSize` when the SwiftUI view layouts, in order to inform the system to update the size of the `inputAccessoryView`
///
/// **Usage**
///

OptionSet vs Set

For a simple element type, pros & cons

Example UseCase

struct Model {
   let id: String
   let title: String
@hannesoid
hannesoid / iOS15-UITableView-DuplicateCellUseIssue.md
Last active June 22, 2023 14:26
iOS15 UITableView Duplicate Cell Usage Issue

This only happens on iOS 15 and only when building with Xcode 13. UITableView with (non-diffable) UITableViewDataSource and just one cell type.

Steps

The set up where the issues arises is, we have a row in which we are editing text, insert another after it, and want to continue editing in the new row.

  • [row 0] cell_0 with textView that is firstresponder

Then we call insertRows(at: [.init(row: 1, section:0)], with: …). Within UITableViewDataSource.tableView(…, cellForRowAt:) we correctly get tableView do dequeue a new instance cell_1. (side note: all cells have same reuseIdentifier). In the same tableView(…, cellForRowAt:) method we configure the cell's textView to become the new firstResponder.

@hannesoid
hannesoid / Locked.swift
Last active December 3, 2019 11:24
Atomically locking property wrapper for swift
//
// Locked.swift
//
//
// Created by Hannes Oud on 29.11.19.
// Helpful reading https://www.vadimbulavin.com/swift-atomic-properties-with-property-wrappers/
import Foundation
/// Atomically locks a property using `os_unfair_lock`
@hannesoid
hannesoid / run_antelope.sh
Created November 18, 2019 10:36 — forked from hayakawa/run_antelope.sh
Little code to run Antelope Launcher and Server on MacOS Catalina Public beta
#!/bin/bash
### Get my uid
UserID=`id -u`
echo "Your UID: ${UserID}"
### Unload root process
sudo launchctl unload /Library/LaunchDaemons/com.antelopeaudio.daemon.plist
### Kill the process if it was already awaken
@hannesoid
hannesoid / SwiftUIPropertyRecreation.swift
Created October 27, 2019 19:51
Experiments with SwiftUI view initialization creation
import SwiftUI
final class SomeClass {
static var instanceNr: Int = 0
init(_ owner: String) {
SomeClass.instanceNr += 1
print("Created new instance (nr. \(SomeClass.instanceNr)) for owner \(owner)")
}
@hannesoid
hannesoid / gitupdatefrom
Created January 8, 2019 10:44
Update current & base branch, then merge into current branch
# Copy this into you .bashrc or similar
# Pull current & base branch, then merge-in base
gitupdatefrom() {
echo "➖ Pull current branch…"
git pull
echo "➖ Pull $1…"
git fetch origin "$1":"$1"
echo "➖ Merge $1 into current branch…"
git merge "$1"
@hannesoid
hannesoid / Swift4JSONValue.swift
Created June 26, 2017 13:47
Decodes an unknown JSON structure using Swift 4 Decoding
import XCTest
// Requires a recent Swift 4 snapshot toolchain (Xcode 9 beta 2 not new enough)
public indirect enum JSONValue: Decodable {
case double(Double)
case string(String)
case bool(Bool)
case dictionary([String: JSONValue])
case array([JSONValue])
@hannesoid
hannesoid / AutoEquatable.stencil
Created June 1, 2017 12:06
AutoEquatable.stencil for Sourcery with softness for Doubles and Dates
// Based on https://github.com/krzysztofzablocki/Sourcery/blob/master/Templates/Templates/AutoEquatable.stencil
// 1. Uses =~= for Dates and Double comparison by default
// 2. Supports custom operators specified by annotation above property like // sourcery equalityOperator="=~="
// Currently not supporting enum associated types
// swiftlint:disable file_length
fileprivate func compareOptionals<T>(lhs: T?, rhs: T?, compare: (_ lhs: T, _ rhs: T) -> Bool) -> Bool {
switch (lhs, rhs) {
case let (lValue?, rValue?):
return compare(lValue, rValue)
@hannesoid
hannesoid / xcode_show_build_duration.sh
Created December 7, 2016 10:52
Make Xcode show total build duration
defaults write com.apple.dt.Xcode ShowBuildOperationDuration YES