Skip to content

Instantly share code, notes, and snippets.

View macu's full-sized avatar

Matt Cudmore macu

  • Halifax, NS
View GitHub Profile
@macu
macu / adjust-insets-for-keyboard.swift
Last active September 9, 2015 00:24
WIP and has a bug
// ...
override func viewDidLoad()
{
super.viewDidLoad()
// Thanks http://stackoverflow.com/a/27665207
NSNotificationCenter.defaultCenter().addObserverForName(
UIKeyboardWillChangeFrameNotification,
object: nil,
@macu
macu / UIViewController+Modal.swift
Created September 2, 2015 13:19
Useful modal UIViewController extensions.
import Foundation
import UIKit
extension UIViewController {
/// Whether the view is currently displayed in modal style.
var isModal: Bool {
return self.presentingViewController?.presentedViewController == self
|| self.navigationController?.presentingViewController?.presentedViewController == self.navigationController
|| self.tabBarController?.presentingViewController?.isKindOfClass(UITabBarController) == true
@macu
macu / minesweeper.go
Created September 6, 2015 02:28
Quick and dirty console minesweeper game
package main
import (
"fmt"
"time"
)
import "math/rand"
func main() {
interactiveGame()
@macu
macu / blob-save-demo.html
Last active March 30, 2017 08:11
Demo saving a blob from a webpage
<!doctype html>
<html>
<body>
<textarea rows=4 cols=40></textarea>
<br/>
<button onclick="saveTextarea()">Save text</button>
<br/>
<br/>
<button onclick="save256Bytes()">Save example typed array as binary file</button>
@macu
macu / blob-load-demo.html
Created September 7, 2015 23:28
Demo loading a blob in a webpage
<!doctype html>
<html>
<body>
<style>
.drop-area {
display: inline-block;
padding: 1em;
border: thin dotted grey;
border-radius: 1em;
@macu
macu / MainActivity.java
Last active September 9, 2015 19:45
Android connect to SPP scanner and read scan data. Socket CHS appears to use proprietary data format; use the official Socket SDK. This code can read scan results from Unitech scanners, but data may be split across multiple reads until a newline. Requires BLUETOOTH permission.
package io.macu.sppreader;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
@macu
macu / copy-database-to-vm.sh
Created October 13, 2016 01:09
Copy database from actual device to VM for testing
# Ensure all AVDs are closed, and your Android device is connected.
# To make a backup of the ca.mattcudmore.day2day app:
~/Library/Android/sdk/platform-tools/adb backup -f backup.ab ca.mattcudmore.day2day
# Convert the backup file to TAR:
dd if=backup.ab bs=1 skip=24 | python -c "import zlib,sys;sys.stdout.write(zlib.decompress(sys.stdin.read()))" > backup.tar
# Extract the TAR to access files.
tar -xvf backup.tar
# Now disconnect Android device and launch AVD where files will be copied to.
@macu
macu / toggle-demo.html
Created December 8, 2016 15:36
Saves toggle state in localStorage and restores on next page load. Note this does not allow some plugins to calculate positions and dimensions of elements in the hidden area.
<a data-toggle=".demo-layout .demo-target" href="#">[-]</a>
<div class="demo-layout">
<div class="demo-target">
<p>Content hidden or displayed</p>
</div>
</div>
@macu
macu / jQuery-POST-handle-errors.js
Created October 29, 2019 13:53
Detect AJAX response error codes using jQuery
$.post('/ajax/test', {
field,
}).then(response => {
// Success
}).fail((jqXHR) => {
if (jqXHR.status === 409) {
alert('Conflict');
} else {
alert('Error code: ' + jqXHR.status);
}
@macu
macu / extract-video-id-from-embed.txt
Last active March 25, 2020 19:24
Extract video IDs from YouTube and Vimeo embed codes
YouTube embed regex:
/<iframe\s+[^>]*?src="(?:(?:https?:)?\/\/)?(?:(?:www|m)\.)?(?:youtube\.com|youtu.be)(?:\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)[^">]*?"[^>]*>[^<]*<\/iframe>/
Vimeo embed regex:
/<iframe\s+[^>]*?src="(?:(?:https?:)?\/\/)?(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/[^\/]+\/videos\/|album\/\d+\/video\/|video\/|)(\d+)\/?(?:[?]?[^">]*)"[^>]*>[^<]*<\/iframe>/