I have a method that inserts into the Android Calendar an event. You can see a simplified snippet in two steps: 1)declare the variables 2) I put
them into a kind of array that in the end will insert
it into the calendar, and it works flawlessy:
private void InsertCalendarEvent() {
//1)here I declare the variables startMills and endMills
Calendar beginTime = Calendar.getInstance();
beginTime.set(2018, 9, 11, 8, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2018, 9, 11, 10, 30);
endMillis = endTime.getTimeInMillis();
//2)here insert the values in the db with put(to buid the array)
// and then finalizing with insert
cr = this.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.TITLE, editTextValue);
values.put(CalendarContract.Events.DESCRIPTION, "Time to Run!");
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
}//end of the method
Now my use case is that I need to insert the same event at different times.
So I am wondering which design pattern I should use to do that to avoid duplications. Basically I need to change the point "1)" the 2 variables of the time startMillis
and endMillis
, and maintain iterating i guess the point "2)" so that the two different time variables go into the point "2" and different events get inserted
I thought different approaches 1)REALLY BAD. Repeat all the code every time writing consecutive methods as
insertCalendarEventNow();
insertCalendarEventAfterFifteenHours();
insertCalendarEventAfterTweentyEightHours();
//TODO and so on
2)Split the insertCalendarEvent in two methods, somehow refactoring the code in another class, maybe using a POJO, ArrayList with Map<>, but I am not sure how to implement
3) Create more classes to maintain the Single Responsibility but I do not know how to do it
What is the way a good architect would proceed with this use case?
EDIT:
Would be the second solution the way to go, avoiding any ArrayList
and POJO
?
1)Easiest but bad The easiest solution duplicate a lot of code:
//just pass equal methods with different settings
addEventAfterOneHour();
addEventAfterSixHours();
addEventAfterEightHours();
addEventAfterThirtyHours();
2)Better in the same class without using POJO:
addEvent(startTimeOneHour,EndTimeOneHour);
addEvent(startTimeSixhours,endTimeSixHours);
addEvent(startTimeEighthours,endTimeEightHours);
addEvent(startTimeThirtyhours,endTimeThirtyHours);
//with an addEvent(Long startTime,Long endTime)