Skip to content

Instantly share code, notes, and snippets.

extension Bundle {
func decode<T: Decodable>(_ type: T.Type, from file: String, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) -> T {
guard let url = self.url(forResource: file, withExtension: nil) else {
fatalError("Failed to locate \(file) in bundle.")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Failed to load \(file) from bundle.")
}
@lgastler
lgastler / BackgroundBlur.swift
Created February 23, 2023 08:53
BackgroundBlur
import SwiftUI
struct BackgroundBlob: View {
@State private var rotationAmount = 0.0
let alignment: Alignment = [.topLeading, .topTrailing, .bottomLeading, .bottomTrailing].randomElement()!
let color: Color = [.blue, .cyan, .indigo, .mint, .purple, .teal].randomElement()!
var body: some View {
Ellipse()
.fill(color)
@lgastler
lgastler / network-stack.swift
Created January 28, 2023 19:28
Swift NetworkStack
struct Endpoint<T: Decodable> {
var path: String
var type: T.Type
var method = HTTPMethod.get
var headers = [String: String]()
var keyPath: String?
}
extension Endpoint where T == Array<News> {
static let headlines = Endpoint(path: "api/users", type: Array<User>.self, keyPath: "data")
@lgastler
lgastler / Debouncer.swift
Created January 28, 2023 16:33
Debouncer
class Debouncer<T>: ObservableObject {
@Published var input: T
@Published var output: T
private var debounce: AnyCancellable?
init(initialValue: T, delay: Double = 1) {
self.input = initialValue
self.output = initialValue
@lgastler
lgastler / DebouncedObservedObject.swift
Created January 28, 2023 16:32
DebouncedObservedObject
import Combine
import Foundation
@propertyWrapper @dynamicMemberLookup
class DebouncedObservedObject<Wrapped: ObservableObject>: ObservableObject {
var wrappedValue: Wrapped
private var subscription: AnyCancellable?
init(wrappedValue: Wrapped, delay: Double = 1) {
self.wrappedValue = wrappedValue
import {
AnimatePresence,
motion,
useScroll,
useTransform,
} from "framer-motion";
import { useEffect, useRef, useState } from "react";
export default function HomePage() {
const stopAmount = 8;
// some credit for the base structure goes to Jacob Ebey who implemented a similar cache using redis (see: https://github.com/jacob-ebey/remix-ecommerce/tree/main/app)
interface RequestResponseCache {
(request: Request, maxAgeSeconds: number): Promise<Response>;
}
const YEAR_AGE = 31556926
// a small hash function to hash a string using the worker compatible crypto engine
async function sha256(message: string) {
stages:
- build
- prepare
- deploy
build:
stage: build
image: node:12
environment:
name: stage
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH="/Users/lennart/.oh-my-zsh"
# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
@lgastler
lgastler / machine.js
Created October 21, 2020 19:23
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions