Created
April 15, 2024 18:30
-
-
Save christianselig/9fddd8ba0059f8b069fa46ca7dabb58f 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
import SwiftUI | |
struct ContentView: View { | |
var body: some View { | |
IceCreamView(viewModel: ViewModel(iceCream: "banana")) | |
} | |
} | |
struct IceCreamView: View { | |
@State var viewModel: ViewModel | |
var body: some View { | |
VStack { | |
Text("Flavor: \(viewModel.iceCream)") | |
// This random number changes every 1 second | |
// and ideally it wouldn't | |
Text("Enjoy your ice cream! \(arc4random_uniform(9999999))") | |
} | |
.overlay { | |
// Option 1: ❌ | |
if let secondsSinceCreation = viewModel.secondsSinceCreation { | |
// Still will cause updates as view model is still rebuilding the body | |
IceCreamOverlayView(secondsSinceCreation: secondsSinceCreation) | |
} | |
// Option 2: ✅ | |
MaybeExistsIceCreamOverlayView(viewModel: viewModel) | |
} | |
} | |
} | |
// ❌ This would not be enough, as it would be recreated | |
// each time secondsSinceCreation changes | |
struct IceCreamOverlayView: View { | |
let secondsSinceCreation: Int | |
var body: some View { | |
Text("Ordered \(secondsSinceCreation) second(s) ago") | |
.padding(.top, 100) | |
} | |
} | |
// ✅ This WOULD be enough, but feels weird. What | |
// do you even call it? | |
struct MaybeExistsIceCreamOverlayView: View { | |
let viewModel: ViewModel | |
var body: some View { | |
if let secondsSinceCreation = viewModel.secondsSinceCreation { | |
// Still will cause updates as view model is still rebuilding the body | |
IceCreamOverlayView(secondsSinceCreation: secondsSinceCreation) | |
} | |
} | |
} | |
@Observable class ViewModel { | |
let iceCream: String | |
var secondsSinceCreation: Int? | |
init(iceCream: String) { | |
self.iceCream = iceCream | |
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in | |
if let secondsSinceCreation = self.secondsSinceCreation { | |
self.secondsSinceCreation = secondsSinceCreation + 1 | |
} else { | |
self.secondsSinceCreation = 1 | |
} | |
} | |
} | |
} |
@aerickson14 This would still result in the number updating, I believe
The seconds ago number will updated but the random number in IceCreamFlavorView
will remain the same. I just double checked and still seeing that behaviour.
@aerickson14 Interesting, okay thanks! Will have to dig a bit deeper and try to understand
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's my attempt to refactor in a way that I would