'while updating snapshot data is not working exactly right in my code swiftui
The contents of redDates will be displayed in red, and the contents of yellowDates in yellow, and the hours other than these will be displayed in green. This works correctly but during instant update it works when changing from green to yellow, from yellow to green but not updating instantaneously when switching from red to others I have to refresh the page.
.background(redDates.contains(hour) ? Color.red: yellowDates.contains(hour) ? Color.yellow: .green)
struct DateView: View {
@StateObject var stadiuminfo=StadiumInfoFromUserModel()
var firestoreDatabase=Firestore.firestore()
var currentUser=Auth.auth().currentUser
@StateObject var daysInfo=dayArrayForUser()
@State var chosenDay=""
@State var preDay=""
@State var yellowDates=[String]()
@State var redDates=[String]()
@State var redHours=[String]()
@State var yellowHours=[String]()
@State var selectedField=""
@State var shown=false
@State var hourType="Full"
@State var checkInfos=false
var didChange=PassthroughSubject<Array<Any>,Never>()
var body: some View {
VStack{
Group{
ScrollView(.horizontal,showsIndicators: false){
HStack{
ForEach(daysInfo.daysArray,id:\.self){day in
Button(action: {
chosenDay=day
getDatefromCalendar(day: chosenDay)
}, label:{
Text(day)
.padding()
.foregroundColor(.black)
.border(Color.black, width: 2)
.background(chosenDay==day ? Color.red: .white)
})
}
}.padding()
}
}
VStack {
HStack{
Text("Müsait değil").scaledToFill()
//.padding()
.foregroundColor(.black)
.frame(width: UIScreen.main.bounds.width * 0.33, height: UIScreen.main.bounds.height * 0.05)
.background(Color.red)
Text("Müsait").scaledToFill()
//.padding()
.foregroundColor(.black)
.frame(width: UIScreen.main.bounds.width * 0.33, height: UIScreen.main.bounds.height * 0.05)
.background(Color.green)
//.border(Color.black, width: 2)
Text("Onay Bekliyor").scaledToFill()
//.padding()
.foregroundColor(.black)
.frame(width: UIScreen.main.bounds.width * 0.33, height: UIScreen.main.bounds.height * 0.05)
.background(Color.yellow)
}
}
Spacer()
if chosenDay=="" {
VStack {
Text("Lütfen tarih seçiniz.")
}
} else {
List(hourType=="Full" ? stadiuminfo.workingHour: stadiuminfo.workingHour2,id:\.self){ hour in
NavigationLink(destination: RequestAppointmentView(numberField: selectedField, selectedDate: chosenDay, selectedHour: hour)){
Button(action: {
}, label: {
Text(hour)
.frame(width: UIScreen.main.bounds.width * 0.7)
.padding()
.background(redDates.contains(hour) ? Color.red: yellowDates.contains(hour) ? Color.yellow: .green)
.foregroundColor(.black)
})
}.disabled(yellowDates.contains(hour) ? true: redDates.contains(hour) ? true: false)
}
}
Spacer()
}.onAppear{
daysInfo.days()
stadiuminfo.getDataFromInfoForUser()
checkInfo()
getDatefromCalendar(day: chosenDay)
}
.navigationTitle(Text("Tarih ve Saat Seçimi")).navigationBarTitleDisplayMode(.inline)
.navigationBarItems(trailing:
Button(action: {
shown.toggle()
})
{
Image(systemName: "info.circle.fill").resizable().frame(width: 30, height: 30)
.sheet(isPresented: $shown) { () -> FieldInformationsView in
return FieldInformationsView(fieldname:selectedField)
}
}
)
}
func getInfos(){
let fb=Firestore.firestore()
fb.collection("FieldInformations").document(chosenStadiumName).collection("Fields").document(selectedField).addSnapshotListener { (document, error) in
if error == nil {
if let hourtype=document?.get("HourType") as? String {
hourType=hourtype
}
//fiyat ve kaporaları çekip listede gösterebilirsin
}
}
}
func checkInfo(){
let fb=Firestore.firestore()
fb.collection("FieldInformations").document(chosenStadiumName).collection("Fields").addSnapshotListener { (snapshot, error) in
if error == nil {
if snapshot?.isEmpty == true {
checkInfos=false
} else {
for document in snapshot!.documents {
if document.documentID==selectedField {
checkInfos=true
getInfos()
}
}
}
}
}
}
func getDatefromCalendar(day: String){
if preDay != day {
redDates.removeAll()
yellowDates.removeAll()
self.firestoreDatabase.collection("Calendar").document(chosenStadiumName).collection(selectedField).whereField("AppointmentDate", isEqualTo: day).addSnapshotListener { (snapshot, error) in
if error == nil {
for document in snapshot!.documents {
if let status=document.get("Status") {
if status as! String == "Onaylandı." {
let redhours=document.get("Hour")
if self.redDates.contains(redhours as! String) {
} else {
self.redDates.append(redhours as! String)
}
}
else if status as! String == "Onay bekliyor." {
let yellowhours=document.get("Hour")
if self.yellowDates.contains(yellowhours as! String) {
} else {
self.yellowDates.append(yellowhours as! String)
}
}
}
}
}
}
self.didChange.send(redDates)
self.didChange.send(yellowDates)
} else {
redDates.removeAll()
yellowDates.removeAll()
self.firestoreDatabase.collection("Calendar").document(chosenStadiumName).collection(selectedField).whereField("AppointmentDate", isEqualTo: day).addSnapshotListener { (snapshot, error) in
if error == nil {
for document in snapshot!.documents {
if let status=document.get("Status") {
if status as! String == "Onaylandı." {
let redhours=document.get("Hour")
if self.redDates.contains(redhours as! String) {
} else {
self.redDates.append(redhours as! String)
}
}
else if status as! String == "Onay bekliyor." {
let yellowhours=document.get("Hour")
if self.yellowDates.contains(yellowhours as! String) {
} else {
self.yellowDates.append(yellowhours as! String)
}
}
}
}
}
}
self.didChange.send(redDates)
self.didChange.send(yellowDates)
}
}
}
In this code, whichever color I get for the head, this time it is not updated instantly from that color to the others.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
