I am having it so users can leave comments and then want others to be able to vote if the comment is helpful or not.
The preview of the first view doesn’t change the number but toggling of the up and down arrow work correctly.
In the second view if you start at 0 and toggle up (1), down (0) and then down again (so nothing is selected) you are at 1 instead of 0.
If someone can explain what I am doing wrong or point me to a video or course that uses this with firebase that would greatly appreciated.
import SwiftUI
struct isHelpfulView: View {
@Binding var isHelpful: Int
@State private var isUpSelected: Bool = false
@State private var isDownSelected: Bool = false
var body: some View {
HStack {
Button(role: .none) {
isUpSelected.toggle()
isDownSelected = false
if isUpSelected == true {
isHelpful += 1
} else {
isHelpful -= 1
}
} label: {
Image(systemName: isUpSelected ? "arrowshape.up.fill" : "arrowshape.up")
}
Text(isHelpful, format: .number)
Button(role: .none) {
isDownSelected.toggle()
isUpSelected = false
if isDownSelected == true {
isHelpful -= 1
} else {
isHelpful += 1
}
} label: {
Image(systemName: isDownSelected ? "arrowshape.down.fill" : "arrowshape.down")
}
}
}
}
#Preview {
isHelpfulView(isHelpful: .constant(1759))
}
import SwiftUI
struct NoteView: View {
@State var note: Note
var body: some View {
VStack {
HStack {
Text(note.postedOn, style: .date)
.bold()
Spacer()
isHelpfulView(isHelpful: $note.isHelpful)
}
Text(note.noteText)
}
.frame(width: UIScreen.main.bounds.width - 64)
.foregroundStyle(Color(hex: note.foregroundColor))
.padding()
.background(RoundedRectangle(cornerRadius: 10).fill(Color(hex: note.backgroundColor)))
}
}
#Preview {
NoteView(note: Note(noteText: "Park around back",
postedOn: Date(),
backgroundColor: "800080",
foregroundColor: ""))
}