To complete Milestone 5, we will be adding at least 3 custom elements to the notifications
array. We will then print out the values of the notifications in the Debug Console for testing purposes.
Here’s how you can implement these requirements in your existing code:
Step 1: Modify the updateNotification
function
First, let's make the updateNotification
function populate the notifications
array with custom elements whenever it updates the favorites. We can also display these notifications on the screen if necessary.
Step 2: Add Elements to the Notifications List
Let's assume you want to add notifications when:
- A new favorite bird is added.
- The user navigates left.
- The user navigates right.
Now, update your code as follows:
var names = getColumn("100 Birds of the World", "Name");
var pictures = getColumn("100 Birds of the World", "Image of Bird");
var index = 0;
var favorites = [];
var notifications = [];
updateHomeScreen();
function updateHomeScreen() {
hideElement("notification_label");
setText("name_label", names[index]);
setImageURL("bird_Image", pictures[index]);
// Test Code
print("Current Notifications: " + notifications.join(", "));
}
onEvent("right_button", "click", function() {
if (index < names.length - 1) {
index = index + 1;
notifications.push("Navigated to next bird: " + names[index]);
}
updateHomeScreen();
updateNotification();
});
onEvent("left_button", "click", function() {
if (index > 0) {
index = index - 1;
notifications.push("Navigated to previous bird: " + names[index]);
}
updateHomeScreen();
updateNotification();
});
onEvent("favorite_button", "click", function() {
if (favorites.length < 3) {
favorites.push(names[index]);
notifications.push("Added to favorites: " + names[index]);
}
updateFavorites();
});
function updateFavorites() {
setText("favorites_output", favorites.join("\n\n"));
if (favorites.length > 3){
showElement("favorite_button");
} else {
hideElement("favorite_button");
}
updateNotification();
}
function updateNotification() {
showElement("notification_label");
// Display notifications on the screen or handle them as needed
setText("notification_label", notifications.join("\n"));
}
// If you want to add notifications on insert_button click or any other event, you can do it like so:
onEvent("insert_button", "click", function() {
// E.g., pushing a notification when button is clicked
notifications.push("Insert button clicked.");
// Call update favorites or any other logic as needed
updateFavorites();
});
Step 3: Testing
In the updateHomeScreen
function, I added a debug statement to print the current notifications to the console to ensure we can track what’s being added to the notifications list.
You should run this code and click the buttons to see the notifications being generated in response to different actions. Check the Debug Console to verify that the correct notifications are being printed.
Summary
In this code, we added functionality to track and display notifications related to user actions. Each relevant action (navigating left or right, adding to favorites) appends a message to the notifications
array, and we print or display these notifications accordingly. Make sure that you customize the notifications to suit the functionality of your application!