Skip to content

Instantly share code, notes, and snippets.

View andresr-dev's full-sized avatar

Andres Raigoza andresr-dev

View GitHub Profile
@andresr-dev
andresr-dev / OptionalAnimation.swift
Last active May 1, 2022 17:32
This helps you to run an optional animation depending on a condition without needing to put your conditional logic inside your withAnimation closure. In this example we support Reduce Motion Accessibility
import SwiftUI
// MARK: GLOBAL FUNCTION
func withOptionalAnimation<Result>(_ animation: Animation? = .default, _ body: () throws -> Result) rethrows -> Result {
// Evaluate the parameter you want to check: ReduceMotion in this case
if UIAccessibility.isReduceMotionEnabled {
// The body gets executed without animation
return try body()
} else {
// The body gets executed with animation
@andresr-dev
andresr-dev / Astronaut.swift
Last active June 30, 2022 18:31
This is a bundle extension that helps you find a file previously saved in your app bundle and decode it from JSON format into any custom type you want, as long as it conforms to decodable protocol
struct Astronaut: Codable, Identifiable {
let id: String
let name: String
let description: String
static let allAstronauts = Bundle.main.decode([Astronaut].self, from: "astronauts.json")
static let example = allAstronauts[0]
}
@andresr-dev
andresr-dev / FileManagerDecodableExtension.swift
Last active November 3, 2022 21:02
This is a FileManager extension that helps you find the documents directory of the user, decode data saved in documents and save data to documents, and bellow there's a file on how to use it.
import Foundation
extension FileManager {
static var documentsDirectory: URL {
URL.documentsDirectory
}
func decode<T: Codable>(from url: URL) throws -> T {
let data = try Data(contentsOf: url)
@andresr-dev
andresr-dev / UserDefaultsExample.swift
Last active May 1, 2022 17:32
This is an example of how to use UserDefaults inside a ViewModel. We should not store more than 500KB of data in UserDefaults
import Foundation
struct Card: Identifiable, Codable {
var id = UUID()
let prompt: String
let answer: String
}
@MainActor class ViewModel: ObservableObject {
@Published var cards = [Card]()
@andresr-dev
andresr-dev / AppStorageExample.swift
Last active May 1, 2022 17:31
This is an example of how to use @AppStorage
import SwiftUI
struct ContentView: View {
@AppStorage("tapCount") private var tapCount = 0
var body: some View {
Button("Tap count: \(tapCount)") {
tapCount += 1
}
}
@andresr-dev
andresr-dev / CustomAlignmentGuideExample.swift
Last active May 1, 2022 17:30
This is how you can make custom alignments and how to use it. In this case we do a custom alignment for two separate VStacks
import SwiftUI
extension VerticalAlignment {
enum MidAccountAndName: AlignmentID {
static func defaultValue(in context: ViewDimensions) -> CGFloat {
context[.top]
}
}
static let midAccountAndName = VerticalAlignment(MidAccountAndName.self)
@andresr-dev
andresr-dev / CustomColorExtension.swift
Last active May 1, 2022 17:29
This ShapeStyle extension helps you to use your custom colors in a very easy way
import SwiftUI
extension ShapeStyle where Self == Color {
static var darkBackground: Color {
Color(red: 0.1, green: 0.1, blue: 0.2)
}
}
// HOW TO USE IT
struct ContentView: View {
@andresr-dev
andresr-dev / SimpleHaptics.swift
Created April 25, 2022 17:52
This is how you can add simple haptics to your applications.
import SwiftUI
struct SimpleHaptics: View {
var body: some View {
Text("Hello, World!")
.onTapGesture {
simpleSuccess()
}
}
@andresr-dev
andresr-dev / CustomHaptics.swift
Created April 25, 2022 18:40
This is an example of how you can create custom haptics
import SwiftUI
import CoreHaptics
struct CustomHaptics: View {
// We use @State here because we want the engine property be alive even if this view gets recreated.
@State private var engine: CHHapticEngine?
var body: some View {
Text("Hello, World!")
.onAppear(perform: prepareHaptics)
@andresr-dev
andresr-dev / AccessibilityExample.swift
Last active May 1, 2022 17:30
This is an example of how you can support voice over accessibility in your applications.
import SwiftUI
struct AccessibilityExample: View {
var body: some View {
ZStack {
// Background
Image(decorative: "wood")
.resizable()
.ignoresSafeArea()