尝试创建一个包含测试点操作列表的测试卡。 navigationLink转到测试点操作需要完成的数据点列表。
在我尝试添加@Binding之前,以下代码非常有用,因为我需要为DataPointRow上的数据点添加控件和textFields。
ExecutionChecklistView
@ObservedObject var viewModel: ExecutionChecklistViewModel
var body: some View {
NavigationView {
List (viewModel.testCards, id: \.testPointAction.id) { testCard in
TestCardView(testCard: testCard)
}
}
}
}
struct TestCardView: View {
var testCard: TestPoint
var body: some View {
NavigationLink(destination: DataPointRow(testCard: testCard)) {
Text("\(testCard.id)")
Text("\(testCard.testPointAction.tpAction)")
}
}
}
struct DataPointRow: View {
var testCard: TestPoint
var body: some View {
List(testCard.dataPoints, id: \.id) {dataPoint in
Text("\(dataPoint.name)")
}
}
}
testCard:[TestPoint]
struct TestPoint: Decodable, FetchableRecord {
var testPointAction: TestPointAction
var dataPoints: [DataPoint]
}
extension TestPoint: Identifiable {
var id: Int { testPointAction.id }
}
这是我尝试过的。 但是ForEach给出了类型'_'没有成员'element'
struct ExecutionChecklistView: View {
@ObservedObject var viewModel: ExecutionChecklistViewModel
var body: some View {
NavigationView {
List {
ForEach(
Array( viewModel.testCards.enumerated() ),
id: \.element.id
) { index, _ in
TestCardView(testCard: $viewModel.testCards[index])
}
}
}
}
}
struct TestCardView: View {
@Binding var testCard: TestPoint
var body: some View {
NavigationLink(destination: DataPointRow(testCard: $testCard)) {
Text("\(testCard.id)")
Text("\(testCard.testPointAction.tpAction)")
}
}
}
struct DataPointRow: View {
@Binding var testCard: TestPoint
var body: some View {
List(testCard.dataPoints, id: \.id) {dataPoint in
Text("\(dataPoint.name)")
}
}
}
ExecutionChecklistViewModel
class ExecutionChecklistViewModel{
@Published private var executionChecklist = TestPoints.ExecutionChecklist.empty
private var cancellables: [AnyCancellable] = []
init() {
Current.testPoints()
.executionChecklistPublisher()
//Performa a synchronous initial fetch
.fetchOnSubscription()
//Ignore database errors
.catch { _ in Empty() }
.sink { [unowned self] in self.executionChecklist = $0 }
.store(in: &cancellables)
}
}
extension ExecutionChecklistViewModel: ObservableObject, Identifiable {
var testCards: [TestPoint] {
executionChecklist.testCards
}
}
测试点
class TestPoints {
private let database: DatabaseWriter
init(database: DatabaseWriter) {
self.database = database
}
struct ExecutionChecklist {
var testCards: [TestPoint]
static let empty = ExecutionChecklist(testCards: [])
}
func executionChecklistPublisher() -> DatabasePublishers.Value<ExecutionChecklist> {
ValueObservation
.tracking(value: {db in
let request = TestPointAction
.including(all: TestPointAction.dataPoints)
let testCards = try TestPoint
.fetchAll(db, request)
print (ExecutionChecklist(testCards: testCards))
return ExecutionChecklist(testCards: testCards)
})
.publisher(in: database)
}
}