Skip to content

Instantly share code, notes, and snippets.

View peatiscoding's full-sized avatar
🏠
Working from home

peatiscoding peatiscoding

🏠
Working from home
View GitHub Profile
@peatiscoding
peatiscoding / map-reduce-android.java
Last active March 13, 2019 07:24
A thin map reduce utility class for syntax purpose in Android: see http://peatiscoding.me/geek-stuff/map-reduce-android/
// Quick - utility class
public class Quick {
// Map
public interface IMapper<V, T> {
T each(V i);
}
function GCMCourier(apiKey) {
this.apiKey = apiKey;
this.push = function(notification, callback) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState==4) {
if (xhttp.status == 200) {
var output = xhttp.responseText;
callback && callback(true, output);
} else {
@peatiscoding
peatiscoding / batch.js
Created May 19, 2016 16:48
JavaScript looping asynchronous calls such as AJAX.
var BATCH = function(options) {
var args = options.args, // arguments for method without callback
method = options.method, // method to be invoked
done = options.done,
each = options.each;
// validate options
if ( ! args || ! Array.isArray(args)) {
throw 'args is required and must be array';
}
@peatiscoding
peatiscoding / basic.swift
Last active September 25, 2016 15:57
JavaScript setTimeout in Swift 2.0
// Basic.swift
func setTimeout(delay:NSTimeInterval, block:()->Void) -> NSTimer {
return NSTimer.scheduledTimerWithTimeInterval(delay, target: NSBlockOperation(block: block), selector: #selector(NSOperation.main), userInfo: nil, repeats: false)
}
func setInterval(interval:NSTimeInterval, block:()->Void) -> NSTimer {
return NSTimer.scheduledTimerWithTimeInterval(interval, target: NSBlockOperation(block: block), selector: #selector(NSOperation.main), userInfo: nil, repeats: true)
}
@peatiscoding
peatiscoding / greyscaled.swift
Created October 17, 2016 15:04
Creating a Gray scaled image out of UIImage, using Quartz 2DDrawing (CGContext)
extension UIImage {
var greyScaled:UIImage? {
let width = self.size.width
let height = self.size.height
let rect = CGRectMake(0, 0, width, height)
let colorSpace = CGColorSpaceCreateDeviceGray()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.None.rawValue)
let context = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo.rawValue)
CGContextDrawImage(context, rect, self.CGImage!)
@peatiscoding
peatiscoding / UIView+snapshot.swift
Created November 7, 2016 14:16
An easy way to create a UIImage snapshot from given view.
extension UIView {
func snapshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, true, UIScreen.mainScreen().scale)
self.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
}
// Basic.swift
extension Notification.Name {
func post(object:Any? = nil, userInfo:[AnyHashable: Any]? = nil) {
NotificationCenter.default.post(self, object: object, userInfo: userInfo)
}
}
// DataModel.swift
extension Notification.Name {
static let AccountBalanceUpdated = Notification.Name("accountBalanceUpdated")
import AVFoundation
extension AVPlayerItem {
enum TrackType {
case subtitle
case audio
@peatiscoding
peatiscoding / Basic.swift
Created March 17, 2017 03:11
A setTimeout script for Swift3.0
/**
setTimeout()
Shorthand method for create a delayed block to be execute on started Thread.
This method returns ``Timer`` instance, so that user may execute the block
within immediately or keep the reference for further cancelation by calling
``Timer.invalidate()``
//
// PromiseKitHelper.swift
// TrueID
//
// Created by Kittiphat Srilomsak on 3/21/2560 BE.
// Copyright © 2017 peatiscoding.me all rights reserved.
//
import PromiseKit