Skip to content

Instantly share code, notes, and snippets.

View nalexn's full-sized avatar
😈
Designing software the ruthless way

Alexey Naumov nalexn

😈
Designing software the ruthless way
View GitHub Profile
//
// Deduplicated.swift
// CountriesSwiftUI
//
// Created by Alexey Naumov on 17.12.2019.
// Copyright © 2019 Alexey Naumov. All rights reserved.
//
import Foundation
import Combine
@nalexn
nalexn / CancelBag.swift
Last active November 1, 2023 06:43
Collecting AnyCancellable tokens in declarative SwiftUI fashion
// Copyright © 2019 Alexey Naumov. MIT License
import Combine
typealias CancelBag = Set<AnyCancellable>
extension CancelBag {
mutating func collect(@Builder _ cancellables: () -> [AnyCancellable]) {
formUnion(cancellables())
}
struct NavigationCoordinator: EnvironmentKey {
let selectedTab = PassthroughSubject<ContentView.Tab, Never>()
let showActionSheet = PassthroughSubject<Bool, Never>()
}
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
lazy var navigation = NavigationCoordinator()
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
@nalexn
nalexn / BinaryEquatable.swift
Created April 6, 2020 08:17
Equatable without the need to implement == func
protocol BinaryEquatable: Equatable { }
extension BinaryEquatable {
static func == (lhs: Self, rhs: Self) -> Bool {
withUnsafeBytes(of: lhs) { lhsBytes -> Bool in
withUnsafeBytes(of: rhs) { rhsBytes -> Bool in
lhsBytes.elementsEqual(rhsBytes)
}
}
}
@nalexn
nalexn / rsync_backup.plist
Created May 17, 2020 21:56
Setting up rsync with LaunchAgent
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.rsync.backup</string>
<key>ProgramArguments</key>
<array>
<string>open</string>
class AppState: ObservableObject {
@Published var value: Int = 0
}
struct CustomView: View {
@EnvironmentObject var appState: AppState
var body: some View {
Text("Value: \(appState.value)")
}
class HomeViewModel: ObservableObject {
@Published var isLoadingData = false
func doSomething() { ... }
}
class HomeViewController: UIViewController {
let loadingIndicator: UIActivityIndicatorView!
let viewModel = HomeViewModel()
viewModel.$userName // Publisher
.asObservable() // Observable
.bind(to: nameLabel.rx.text) // RxCocoa binding
.disposed(by: disposeBag)
viewModel.$userName
.sink { [weak self] name in
self?.nameLabel.text = name
}
.store(in: &cancelBag)
import RxCombine
import RxCocoa
extension Driver {
var publisher: AnyPublisher<Element, Never> {
return self.asObservable()
.publisher
.catch { _ in Empty<Element, Never>() }
.eraseToAnyPublisher()
}