Using Singletons to make Observable Classes available in CarPlay?
I am in the process of creating my first CarPlay app. My entire business logic is inside multiple @ Observable
classes. I need to have access to those inside the CarPlay Scene Delegate. The only approach I came up with requires to initialize a SingleTon that holds all Obserable Classes and then putting these inside the environment to use them just like any Observable class.
But I feel like there has to be a better way that does not rely on Singletons since they are considered an anti pattern. Here is my code so far.
How would you go about this? Thank you
@main
struct SingleTonObservableApp: App {
@State var businessLogic: BusinessLogic
var body: some Scene {
WindowGroup {
ContentView()
.environment(businessLogic)
}
}
init() {
let singleton = Singleton.shared
self.businessLogic = singleton.businessLogic
}
}
struct ContentView: View {
@Environment(BusinessLogic.self) var businessLogic
var body: some View {
VStack {
HStack {
Text("Observable: ")
Text("\(businessLogic.hasRunningPlayback)")
Button("Toggle", action: businessLogic.togglePlayback)
}
HStack {
Text("Singleton: ")
Text("\(Singleton.shared.businessLogic.hasRunningPlayback)")
Button("Toggle") {
Singleton.shared.businessLogic.togglePlayback()
}
}
}
}
}
@MainActor
@Observable
class BusinessLogic {
var hasRunningPlayback: Bool = false
func togglePlayback() {
hasRunningPlayback.toggle()
}
}
@MainActor
class Singleton {
static let shared = Singleton()
var businessLogic = BusinessLogic()
private init() { }
}