Created
February 7, 2021 03:56
-
-
Save azamsharp/064aad904f42845761035eb20b8557ec to your computer and use it in GitHub Desktop.
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
// | |
// MainScreen.swift | |
// IntroductionToSwiftUI-Workshop | |
// | |
// Created by Mohammad Azam on 2/6/21. | |
// | |
import SwiftUI | |
class Settings: ObservableObject { | |
@Published var isOn: Bool = false | |
} | |
struct MainScreen: View { | |
var body: some View { | |
VStack { | |
Text("Main Screen") | |
LightView() | |
LightSwitchView() | |
}.frame(maxWidth: .infinity, maxHeight: .infinity) | |
} | |
} | |
struct MainScreen_Previews: PreviewProvider { | |
static var previews: some View { | |
MainScreen().environmentObject(Settings()) | |
} | |
} | |
struct LightView: View { | |
@EnvironmentObject var settings: Settings | |
var body: some View { | |
VStack { | |
Group { | |
if settings.isOn { | |
Image(systemName: "lightbulb.fill") | |
.font(.largeTitle) | |
.foregroundColor(.yellow) | |
} else { | |
Image(systemName: "lightbulb") | |
.font(.largeTitle) | |
} | |
} | |
}.frame(maxWidth: .infinity, maxHeight: .infinity) | |
.background(Color.blue) | |
} | |
} | |
struct LightSwitchView: View { | |
@EnvironmentObject var settings: Settings | |
var body: some View { | |
VStack { | |
Toggle(isOn: $settings.isOn, label: { | |
EmptyView() | |
}).fixedSize() | |
}.frame(maxWidth: .infinity, maxHeight: .infinity) | |
.background(Color.orange) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment