Skip to content

Instantly share code, notes, and snippets.

View theoknock's full-sized avatar

James Alan Bush theoknock

View GitHub Profile
@theoknock
theoknock / DispatchGroup.swift
Last active May 16, 2024 19:46
Using a serial and global dispatch queue to synchronize calls to functions that should be executed one after another, preventing overlap.
import Foundation
public func randomizeDurationSplits(group: DispatchGroup? = nil, completion: @escaping ([[Double]]) -> Void) {
group?.enter()
DispatchQueue.global().async {
let dyad0harmony0: Double = Double.random(in: durationLowerBound ... durationUpperBound)
var dyad1harmony0: Double = dyad0harmony0
repeat {
@theoknock
theoknock / ConcentricCircularArrayView.swift
Last active May 10, 2024 09:35
A SwiftUI view consisting of a specified number of Circle() views, each with a diameter matching the standard Slider() "thumb" image (30 points), arranged in a perfect circle with an adjustable radius calculated to space them equidistantly, incorporating additional spacing between each circle.
import SwiftUI
struct CircularArrayView: View {
private let diameter: CGFloat = 30
var numberOfCircles: Int = 12
var spacing: CGFloat = 1.0
// Ensures the number of circles is always even
private var evenNumberOfCircles: Int {
numberOfCircles % 2 == 0 ? numberOfCircles : numberOfCircles + 1
@theoknock
theoknock / SimulateColorOpacity.swift
Created May 6, 2024 03:00
Simulates a color at opacity < 1.0 when placed over a black or white background
func hueAccentColor(angle: CGFloat) -> Color {
return Color(hue: CGFloat(angle / 360.0), saturation: 1.0, brightness: 1.0, opacity: 1.0)
}
func whiteColor() -> Color {
return Color.init(uiColor: .white) // return Color(hue: CGFloat(hueAngle / 360.0), saturation: 0.0, brightness: 1.0, opacity: 1.0)
}
func blackColor() -> Color {
return Color.init(uiColor: .black) // return Color(hue: CGFloat(hueAngle / 360.0), saturation: 0.0, brightness: 0.0, opacity: 1.0)
@theoknock
theoknock / Hue Circular Slider Control
Created May 2, 2024 08:47
A circular slider that sets the hue
import SwiftUI
struct ContentView: View {
var body: some View {
HueControlView()
}
}
struct HueControlView: View {
@State var hueAngle: CGFloat = 0.0
@theoknock
theoknock / AccessibilityColorCombo.swift
Last active April 25, 2024 22:19
Generating accessible text and background colors (in progress)
//
// ContentView.swift
// AccessibilityColorPicker
//
// Created by Xcode Developer on 4/25/24.
//
import SwiftUI
import UIKit
import Observation
@theoknock
theoknock / Site Crawler.py
Last active April 22, 2024 20:43
Collects data from a given site based on tag and parameter criteria
import requests
from bs4 import BeautifulSoup
import json
def fetch_posts(url, visited_urls, base_url, file):
if url in visited_urls:
return
print(f"Visiting {url}")
visited_urls.add(url)
@theoknock
theoknock / CustomSliderView.swift
Last active March 26, 2024 14:59
Adds a UISlider to a SwiftUI view. The track is replaced by a linear gradient, which has the same height as the thumb.
import SwiftUI
import UIKit
struct CustomUISlider: UIViewRepresentable {
@Binding var value: Double
var range: ClosedRange<Double>
func makeUIView(context: Context) -> UISlider {
let slider = UISlider(frame: .zero)
slider.minimumTrackTintColor = UIColor.clear
.fileImporter(isPresented: $isImporting, allowedContentTypes: [.plainText], allowsMultipleSelection: false) { result in
switch result {
case .success(let urls):
guard let url = urls.first else { return }
do {
// Ensure you have access to the resource
guard url.startAccessingSecurityScopedResource() else {
print("Access to the file was denied.")
return
}
import SwiftUI
import UniformTypeIdentifiers
struct MyStruct: Codable, Identifiable {
let id: Int
var name: String
}
struct ContentView: View {
@State private var structs = [MyStruct]()
@theoknock
theoknock / getAssistant.swift
Last active December 8, 2023 19:28
Getting the Assistant ID (OpenAI - ChatGPT). The first step to setting up a multiturn conversation with ChatGPT...
struct Assistant: Codable {
let id: String
}
func getAssistant(globalData: GlobalData) {
let url = URL(string: "https://api.openai.com/v1/assistants")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")