Code: Select all
fullScreenCover
Code: Select all
struct DocumentControllerView: View
{
@Binding var commentToEdit: Comment?
@Binding var commentPassagesToEdit: [Passage]?
@Binding var commentDocumentToPresent: Document?
@State private var interiorDocumentToPresent: Document?
private var documentToPresent: Binding
{
Binding(get:
{
if self.commentToEdit != nil && self.commentPassagesToEdit != nil,
let commentDocumentToPresent = self.commentDocumentToPresent
{
commentDocumentToPresent
}
else
{
self.interiorDocumentToPresent
}
},
set:
{
newValue in
if (newValue == nil)
{
self.commentDocumentToPresent = nil
self.commentToEdit = nil
self.commentPassagesToEdit = nil
self.interiorDocumentToPresent = nil
}
})
}
...
Code: Select all
commentPassagesToEdit
Code: Select all
Text(comment.text)
if let document = comment.document,
let passages = comment.passages
{
Button
{
onPresentCommentInDocument(comment, document, passages)
}
label:
{
Image(systemName: "document")
}
}
else
{
//handle error
}
Code: Select all
.fullScreenCover(item: self.documentToPresent)
{
_ in
if let commentPassagesToEditBinding = Binding(self.$commentPassagesToEdit)
{
Button("Add Passage")
{
commentPassagesToEditBinding.wrappedValue.append(Passage())
self.commentToEdit?.passages?.append(Passage())
}
Text("commentPassagesToEditBinding.wrappedValue.count: \(commentPassagesToEditBinding.wrappedValue.count)")
Text("self.commentToEdit?.passages?.count: \(String(describing: self.commentToEdit?.passages?.count))")
}
}
< /code>
Es scheint seltsam und ich frage mich, wo sich der Fehler in meinem Design befindet. Jede Hilfe würde geschätzt! < /P>
unten ist ein vollständig@Model
final class Comment
{
var text: String
@Relationship(deleteRule: .cascade, inverse: \Document.comment) var document: Document?
@Relationship(deleteRule: .cascade, inverse: \Passage.comment) var passages: [Passage]?
init(text: String)
{
self.text = text
self.passages = []
}
}
@Model
final class Document
{
var comment: Comment?
init()
{
}
}
@Model
final class Passage
{
var comment: Comment?
init()
{
}
}
struct ContentView: View
{
@Environment(\.modelContext) private var modelContext
@State private var commentToEdit: Comment?
@State private var commentPassagesToEdit: [Passage]?
@State private var commentDocumentToPresent: Document?
var body: some View
{
CommentList(onPresentCommentInDocument:
{
comment, document, passages in
self.commentToEdit = comment
self.commentPassagesToEdit = passages
self.commentDocumentToPresent = document
})
.toolbar
{
ToolbarItem(placement: .navigationBarLeading)
{
DocumentControllerView(commentToEdit: self.$commentToEdit,
commentPassagesToEdit: self.$commentPassagesToEdit,
commentDocumentToPresent: self.$commentDocumentToPresent)
}
}
.task
{
do
{
try modelContext.delete(model: Comment.self)
let comment = Comment(text: "Comment 1")
comment.document = Document()
comment.passages?.append(Passage())
self.modelContext.insert(comment)
}
catch
{
fatalError(error.localizedDescription)
}
}
}
}
struct CommentList: View
{
var onPresentCommentInDocument: ((Comment, Document, [Passage]) -> Void)
@Query private var comments: [Comment]
var body: some View
{
List
{
ForEach(self.comments)
{
comment in
HStack
{
Text(comment.text)
if let document = comment.document,
let passages = comment.passages
{
Button
{
onPresentCommentInDocument(comment, document, passages)
}
label:
{
Image(systemName: "document")
}
}
else
{
//handle error
}
}
}
}
}
}
struct DocumentControllerView: View
{
@Binding var commentToEdit: Comment?
@Binding var commentPassagesToEdit: [Passage]?
@Binding var commentDocumentToPresent: Document?
@State private var interiorDocumentToPresent: Document?
private var documentToPresent: Binding
{
Binding(get:
{
if self.commentToEdit != nil && self.commentPassagesToEdit != nil,
let commentDocumentToPresent = self.commentDocumentToPresent
{
commentDocumentToPresent
}
else
{
self.interiorDocumentToPresent
}
},
set:
{
newValue in
if (newValue == nil)
{
self.commentDocumentToPresent = nil
self.commentToEdit = nil
self.commentPassagesToEdit = nil
self.interiorDocumentToPresent = nil
}
})
}
var body: some View
{
Text("Document Controller")
.fullScreenCover(item: self.documentToPresent)
{
_ in
if let commentPassagesToEditBinding = Binding(self.$commentPassagesToEdit)
{
Button("Add Passage")
{
commentPassagesToEditBinding.wrappedValue.append(Passage())
self.commentToEdit?.passages?.append(Passage())
}
Text("commentPassagesToEditBinding.wrappedValue.count: \(commentPassagesToEditBinding.wrappedValue.count)")
Text("self.commentToEdit?.passages?.count: \(String(describing: self.commentToEdit?.passages?.count))")
}
}
}
}