Reputation: 5626
In my android app, I have a simple UI with two elements(widgets) : a spinner that lets users pick a DAY_OF_WEEK & a TimePicker that let's them choose time of day and minutes (AM/PM) included.
Here is the UI:
There are three possible outcomes in an event that a user updates their settings:
I do not know why I feel confused here but here is the code that I have tried to write to handle the situation:
int hour = Utils.getValueFromSharedPrefs(context.getString(R.string.hour_key), activity);
int minutes = Utils.getValueFromSharedPrefs(context.getString(R.string.minute_key), activity);
if (day >= Calendar.DAY_OF_WEEK){
if (hour >= Calendar.HOUR_OF_DAY){
calendar.set(Calendar.DAY_OF_WEEK, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, 0);
}else{
int diff = Calendar.DAY_OF_WEEK - day;
calendar.set(Calendar.DAY_OF_WEEK, Calendar.DAY_OF_WEEK + diff);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, 0);
}
}else{
calendar.set(Calendar.DAY_OF_WEEK, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, 0);
}
The variable day is an integer equivalent of the day selected by a user from the spinner.
My question is this: what is the best approach to solving this problem? I just want to make sure that when a user selects Today[WEDNESDAY] and a time less than right now, I set the calendar for next week since that hour has already passed. The same should be true for when a user selects a day before today [e.g TUESDAY, MONDAY, SUNDAY] - the alarm should be set for Next week on the selected day.
Thank you in advance.
Upvotes: 1
Views: 327
Reputation: 11251
It's always a good idea, according to me, to convert all your Calendar values to a timestamp, do the math on it, and then convert them back.
Makes it easier for you.
Upvotes: 1