Skip to content

Instantly share code, notes, and snippets.

View ts95's full-sized avatar
🦉
🍰

Toni Sučić ts95

🦉
🍰
  • TET Digital
  • Oslo, Norway
View GitHub Profile
import SwiftUI
import MapKit
extension Map {
init<Items>(
coordinateRegion: Binding<MKCoordinateRegion>,
interactionModes: MapInteractionModes = .all,
showsUserLocation: Bool = false,
userTrackingMode: Binding<MapUserTrackingMode>? = nil,
@ts95
ts95 / swiftlint.sh
Created October 11, 2020 11:40
Only run SwiftLint for chunks that differ from HEAD
#!/bin/bash
# Parameters
source="$(pwd)/" # Track folder source where diffs are checked
project_root=${0%/*} # Track project root folder
swiftlint_path='swiftlint' # Track SwiftLint path
changed_files () {
sort -u \
<(git diff --name-only --diff-filter=d --cached) \
@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 / keybase.md
Last active February 8, 2021 10:10
keybase.md

Keybase proof

I hereby claim:

  • I am ts95 on github.
  • I am tonisucic (https://keybase.io/tonisucic) on keybase.
  • I have a public key ASCGFglVtkxTyx1EAXY733WiiJFdpaZ9yOOloqybTnHorAo

To claim this, I am signing this object:

@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 / 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 / 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 / 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 / WilfaSvart_Scale_BLE_Protocol.md
Last active May 9, 2025 09:46
Wilfa Svart scale BLE Protocol

Wilfa Svart BLE Scale Protocol Documentation

This document outlines the Bluetooth Low Energy (BLE) protocol for communicating with Wilfa Svart Uni Scale devices, specifically the Gen 1 and Gen 2 models, the former being the model powered by AA batteries, and the latter being rechargeable with a USB-C cable. The implementation is reverse-engineered from official firmware behavior and the Android application.

Overview

The Wilfa Svart scale exposes BLE services and characteristics for:

  • Receiving weight data
  • Monitoring unit changes and other state updates
  • Sending commands (e.g., tare, change unit)