NotifManager - UQcsse3200/2024-studio-2 GitHub Wiki

1. Overview/Description

The Notification Manager is a utility for creating pop-up notifications at the top of the screen, providing feedback to users through different types of messages. It supports three types of notifications:

  1. Normal Notification: Used for general information.
  2. Error Notification: Used to display error messages.
  3. Success Notification: Used to confirm successful actions. The notifications appear as a banner at the top of the screen and automatically disappear after a short duration.


Figure 1: Success Notification


Figure 2: Success Notification

2. Implementation

The Notification Manager provides methods for displaying different types of notifications. Each notification is customizable based on the type, background, and content.

2.1 Normal Notifcation

To display a normal notification, the displayNotif method is used, which accepts the message contents and a boolean indicating whether the notification should be a success or error type. This method will determine the notification background and display it on the screen. The Timer.schedule will help to make the notification disappear after a few second.

    public static void displayNotif(String contents, Boolean isSucceed) {
        if (isSucceed) {
            notifTable.setBackground(new TextureRegionDrawable(new TextureRegion(notifSuccessBackground)));
        } else {
            notifTable.setBackground(new TextureRegionDrawable(new TextureRegion(notifErrorBackground)));
        }
        notifTable.setVisible(true);
        notifLabel.setText(contents);
        notifTable.addAction(Actions.moveTo(
                (Gdx.graphics.getWidth()- notifTable.getWidth())/2,
                Gdx.graphics.getHeight() / 2 + 300,
                dropDuration
        ));
        // Schedule the notification to disappear after 2 seconds
        Timer.schedule(new Timer.Task() {
            @Override
            public void run() {
                hideNotif();
            }
        }, dropDuration + 2);  // +2 seconds after the drop
    }

2.2 Success and Error Notification (Overload method)

The displayNotif method can be overloaded to show a default notification, typically a normal notification. This version accepts only the content parameter and automatically uses a neutral background.

public static void displayNotif(String contents) {
        notifTable.setBackground(new TextureRegionDrawable(new TextureRegion(notifNormalBackground)));
        notifTable.setVisible(true);
        notifLabel.setText(contents);
        notifTable.addAction(Actions.moveTo(
                (Gdx.graphics.getWidth()- notifTable.getWidth())/2,
                Gdx.graphics.getHeight() / 2 + 300,
                dropDuration
        ));
        // Schedule the notification to disappear after 2 seconds
        Timer.schedule(new Timer.Task() {
            @Override
            public void run() {
                hideNotif();
            }
        }, dropDuration + 2);  // +2 seconds after the drop
    }
⚠️ **GitHub.com Fallback** ⚠️