Skip to content

Shush alarms UI#5

Open
RohanBh wants to merge 16 commits into
masterfrom
shush-alarms-ui
Open

Shush alarms UI#5
RohanBh wants to merge 16 commits into
masterfrom
shush-alarms-ui

Conversation

@RohanBh

@RohanBh RohanBh commented Aug 19, 2017

Copy link
Copy Markdown
Collaborator

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

RohanBh commented Sep 1, 2017

Copy link
Copy Markdown
Collaborator Author

@samagra14 please test and review this PR.

@samagra14 samagra14 left a comment

Copy link
Copy Markdown
Owner

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.

Comment thread app/src/main/AndroidManifest.xml Outdated
</application>

</manifest>
</manifest> No newline at end of file

Copy link
Copy Markdown
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
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Sure.

if (rowID == null || days.length != 7) {
return;
}
// Instance of calendar that holds current system time

Copy link
Copy Markdown
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
Copy Markdown
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.

* 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.
*/

Copy link
Copy Markdown
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.

@RohanBh RohanBh Oct 7, 2017

Copy link
Copy Markdown
Collaborator Author

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 ?

Copy link
Copy Markdown

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

* @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) {

Copy link
Copy Markdown
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.

@suyashmahar suyashmahar left a comment

Copy link
Copy Markdown

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! 🎉

* @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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No exception?

Copy link
Copy Markdown
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.

Copy link
Copy Markdown

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.

* 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) {

Copy link
Copy Markdown

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
Copy Markdown
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.

Copy link
Copy Markdown

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.

endTime.add(Calendar.DAY_OF_MONTH, 1);
}

// get Day index of start Time

Copy link
Copy Markdown

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
Copy Markdown
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?

Copy link
Copy Markdown

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.

endTime.getTimeInMillis(),
getStartDayID(j, rowID),
getEndDayID(j, rowID));
}

Copy link
Copy Markdown

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
Copy Markdown
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

RohanBh commented Oct 7, 2017

Copy link
Copy Markdown
Collaborator Author

@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

Copy link
Copy Markdown

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;

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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;

Copy link
Copy Markdown

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
Copy Markdown
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.

Copy link
Copy Markdown

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@suyashmahar suyashmahar left a comment

Copy link
Copy Markdown

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.

* 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) {

Copy link
Copy Markdown

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.

endTime.add(Calendar.DAY_OF_MONTH, 1);
}

// get Day index of start Time

Copy link
Copy Markdown

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.

* 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.
*/

Copy link
Copy Markdown

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;

Copy link
Copy Markdown

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?

@RohanBh RohanBh Oct 10, 2017

Copy link
Copy Markdown
Collaborator Author

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

epoch?

Copy link
Copy Markdown
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)

Copy link
Copy Markdown

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?

@RohanBh RohanBh Oct 10, 2017

Copy link
Copy Markdown
Collaborator Author

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;

Copy link
Copy Markdown

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;

Copy link
Copy Markdown

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
Copy Markdown
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;

Copy link
Copy Markdown

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