Skip to content

Instantly share code, notes, and snippets.

View ts95's full-sized avatar
🦉
🍰

Toni Sučić ts95

🦉
🍰
View GitHub Profile
@ts95
ts95 / CLLocationCoordinate2D+Midpoint.swift
Last active January 27, 2024 01:35
CLLocationCoordinate2D + midpoint function extension
import Foundation
import CoreLocation
// based on https://gis.stackexchange.com/a/18740
extension CLLocationCoordinate2D {
var cartesian: CartesianCoordinate3D { .init(from: self) }
// Returns the coordinates of the midpoint between point a and b.
// Limitation: point a and b may not be diametrically opposite.
@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 / Injected.swift
Created September 26, 2022 10:03
Swift basic singleton-based dependency injection
import Foundation
@propertyWrapper
struct Injected<T> {
private let keyPath: WritableKeyPath<InjectedValues, T>
var wrappedValue: T {
get { InjectedValues[keyPath] }
set { InjectedValues[keyPath] = newValue }
}
@ts95
ts95 / Cache.swift
Last active January 15, 2023 21:52
Hashable & Codable struct-based cache implementation in Swift
import OrderedCollections
protocol HasCacheCost {
var cacheCost: Int { get }
}
extension HasCacheCost {
var cacheCost: Int {
type(of: self) == AnyObject.self ? 0 : MemoryLayout.size(ofValue: self)
}
@ts95
ts95 / AutoId.swift
Created January 5, 2023 09:25
Swift port of the ID generator used in Firebase Firestore
import Foundation
class AutoId {
private init() {}
static func newId() -> String {
// Alphanumeric characters
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
// The largest byte value that is a multiple of `char.length`.
let maxMultiple = Int(256 / chars.count) * chars.count
@ts95
ts95 / Result+Codable.swift
Created January 7, 2023 18:41
Makes the Result type in Swift conform to Codable
import Foundation
extension Result: Codable where Success: Codable, Failure: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
switch try container.decode(ResultType.self, forKey: .type) {
case .success:
self = .success(try container.decode(Success.self, forKey: .value))
case .failure:
@ts95
ts95 / Validatable.swift
Last active July 7, 2022 09:34
A Swift protocol for making a model validatable
import Foundation
protocol Rule {
associatedtype Option
var errorMessage: String { get }
init(_ option: Option, error: String)
func validate(for value: ValidatableType) -> Bool
@ts95
ts95 / AppendOnlyDatabase+TextEditor.swift
Created March 29, 2022 18:51
Append-only database + Text editor
import Foundation
protocol AppendOnlyDatabaseProtocol {
var count: Int { get }
mutating func append(_ other: Data)
subscript(index: Data.Index) -> UInt8 { get }
subscript(bounds: Range<Data.Index>) -> Data { get }
}
@ts95
ts95 / ScreenshotDetector.swift
Last active February 14, 2022 19:49
OS X detect new screenshot event with Swift
import Foundation
typealias NewFileCallback = (fileURL: NSURL) -> Void
class ScreenshotDetector: NSObject, NSMetadataQueryDelegate {
let query = NSMetadataQuery()
var newFileCallback: NewFileCallback?
import FirebaseFirestore
private struct Property {
let label: String
let value: Any
}
struct FirestoreModelData {
let snapshot: DocumentSnapshot