Skip to content

Instantly share code, notes, and snippets.

@smosko
smosko / TouchBarFix.scpt
Created November 29, 2023 13:27
Macbook Pro TouchBar Flicker Fix
# 1. Paste this into Script Editor, replace username and password;
# 2. Export as "Application", check "Stay open" and "Run-only";
# 3. Add to Login Items in Settings/General to run automatically;
# 4. To hide the Dock icon, add to Content/Info.plist:
# <key>LSUIElement</key>
# <string>1</string>
on idle
set idleTime to (do shell script "ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print $NF/1000000000; exit}'") as integer
if idleTime is less than 58 then
@smosko
smosko / ConfigurableCell.swift
Created August 21, 2022 12:07
Generic cell provider for UICollectionViewDiffableDataSource
import UIKit
protocol ConfigurableCell: UICollectionViewCell {
associatedtype Item
func configure(with item: Item, for indexPath: IndexPath)
}
extension ConfigurableCell {
public static var provider: (UICollectionView, IndexPath, Item) -> Self {
let registration = registration()
@smosko
smosko / String+hashCode.swift
Created October 17, 2021 14:21
Swift implementation of the String.hashCode() from Java / Android
extension String {
func hashCode() -> Int32 {
unicodeScalars.map { Int32($0.value) }.reduce(0) { 31 &* $0 &+ $1 }
}
}
@smosko
smosko / RadarView.swift
Created April 15, 2021 13:08
Radar animation
import UIKit
class RadarView: UIView {
private let waveShape = CAShapeLayer()
private let waveReplicator = CAReplicatorLayer()
override init(frame: CGRect = .zero) {
super.init(frame: frame)
@smosko
smosko / ViewModel.swift
Created June 6, 2020 16:49
Base ViewModel using dynamic member lookup to access object properties and property publishers
import Combine
import Foundation
@dynamicMemberLookup
public class ViewModel<Object> {
private var object: Object
init(_ object: Object) {
self.object = object
}
@smosko
smosko / EditableList.swift
Created April 18, 2020 08:40
SwiftUI editable list
import SwiftUI
class Item: ObservableObject, Identifiable {
let id = UUID()
@Published var name = ""
@Published var value = ""
}
struct ContentView: View {
@State private var items: [Item] = []
@smosko
smosko / AutoLayout.swift
Last active December 2, 2021 12:09
AutoLayout micro DSL
import UIKit
public protocol LayoutAnchor {}
extension NSLayoutAnchor: LayoutAnchor {}
public struct AnchorPair<T: AnyObject, U: AnyObject>: LayoutAnchor {
public var first: NSLayoutAnchor<T>
public var second: NSLayoutAnchor<U>
}
@smosko
smosko / Reusable.swift
Last active July 20, 2021 10:58
Reusable UITableView and UICollectionView cells
import UIKit
public protocol Reusable: AnyObject {
static var reuseIdentifier: String { get }
}
extension Reusable {
public static var reuseIdentifier: String {
String(describing: self)
}
import UIKit
import PlaygroundSupport
class AnimatedButton: UIButton {
var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
@smosko
smosko / AsyncOperation.swift
Created March 7, 2019 09:37
AsyncOperation
import Foundation
open class AsyncOperation: Operation {
private enum State: String {
case ready = "isReady"
case executing = "isExecuting"
case finished = "isFinished"
}