Skip to content

Instantly share code, notes, and snippets.

View ts95's full-sized avatar
🦉
🍰

Toni Sučić ts95

🦉
🍰
View GitHub Profile
import SwiftUI
import MapKit
protocol MapAnnotationItem: Identifiable {
associatedtype Annotation: MapAnnotationProtocol
var coordinate: CLLocationCoordinate2D { get }
var annotation: Annotation { get }
}
@ts95
ts95 / Dial.swift
Last active December 29, 2023 06:59
Dial component in SwiftUI
import SwiftUI
struct Dial: View {
@Binding public var value: Double
public var minValue: Double = 0
public var maxValue: Double = .greatestFiniteMagnitude
public var divisor: Double = 1
public var stepping: Double = 1
@State private var dialAngle: Angle = .zero
@State private var dialShadowAngle: Angle = .zero
@ts95
ts95 / PropertyStore.swift
Created May 27, 2020 11:39
Swift PropertyStore class for protocols
class PropertyStore<Root> {
private var store = [AnyHashable : Any]()
/// Runs the computedValue closure once and stores its result in a cache for the duration
/// of the property store object's lifetime. Subsequent calls to this method will return the cached value.
func cached<T>(_ keyPath: KeyPath<Root, T>, _ computedValue: @escaping () -> T) -> T {
if let storedValue = store[keyPath] as? T {
return storedValue
}
let value = computedValue()
@ts95
ts95 / ConfigurableView.swift
Last active April 17, 2020 08:03
Calculates the size of a configured view given its constraints and intrinsic size.
import UIKit
/// Implemented by views that can be configured with a view model
///
/// The view model must conform to Hashable.
public protocol ConfigurableView: UIView {
associatedtype ViewModel: Hashable
func configure(with viewModel: ViewModel)
}
@ts95
ts95 / String+Regex.swift
Created February 14, 2019 11:10
Some useful regex extension methods for the String type
// MARK: - Regex extensions
extension String {
func test(for pattern: String, options: NSRegularExpression.Options = []) -> Bool {
guard let groups = try? matches(for: pattern, options: options) else { return false }
return groups.count >= 1
}
func matches(for pattern: String, options: NSRegularExpression.Options = []) throws -> [[String]] {
let regex = try NSRegularExpression(pattern: pattern, options: options)
import UIKit
/// Struct containing the arguments for the UIView.systemLayoutSizeFitting() method
public struct LayoutSizeFitting {
let targetSize: CGSize
let horizontalFittingPriority: UILayoutPriority
let verticalFittingPriority: UILayoutPriority
public static let `default` = LayoutSizeFitting(targetSize: UIView.layoutFittingCompressedSize,
horizontalFittingPriority: .fittingSizeLevel,
@ts95
ts95 / Array+LookupTable.swift
Created October 1, 2018 11:08
Array extension method for generating a look-up table
extension Array {
/// Returns a dictionary keyed by an arbitrary attribute of an element in the array.
///
/// The returned dictionary can be used for look-ups by an arbitrary attribute
/// in an array of models with constant time complexity instead of the typical
/// linear complexity associated with looking up elements in an unsorted list.
///
/// - Parameters:
/// - indexClosure: Callback used to specify which attribute should be used for look-ups (usually an ID)
func lookupTable<Index: Hashable>(indexedBy indexClosure: (Element) -> Index) -> [Index : Element] {
@ts95
ts95 / LayoutConstrainable.swift
Last active March 19, 2018 09:11
Layout constraint extensions for UIView
import UIKit
protocol LayoutConstrainable {
@discardableResult func height(_ height: CGFloat, priority: UILayoutPriority) -> Self
@discardableResult func width(_ width: CGFloat, priority: UILayoutPriority) -> Self
@discardableResult func size(_ size: CGSize, priority: UILayoutPriority) -> Self
@discardableResult func top(to: Self, constant: CGFloat, priority: UILayoutPriority) -> Self
@discardableResult func leading(to: Self, constant: CGFloat, priority: UILayoutPriority) -> Self
@discardableResult func trailing(to: Self, constant: CGFloat, priority: UILayoutPriority) -> Self
@discardableResult func bottom(to: Self, constant: CGFloat, priority: UILayoutPriority) -> Self
//
// AgnosticSafeArea.swift
//
import UIKit
extension UIViewController {
/**
Auxiliary version-agnostic safe area layout guide
@ts95
ts95 / PathParams.swift
Last active February 24, 2018 18:03
Extracting keys and values from a path in Swift
import Foundation
enum PathParamsError: Error {
case unbalanced
case nonExistentParam(key: String)
case invalidConversion(value: String, type: String)
var localizedDescription: String {
switch self {
case .unbalanced: