Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created February 7, 2021 03:56
Show Gist options
  • Save azamsharp/064aad904f42845761035eb20b8557ec to your computer and use it in GitHub Desktop.
Save azamsharp/064aad904f42845761035eb20b8557ec to your computer and use it in GitHub Desktop.
//
// 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