Skip to content

Instantly share code, notes, and snippets.

View maximkrouk's full-sized avatar
🇺🇦

Maxim Krouk maximkrouk

🇺🇦
View GitHub Profile
@maximkrouk
maximkrouk / install-swift-5.1.3-RELEASE-ubuntu16.04
Last active January 3, 2020 10:14
Install swift 5.1.3 on ubuntu
echo 'Preparations'
cd ~
mkdir SwiftSetup
cd ~/SwiftSetup
mkdir Backup
echo 'cd to ~/SwiftSetup'
echo 'Fetching dependencies: clang, libicu-dev libssl-dev openssl'
sudo apt-get install clang libicu-dev libssl-dev openssl
import UIKit
func valueForTheme<Value>(light: Value, dark: Value) -> Value {
return UITraitCollection.current.userInterfaceStyle == .dark ? dark : light
}
import SwiftUI
extension Color {
public init(light: Color, dark: Color) {
self = valueForTheme(light: light, dark: dark)
}
}
@maximkrouk
maximkrouk / AppleIDButton.swift
Last active May 30, 2020 17:02
SwiftUI AppleIDButton
import SwiftUI
import AuthenticationServices
public struct AppleIDButton: View {
public typealias ButtonType = ASAuthorizationAppleIDButton.ButtonType
public typealias Style = ASAuthorizationAppleIDButton.Style
public var type: ButtonType = .default
public var style: () -> Style = Style.userInterface
import Fluent
private struct _Migration<T>: Migration {
var preparation: (Database) -> EventLoopFuture<Void>
var revertion: (Database) -> EventLoopFuture<Void>
func prepare(on database: Database) -> EventLoopFuture<Void> { preparation(database) }
func revert(on database: Database) -> EventLoopFuture<Void> { revertion(database) }
}
public protocol MigrationProvider {
@maximkrouk
maximkrouk / OrderedSet.swift
Last active May 30, 2020 17:03
Ordered collection of unique elements
import Foundation
extension Sequence where Iterator.Element: Hashable {
public func unique() -> OrderedSet<Element> { .init(self) }
}
public struct OrderedSet<Element: Hashable>: BidirectionalCollection, Equatable, ExpressibleByArrayLiteral {
public typealias Index = Int
private var set: Set<Element> = []
import Combine
typealias Reducer<State, Action> = (inout State, Action) -> Void
final class Store<State, Action>: ObservableObject {
typealias Effect = AnyPublisher<Action, Never>
@Published private(set) var state: State
private let lock: NSLock = .init()
@maximkrouk
maximkrouk / Component.swift
Last active October 23, 2024 09:38
SwiftUI adapter for AppKit/UIKit views/view controllers
import SwiftUI
public struct Component<Target, Coordinator, Body: View>: View {
private var makeBody: () -> Body
public var body: Body { makeBody() }
}
#if os(macOS)
public struct _ViewBody<Target: NSView, Coordinator>: NSViewRepresentable {
internal var _makeTarget: (Context) -> Target
public class CancellablesManager {
private let lock = NSLock()
public typealias Storage = Set<AnyCancellable>
private var cancellables: Storage = []
func store(_ element: AnyCancellable?, _ precondition: Bool = true) {
lock.execute {
guard precondition, let element = element else { return }
cancellables.insert(element)
@maximkrouk
maximkrouk / Scheduled.swift
Last active February 19, 2022 08:23
Scheduled task wrapper
import Foundation
extension Bundle {
var id: String { bundleIdentifier ?? "" }
}
@propertyWrapper
public class Scheduled {
private static let defaultQueue = DispatchQueue(
label: Bundle.main.id.appending(".queues.scheduled"),