Skip to content
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

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open

Shush alarms UI #5

wants to merge 16 commits into from

Conversation

RohanBh
Copy link
Collaborator

@RohanBh RohanBh commented Aug 19, 2017

The work in this branch aims to complete basic functionality of shush alarms.

@RohanBh RohanBh changed the title [WIP] Shush alarms UI Shush alarms UI Sep 1, 2017
@RohanBh
Copy link
Collaborator Author

RohanBh commented Sep 1, 2017

@samagra14 please test and review this PR.

Copy link
Owner

@samagra14 samagra14 left a 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.

</application>

</manifest>
</manifest>
Copy link
Owner

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.

Copy link
Collaborator Author

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();
Copy link
Owner

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.

Copy link
Collaborator Author

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,
Copy link
Owner

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.

Copy link
Collaborator Author

@RohanBh RohanBh Oct 7, 2017

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 ?

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) {
Copy link
Owner

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.

Copy link

@suyashmahar suyashmahar left a 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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No exception?

Copy link
Collaborator Author

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.

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

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?

Copy link
Collaborator Author

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.

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);

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

Copy link
Collaborator Author

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?

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));
}
}

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?

Copy link
Collaborator Author

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.

@RohanBh
Copy link
Collaborator Author

RohanBh commented Oct 7, 2017

@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

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;

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.

Copy link
Owner

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].

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;

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.

Copy link
Owner

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.

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());
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

@suyashmahar suyashmahar left a 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

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);

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,

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;

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?

Copy link
Collaborator Author

@RohanBh RohanBh Oct 10, 2017

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

epoch?

Copy link
Collaborator Author

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)

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?

Copy link
Collaborator Author

@RohanBh RohanBh Oct 10, 2017

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;

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;

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.

Copy link
Collaborator Author

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;

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants