Skip to content

Instantly share code, notes, and snippets.

@ashleyng
ashleyng / StartTimer.js
Created June 18, 2019 20:04
IoT Toggl Code
const https = require('https')
module.exports = {
request : function() {
return new Promise(function(resolve, reject) {
var options = {
host:'www.toggl.com',
path:'/api/v8/time_entries/start',
method: 'POST',
headers: {
@ashleyng
ashleyng / GetPlayingSong.js
Last active February 11, 2019 02:05
Spotify of Things: Use IoT button to add songs to Spotify playlists using AWS Lambdas
const https = require('https')
module.exports = {
request : function() {
return new Promise(function(resolve, reject) {
var options = {
host: 'api.spotify.com',
path: '/v1/me/player/currently-playing',
method: 'GET',
headers: {
@ashleyng
ashleyng / NumberFormatter.swift
Created November 26, 2018 15:55
Showcase what Apple's NumberFormatter can do via unit tests
import XCTest
class NumberFormatterTests: XCTestCase {
func testRoundingTypeFloor() {
let formatter = NumberFormatter()
formatter.roundingMode = .floor
let number = NSNumber(value: 8.8)
let expectedRoundingValue = "8"
@ashleyng
ashleyng / UnitTest.swift
Last active November 2, 2018 00:07
Running Unit Tests in a Playground
import XCTest
class NSNumberTests: XCTestCase {
func testNumberReturnIntValue() {
let rawNumberFloat: Float = 8.74
let number = NSNumber(value: rawNumberFloat)
let expectedReturnValue: Int = 8
let actualReturnValue = number.intValue
@ashleyng
ashleyng / CellForRowAt.swift
Created April 8, 2018 03:14
Difference between MVC and MVVM code for `cellForRowAt`
/* ========== MVC ========== */
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "PhotoInfoCell", for: indexPath) as? PhotoInfoCell {
let photoInfo = photos[indexPath.row]
cell.configure(title: photoInfo.title, date: photoInfo.date)
cell.startAnimating()
ImageCache.shared.imageFor(url: photoInfo.photoUrl)
.observeOn(MainScheduler.instance)
.subscribe(onNext: { image in
cell.setImage(image: image)
@ashleyng
ashleyng / FakePushPop.swift
Created December 19, 2017 20:15
Fake pushing/popping view controller animations without a navigationController
// Present/push view controller
@IBAction func nextTapped(_ sender: UIButton) {
let src = self
let dst = NextViewController()
src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
dst.view.transform = CGAffineTransform(translationX: src.view.frame.size.width, y: 0)
UIView.animate(withDuration: 0.25, delay: 0.0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
protocol Service {}
class RealService: Service {}
class DefaultService: Service {}
class NotDependencyInjection {
private let service: Service
init() {
service = RealService()
}
@ashleyng
ashleyng / Billiards.swift
Created July 2, 2017 02:12
Swift Core Graphics in Playground
import UIKit
let BILLIARD_BALL_SIZE = CGSize(width: 100, height: 100)
let BILLIARD_NUMBER_SIZE = CGSize(width: 50, height: 50)
enum ColorMapping: Int {
case zero = 0 // cue ball
case one = 1
case two = 2
case three = 3