Last active
January 18, 2025 22:30
-
-
Save colinfwren/f4554c4c525890012a569a33e4a04558 to your computer and use it in GitHub Desktop.
SwiftUI View using the Service
This file contains hidden or 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 { | |
@EnvironmentObject private var exampleService: ExampleService | |
var body: some View { | |
List { | |
ForEach(exampleSerivce.someList, id: \.self) { listItem in | |
Text(listItem.name) | |
} | |
} | |
.navigationTitle("Some List") | |
.toolbar { | |
Button("Add Something") { | |
exampleService.someComplexMultiModelAction() | |
} | |
} | |
} | |
} | |
#Preview { | |
do { | |
let configuration = ModelConfiguration(isStoredInMemoryOnly: true) | |
let container = try! ModelContainer(for: SomeModel.self, SomeOtherModel.self, configurations: configuration) | |
let someOtherModel = SomeOtherModel(mame: "Foo") | |
let someModel = SomeModel(name: "Bar", otherModelInstance: someOtherModel) | |
container.mainContext.insert(someModel) | |
return ContentView() | |
.environmentObject(ExampleService(modelContext: container.mainContext)) | |
} catch { | |
return Text("oops") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment