The purpose of this function is to go through the first 200 hundred days of each user's schedule. Go through a each user's schedule one by one to check if they're available on a certain day. If they are available on a certain day a counter will increment and if the counter matches the amount of users the specified event will be added to the user's schedule. This isn't working however as my code is not reading the data from firebase correctly. Here is the code for the function:
private void findTime(String eventName, int EventTime)
{
Boolean dayFound = false;
countNeeded = TheUserList.size();
DaySearched = LocalDate.now();
while (!dayFound) {
for (int i = 0; i <200; i++)
{
// the current count variable tracks the amount of users in the schedule have free tim slots for a specific day
currentCount = 0;
Toast.makeText(this, ("The first for loop has begun " + String.valueOf(currentCount)), Toast.LENGTH_SHORT).show();
for (int e =0; e < TheUserList.size(); e++)
{
String currentUser = TheUserList.get(e);
Log.i("Firebase", Login.reference.child(currentUser).child(String.valueOf(DaySearched)).child("Time for day").toString());
Login.reference.child(currentUser).child(String.valueOf(DaySearched)).child("Time for day").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
int TimeForDay = snapshot.getValue(int.class);
if (TimeForDay + EventTime < 1441)
{
currentCount = currentCount + 1;
}
else
{
currentCount = 0;
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(PlanEvent.this, "Failed to get Data", Toast.LENGTH_SHORT).show();
}
});
}
if (currentCount == countNeeded)
{
Log.d("FindTime Function", "Found and available day at" + String.valueOf(DaySearched));
Tasks newEvent = new Tasks(eventName, DaySearched, LocalTime.now(), EventTime);
Day.enoughTimeChecker(DaySearched, EventTime, newEvent);
dayFound = true;
}
DaySearched = DaySearched.plusDays(1);
}
}
}
I have added Toast messages in the while loop to see if it being called (this works) and also one to see if the function has found an appropriate date (this isn't appearing so I assume it cannot find a day for some reason).
Here is an image to understand the structure of the real-time-database
Here is the output of the log.i()
line:
Any help would be greatly appreciated.