Skip to content

Instantly share code, notes, and snippets.

@shnhrrsn
shnhrrsn / OnScrollModifier.swift
Created February 10, 2023 02:39
Simple SwiftUI onScroll Workaround
extension View {
func onScroll(_ isScrolling: Binding<Bool>) -> some View {
modifier(OnScrollModifier(isScrolling: isScrolling))
}
}
private struct OnScrollModifier: ViewModifier {
@State private var callbackID = 0
@Binding var isScrolling: Bool
import SwiftUI
/// Implementation of `Layout` that aligns subviews relative it their `anchor` value
///
/// Considerations before using:
/// * Sizing is not taken into account
/// * No stacking is provided if multiple subviews are anchored to the same position
/// * Subviews will be able to overlap if anchored closely together and they're both large enough
public struct AnchorLayout: Layout {
public init() { }
@shnhrrsn
shnhrrsn / String+Hashing.swift
Created September 30, 2019 23:55
Swift CryptoKit MD5/SHA1 Hash
import Foundation
import CryptoKit
private protocol ByteCountable {
static var byteCount: Int { get }
}
extension Insecure.MD5: ByteCountable { }
extension Insecure.SHA1: ByteCountable { }
func savePNG(_ frame: AVFrame, to url: String) throws {
let fmtCtx = try AVFormatContext(format: nil, filename: url)
guard let codec = AVCodec.findEncoderById(.PNG) else {
fatalError("png codec doesn't exist")
}
guard let codecCtx = AVCodecContext(codec: codec) else {
fatalError("Could not allocate video codec context")
}
codecCtx.width = frame.width
@shnhrrsn
shnhrrsn / AuthController.js
Last active October 22, 2017 19:24
Example of using PassportJS with Grind
import { Controller } from 'grind-framework'
export class AuthController extends Controller {
login(req, res) {
if(!req.user.isNil) {
return res.redirect('/')
}
return res.render('auth.login')
@shnhrrsn
shnhrrsn / plexDatabaseBackupScript.sh
Last active January 5, 2022 13:46 — forked from ssmereka/plexDatabaseBackupScript.sh
Plex Media Server database backup script.
#!/usr/bin/env bash
# Backup a Plex database.
# Author Scott Smereka
# Version 1.0
# Modified by Shaun Harrison
# Version 1.1
# Script Tested on:
@shnhrrsn
shnhrrsn / urlsession-mitmproxy.swift
Created March 12, 2017 20:02
URLSession + MITM Proxy
fileprivate class NetworkingDelegate: NSObject, URLSessionDelegate {
fileprivate func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
}
}
private let proxySessionDelegate = NetworkingDelegate()
extension CGRect: DebugPrintable {
var debugDescription: String {
return NSStringFromCGRect(self)
}
}
extension CGSize: DebugPrintable {
var debugDescription: String {
return NSStringFromCGSize(self)
}
@shnhrrsn
shnhrrsn / SHNFakeStatusBar.m
Created December 3, 2013 18:53
Include in your code to create a fake and consistent status bar to take screenshots with.
//
// SHNFakeStatusBar
// Created by Shaun Harison on 12/3/13.
//
#import <objc/runtime.h>
#define CARRIER_STRING @"Apple"
#define TIME_STRING @"Timestamp"
@shnhrrsn
shnhrrsn / gist:5723701
Last active December 18, 2015 04:09
Swap class selectors with a block
#import <objc/runtime.h>
#import <objc/message.h>
SEL SHNSwapSelectorWithBlock(Class class, SEL selector, BOOL isClassMethod, id block) {
Method method1 = isClassMethod ? class_getClassMethod(class, selector) : class_getInstanceMethod(class, selector);
// Generate new selector to inject
SEL newSelector = NSSelectorFromString([@"_swappedBlock_" stringByAppendingString:NSStringFromSelector(selector)]);
// Inject the block as method in the class