Skip to content

Instantly share code, notes, and snippets.

View StewartLynch's full-sized avatar

Stewart Lynch StewartLynch

View GitHub Profile
@StewartLynch
StewartLynch / SampleParisLocations.swift
Last active January 2, 2024 06:02
Sample Paris Locations
// Created for MyTrips
// by Stewart Lynch on 2024-01-01
//
// Follow me on Mastodon: @[email protected]
// Follow me on Threads: @StewartLynch (https://www.threads.net)
// Follow me on X: https://x.com/StewartLynch
// Subscribe on YouTube: https://youTube.com/@StewartLynch
// Buy me a ko-fi: https://ko-fi.com/StewartLynch
import UIKit
extension UIImage {
func resizeImage(targetSize: CGSize) -> UIImage {
let size = self.size
let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height
let newSize = widthRatio > heightRatio ? CGSize(width: size.width * heightRatio, height: size.height * heightRatio) : CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
@StewartLynch
StewartLynch / Configuration.swift
Created November 2, 2023 23:00
Configuration Key Value enum
// Code from NSHipster: https://nshipster.com/xcconfig/
import Foundation
enum Configuration {
enum Error: Swift.Error {
case missingKey, invalidValue
}
static func value<T>(for key: String) throws -> T where T: LosslessStringConvertible {
guard let object = Bundle.main.object(forInfoDictionaryKey:key) else {
@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: "")
@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 / 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",
@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 / 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))
// 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()
import SwiftUI
struct PreviewLocation: ViewModifier {
enum LocationDir {
case documentsDirectory
case applicationSupportDirectory
}
let directory: LocationDir
func body(content: Content) -> some View {