Skip to content

Instantly share code, notes, and snippets.

@matnogaj
matnogaj / StateMachine.kt
Last active December 26, 2017 21:37
State machine in Kotlin
class StateMachine(private val initialState: State) {
interface Event
interface State
class Edge(private val triggerEvent: Event, private val targetState: State) {
private val actionList = mutableListOf<(Edge) -> Unit>()
fun action(action: (Edge) -> Unit) {
actionList.add(action)
}
@matnogaj
matnogaj / add_ahoc_config.rb
Last active June 23, 2017 10:46
Adds adhoc config to projects found in node_modules
require 'xcodeproj'
require 'find'
require 'yaml'
require 'set'
class XCodeProject
def initialize(path)
unless File.exists?(path)
raise Errno::ENOENT, "#{path} does not exist."
@matnogaj
matnogaj / CustomBarButton.swift
Created June 15, 2017 10:54
Custom view for UIBarButtonItem
public class CustomBarButton: UIControl {
@IBInspectable
public var image: UIImage? {
didSet {
imageView.image = image
}
}
@IBInspectable
public var text: String? {
@matnogaj
matnogaj / gist:73c8afc6d4c330a6c2276f78a43292b4
Created June 12, 2017 08:40
build.gradle fix for different sdk versions
allprojects { project ->
afterEvaluate {
if((project.plugins.hasPlugin('android') || project.plugins.hasPlugin('android-library'))) {
android {
compileSdkVersion myCompileSdkVersion as Integer
buildToolsVersion myBuildToolsVersion
}
}
}
}
@matnogaj
matnogaj / Description.swift
Created March 30, 2017 01:01
Description of Array, Dictionary and NSSet that is human readable
extension Array {
func properDescribe(indent: Int = 0) {
var indentString: String = ""
for _ in 0...indent {
indentString += "\t"
}
for value in self {
switch value {
case let x as NSManagedObject:
@matnogaj
matnogaj / Differences.swift
Created March 20, 2017 22:26
Dictionary differences
import UIKit
import CoreData
var dictA: [String : Any] = ["key1": "value1", "key3": "value3", "key4": "notModified",
"key5" : 1, "key6" : 1.0,
"key7": ["a", "b"], "key8": ["a"], "key9": ["a", "b"]]
var dictB: [String : Any] = ["key2": "value2", "key3": "modified", "key4": "notModified",
"key5" : 2, "key6" : 2.0,
"key7": ["a", "c"], "key8": ["a", "b"], "key9": ["a"]]
@matnogaj
matnogaj / PercentageDistribution.swift
Last active February 7, 2017 11:59
Calculate percentage distribution of values
func calculatePercentageDistribution(ofValues inputValues: [Double]) -> [Double] {
let totalValue = inputValues.reduce(0.0) { $0.0 + $0.1 }
guard totalValue > 0 else {
return Array<Double>(repeating: 0.0, count: inputValues.count)
}
var fractionSum: Double = 0.0
var percentArray = inputValues.map { value -> Double in
let value = (value * 100.0) / totalValue
@matnogaj
matnogaj / FontUtility.swift
Created January 25, 2017 15:35
Register fonts from files. Useful especially for fonts in frameworks.
public class FontsUtility {
/**
Call this at the app startup to register custom fonts and use them in the app.
*/
public static func registerFonts() {
let bundle = Bundle(for: FontsUtility.self)
guard let path = bundle.path(forResource: "Info", ofType: "plist") else {
print("[RegisterFonts] Failed to find Info.plist")
return
}
@matnogaj
matnogaj / chinese.swift
Last active January 17, 2017 09:43
ChineseNewYear
func isChineseNewYear() -> Bool {
let today = Date()
let chineseCalendar = Calendar(identifier: Calendar.Identifier.chinese)
var components = chineseCalendar.dateComponents([.year, .timeZone], from: today)
guard let chineseDate = chineseCalendar.date(from: components) else { return false }
let chineseYear = Calendar.current.component(.year, from: chineseDate)
let currentYear = Calendar.current.component(.year, from: today)
guard let year = components.year else { return false }
components.year = year + (currentYear - chineseYear)
@matnogaj
matnogaj / ShortestMaze.swift
Created December 27, 2016 03:46
Shortest maze
//: Playground - noun: a place where people can play
import UIKit
class Step {
let x: Int
let y: Int
init?(_ x: Int, _ y: Int) {
guard x >= 0 && x < Step.maze.count else { return nil }