Skip to content

Instantly share code, notes, and snippets.

View shaundon's full-sized avatar

Shaun Donnelly shaundon

View GitHub Profile
@shaundon
shaundon / ContentView.swift
Created February 4, 2024 10:20
Proof of concept of UserDefaults plus Zephyr, making use of notifications to update the UI when a value changes externally.
import SwiftUI
struct ContentView: View {
@State private var preferences = PreferencesModel()
var body: some View {
VStack {
HStack {
Text("Value: ")
Text(preferences.theNumber.formatted()).bold()
@shaundon
shaundon / ContentView.swift
Last active February 3, 2024 15:05
UserDefaults group proof of concept
import SwiftUI
struct ContentView: View {
@Environment(PreferencesModel.self) var preferences
var body: some View {
Text("Salary: \(preferences.salary)")
}
}
import Foundation
import SwiftUI
protocol Achievement {
var id: UUID { get }
var name: String { get }
var description: String { get }
var isAchieved: Bool { get set }
var primaryTint: Color { get }
var secondaryTint: Color { get }
@shaundon
shaundon / WorkoutSplit.swift
Created February 21, 2021 17:34
Convert an array of HKQuantitySample into splits.
import Foundation
import HealthKit
struct WorkoutSplit: Hashable {
let label: String
let distance: HKQuantity
let duration: TimeInterval
}
extension WorkoutSplit {
@shaundon
shaundon / ContentView.swift
Created March 4, 2021 11:45
MapView with polyline support in SwiftUI
import SwiftUI
import MapKit
struct ContentView: View {
@State private var region = MKCoordinateRegion(
// Apple Park
center: CLLocationCoordinate2D(latitude: 37.334803, longitude: -122.008965),
span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
)
@shaundon
shaundon / MyWidget.swift
Last active February 26, 2023 02:53
Proof of concept of using caches in WidgetKit.
/**
Most of this file has been omitted to only focus on the caching aspect, but in reality
it would be a standard widget template.
**/
struct MyWidgetProvider: IntentTimelineProvider {
// Initialise the cache.
private let cache = MyWidgetCache()
@shaundon
shaundon / FallWithRotation.swift
Created February 21, 2023 20:30
A SwiftUI transition for making views fall off the screen with a slight rotation effect.
struct FallWithRotationModifier: ViewModifier {
let isHidden: Bool
var yOffset: CGFloat {
return isHidden ? 1000 : 0
}
var rotationDegrees: Double {
guard isHidden else { return 0 }
let degrees = Double.random(in: 30...90)
import SwiftUI
struct ScrollingEmojiView: View {
@Environment(\.accessibilityReduceMotion) var reduceMotion
@State private var isAnimating = false
/*
In Personal Best (getpersonalbest.com) I get a random emoji to represent a workout type,
using an extension to HKWorkoutActivityType. Here I've stubbed it out with a more basic
implementation.
@shaundon
shaundon / NavigationStack.swift
Created July 24, 2022 13:18
Proof of concept of a NavigationStack not updating when a value changes
import SwiftUI
struct ContentView: View {
@State private var foo: String? = nil
var body: some View {
// Change this to `NavigationView` and it'll work.
NavigationStack {
@shaundon
shaundon / ContentView.swift
Last active July 4, 2022 11:31
PHPicker in SwiftUI
import SwiftUI
struct ContentView: View {
@State private var showPhotoSheet = false
@State private var image: UIImage? = nil
var body: some View {
VStack {
Button(action: { showPhotoSheet = true }) {
Label("Choose photo", systemImage: "photo.fill")