Skip to content

Instantly share code, notes, and snippets.

View danielgarbien's full-sized avatar

Daniel Garbień danielgarbien

  • 13:50 (UTC +01:00)
View GitHub Profile
class TouchGoThroughView: UIView {
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
super.point(inside: point, with: event) && pointInsideVisibleSubviews(point, with: event)
}
}
extension UIView {
func pointInsideVisibleSubviews(_ point: CGPoint, with event: UIEvent?) -> Bool {
@danielgarbien
danielgarbien / SortDescriptor.swift
Created July 14, 2022 14:45
Multi-attribute sorting with sort descriptors
import Foundation
struct SortDescriptor<Value> {
let compare: (Value, Value) -> ComparisonResult
}
extension Collection {
func sorted(by sortDescriptors: [SortDescriptor<Element>]) -> [Element] {
sorted { a, b in
@danielgarbien
danielgarbien / NDJSONParserTests.swift
Last active November 12, 2021 17:56
NDJSON parser
import Foundation
import XCTest
class NDJSONParserTests: XCTestCase {
let ndJson = "{ \"key\": \"value\" }\n"
func testSimpleNDJSON() {
let twoLines = ndJson + ndJson
XCTAssert(parseDataFromString(twoLines).count == 2)
extension KeyedDecodingContainer {
func decode<T: NSCoding>(_ type: T.Type, forKey key: KeyedDecodingContainer<K>.Key) throws -> T? {
let data = try decode(Data.self, forKey: key)
return try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? T ?? nil
}
}
extension KeyedEncodingContainer {
extension String {
func draw(fontSize: CGFloat) -> UIImage? {
draw(font: .systemFont(ofSize: fontSize))
}
func draw(font: UIFont) -> UIImage? {
draw(attributes: [.font: font], backgroundColor: .clear)
}
@objcMembers
private class BlockHandler: NSObject {
var block: (() -> Void)?
@objc func runBlock() {
block?()
}
}
@danielgarbien
danielgarbien / SessionDelegate.swift
Last active March 14, 2018 06:35
Simple network synchronizer with caching
import Foundation
/**
The only benefit SessionDelegate provides is to accompany URL respones with cache control headers which are not provided by the backend at the time of writing.
(Cache control headers in responses let us use Foundation's NSURLCache abilities.)
Once the backend implementation is changed to provide responses with cache control headers SessionDelegate should no longer be used.
NOT THREAD SAFE - it should be used on the delegation queue of NSURLSession
*/
import UIKit
class FullContentIntrinsicSizeTableView: UITableView {
override var contentSize: CGSize {
didSet {
if contentSize != oldValue {
invalidateIntrinsicContentSize()
}
}
import UIKit
extension UIAlertController {
func setLeftAlignedMessage(_ message: String) {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .left
let messageText = NSMutableAttributedString(
string: message,
import UIKit
extension UITableView {
func dequeueReusableCell(withIdentifier identifier: String, registerNibIfNeededWithNibName nibName: String) -> UITableViewCell? {
guard let cell = dequeueReusableCell(withIdentifier: identifier) else {
register(UINib(nibName: nibName, bundle: nil), forCellReuseIdentifier: identifier)
return dequeueReusableCell(withIdentifier: identifier)
}