Shush alarms UI#5
Conversation
…Time List activity
|
@samagra14 please test and review this PR. |
samagra14
left a comment
There was a problem hiding this comment.
Please do the necessary changes and merge.
| </application> | ||
|
|
||
| </manifest> | ||
| </manifest> No newline at end of file |
| if (rowID == null || days.length != 7) { | ||
| return; | ||
| } | ||
| // Instance of calendar that holds current system time |
There was a problem hiding this comment.
Will change it in next commit.
| * scheduled for the corresponding day. i.e. days[0] is set to be true | ||
| * then a weekly alarm will be set such that it will shush the phone every Monday. | ||
| * @param rowID Unique primary key of the shush alarm row. | ||
| */ |
There was a problem hiding this comment.
Try using a different method name for both the methods to avoid confusion.
There was a problem hiding this comment.
Reffering to this and this answer to the question When is method overloading appropriate?, the answers provided by various people suggest that overloading is appropriate when -
Each overloaded method should be functionally the same as the others in an overloaded group, otherwise it will be unclear as to how, when or why behaviour is being varied.
The second answer suggests that -
From the information given they appear to be equivalent (since one calls the other) and I would think it reasonable to overload them.
And since the methods I overloaded do exactly the same thing(since one calls the other), I think overloading can be used here.
What do you think @suyashmahar ?
There was a problem hiding this comment.
I think it is right to use same name (this is what overloading is).
| * @param endAlarmID The id that uniquely identifies the end time alarm | ||
| */ | ||
| public void setAlarm(long startTimeInMillis, long endTimeInMillis) { | ||
| public void setAlarm(long startTimeInMillis, long endTimeInMillis, Integer startAlarmID, Integer endAlarmID) { |
There was a problem hiding this comment.
As stated above use different names for different methods.
suyashmahar
left a comment
There was a problem hiding this comment.
Why isn't an Alarm class used to pass data around in methods instead of passing them as individual parameters?
Nice job on formatting and comments! 🎉
| * @param rowID Unique primary key of the shush alarm row. | ||
| */ | ||
| public void setSingleDayAlarm(Calendar startTime, Calendar endTime, int day, Integer rowID) { | ||
| if (day > 6 || rowID == null) { |
There was a problem hiding this comment.
If the method throws and exception, then I will have to catch it every time I use the method. I won't be passing anything that should cause an exception while using this method.
There was a problem hiding this comment.
Isn't this a wrong way? You won't pass such values intentionally, but wouldn't it be better to catch that exception and show the user an error message or something?
This explains why there should be an exception. If by chance something happens and condition day > 6 or rowID == null is evaluated to true then the user wouldn't know that there is some problem. He wouldn't know if his action set an alarm or not.
| * then a weekly alarm will be set such that it will shush the phone every Monday. | ||
| * @param rowID Unique primary key of the shush alarm row. | ||
| */ | ||
| public void setWeeklyAlarm(Calendar startTime, Calendar endTime, boolean[] days, Integer rowID) { |
There was a problem hiding this comment.
Why isn't an exception thrown or logged here?
There was a problem hiding this comment.
I will log in the error stream in next commit.
There was a problem hiding this comment.
Throwing an exception would be better, logging in error stream decreases performance and won't be different from current code (functionality wise), programming defensively is much better.
| endTime.add(Calendar.DAY_OF_MONTH, 1); | ||
| } | ||
|
|
||
| // get Day index of start Time |
There was a problem hiding this comment.
Though nitpicking using i for variables that are used method wide isn't a good choice IMO
There was a problem hiding this comment.
How can this cause a problem? Can you please elaborate?
There was a problem hiding this comment.
Not a logical problem but it easier to move with the flow of code if variable names are descriptive.
| endTime.getTimeInMillis(), | ||
| getStartDayID(j, rowID), | ||
| getEndDayID(j, rowID)); | ||
| } |
There was a problem hiding this comment.
Is there any reason for two loops with similar function, why can't they be merged into a single loop?
There was a problem hiding this comment.
Thanks for pointing this out.
|
@suyashmahar I can not find any Alarm class in android. |
|
|
||
| mContext.getContentResolver().update( | ||
| Uri.withAppendedPath(PlacesContract.TimeEntry.CONTENT_URI, String.valueOf(holder.id)) | ||
| , values, null, null |
There was a problem hiding this comment.
though works, line beginning with comma should be avoided
| private String getColumnFromDay(int day) { | ||
| switch (day) { | ||
| case 0: { | ||
| return PlacesContract.TimeEntry.COLUMN_MONDAY; |
There was a problem hiding this comment.
Why isn't an array of TimeEntry.<something> is used and then accessed by its index? That would have been an easier way for this.
There was a problem hiding this comment.
To remember the names of the columns and to make the code readable this is used. For instance, COLUMN_WEDNESDAY seems more readable than column[2].
There was a problem hiding this comment.
Are you sure? Currently, your code is spanning over 450 lines and everything is repeated 6 times. Using arrays and enums help you write code faster, read it faster and make it shorter that is what they are for.
| CheckBox thursday; | ||
| CheckBox friday; | ||
| CheckBox saturday; | ||
| CheckBox sunday; |
There was a problem hiding this comment.
An array of Checkboxes would be easier and smaller solution, this way you can iterate over each checkbox in a loop.
There was a problem hiding this comment.
This is done to avoid arrays in the TimeEntry class so that the database contract is more readable.
There was a problem hiding this comment.
why not use an array of strings then?
| context.getSystemService(Context.NOTIFICATION_SERVICE); | ||
|
|
||
| notificationManager.notify(0, builder.build()); | ||
| } |
There was a problem hiding this comment.
suyashmahar
left a comment
There was a problem hiding this comment.
Code for TimeListAdapter.java is seriously messed up.
| * then a weekly alarm will be set such that it will shush the phone every Monday. | ||
| * @param rowID Unique primary key of the shush alarm row. | ||
| */ | ||
| public void setWeeklyAlarm(Calendar startTime, Calendar endTime, boolean[] days, Integer rowID) { |
There was a problem hiding this comment.
Throwing an exception would be better, logging in error stream decreases performance and won't be different from current code (functionality wise), programming defensively is much better.
| endTime.add(Calendar.DAY_OF_MONTH, 1); | ||
| } | ||
|
|
||
| // get Day index of start Time |
There was a problem hiding this comment.
Not a logical problem but it easier to move with the flow of code if variable names are descriptive.
| * scheduled for the corresponding day. i.e. days[0] is set to be true | ||
| * then a weekly alarm will be set such that it will shush the phone every Monday. | ||
| * @param rowID Unique primary key of the shush alarm row. | ||
| */ |
There was a problem hiding this comment.
I think it is right to use same name (this is what overloading is).
| context.getString(R.string.alarm_intent_int_extra_key), -1); | ||
|
|
||
| if (timeInMillis == -1 || alarmID == -1){ | ||
| return; |
There was a problem hiding this comment.
alarmID is SQLite generated primary key and is auto incremented. timeInMillis is a millisecond value that has passed since the epoch. Therefore, neither of them can be negative.
There was a problem hiding this comment.
See this.
An instant in time can be represented by a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).
|
|
||
| // Retrieving the whole row from the database. | ||
| int id = timeDataCursor.getInt( | ||
| timeDataCursor.getColumnIndexOrThrow(PlacesContract.TimeEntry._ID) |
There was a problem hiding this comment.
Can this method call throw an exception?
There was a problem hiding this comment.
In theory yes it can, but since we are using the column index from the contract class, we can be sure that the method call will not result in an unhandled exception state.
| private String getColumnFromDay(int day) { | ||
| switch (day) { | ||
| case 0: { | ||
| return PlacesContract.TimeEntry.COLUMN_MONDAY; |
There was a problem hiding this comment.
Are you sure? Currently, your code is spanning over 450 lines and everything is repeated 6 times. Using arrays and enums help you write code faster, read it faster and make it shorter that is what they are for.
| return PlacesContract.TimeEntry.COLUMN_SATURDAY; | ||
| } | ||
| case 6: { | ||
| return PlacesContract.TimeEntry.COLUMN_SUNDAY; |
There was a problem hiding this comment.
Things like this case statement would be just three lines, instead of current 20+ lines and would decrease writing and testing time. Testing because currently, you have 7 possible points of failure instead of 1.
There was a problem hiding this comment.
I have used an array of checkboxes now, I will push the code soon. It did reduce many lines of code 😄
| CheckBox thursday; | ||
| CheckBox friday; | ||
| CheckBox saturday; | ||
| CheckBox sunday; |
There was a problem hiding this comment.
why not use an array of strings then?
The work in this branch aims to complete basic functionality of shush alarms.