Skip to content

Instantly share code, notes, and snippets.

View nalexn's full-sized avatar
😈
Badass software crafting

Alexey Naumov nalexn

😈
Badass software crafting
View GitHub Profile
struct AppState {
var value1: Int = 0
var value2: Int = 0
var value3: Int = 0
}
extension AppState {
struct Injection: EnvironmentKey {
struct ContentView: View {
// The local view's state encapsulated in one container:
@State private var state = ViewState()
// The app's state injection
@Environment(\.injected) private var injected: AppState.Injection
var body: some View {
Text("Value: \(state.value)")
struct CustomView: View {
@EnvironmentObject var appState: Deduplicated<AppState, Snapshot>
var body: some View {
Text("Value: \(appState.value)")
}
}
let view = CustomView()
.environmentObject(appState
struct CustomView: View {
@EnvironmentObject var appState: AppState
var body: some View {
Text("Value: \(appState.value)")
}
}
let view = CustomView()
.environmentObject(appState)
object.objectWillChange
.delay(for: .nanoseconds(1), scheduler: RunLoop.main)
.compactMap { _ in makeSnapshot() }
.prepend(makeSnapshot())
.removeDuplicates()
.dropFirst()
.sink { [weak self] _ in
self?.objectWillChange.send()
}
struct CustomView: View {
@EnvironmentObject var appState: AppState
private var prevValue: Int
var body: some View {
self.prevValue = appState.value
return Text("Value: \(appState.value)")
}
}
class AppState: ObservableObject {
@Published var value: Int = 0
}
struct CustomView: View {
@EnvironmentObject var appState: AppState
var body: some View {
Text("Value: \(appState.value)")
}
//
// Deduplicated.swift
// CountriesSwiftUI
//
// Created by Alexey Naumov on 17.12.2019.
// Copyright © 2019 Alexey Naumov. All rights reserved.
//
import Foundation
import Combine
struct ContentView: View {
@State var selectedAnnotation: MKAnnotationID?
@State var annotations: [MKAnnotation] = []
var body: some View {
ZStack {
MapView(selected: self.$selectedAnnotation, annotations: self.$annotations)
ForEach(self.annotations) { annotation in
NavigationLink(
destination: AnnotationDetailsView(annotation: annotation),
Average FPS ElementView Body Calls Cumulative Body Calculation Time
20 255000 1430 ms