Skip to content

Instantly share code, notes, and snippets.

View strzempa's full-sized avatar

Patryk Strzemiecki strzempa

View GitHub Profile
@strzempa
strzempa / UICollectionView+animationOnCellInsertion
Created February 5, 2021 18:05
UICollectionView performs batch update with custom floating animation of the new cell
import UIKit
private func makeRandomData() -> [MyModel] { [ MyModel(color: .random) ] }
private final class MyUICollectionViewFlowLayout: UICollectionViewFlowLayout {
var insertingIndexPaths = [IndexPath]()
override func prepare(
forCollectionViewUpdates updateItems: [UICollectionViewUpdateItem]
) {
@strzempa
strzempa / HexUidConverter.swift
Created August 3, 2020 14:21
converting string to hex and back
import Foundation
enum HexUidConverter {
static func hexString(from uid: String) -> String {
Data(uid.utf8).hexEncodedString()
}
static func uid(from hexString: String) -> String? {
guard let data = Data(hexString: hexString) else {
return nil
@strzempa
strzempa / LabelWithActivityIndicator.swift
Created November 23, 2019 10:23
while text is nil or empty display activity indicator on uilabel
import UIKit
class LabelWithActivityIndicator: UILabel {
private var activityIndicator: UIActivityIndicatorView = {
let indicator = UIActivityIndicatorView(style: .gray)
indicator.startAnimating()
indicator.hidesWhenStopped = true
return indicator
}()
@strzempa
strzempa / gist:7d39a6cbdcb081fb846b8afc7c2de769
Created November 14, 2019 11:42
xcode 11 scheme build pre-action to bump version
if [ "${CONFIGURATION}" = "Release" ]; then
buildNumber=$(/usr/libexec/PlistBuddy -c 'print :CFBundleVersion' "${PROJECT_DIR}/${INFOPLIST_FILE}")
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $(expr $buildNumber + 1)" "${PROJECT_DIR}/${INFOPLIST_FILE}"
fi
disabled_rules: # rule identifiers to exclude from running
- trailing_whitespace
- function_parameter_count
- line_length
- variable_name
- cyclomatic_complexity
- nesting
- conditional_binding_cascade
- force_cast
@strzempa
strzempa / FileService.swift
Last active October 24, 2019 14:20
Purpose of this service is synchronizing an urlSession delegate to have result accessible in single method where we pass a remote url and retreive a local url in closure.
import Foundation
protocol FileService {
var observation: Any? { get set }
var session: URLSession { get set }
var fileManager: FileManager { get set }
func fetchAndSaveLocally(url: URL, _ completion: @escaping (_ result: Result<URL, ServiceError>) -> Void)
}
final class DefaultFileService: NSObject, FileService {
import UIKit
final class CheckBoxSingleselectButton: UIExpandedTouchAreaButton {
private var shapeLayerRect: CGRect {
let width = bounds.width / 1.4
let height = bounds.height / 1.4
return CGRect(x: (bounds.width - width) / 2,
y: (bounds.height - height) / 2,
width: width,
height: height)
import UIKit
class CircleProgressView: UIView {
var value: CGFloat = 0 {
didSet {
DispatchQueue.main.async { [weak self] in
self?.animate()
}
}
}
import UIKit
@IBDesignable
class UIExpandedTouchAreaButton: UIButton {
@IBInspectable var margin: CGFloat = 40.0
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
let area = self.bounds.insetBy(dx: -margin, dy: -margin)
return area.contains(point)
}
extension UserDefaults {
@objc dynamic var yourKey: Bool {
return bool(forKey: "yourKey")
}
}
protocol UserDefaultsObservable: AnyObject {
var observation: Any? { get set }
typealias UserDefaultsCompletionHandler = (_ defaults: UserDefaults) -> Void
func addObservation(completion: UserDefaultsCompletionHandler?)