Skip to content

Instantly share code, notes, and snippets.

@benigumocom
Last active January 11, 2024 06:13
Show Gist options
  • Save benigumocom/04b647e182ef9839c057600297b8aea9 to your computer and use it in GitHub Desktop.
Save benigumocom/04b647e182ef9839c057600297b8aea9 to your computer and use it in GitHub Desktop.
import SwiftUI
// 1
//struct Parent: View {
// @State private var isOn = false
//
// var body: some View {
// VStack {
// Rectangle().fill(isOn ? .yellow : .gray)
// Toggle(isOn ? "ON" : "OFF", isOn: $isOn)
// }
// }
//}
//struct Parent: View {
// @State private var isOn = false
//
// var body: some View {
// VStack {
// Rectangle().fill(isOn ? .yellow : .gray)
// Toggle(
// isOn ? "ON" : "OFF",
// isOn: Binding(
// get: { isOn },
// set: { value in isOn = value }
// )
// )
// }
// }
//}
//@Observable
//class StateModel: Identifiable {
// var isOn = false
// var label: String { isOn ? "ON" : "OFF" }
// var color: Color { isOn ? .yellow : .gray }
//}
//
//struct Parent: View {
// private let model = StateModel()
//
// var body: some View {
// VStack {
// Rectangle().fill(model.color)
// Toggle(
// model.label,
// isOn: Binding(
// get: { model.isOn },
// set: { value in model.isOn = value }
// )
// )
// }
// }
//}
// 3
//@Observable
//class StateModel: Identifiable {
// var isOn = false
// var label: String { isOn ? "ON" : "OFF" }
// var color: Color { isOn ? .yellow : .gray }
//}
//
//struct Parent: View {
// @Bindable private var model = StateModel()
// //private var model = StateModel()
//
// var body: some View {
// VStack {
// Rectangle().fill(model.color)
//
// //@Bindable var model = model
// Toggle(model.label, isOn: $model.isOn)
// }
// }
//}
// 2
//struct Parent: View {
// @State private var isOn = false
//
// var body: some View {
// VStack {
// Rectangle().fill(isOn ? .yellow : .gray)
// Child(isOn: $isOn)
// }
// }
//}
//
//struct Child: View {
// @Binding var isOn: Bool
//
// var body: some View {
// Toggle(isOn ? "ON" : "OFF", isOn: $isOn)
// }
//}
//@Observable
//class StateModel: Identifiable {
// var isOn = false
// var label: String { isOn ? "ON" : "OFF" }
// var color: Color { isOn ? .yellow : .gray }
//}
//
//struct Parent: View {
// private let model = StateModel()
//
// var body: some View {
// VStack {
// Rectangle().fill(model.color)
// Child(model: model)
// }
// }
//}
//
//struct Child: View {
// var model: StateModel
//
// var body: some View {
// Toggle(
// model.label,
// isOn: Binding(
// get: { model.isOn },
// set: { value in model.isOn = value }
// )
// )
// }
//}
//@Observable
//class StateModel: Identifiable {
// var isOn = false
// var label: String { isOn ? "ON" : "OFF" }
// var color: Color { isOn ? .yellow : .gray }
//}
//
//struct Parent: View {
// @Bindable private var model = StateModel()
//
// var body: some View {
// VStack {
// Rectangle().fill(model.color)
// Child(model: model)
// }
// }
//}
//
//struct Child: View {
// @Bindable var model: StateModel
//
// var body: some View {
// Toggle(model.label, isOn: $model.isOn)
// }
//}
//@Observable
//class StateModel: Identifiable {
// var isOn = false
// var label: String { isOn ? "ON" : "OFF" }
// var color: Color { isOn ? .yellow : .gray }
//}
//
//struct Parent: View {
// private let model = StateModel()
//
// var body: some View {
// VStack {
// Rectangle().fill(model.color)
// Child(model: model)
// }
// }
//}
//
//struct Child: View {
// @Bindable var model: StateModel
//
// var body: some View {
// Toggle(model.label, isOn: $model.isOn)
// }
//}
// 4
@Observable
class StateModel: Identifiable {
var isOn = false
var label: String { isOn ? "ON" : "OFF" }
var color: Color { isOn ? .yellow : .gray }
}
struct Parent: View {
private let model = StateModel()
var body: some View {
VStack {
Rectangle().fill(model.color)
Child(model: model) // *
}
}
}
struct Child: View {
var model: StateModel
var body: some View {
@Bindable var model = model // *
Toggle(model.label, isOn: $model.isOn)
}
}
//@Observable
//class StateModel: Identifiable {
// var isOn = false
// var label: String { isOn ? "ON" : "OFF" }
// var color: Color { isOn ? .yellow : .gray }
//}
//
//struct Parent: View {
// private var model = StateModel()
//
// var body: some View {
// VStack {
// Rectangle().fill(model.color)
// Child(model: model)
// }
// }
//}
//
//struct Child: View {
// @Bindable var model: StateModel
//
// var body: some View {
// Toggle(model.label, isOn: $model.isOn)
// }
//}
//@Observable
//class StateModel: Identifiable {
// var isOn = false
// var label: String { isOn ? "ON" : "OFF" }
// var color: Color { isOn ? .yellow : .gray }
//}
//
//struct Parent: View {
// @State private var model = StateModel()
//
// var body: some View {
// VStack {
// Rectangle().fill(model.color)
// Child(model: $model)
// }
// }
//}
//
//struct Child: View {
// @Binding var model: StateModel
//
// var body: some View {
// Toggle(model.label, isOn: $model.isOn)
// }
//}
#Preview {
Parent()
.frame(width: 100, height: 100)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment