Skip to content

Instantly share code, notes, and snippets.

View noahsark769's full-sized avatar

Noah Gilmore noahsark769

View GitHub Profile
@noahsark769
noahsark769 / useGesture.ts
Last active October 8, 2024 17:20 — forked from KristofferEriksson/useGesture.ts
A custom React Typescript hook for advanced touch gestures in UI
import { useEffect, useRef } from "react";
type GestureType =
| "swipeUp"
| "swipeDown"
| "swipeLeft"
| "swipeRight"
| "tap"
| "pinch"
| "zoom";
@noahsark769
noahsark769 / UserDefaultsWrappers.swift
Created May 14, 2021 18:20
User Defaults Property Wrappers
import Foundation
@propertyWrapper
struct SimpleUserDefault<T> {
let userDefaults: UserDefaults
let key: String
let defaultValue: T
init(
userDefaults: UserDefaults = UserDefaults.standard,
@noahsark769
noahsark769 / SwitchExpression.swift
Created January 8, 2021 17:39
SwitchExpression.swift
@_functionBuilder struct SwitchExpression {
static func buildBlock<T>(_ content: T) -> T {
return content
}
static func buildEither<T>(first: T) -> T {
return first
}
static func buildEither<T>(second: T) -> T {
@noahsark769
noahsark769 / LoginViewController.swift
Created December 29, 2020 20:03
Copying WKWebView cookies into HTTPCookieStorage
extension LoginViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if let url = webView.url, url.absoluteString.contains("/index.action") {
let store = WKWebsiteDataStore.default().httpCookieStore
store.getAllCookies { cookies in
if let sessionIdCookie = cookies.first(where: { cookie in
cookie.name == "JSESSIONID"
}) {
HTTPCookieStorage.shared.setCookies(cookies, for: webView.url, mainDocumentURL: nil)
self.callback(ConfluenceSessionCookie(jSessonId: sessionIdCookie))
@noahsark769
noahsark769 / AnalyticsController.swift
Created December 5, 2020 22:21
AnalyticsController.swift
final class AmplitudeController {
static let shared = AmplitudeController()
private let amplitudeInstance = Amplitude.instance()
private var globalProperties: [String: Any] = [:]
func initialise() {
self.amplitudeInstance.initializeApiKey("bda1f35c62f39a02dc6d4cbf416f9bb8")
self.globalProperties = [
"appVersion": AppEnvironment.version,
"buildNumber": AppEnvironment.buildNumber,
import kotlinx.cinterop.memScoped
import kotlinx.cinterop.allocArrayOf
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.usePinned
import platform.Foundation.NSData
import platform.Foundation.create
import platform.posix.memcpy
public fun ByteArray.toData(): NSData = memScoped {
NSData.create(bytes = allocArrayOf(this@toData),
@noahsark769
noahsark769 / swiftlint-example.swift
Created August 21, 2020 20:48
multiline_arguments_parameters false positives
expect(issue.createdAt).to(equal(
Helper.date(["$date": NSNumber(value: value1)])
))
expect(followers).to(containExactUnorderedElements([
Follower(
followerUid: "a",
followerType: .group
),
Follower(
import SwiftUI
// Note: There are some issues with using these modifiers inside of ButtonStyles on macOS. Please see https://twitter.com/noahsark769/status/1288256379640139776?s=20 for more info.
struct ConditionalContent<TrueContent: View, FalseContent: View>: View {
let value: Bool
let trueContent: () -> TrueContent
let falseContent: () -> FalseContent
@ViewBuilder var body: some View {
// The following is a code sample based on https://twitter.com/noahsark769/status/1264681181435420672?s=20
// It's not really done, but putting it here for the benefit of the community. Maybe at some point soon I'll open source
// this into a proper library.
//
// What it does: Defines a ConstraintSystem SwiftUI view which allows you to specify autolayout constraints between views.
//
// Caveats:
// - Only works for AppKit/NSView/NSViewRepresentable, not UIKit yet
// - Only works on the first render (update(nsView) implementation is empty)
// - The constraint identifiers must be strings, it would be nice to make them generic over some type that is Hashable,
@noahsark769
noahsark769 / Appending.swift
Created May 10, 2020 21:24
Nested property wrappers with Swift
// https://noahgilmore.com/blog/nesting-property-wrappers
import Cocoa
protocol Appendable {
func appending(string: String) -> Self
}
extension String: Appendable {
func appending(string: String) -> String {