This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
var Words = """ | |
""" | |
var keyWords: String { | |
Words.replacingOccurrences(of: " : {", with: "") | |
.replacingOccurrences(of:" \"", with: "") | |
.replacingOccurrences(of: "\"\n\n },", with: "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct PreviewLocation: ViewModifier { | |
enum LocationDir { | |
case documentsDirectory | |
case applicationSupportDirectory | |
} | |
let directory: LocationDir | |
func body(content: Content) -> some View { |