Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View davidsteppenbeck's full-sized avatar

David Steppenbeck davidsteppenbeck

View GitHub Profile
@davidsteppenbeck
davidsteppenbeck / BindableExamples.swift
Created August 16, 2023 18:22
Examples of applying `Bindable` from environment observables in SwiftUI views in iOS 17
import SwiftUI
@Observable class ViewModel {
var isOn: Bool
var text: String
init(isOn: Bool = false, text: String = "default") {
self.isOn = isOn
self.text = text
}
@davidsteppenbeck
davidsteppenbeck / iOS.swift
Created May 15, 2023 12:42
Typealias + extension examples for multiplatform development in Swift.
import SwiftUI
/*
Put these in a file that is compiled for iOS only.
Note that these are just a few examples of things that worked well in my project.
They may or may not be useful to you, but they give an idea of how typealias + extensions
could be used to keep your code tidy.
*/
@davidsteppenbeck
davidsteppenbeck / MultiplatformLabel.swift
Created May 15, 2023 12:13
A platform specific label for SwiftUI.
import SwiftUI
/// Provides a text label appropriate for the current platform.
///
/// Specifically, this provides a `Label` with the specified system image on iOS and `Text` without an image on macOS.
struct MultiplatformLabel: View {
// MARK:- Properties
/// A title generated from a localized string.
@davidsteppenbeck
davidsteppenbeck / MultiplatformWidgetFamily.swift
Created May 15, 2023 11:57
A method for specifying multiplatform widget families in Swift.
import SwiftUI
import WidgetKit
@available(iOS 16.0, macOS 13.0, *)
public enum MultiplatformWidgetFamily: CaseIterable {
/// A small widget.
///
/// The small system widget can appear on the Home Screen or in the Today View in iOS and iPadOS, or in the Notification Center on macOS.
case systemSmall
@davidsteppenbeck
davidsteppenbeck / WidgetFamily+Utilities.swift
Created May 15, 2023 11:54
Methods for providing widget family specific values in Swift.
import SwiftUI
import WidgetKit
@available(iOS 14.0, macOS 11.0, *)
public extension WidgetFamily {
/// Returns the value for the current widget family.
///
/// If no value is provided for the current widget family, the method will return the `defaultValue`.
///
@davidsteppenbeck
davidsteppenbeck / ValueForDevice.swift
Created May 15, 2023 11:45
Global functions for providing device specific values in Swift.
import Foundation
/*
Put these global functions in a file that is compiled for all platforms.
*/
/// Returns the value for the current device from the values that are provided.
///
/// - Parameters:
/// - phoneValue: The value to use for iPhone.
@davidsteppenbeck
davidsteppenbeck / ValueForPlatform.swift
Last active December 25, 2023 22:27
A global function for providing multiplatform specific values in Swift.
import Foundation
/// Returns the value for the current platform from the values that are provided.
///
/// - Parameters:
/// - iOSValue: The value to use for the iOS platform.
/// - macOSValue: The value to use for the macOS platform.
func valueForPlatform<Value>(iOS iOSValue: @autoclosure () -> Value, macOS macOSValue: @autoclosure () -> Value) -> Value {
#if os(iOS)
iOSValue()
@davidsteppenbeck
davidsteppenbeck / SymbolText.swift
Last active May 5, 2023 05:26
Attributed strings in Swift: Add colored SFSymbols to strings directly in your Localizable.strings files.
import SwiftUI
enum ColorAttribute: CodableAttributedStringKey, MarkdownDecodableAttributedStringKey {
enum Value: String, Codable, Hashable {
case red
case orange
case yellow
case green
case mint
case teal
@davidsteppenbeck
davidsteppenbeck / PropertyWrapperApp.swift
Last active January 30, 2023 10:15
A Swift property wrapper for storing colors in User Defaults.
import SwiftUI
@main
struct PropertyWrapperApp: App {
var body: some Scene {
WindowGroup {
VStack {
ContentView()
ContentView()
}
import SwiftUI
import WidgetKit
enum WidgetPreviewProviderMode: CaseIterable {
/// Use for a system small widget preview.
case small
/// Use for a system medium widget preview.
case medium