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 / 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)
@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 / 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 / ImageRendererPoC.swift
Last active June 10, 2022 11:42
Proof of concept of using a SwiftUI `ImageRenderer` with a map, and how it blanks out the map.
import SwiftUI
import MapKit
struct ContentView: View {
@State private var mapRegion = MKCoordinateRegion(
center: CLLocationCoordinate2D(
latitude: 51.5,
longitude: -0.12
),
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 / ScreenshotView.swift
Created December 15, 2021 16:03
Proof of concept of code to take a screenshot of a view in SwiftUI.
import SwiftUI
struct ScreenShotView: View {
var body: some View {
GeometryReader { geo in
VStack {
Text("Hello world")
Button("Take screenshot") {
let generatedImage = self.takeScreenshot(
@shaundon
shaundon / InstagramShareView.swift
Created July 6, 2021 11:48
Proof of concept of sharing to Instagram Stories from SwiftUI
import SwiftUI
struct InstagramShareView: View {
var imageToShare: Image {
// An image defined in your app's asset catalogue.
return Image("SomeImage")
}
var body: some View {