Assume filteredEvents
from below to be a List<Filter>
. Basically a list of objects, each of these holding a list of days in the year, among other not-important-at-the-moment data and a specific colour to draw on the calendar, to represent that there are events for that particular colour distinguishable department.
It's used in the context of filtering a Calendar based on the type of events, "Dept1" is one Filter
, "Dept2" is another Filter
, etc.
Each Filter holds all the dates that should appear in the Calendar for that Department.
The Filter
and Event
classes are at the bottom, for clarification purposes.
Inside the PopulateCalendarWithEvents method I have the following code. What I want to achieve is go through the filterEvents
array of Day
s and sort those based on how many times a given date appears throughout all the Filter
s.
For instance, if Dept2
and Dept5
both have an event for today 16/04/2018, it should put that date into the twoEventDays
only, and put Dept2
and Dept5
's colours correspondingly.
public void PopulateCalendarWithEvents ( List<Filter> filteredEvents) {
//Initiate all the arrays to receive the sorted events. 5 as the maximum number of dots per days is set to 5
Collection<CalendarDay> oneEventDays = new ArrayList<>();
int[] oneColors = new int[1];
Collection<CalendarDay> twoEventDays= new ArrayList<>();
int[] twoColors= new int[2];
Collection<CalendarDay> threeEventDays= new ArrayList<>();
int[] threeColors= new int[3];
Collection<CalendarDay> fourEventDays= new ArrayList<>();
int[] fourColors= new int[4];
Collection<CalendarDay> fiveEventDays= new ArrayList<>();
int[] fiveColors= new int[5];
//Loop trough all the Filters, and get their Day Arrays
for(int i=1;i<filteredEvents.size();i++) {
ArrayList<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
for (int x = 0; x < currentDays.size(); x++) {
if (oneEventDays.contains(currentDays.get(x))) { //If the currently looped date exists in the first array, then this is it's second or more appearance,
// and should be moved to a higher number array
if (twoEventDays.contains(currentDays.get(x))) {//If the currently looped date exists in the 2nd array, it needs to go into the next one
if (threeEventDays.contains((currentDays.get(x)))) {
if (fourEventDays.contains((currentDays.get(x)))) {
if (fiveEventDays.contains((currentDays.get(x)))) {
//As there is room for only so many dots It doesn't matter if I don't add the date and colour here.
// As once you click the date on the Calendar it will show you all events even if they are 100
} else {
//If the date existed in the 4th List and not the 5th it then goes into the 5th list, including it's colour
fiveEventDays.add(currentDays.get(x));
System.arraycopy(fourColors, 0, fiveColors, 0, fourColors.length);
fiveColors[4] = filteredEvents.get(i).color;
}
} else {
//If the date existed in the 3th List and not the 4th it then goes into the 4th list, including it's colour
fourEventDays.add(currentDays.get(x));
System.arraycopy(threeColors, 0, fourColors, 0, threeColors.length);
fourColors[3] = filteredEvents.get(i).color;
}
} else {
threeEventDays.add(currentDays.get(x));
System.arraycopy(twoColors, 0, threeColors, 0, twoColors.length);
threeColors[2] = filteredEvents.get(i).color;
}
} else {
twoEventDays.add(currentDays.get(x));
System.arraycopy(oneColors, 0, twoColors, 0, oneColors.length);
twoColors[1] = filteredEvents.get(i).color;
}
} else { //If the date doesn't exist anywhere, add it into the first List
oneEventDays.add(currentDays.get(x));
oneColors[0] = filteredEvents.get(i).color;
}
}
}
//Remove the higher number of Dates from the lower lists, so that you don't end up with repeating dates for all Lists up to the maximum one, a date should be into
oneEventDays.removeAll(twoEventDays);
twoEventDays.removeAll(threeEventDays);
threeEventDays.removeAll(fourEventDays);
fourEventDays.removeAll(fiveEventDays);
//Send all the Lists and their colors to the Decorator function, that is from the library, loops trough the dates and paints the appropriate number of coloured dots onto each day
calendarView.addDecorator(new EventDecorator(oneEventDays,oneColors));
calendarView.addDecorator(new EventDecorator(twoEventDays,twoColors));
calendarView.addDecorator(new EventDecorator(threeEventDays,threeColors));
calendarView.addDecorator(new EventDecorator(fourEventDays,fourColors));
calendarView.addDecorator(new EventDecorator(fiveEventDays,fiveColors));
}
This is how the Filter
and Event
classes look, though I'm happy enough with those, I've added them just for clarification.
public class Filter {
public int filterIndex;
public String filterName;
public boolean selected;
public ArrayList<Event> eventList;
public int color;
public ArrayList<CalendarDay> calDayArr;
public Filter(int filterIndex, String filterName, boolean selected, ArrayList<Event> eventList, int color) {
this.filterIndex = filterIndex;
this.filterName = filterName;
this.selected=selected;
this.eventList=eventList;
this.color = color;
if(eventList!=null)
calDayArr = processCalDays(eventList);
}
private ArrayList<CalendarDay> processCalDays(ArrayList<Event> eventList) {
ArrayList<CalendarDay> calDayArr=new ArrayList<>();
for(int i=0;i<eventList.size();i++)
{
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, eventList.get(i).day);
calendar.set(Calendar.MONTH,eventList.get(i).month);
calendar.set(Calendar.YEAR,eventList.get(i).year);
calDayArr.add(CalendarDay.from(calendar));
}
return calDayArr;
}
}
public class Event{
int eventId;
String eventTitle;
Calendar startDate;
Calendar endDate;
int year;
int month;
int day;
public Event(int eventInd, String eventTitle, Calendar startDate, Calendar endDate)
{
this.eventId=eventInd;
this.eventTitle=eventTitle;
this.startDate=startDate;
this.endDate=endDate;
this.year = startDate.get(Calendar.YEAR);
this.month= startDate.get(Calendar.MONTH);
this.day = startDate.get(Calendar.DAY_OF_MONTH);
}
}
Obviously, this is extremely ugly and not at all great way of doing this, but I can't figure out a better way to represent all of this...
This is how the app looks like in action.
- Dept1 has 4 events
- Dept2 has 3 events
- Dept3 has 2 events
- Dept4 has 1 event
Each Dept has it's own colour as can be seen when toggling them.