Skip to content

Instantly share code, notes, and snippets.

@colinfwren
Last active January 18, 2025 22:30
Show Gist options
  • Save colinfwren/f4554c4c525890012a569a33e4a04558 to your computer and use it in GitHub Desktop.
Save colinfwren/f4554c4c525890012a569a33e4a04558 to your computer and use it in GitHub Desktop.
SwiftUI View using the Service
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