Skip to content

Commit

Permalink
Simplify code; change notification actions to Yes/No/Later
Browse files Browse the repository at this point in the history
  • Loading branch information
iSoron committed Nov 15, 2017
1 parent 7f1a35e commit 9d48b4b
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ class PendingIntentFactory
},
FLAG_UPDATE_CURRENT)

fun cancelNotification(habit: Habit): PendingIntent =
fun removeRepetition(habit: Habit): PendingIntent =
PendingIntent.getBroadcast(
context, 3,
Intent(context, WidgetReceiver::class.java).apply {
action = WidgetReceiver.ACTION_CANCEL_REPETITION
action = WidgetReceiver.ACTION_REMOVE_REPETITION
data = Uri.parse(habit.uriString)
},
FLAG_UPDATE_CURRENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,20 @@ class AndroidNotificationTray
@NonNull timestamp: Timestamp) : Notification
{

val checkAction = Action(
val addRepetitionAction = Action(
R.drawable.ic_action_check,
context.getString(R.string.check),
context.getString(R.string.yes),
pendingIntents.addCheckmark(habit, timestamp))

val snoozeAction = Action(
R.drawable.ic_action_snooze,
context.getString(R.string.snooze),
pendingIntents.snoozeNotification(habit))

val cancelAction = Action(
val removeRepetitionAction = Action(
R.drawable.ic_action_cancel,
context.getString(android.R.string.no),
pendingIntents.cancelNotification(habit)
context.getString(R.string.no),
pendingIntents.removeRepetition(habit)
)

val wearableBg = decodeResource(context.resources, R.drawable.stripe)
Expand All @@ -89,19 +89,19 @@ class AndroidNotificationTray
// WearableExtender.
val wearableExtender = WearableExtender()
.setBackground(wearableBg)
.addAction(checkAction)
.addAction(addRepetitionAction)
.addAction(removeRepetitionAction)
.addAction(snoozeAction)
.addAction(cancelAction)

val builder = NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(habit.name)
.setContentText(habit.description)
.setContentIntent(pendingIntents.showHabit(habit))
.setDeleteIntent(pendingIntents.dismissNotification(habit))
.addAction(checkAction)
.addAction(addRepetitionAction)
.addAction(removeRepetitionAction)
.addAction(snoozeAction)
.addAction(cancelAction)
.setSound(ringtoneManager.getURI())
.extend(wearableExtender)
.setWhen(reminderTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ public class WidgetReceiver extends BroadcastReceiver
public static final String ACTION_REMOVE_REPETITION =
"org.isoron.uhabits.ACTION_REMOVE_REPETITION";

public static final String ACTION_CANCEL_REPETITION =
"org.isoron.uhabits.ACTION_CANCEL_REPETITION";

public static final String ACTION_TOGGLE_REPETITION =
"org.isoron.uhabits.ACTION_TOGGLE_REPETITION";

Expand Down Expand Up @@ -91,11 +88,6 @@ public void onReceive(final Context context, Intent intent)
controller.onRemoveRepetition(data.getHabit(),
data.getTimestamp());
break;

case ACTION_CANCEL_REPETITION:
controller.onCancelRepetition(data.getHabit(),
data.getTimestamp());
break;
}
}
catch (RuntimeException e)
Expand Down
2 changes: 2 additions & 0 deletions uhabits-android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,6 @@
<string name="example_question_boolean">e.g. Did you exercise today?</string>
<string name="question">Question</string>
<string name="target">Target</string>
<string name="yes">Yes</string>
<string name="no">No</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,20 @@ public WidgetBehavior(@NonNull HabitList habitList,

public void onAddRepetition(@NonNull Habit habit, Timestamp timestamp)
{
notificationTray.cancel(habit);
Repetition rep = habit.getRepetitions().getByTimestamp(timestamp);
if (rep != null) return;
performToggle(habit, timestamp);
notificationTray.cancel(habit);
}

public void onRemoveRepetition(@NonNull Habit habit, Timestamp timestamp)
{
notificationTray.cancel(habit);
Repetition rep = habit.getRepetitions().getByTimestamp(timestamp);
if (rep == null) return;
performToggle(habit, timestamp);
}

public void onCancelRepetition(@NonNull Habit habit, Timestamp timestamp)
{
onRemoveRepetition(habit, timestamp);
notificationTray.cancel(habit);
}

public void onToggleRepetition(@NonNull Habit habit, Timestamp timestamp)
{
performToggle(habit, timestamp);
Expand Down

0 comments on commit 9d48b4b

Please sign in to comment.