Skip to content

Instantly share code, notes, and snippets.

View srstanic's full-sized avatar

Srđan Stanić srstanic

View GitHub Profile
typealias AlertActionStyle = UIAlertAction.Style
extension UIAlertController {
static func create(
style: UIAlertController.Style,
title: String?,
message: String?,
actions: [AlertAction] = [],
onDismiss: VoidHandler? = nil
) -> UIAlertController {
import UIKit
extension UIViewController {
/// Creates an instance of `ViewControllerType` defined in a storyboard.
///
/// The setup for this method to work is the following:
/// 1. The storyboard needs to have a single view controller of the type `ViewControllerType`
/// 2. This view controller needs to be set as the initial view controller
/// 3. The name of the view controller needs to match the name of the storyboard and end with "ViewController".
/// e.g. Some.storyboard and SomeViewController
#!/bin/sh
echo $1 | python3 -c "import sys; from urllib.parse import unquote; print(unquote(sys.stdin.read()));"
@srstanic
srstanic / firebase-bd.sh
Created October 22, 2020 10:56
Firebase build and deploy jekyll site to production
JEKYLL_ENV=production bundle exec jekyll build
firebase deploy
extension Optional where Wrapped == String {
var isEmptyOrNil: Bool {
return self?.isEmpty != false
}
var isNotEmptyNorNil: Bool {
return !isEmptyOrNil
}
}
extension Array where Element: Equatable {
func distinctUntilChanged() -> Self {
var resultArray = Self()
for element in self {
if resultArray.last != element {
resultArray.append(element)
}
}
return resultArray
}
@srstanic
srstanic / UIImage+tinting.swift
Created November 16, 2019 16:08
UIImage+tinting.swift
extension UIImage {
func tinted(with color: UIColor) -> UIImage? {
let maskImage = cgImage
let bounds = CGRect(origin: .zero, size: size)
let renderer = UIGraphicsImageRenderer(size: size)
// QuartzCore origin .zero is bottom-left, while UIKit's is top-left.
let tintedButFlippedImage = renderer.image { context in
let cgContext = context.cgContext
cgContext.clip(to: bounds, mask: maskImage!)
extension Optional where Wrapped == String {
var orEmpty: String {
switch self {
case .some(let value):
return value
case .none:
return ""
}
}
}
@srstanic
srstanic / NibView.swift
Last active December 27, 2020 14:46
NibView
import UIKit
/// Extend `NibView` to create a reusable view in a XIB file.
/// The contents of the view are designed in a XIB file.
/// The `NibView` class is responsible for loading the
/// contents of the XIB file and setting it as its subview.
/// File's Owner in the XIB file needs to be set to the
/// view class extending the `NibView`. Any outlets defined
/// in that class need to be connected to the File's Owner.
open class NibView: UIView {
@srstanic
srstanic / Repeater.swift
Created December 2, 2018 11:02
Repeating blocks
import Foundation
protocol Repeating {
func startRepeating(withIntervalInSeconds: TimeInterval)
func stopRepeating()
}