Skip to content
This repository has been archived by the owner on Jan 11, 2021. It is now read-only.

Notification enhancement #31

Merged
merged 6 commits into from
Sep 8, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

import com.alexstyl.specialdates.R;
import com.alexstyl.specialdates.analytics.Action;
import com.alexstyl.specialdates.analytics.Analytics;
import com.alexstyl.specialdates.analytics.ActionWithParameters;
import com.alexstyl.specialdates.analytics.Analytics;
import com.alexstyl.specialdates.analytics.Firebase;
import com.alexstyl.specialdates.service.DailyReminderIntentService;
import com.alexstyl.specialdates.ui.widget.TimePreference;
Expand Down
61 changes: 56 additions & 5 deletions mobile/src/main/java/com/alexstyl/specialdates/util/Notifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.StyleSpan;

import com.alexstyl.specialdates.R;
import com.alexstyl.specialdates.contact.Contact;
import com.alexstyl.specialdates.date.ContactEvent;
import com.alexstyl.specialdates.date.Date;
import com.alexstyl.specialdates.datedetails.DateDetailsActivity;
import com.alexstyl.specialdates.events.ContactEvents;
import com.alexstyl.specialdates.events.EventType;
import com.alexstyl.specialdates.events.bankholidays.BankHoliday;
import com.alexstyl.specialdates.events.namedays.NamedayPreferences;
import com.alexstyl.specialdates.images.ImageLoader;
Expand Down Expand Up @@ -89,19 +95,66 @@ public void forDailyReminder(ContactEvents events) {
);

String title = NaturalLanguageUtils.joinContacts(context, events.getContacts(), 3);
String fullText = TextUtils.join(", ", events.getContacts());

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_contact_event)
.setContentTitle(title)
.setLargeIcon(largeIcon)
.setStyle(new NotificationCompat.BigTextStyle().bigText(fullText))
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
.setAutoCancel(true)
.setContentText(fullText)
.setContentIntent(intent)
.setNumber(events.size())
.setColor(context.getResources().getColor(R.color.main_red));

if (events.size() == 1) {
ContactEvent event = events.getEvent(0);
String msg = "";

if (EventType.BIRTHDAY.equals(event.getType())) {
Copy link
Owner

Choose a reason for hiding this comment

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

Now that we have the getLabelFor() in place. Why not use it here as well? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry 😊

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is not a good first impression I'm giving right now 😳

Copy link
Owner

Choose a reason for hiding this comment

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

You are making a contribution to the project. You have made the best impression already so don't you worry about it :)

int age = event.getContact().getBirthday().getAgeOnYear(event.getYear());
msg = context.getString(R.string.turns_age, age);

} else if (EventType.NAMEDAY.equals(event.getType())) {
Copy link
Owner

Choose a reason for hiding this comment

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

👍

msg = context.getString(R.string.nameday);
}

NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle().bigText(msg);
bigTextStyle.setBigContentTitle(title);
builder.setContentText(msg);

builder.setStyle(bigTextStyle);

} else if (events.getContacts().size() > 1) {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(title);

for (int i = 0; i < events.size(); ++i) {

ContactEvent event = events.getEvent(i);
Contact contact = event.getContact();
String name = contact.getDisplayName().toString();

String lineFormatted = name;

if (EventType.BIRTHDAY.equals(event.getType())) {
Copy link
Owner

@alexstyl alexstyl Sep 8, 2016

Choose a reason for hiding this comment

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

❌ This is hard to extend. What happens when more events are added in the future? We would have to check all bits of code that do checks with specific events.

Also, a Birthday might not include a year of birth, which means that the following bit will throw an exception if the contact does not have a specified year of birth.

You could just copy-paste the getLabelFor() method. It checks only for that special case, and fallbacks to using the label of the eventType if there is no special case.

Don't worry about the code duplication from the other class. I'll solve it nicely in a later PR :)

Copy link
Contributor Author

@auricgoldfinger auricgoldfinger Sep 8, 2016

Choose a reason for hiding this comment

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

Copy Paste! Oh no!
...
Well, ok then. I'm not yet familiar enough with your code so I'm going to leave such a refactoring up to you. Let me add a big TODO and deprecated so that it can be found later.

I'm going to create an issue as well so that it is not forgotten

int age = contact.getBirthday().getAgeOnYear(events.getDate().getYear());

lineFormatted += " " + context.getString(R.string.turns_age, age);

} else if (EventType.NAMEDAY.equals(event.getType())) {
lineFormatted += " " + context.getString(R.string.nameday);

}

Spannable sb = new SpannableString(lineFormatted);
sb.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
inboxStyle.addLine(sb);
}

builder.setStyle(inboxStyle);
builder.setContentText(TextUtils.join(", ", events.getContacts()));
}

if (supportsPublicNotifications()) {
String publicTitle = context.getString(R.string.contact_celebration_count, contactCount);
NotificationCompat.Builder publicNotification = new NotificationCompat.Builder(context)
Expand All @@ -114,8 +167,6 @@ public void forDailyReminder(ContactEvents events) {
builder.setPublicVersion(publicNotification.build());
}

builder.setNumber(contactCount);

for (Contact contact : events.getContacts()) {
Uri uri = contact.getLookupUri();
if (uri != null) {
Expand Down
1 change: 0 additions & 1 deletion secret.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
ext {

// Replace the existing values with your own unique keys
crashlyticsKey = "1234567890123456789012345678901234567890"
androidVendingKey = "Include your vending key here"
Expand Down