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
// Main target
class EnvObj: ObservableObject {
@Published var str: String
init(str: String) {
_str = .init(initialValue: str)
}
}
@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>
@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)
}
}
}
struct UIKitTestView: UIViewRepresentable {
typealias UpdateContext = UIViewRepresentableContext<UIKitTestView>
@Binding var flag: Bool
internal var didUpdate: () -> Void
func makeUIView(context: UpdateContext) -> UIView {
return UIView()
}
struct DummyListRepository: ListRepository {
func loadList(completion: @escaping ([Item], Error?) -> Void) {
DispatchQueue.main.async {
let list = [
Item(id: "1", name: "First item"),
Item(id: "2", name: "Second item")
]
completion(list, nil)
}
}
struct RealListRepository: ListRepository {
func loadList(completion: @escaping ([Item], Error?) -> Void) {
// networking code
}
}
protocol ListRepository {
func loadList(completion: @escaping ([Item], Error?) -> Void)
}
class ListViewController: UIViewController {
var items: [Item] = []
var tableView: UITableView?
override func viewDidLoad() {
let url = URL(string: "https://api.service.com/list")!
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { [weak self] (data, response, error) in
if let list = try? JSONDecoder().decode([Item].self, from: data ?? Data()) {
extension Binding {
func dispatched(to state: CurrentValueSubject<AppState, Never>,
_ keyPath: WritableKeyPath<AppState, Value>) -> Self {
return .init(get: { () -> Value in
self.wrappedValue
}, set: { newValue in
self.wrappedValue = newValue
state.value[keyPath: keyPath] = newValue
})
}
struct AppState {
var value1: Int = 0
var value2: Int = 0
var value3: Int = 0
}
extension AppState {
struct Injection: EnvironmentKey {