This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// Learn | |
// | |
// Created by Mohammad Azam on 6/29/22. | |
// | |
import SwiftUI | |
class AppState: ObservableObject { | |
@Published var value: Int = 0 | |
@Published var isAuthenticated: Bool = false | |
var counterState = CounterState() | |
var authState = AuthState() | |
} | |
class CounterState: ObservableObject { | |
@Published var value: Int = 0 | |
} | |
class AuthState: ObservableObject { | |
@Published var isAuthenticated: Bool = false | |
} | |
protocol IncrementCounterViewModelProtocol { | |
func increment() | |
} | |
protocol DisplayCounterViewModelProtocol { | |
var counter: Int { get } | |
} | |
class IncrementCounterViewModel: IncrementCounterViewModelProtocol { | |
var appState: AppState | |
init(appState: AppState) { | |
self.appState = appState | |
} | |
func increment() { | |
appState.value += 1 | |
} | |
} | |
class DisplayCounterViewModel: DisplayCounterViewModelProtocol { | |
private var appState: AppState | |
init(appState: AppState) { | |
self.appState = appState | |
} | |
var counter: Int { | |
appState.value | |
} | |
} | |
struct DisplayCounterView: View { | |
let vm: DisplayCounterViewModelProtocol | |
init(vm: any DisplayCounterViewModelProtocol) { | |
self.vm = vm | |
} | |
var body: some View { | |
let _ = Self._printChanges() | |
Text("\(vm.counter)") | |
} | |
} | |
struct IncrementCounterView: View { | |
let vm: IncrementCounterViewModelProtocol | |
init(vm: any IncrementCounterViewModelProtocol) { | |
self.vm = vm | |
} | |
var body: some View { | |
let _ = Self._printChanges() | |
VStack { | |
Button("Increment") { | |
vm.increment() | |
} | |
} | |
} | |
} | |
struct FancyCounterView: View { | |
let counter: Int | |
var body: some View { | |
Text("\(counter)") | |
} | |
} | |
struct FancyView: View { | |
let vm: FancyViewModel | |
var body: some View { | |
FancyCounterView(counter: vm.counter) | |
} | |
} | |
class FancyViewModel { | |
private var appState: AppState | |
init(appState: AppState) { | |
self.appState = appState | |
} | |
var counter: Int { | |
self.appState.value | |
} | |
} | |
struct GrandChildView: View { | |
@EnvironmentObject var counterState: CounterState | |
var body: some View { | |
let _ = Self._printChanges() | |
VStack { | |
Text("GRAND CHILD VIEW") | |
Text("\(counterState.value)") | |
} | |
} | |
} | |
struct ChildView: View { | |
var body: some View { | |
GrandChildView() | |
} | |
} | |
struct ParentView: View { | |
var body: some View { | |
ChildView() | |
} | |
} | |
struct AuthView: View { | |
@EnvironmentObject var authState: AuthState | |
var body: some View { | |
VStack { | |
Text(authState.isAuthenticated ? "AUTHENTICATED": "NOT AUTHENTICATED") | |
Button("Toggle Auth") { | |
authState.isAuthenticated.toggle() | |
} | |
} | |
} | |
} | |
struct ContentView: View { | |
@EnvironmentObject var appState: AppState | |
var body: some View { | |
let _ = Self._printChanges() | |
VStack { | |
DisplayCounterView(vm: DisplayCounterViewModel(appState: appState)) | |
IncrementCounterView(vm: IncrementCounterViewModel(appState: appState)) | |
ParentView() | |
AuthView() | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView().environmentObject(AppState()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LearnApp.swift | |
// Learn | |
// | |
// Created by Mohammad Azam on 6/29/22. | |
// | |
import SwiftUI | |
@main | |
struct LearnApp: App { | |
let appState = AppState() | |
var body: some Scene { | |
WindowGroup { | |
ContentView() | |
.environmentObject(appState) | |
.environmentObject(appState.counterState) | |
.environmentObject(appState.authState) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment