Skip to content

Instantly share code, notes, and snippets.

View StewartLynch's full-sized avatar

Stewart Lynch StewartLynch

View GitHub Profile
// How can I transition from the sidebar view to the contentView on iPhone?
import SwiftUI
struct Name: Identifiable, Hashable {
let id = UUID()
var firstName: String
var lastName: String
static var sample: [Name] {
@StewartLynch
StewartLynch / PrintDetail
Created November 6, 2022 22:20
Print file, function and line along with the printed items
public func printDetail( _ items: Any...,
fileID: String = #fileID,
function: String = #function,
line: Int = #line,
separator: String = " ",
terminator: String = "\n") {
print("___________________________")
print(items.map{ "\($0)" }.joined(separator: separator))
print(
"""
import SwiftUI
enum Style: String, CaseIterable {
case grouped, inset, insetGrouped, plain, sidebar
}
struct ContentView: View {
@State private var selectedStyle: Style = .plain
var body: some View {
import SwiftUI
struct PreviewLocation: ViewModifier {
enum LocationDir {
case documentsDirectory
case applicationSupportDirectory
}
let directory: LocationDir
func body(content: Content) -> some View {
// Sample Use
struct ContentView: View {
@State private var size: CGSize = .zero
var body: some View {
VStack {
Text("\(size.width)")
Text("Hello World")
.getCGSize($size)
}
.padding()
@StewartLynch
StewartLynch / Timer.swift
Last active December 30, 2023 19:09
ContentTransition
import SwiftUI
struct ContentView: View {
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
@State private var totalSeconds = 30
var body: some View {
Text(timeStringFromSeconds(totalSeconds))
.monospaced()
.contentTransition(.numericText())
.font(.system(size: 60))
@StewartLynch
StewartLynch / RatingsView.swift
Last active December 6, 2023 22:58
Custom Ratings View
// https://gist.github.com/StewartLynch/03372c873fef568e0a613a968adbae69
import SwiftUI
/// A view of inline images that represents a rating.
/// Tapping on an image will change it from an unfilled to a filled version of the image.
///
/// The following example shows a Ratings view with a maximum rating of 10 red flags, each with a width of 20:
///
/// RatingsView(maxRating: 10,
@StewartLynch
StewartLynch / BookSamples.swift
Last active October 1, 2023 22:23
Sample Books for the SwiftData project
import Foundation
extension Book {
static let lastWeek = Calendar.current.date(byAdding: .day, value: -7, to: Date.now)!
static let lastMonth = Calendar.current.date(byAdding: .month, value: -1, to: Date.now)!
static var sampleBooks: [Book] {
[
Book(title: "QBVII",
author: "Leon Uris"),
Book(title: "Macbeth",
@Observable class SomeClass {
// @AppStorage("name") var name = "" // Does not work
/// This will do the same thing
var name: String {
get {
access(keyPath: \.name)
return UserDefaults.standard.string(forKey: "name") ?? ""
}
set {
withMutation (keyPath: \.name) {
@StewartLynch
StewartLynch / gist:977b90d2db18384ddcacf444902d8702
Last active March 21, 2024 08:46
KeyWord Extraction Playground for Localizations Video
import UIKit
var Words = """
"""
var keyWords: String {
Words.replacingOccurrences(of: " : {", with: "")
.replacingOccurrences(of:" \"", with: "")
.replacingOccurrences(of: "\"\n\n },", with: "")