Skip to content

Instantly share code, notes, and snippets.

View achernoprudov's full-sized avatar
🏠
Working from home

Andrei Chernoprudov achernoprudov

🏠
Working from home
View GitHub Profile
@achernoprudov
achernoprudov / Analytics.swift
Created August 21, 2020 05:12
Analytics system contract
public protocol Analytics {
typealias Attributes = [String: Any]
func log(
event eventName: String,
with attributes: Attributes?
)
}
public extension Analytics {
func log(_ event: AnalyticsEvent) {
@achernoprudov
achernoprudov / UINavigationItem+SetTitle.swift
Created October 28, 2020 05:38
UINavigationItem with subtitle
extension UINavigationItem {
func setTitle(title: String, subtitle: String) {
let one = UILabel()
one.text = title
one.font = UIFont.systemFont(ofSize: 17)
one.sizeToFit()
let two = UILabel()
two.text = subtitle
@achernoprudov
achernoprudov / Action.swift
Last active November 24, 2020 05:06
Redux implementation with Combine framework
/// This is peruly marker protocol,
/// it allows us to keep action list open, and add more actions
/// without breaking existed components.
///
/// Also sometimes it is nice to implement `Codable` or `Equatable`
public protocol Action {}
/// Utility protocol for cloning objects.
public protocol Clonable {
}
public extension Clonable {
/// Clones object with modifictation:
///
/// Example:
/// ```
@achernoprudov
achernoprudov / UnfairLock.swift
Created March 12, 2021 06:31
Unfair lock swift wrapper
/// Low-level lock that allows waiters to block efficiently on contention.
///
/// Based on `os_unfair_lock_s`
public class UnfairLock {
// MARK: - Instance variables
private var unfairLock = os_unfair_lock_s()
// MARK: - Public
@achernoprudov
achernoprudov / CancellableFuture.swift
Created April 8, 2021 16:15
CancellableFuture for Combine framework
import Combine
/// Builds `Publisher` with callable promise that can be cancelled.
/// Used in requests and other cancellable tasks.
///
/// Usage:
/// ```
/// CancellableFuture<String, Error> { promise -> AnyCancellable in
/// let request = buildHttpRequest()
/// request.listenToResponse { result in
@achernoprudov
achernoprudov / Container.swift
Created April 30, 2021 10:19
Feed Collection Container Example
// MARK: - Data
protocol FeedItem {
}
struct ShareFeedItem: FeedItem {
let id: String
}
// MARK: - Delegates
extension AnalyticsEvent {
static func searchForUser(with query: String) -> AnalyticsEvent {
return AnalyticsEvent(name: "Search for user", attributes: [.query: query])
}
}
@achernoprudov
achernoprudov / uimenu-build-old.swift
Created July 27, 2021 10:07
Building UIMenu in an old way
var actions: [UIAction] = []
actions.append(UIAction(title: "Open", image: UIImage(systemName: "xmark.octagon")) { _ in
...
})
// Sub menu
var subMenuActions: [UIAction] = []
if item.isHidden {
subMenuActions.append(UIAction(title: "Show", image: UIImage(systemName: "eye")) { _ in
...
})
@achernoprudov
achernoprudov / uimenu-build-array.swift
Last active July 27, 2021 11:03
Building UIMenu in array DSL way
UIMenu(children: [
UIAction(title: "Open", image: UIImage(systemName: "xmark.octagon")) { _ in
...
},
UIMenu(title: "Edit", children: [
item.isHidden
? UIAction(title: "Show", image: UIImage(systemName: "eye")) { _ in
...
}
: UIAction(title: "Hide", image: UIImage(systemName: "eye.slash")) { _ in