Skip to content

Instantly share code, notes, and snippets.

View lukepistrol's full-sized avatar

Lukas Pistrol lukepistrol

View GitHub Profile
import Foundation
import SwiftUI
// MARK: - Custom Button Style
struct MobileMeButtonStyle: ButtonStyle {
// MARK: Metrics
@ScaledMetric private var cornerRadius = 12
@ScaledMetric private var horizontalLabelPadding = 12
@ScaledMetric private var verticalLabelPadding = 8
@lukepistrol
lukepistrol / TaskTrigger.swift
Last active November 19, 2023 19:32
Attach async tasks to SwiftUI views using a trigger mechanism.
import SwiftUI
struct TaskTrigger<T: Equatable>: Equatable {
fileprivate enum TaskState<S: Equatable>: Equatable {
case inactive
case active(value: S, uniqueId: UUID? = nil)
}
fileprivate var state: TaskState<T> = .inactive
@lukepistrol
lukepistrol / View+TaskID.swift
Created April 10, 2023 08:25
Provides a task modifier with an optional binding that will run the task if the binding is set to a non-nil value and reset it to nil once the task is done. The purpose of this is to bind a one-shot task to the view, i.e. the cancellation is automatically performed if the view should be dismissed.
/// Provides a task modifier with an optional binding that will run the task if the binding is set to a
/// non-nil value and reset it to nil once the task is done.
///
/// The purpose of this is to bind a one-shot task to the view, i.e. the cancellation is automatically
/// performed if the view should be dismissed.
struct TaskViewModifier<T: Equatable>: ViewModifier {
@Binding var taskId: T?
let action: @Sendable (_: T) async -> Void
@lukepistrol
lukepistrol / AsyncButton.swift
Created October 17, 2022 21:00
A SwiftUI Button that can perform asyncronous tasks.
import SwiftUI
/// A control that initiates an action asyncronously.
///
/// You create a button by providing an action and a label.
/// The action is either a method or closure property that
/// does something when a user clicks or taps the button.
/// The label is a view that describes the button’s action —
/// for example, by showing text, an icon, or both:
///
@lukepistrol
lukepistrol / FormatStyle+AbbreviatedIntegerStyle.swift
Created September 6, 2022 19:46
Large Integer Formatter (K,M,B,…) Swift
struct AbbreviatedIntegerStyle: FormatStyle {
typealias FormatInput = Int
typealias FormatOutput = String
func format(_ value: Int) -> String {
let absolute = abs(value)
let number = Double(value)
func rnd(_ number: Double) -> String {