package com.novoda.downloadmanager;

import android.app.Notification;
import android.content.Context;
import android.support.v4.app.NotificationCompat;

class NotificationCreator<T> {

    private final Context applicationContext;
    private final NotificationCustomizer<T> notificationCustomizer;
    private NotificationChannelProvider notificationChannelProvider;

    NotificationCreator(Context context, NotificationCustomizer<T> customizer, NotificationChannelProvider notificationChannelProvider) {
        this.applicationContext = context.getApplicationContext();
        this.notificationCustomizer = customizer;
        this.notificationChannelProvider = notificationChannelProvider;
    }

    void setNotificationChannelProvider(NotificationChannelProvider notificationChannelProvider) {
        this.notificationChannelProvider = notificationChannelProvider;
    }

    private int notificationId;
    private NotificationCompat.Builder builder;

    NotificationInformation createNotification(final T notificationPayload) {
        return new NotificationInformation() {
            @Override
            public int getId() {
                if (notificationPayload instanceof DownloadBatchStatus) {
                    return ((DownloadBatchStatus) notificationPayload).getDownloadBatchId().hashCode();
                }

                return notificationPayload.hashCode();
            }

            @Override
            public Notification getNotification() {
                int newNotificationId = getId();

                if (builder == null || newNotificationId != notificationId) {
                    builder = new NotificationCompat.Builder(applicationContext, notificationChannelProvider.channelId());
                    notificationId = newNotificationId;
                }

                return notificationCustomizer.customNotificationFrom(builder, notificationPayload);
            }

            @Override
            public NotificationCustomizer.NotificationDisplayState notificationDisplayState() {
                return notificationCustomizer.notificationDisplayState(notificationPayload);
            }
        };
    }
}

@@ -890,17 +890,25 @@
 ate int 
-n
+previousN
 otificat
@@ -910,24 +910,118 @@
 ficationId;%0A
+    private NotificationCustomizer.NotificationDisplayState previousNotificationDisplayState;%0A
     private 
@@ -1599,19 +1599,16 @@
    int n
-ewN
 otificat
@@ -1623,16 +1623,135 @@
 getId();
+%0A                NotificationCustomizer.NotificationDisplayState notificationDisplayState = notificationDisplayState();
 %0A%0A      
@@ -1783,19 +1783,35 @@
 null %7C%7C 
-new
+hasChanged(previous
 Notifica
@@ -1820,11 +1820,9 @@
 onId
- !=
+,
  not
@@ -1830,18 +1830,29 @@
 fication
-Id
+DisplayState)
 ) %7B%0A    
@@ -1990,17 +1990,25 @@
         
-n
+previousN
 otificat
@@ -2020,11 +2020,8 @@
  = n
-ewN
 otif
@@ -2025,24 +2025,105 @@
 tificationId
+;%0A                    previousNotificationDisplayState = notificationDisplayState
 ;%0A          
@@ -2238,32 +2238,329 @@
 %0A            %7D%0A%0A
+            private boolean hasChanged(int newNotificationId, NotificationCustomizer.NotificationDisplayState newNotificationDisplayState) %7B%0A                return previousNotificationId != newNotificationId %7C%7C !previousNotificationDisplayState.equals(newNotificationDisplayState);%0A            %7D%0A%0A
             @Ove

Use the display state when determining whether to change the display state.