[ad_1]
I don’t get it, why i can’t have a simple if/else statement in the view of my body. Basically in this view there will be other views placed into a scrollView, but only if the depending boolean is true – that was the plan. But I get a “Type ‘()’ cannot conform to ‘View'” error message on the “if showFurtherContent {…” line (marked with **). I know, that views aren’t made for logic – but I have no clue, why I can’t put a simple if/else statement in there to create view elements – that should be possible in my opinion. Can someone help me out, how to achieve this? The application will be a chatbot and the boolean for visualizing different “modules” (basically other SwiftUI View files) should let the view place it, if true. Otherwise I can’t figure out, how to dynamically fill a scrollView with different ui elements – the swiftui is currently driving me crazy but plz note: im still learning it but I thought such simple things should be easily possible.
struct ChatBot: View {
@State var _ratio = 60.0;
@State var _start : Bool = false;
@State private var messageText = ""
@State var messages: [String] = [""]
@State public var messageProperty = "Y"
@State private var botWriting = false
@State private var textInputEnabled = false
@ObservedObject var vm = MainMessagesViewModel() //reference to MMVM
@StateObject var cbl = ChatBotVariables()
@State var _case: String = "text"
//&@EnvironmentObject var cbl: ChatBotVariables
@State var showFurtherContent: Bool = false
init() {
initChatbot()
}
var body: some View {
NavigationView {
VStack {
Button{
showFurtherContent.toggle()
//switchHappyBar() // Debug
}label:{
Image(systemName: "trash")
}
ScrollView{
ForEach(messages, id: \.self){ message in
if message.contains("[USER]"){
let newMessage = message.replacingOccurrences(of:"[USER]", with: "")
HStack{
Spacer()
Text(newMessage)
.padding()
.foregroundColor(Color.white)
.background(.blue.opacity(0.8))
.cornerRadius(10)
.padding(.horizontal, 16)
.padding(.bottom, 10)
}
}else{
HStack{
Text(message)
.padding()
.background(.gray.opacity(0.15))
.cornerRadius(10)
.padding(.horizontal, 16)
.padding(.bottom, 10)
Spacer()
}
HStack{
**if showFurtherContent {**
ChatBot_EmojiBar()
showFurtherContent.toggle()
}else{
ChatBot_Answer()
}
}
}
}//.rotationEffect(.degrees(180))
/*
if cbl.showFurtherContent {
switch cbl.answerCase{
case "emoji":
ChatBot_EmojiBar()
break;
case "video":
ChatBot_EmojiBar()
break;
case "yesno":
ChatBot_Answer()
break;
default:
()
break;
}
cbl.showFurtherContent.toggle()
}*/
}//.rotationEffect(.degrees(180))
.background(Color.gray.opacity(0.05))
if(messages.count > 1){
let mCount = messages.count
Text("count: \(mCount) answer: \(cbl.answerCase) Property: \(messageProperty)")
}
HStack {
if botWriting{
Image(systemName: "ellipsis.bubble.fill")
.font(.system(size: 26))
.foregroundColor(Color.blue)
}
}
[..] further code but not relevant
[ad_2]