Skip to content

Instantly share code, notes, and snippets.

View KaQuMiQ's full-sized avatar

Kacper Kaliński KaQuMiQ

View GitHub Profile
@KaQuMiQ
KaQuMiQ / CodeData.swift
Last active June 18, 2020 08:00
CodeData - CoreData from code
// MARK: - Storage.swift
import CoreData
public final class Storage {
private let queue: DispatchQueue
private let context: NSManagedObjectContext
private let schema: StorageSchema
@KaQuMiQ
KaQuMiQ / Diagnostics.swift
Created June 9, 2020 13:36
Easy diagnostics for apple platforms
import os
internal enum Diagnostics {
internal static let logger: OSLog = OSLog(subsystem: "com.company", category: "diagnostics")
@inline(__always)
internal static func log(_ type: OSLogType = .debug, _ message: StaticString) {
os_log(type, log: logger, message)
}
@KaQuMiQ
KaQuMiQ / Command.swift
Last active June 5, 2020 10:55
Cockoo
import class Foundation.NSTask.Process
import class Foundation.Pipe
import class Foundation.FileHandle
import struct Foundation.NSData.Data
import class Foundation.NSLock.NSConditionLock
public final class Command {
public var isRunning: Bool { lock.condition == 1 }
public var isCompleted: Bool { lock.condition == 0 }
@KaQuMiQ
KaQuMiQ / TouchPresentingWindow.swift
Last active June 3, 2020 07:16
Visualize touches
import UIKit
public final class TouchCaptureWindow: UIWindow {
fileprivate lazy var presentationWindow: TouchPresentationWindow = { [unowned self] in
if #available(iOS 13.0, *) {
guard let scene = self.windowScene else { return TouchPresentationWindow(frame: self.frame) }
return TouchPresentationWindow(windowScene: scene)
} else {
return TouchPresentationWindow(frame: self.frame)
@KaQuMiQ
KaQuMiQ / CardsView.swift
Last active May 13, 2020 07:55
Cards with pagination
public final class CardsView: UIView {
private enum Constants {
static let margins = UIEdgeInsets(top: 8, left: spacing / 2, bottom: 8, right: spacing / 2)
static let spacing = CGFloat(16)
}
private lazy var scrollView = UIScrollView()
private lazy var stackView = UIStackView()
public var previousPageHandler: (() -> Array<CardViewModel>)? {
@KaQuMiQ
KaQuMiQ / TextView.swift
Created March 19, 2020 18:56
Autoresizing text view
import UIKit
public final class TextView: UITextView {
public override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
layoutManager.delegate = self
isScrollEnabled = false
}
@KaQuMiQ
KaQuMiQ / UITextView+Lines.swift
Created March 19, 2020 18:46
Number of text lines in UITextView
extension NSLayoutManager {
var numberOfLines: Int {
var linesCount: Int = 0
var idx: Int = 0
let lineRange: NSRangePointer = .allocate(capacity: 1)
while idx < numberOfGlyphs {
lineFragmentRect(forGlyphAt: idx, effectiveRange: lineRange)
idx = NSMaxRange(lineRange.pointee)
linesCount += 1
}
@KaQuMiQ
KaQuMiQ / SetOnly.swift
Created March 17, 2020 12:15
Set only property example
struct SomeType {
private var store: Any = 0 // or something else to proxy properties
var value: Any {
@available(*, unavailable)
get { fatalError("unavailable") }
set { store = newValue}
}
}
@KaQuMiQ
KaQuMiQ / Command
Created March 17, 2020 09:58
Swift LLDB pretty print json data
e print(String(data: JSONSerialization.data(withJSONObject: JSONSerialization.jsonObject(with: data, options: []), options: .prettyPrinted), encoding: .utf8)!)
@KaQuMiQ
KaQuMiQ / UIResponder+Current.swift
Created March 17, 2020 07:50
Getting current first responder
import UIKit
extension UIResponder {
private static weak var _currentFirstResponder: UIResponder?
public static var currentFirstResponder: UIResponder? {
dispatchPrecondition(condition: .onQueue(.main))
_currentFirstResponder = nil
UIApplication.shared.sendAction(#selector(UIResponder.findFirstResponder(_:)), to: nil, from: nil, for: nil)