Skip to content

Instantly share code, notes, and snippets.

View mkj-is's full-sized avatar
📱
Building iOS apps

Matěj Kašpar Jirásek mkj-is

📱
Building iOS apps
View GitHub Profile
@mkj-is
mkj-is / Discogs-scriptable-widget.js
Created October 8, 2020 18:32
Discogs collection widget
// This widgets show approximate value of your music collection on discogs.com
// and the last addition to the collection in the background.
// Configuration:
// Your Discogs personal token generated here: https://www.discogs.com/settings/developers
let token = "..."
// Your Discogs username
let username = "..."
// Widget:
@mkj-is
mkj-is / ResultCodes.swift
Created April 7, 2019 08:53
Sqlite result codes enum for Swift
/// https://sqlite.org/rescode.html
enum ResultCode: Int {
/// The SQLITE_OK result code means that the operation was successful and that there were no errors. Most other result codes indicate an error.
case ok = 0
/// The SQLITE_ERROR result code is a generic error code that is used when no other more specific error code is available.
case error = 1
/// The SQLITE_INTERNAL result code indicates an internal malfunction. In a working version of SQLite, an application should never see this result code. If application does encounter this result code, it shows that there is a bug in the database engine.
///
@mkj-is
mkj-is / LiftOperator.swift
Last active March 28, 2019 16:21
Lift/const operator in Swift
/// Lift operator (instance to function, function to function with one more parameter, key-path to function)
/// This operator is similar to `const` function from Haskell.
prefix operator ^
/// Lift instance to function returning constant result.
@inline(__always)
prefix func ^ <T>(instance: @autoclosure @escaping () -> T) -> () -> T {
return instance
}
@mkj-is
mkj-is / gpxs-to-geojson.sh
Last active June 5, 2020 14:53
Combine GPX files to one GeoJSON
#!/bin/bash
# Decompress all files (only some are compressed, primarily the newer ones)
for filename in *.gz
do
gzip -d $filename
done
# Convert all from GPX to GeoJSON
for filename in *.gpx
do
togeojson $filename > geojsons/$filename.geojson
@mkj-is
mkj-is / XCTestCase+ScreenshotAssert.swift
Last active September 16, 2023 04:08
Swift to xcpretty screenshot saving
import XCTest
extension XCTestCase {
func assertFailScreenshot(_ closure: @autoclosure () -> Bool, message: String? = nil, file: String = #file, line: UInt = #line, function: String = #function) {
if !closure() {
takeScreenshot(description: message, file: file, line: line, function: function)
if let message = message {
XCTFail(message)
} else {
XCTFail()
@mkj-is
mkj-is / UIView+ignoresInvertColors.swift
Created February 1, 2018 15:29
Dark mode inspectable
extension UIView {
@IBInspectable var ignoresInvertColors: Bool {
get {
if #available(iOS 11.0, *) {
return accessibilityIgnoresInvertColors
}
return false
}
set {
if #available(iOS 11.0, *) {
@mkj-is
mkj-is / DefaultsStore.swift
Created November 15, 2017 15:35
Wrapper for UserDefaults with Codable in Swift 4
import Foundation
final class DefaultsStore<Value: Codable> {
private let defaults: UserDefaults
private let encoder: PropertyListEncoder
private let decoder: PropertyListDecoder
@mkj-is
mkj-is / ImageClassificationService.swift
Created August 7, 2017 15:16
Example use of image classification using Vision framework
import Foundation
import FuntastyKit
import PromiseKit
import Photos
import Vision
import QuartzCore
protocol ImageClassificationService: Service {
func classify(asset: PHAsset) -> Promise<String>
@mkj-is
mkj-is / HueColorAnimation.swift
Last active January 24, 2017 12:10
Hue color animation
// We will offset the starting hue of every letter a bit
let startHue = CGFloat(offset) / CGFloat(layers.count)
// And also we offset the end hue a bit
// (We need to check if the hue is not larger than 1, then we use the remainder after dividing by 1 as a correct hue value)
let endHue = (CGFloat(offset + 1) / CGFloat(layers.count + 1)).truncatingRemainder(dividingBy: 1)
// And we create the colors from the hue values
let startColor = UIColor(hue: startHue, saturation: 1, brightness: 1, alpha: 1)
let endColor = UIColor(hue: endHue, saturation: 1, brightness: 1, alpha: 1)
// Finally, we create the animation similarly to the stroke end animation
@mkj-is
mkj-is / LayerAnimations.swift
Last active March 3, 2019 10:22
Stroke animations on shape layers
// Set duration for single animation
let duration: TimeInterval = 2
// Enumerate all the layers
layers.enumerated().forEach { offset, layer in
// Create animation for each layer
let animation = CABasicAnimation(keyPath: "strokeEnd")
// Begin each animation after the one before
animation.beginTime = duration * CFTimeInterval(offset) + delay
animation.toValue = 1
animation.duration = duration