I have two view modifiers that work identically in my tests, but I'm concerned I'm missing some case where they wouldn't.
Which is better and why: To pass in the ScenePhase from the parent view or to call it directly from the environment?
extension View {
func reportPhaseChange(phase: ScenePhase) -> some View {
modifier(ReportPhaseChange(phase: phase))
}
func reportPhaseChange() -> some View {
modifier(ReportPhaseChange2())
}
}
struct ReportPhaseChange: ViewModifier {
var phase:ScenePhase
func body(content: Content) -> some View {
content.onChange(of: phase) { _, newPhase in
switch newPhase {
case .active:
print("going active")
case .background:
print("going background")
case .inactive:
print("going inactive")
@unknown default:
fatalError()
}
}
}
}
struct ReportPhaseChange2: ViewModifier {
@Environment(\.scenePhase) var phase
func body(content: Content) -> some View {
content.onChange(of: phase) { _, newPhase in
switch newPhase {
case .active:
print("going active")
case .background:
print("going background")
case .inactive:
print("going inactive")
@unknown default:
fatalError()
}
}
}
}