-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shush alarms UI #5
base: master
Are you sure you want to change the base?
Conversation
…Time List activity
@samagra14 please test and review this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do the necessary changes and merge.
app/src/main/AndroidManifest.xml
Outdated
</application> | ||
|
||
</manifest> | ||
</manifest> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leave a line at EOF.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.
return; | ||
} | ||
// Instance of calendar that holds current system time | ||
Calendar rightNow = Calendar.getInstance(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a better variable name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will change it in next commit.
* 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(int startHour, int startMinutes, int endHour, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try using a different method name for both the methods to avoid confusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is right to use same name (this is what overloading is).
*/ | ||
public void setAlarm(long startTimeInMillis, long endTimeInMillis) { | ||
public void setAlarm(long startTimeInMillis, long endTimeInMillis, Integer startAlarmID, Integer endAlarmID) { | ||
if (startTimeInMillis >= endTimeInMillis) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As stated above use different names for different methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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! 🎉
*/ | ||
public void setSingleDayAlarm(Calendar startTime, Calendar endTime, int day, Integer rowID) { | ||
if (day > 6 || rowID == null) { | ||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No exception?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
* @param rowID Unique primary key of the shush alarm row. | ||
*/ | ||
public void setWeeklyAlarm(Calendar startTime, Calendar endTime, boolean[] days, Integer rowID) { | ||
// To avoid various exceptions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why isn't an exception thrown or logged here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will log in the error stream in next commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
} | ||
|
||
// get Day index of start Time | ||
int i = getDay(startTime); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though nitpicking using i for variables that are used method wide isn't a good choice IMO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can this cause a problem? Can you please elaborate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a logical problem but it easier to move with the flow of code if variable names are descriptive.
getStartDayID(j, rowID), | ||
getEndDayID(j, rowID)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code for TimeListAdapter.java is seriously messed up.
* @param rowID Unique primary key of the shush alarm row. | ||
*/ | ||
public void setWeeklyAlarm(Calendar startTime, Calendar endTime, boolean[] days, Integer rowID) { | ||
// To avoid various exceptions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
} | ||
|
||
// get Day index of start Time | ||
int i = getDay(startTime); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a logical problem but it easier to move with the flow of code if variable names are descriptive.
* 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(int startHour, int startMinutes, int endHour, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if time
or alarmID
is -ve?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
epoch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this method call throw an exception?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not use an array of strings then?
The work in this branch aims to complete basic functionality of shush alarms.