Skip to content

Instantly share code, notes, and snippets.

@cgfarmer4
cgfarmer4 / AudioSessionManager.swift
Last active October 12, 2023 09:29
Poor mans streaming using AVCaptureSession
class AudioSessionManager: NSObject, ObservableObject {
@Published var microphones: [AVCaptureDevice] = []
var captureSession: AVCaptureSession = .init()
var audioOutput: AVCaptureAudioDataOutput?
var configured: Bool = false
private var audioInput: AVCaptureDeviceInput?
let dataOutputQueue = DispatchQueue(label: "audio_queue",
qos: .userInteractive,
@citelao
citelao / README.md
Created February 27, 2023 02:39
Swift NSCollectionView

This is straightforward, copy-pasted code from an app I'm building, where I needed to wrap NSCollectionView for SwiftUI.

It's really bad code, but it works at least.

Usage:

SwiftNSCollectionView(items: ["a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "c"], itemSize: nil) { item in
     Text(item)
}
@4v3ngR
4v3ngR / ytadblock.js
Last active March 27, 2024 15:50
Block youtube ads in Safari with userscripts
// ==UserScript==
// @name Youtube adblock
// @namespace http://tampermonkey.net/
// @version 0.2
// @description enough with the ads!
// @author 4v3ngR
// @match https://*.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant none
// @run-at document-start
@cjwcommuny
cjwcommuny / MacEditorTextView.swift
Created October 31, 2021 10:46
An NSTextView wrapped by SwiftUI with TextKit 2
/**
* MacEditorTextView
* Copyright (c) Thiago Holanda 2020-2021
* https://twitter.com/tholanda
*
* MIT license
* Modified by https://github.com/cjwcommuny for TextKit 2
*/
import Combine
// A URLSession extension that fetches data from a URL and decodes to some Decodable type.
// Usage: let user = try await URLSession.shared.decode(UserData.self, from: someURL)
// Note: this requires Swift 5.5.
extension URLSession {
func decode<T: Decodable>(
_ type: T.Type = T.self,
from url: URL,
keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys,
dataDecodingStrategy: JSONDecoder.DataDecodingStrategy = .deferredToData,
dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate
@danhalliday
danhalliday / TappableContentView.swift
Last active February 29, 2024 07:07
SwiftUI solution for responsive, instantly-tappable tiles in a scroll view.
import SwiftUI
/// SwiftUI implementation of a responsive-feeling scrollview with tappable tiles.
/// We use a custom UIScrollView via UIViewRepresentable and a UIView tap overlay to
/// get around current SwiftUI issues with simultaneous gesture recognition and allow
/// the tiles to respond instantly to presses while scrolling.
struct ContentView: View {
@State private var showSheet = false
@timdown
timdown / range_selection_save_restore.js
Last active January 27, 2024 18:41
Range and selection marker-element-based save and restore
/**
* This is ported from Rangy's selection save and restore module and has no dependencies.
* Copyright 2019, Tim Down
* Licensed under the MIT license.
*
* Documentation: https://github.com/timdown/rangy/wiki/Selection-Save-Restore-Module
* Use "rangeSelectionSaveRestore" instead of "rangy"
*/
var rangeSelectionSaveRestore = (function() {
var markerTextChar = "\ufeff";
@toioski
toioski / script.applescript
Last active June 5, 2023 01:30
Automatically Open Safari Dev Tools for iOS Simulator
-- `menu_click`, by Jacob Rus, September 2006.
-- Ref: https://stackoverflow.com/questions/14669542/automatically-open-the-safari-debugger-when-the-iphone-simulator-is-launched
on menu_click(mList)
local appName, topMenu, r
-- Validate our input
if mList's length < 3 then error "Menu list is not long enough"
-- Set these variables for clarity and brevity later on
@mwrouse
mwrouse / Autocomplete.js
Last active April 25, 2024 08:47
Autocompletion for an object in the monaco editor
function ShowAutocompletion(obj) {
// Disable default autocompletion for javascript
monaco.languages.typescript.javascriptDefaults.setCompilerOptions({ noLib: true });
// Helper function to return the monaco completion item type of a thing
function getType(thing, isMember) {
isMember = (isMember == undefined) ? (typeof isMember == "boolean") ? isMember : false : false; // Give isMember a default value of false
switch ((typeof thing).toLowerCase()) {
case "object":
@inexorabletash
inexorabletash / @ IndexedDB Full Text Search (Proof of Concept).md
Last active March 19, 2024 13:08
IndexedDB Full Text Search (Proof of Concept)

This demonstrates the implementation of full text search for documents in Indexed DB.

  • Word-breaking and stemming is used to create a list of terms for each document.
  • Document records are annotated with the list of terms when added to the database.
  • A multi-entry index on the list of terms is populated.
  • A query is similarly processed into a list of terms.
  • A join over the terms is implemented using multiple cursors on the index.

The necessity of annotating records with the word list to populate the index is a limitation of the current Indexed DB API. A feature request to support custom