Skip to content

Instantly share code, notes, and snippets.

@bradley
bradley / RefreshableScrollView.swift
Last active November 28, 2021 00:46
Pull-to-Refresh: SwiftUI, UIKit-Backed, Actually-Usable
//
// RefreshableScrollView.swift
// --
//
// Created by Bradley on 3/24/21.
//
import Combine
import SwiftUI
import UIKit
@bradley
bradley / anypublisher-all.swift
Last active March 2, 2021 21:05
Rough Replication of JavaScript's Promise.All using AnyPublisher in Swift's Combine.
import Combine
import Foundation
/// AnyPublisher.All
///
/// Implementation of `AnyPublisher.all`. Takes an array of `AnyPublisher`
/// objects which must all conform to the specified `Output` and `Error` types.
///
/// func foo() -> AnyPublisher<Int, Never> {
/// return Deferred {
@bradley
bradley / getGatsbyImageProps.tsx
Created October 27, 2020 03:27
Hack of gatsby-source-sanity's getGatsbyImageProps.ts to support more Sanity image options (https://www.sanity.io/docs/image-urls). Use at your own risk.
import parseUrl from 'url-parse'
/* What's with this file?
*
* This file is a hacked version of the `getGatsbyImageProps.ts` file provided
* by `gatsby-source-sanity` at:
* https://github.com/sanity-io/gatsby-source-sanity/blob/main/src/images/getGatsbyImageProps.ts.
*
* There are existing lacks in functionality at the time of this code being
* written that are having detrimental impacts on our ability to get acceptable
@bradley
bradley / remoteStorageService.js
Created September 10, 2020 22:10
Service for persisting data to S3
import AWS from "aws-sdk";
import config from "config";
import uuidV4 from "uuid/v4";
import querystring from "querystring";
class RemoteStorageService {
constructor() {
this._apiVersion = "2011-06-15";
this._s3 = new AWS.S3({
@bradley
bradley / IconButton.js
Last active April 19, 2019 17:13
Class based styled-components with styled-system
/*
StyledButton
Example:
import { Icon } from "UI/Icon";
import { IconButton } from "UI/Button";
const MyIconButton = () => (
<IconButton primary type="success">
@bradley
bradley / user_media_stream_recording.js
Created November 16, 2018 22:40
Useful for building recording from a user media stream.
class StreamRecording {
constructor(stream, mimeTypes = StreamRecording.DefaultMimeTypes) {
this._stream = null;
this._mimeType = null;
this._recordedBlobs = [];
this._buildMediaRecorder(stream, mimeTypes);
}
get stream() {
@bradley
bradley / user_media_stream_manager.js
Created November 16, 2018 22:39
Useful for creating user media streams and tearing them back down.
// StreamManager
// Useful for creating user media streams and tearing them back down.
//
class StreamManager {
constructor() {
this._stream = null;
this._constraints = null;
}
get stream() {
@bradley
bradley / user_media_detector.js
Created November 16, 2018 20:18
Detect User Media Devices and Permission (Async/Await)
class UserMediaDetector {
// Returns info on all user media devices.
async devices() {
if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
throw(Error("UserMediaDetector getDevices failed: enumerateDevices is not supported"));
}
const mediaDevices = await navigator.mediaDevices.enumerateDevices();
@bradley
bradley / user_media_detector.js
Created November 16, 2018 05:49
Detect User Media Devices and Permission
class UserMediaDetector {
// Returns info on all user media devices in successful promise resolve.
getDevices() {
return new Promise((resolve, reject) => {
if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
reject(Error("UserMediaDetector getDevices failed: enumerateDevices is not supported"));
return;
}
@bradley
bradley / rails_production_csv_export.rb
Created October 3, 2018 16:39
Export a CSV on a production Rails Server
# When ran in the rails console, this code will create and place a
# new csv file in the tmp directory of your rails app. Once done,
# simply exit the rails console and cd into the tmp directory, cat
# the file, and copy its content to a local file. There are probably
# better ways to do this but this does work. Be sure to clean up
# after yourself by rm'ing the file when youre done with it.
require 'csv'
zoo = Zoo.find(id)