Skip to content

Commit

Permalink
Merge pull request #75 from uOttawaSEGA2022/pendingOrders
Browse files Browse the repository at this point in the history
Pending orders
  • Loading branch information
amyhuang8 authored Nov 24, 2022
2 parents 71cc2d4 + 2e95f15 commit 70ec22e
Show file tree
Hide file tree
Showing 13 changed files with 440 additions and 40 deletions.
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".ui.screens.PendingOrdersScreen"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>

<meta-data
android:name="preloaded_fonts"
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/example/mealer_project/app/AppInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.example.mealer_project.data.models.inbox.AdminInbox;
import com.example.mealer_project.data.models.orders.OrderItem;
import com.example.mealer_project.data.sources.FirebaseRepository;
import com.example.mealer_project.ui.screens.PendingOrdersScreen;
import com.example.mealer_project.ui.screens.meals.MealsListScreen;
import com.example.mealer_project.utils.Preconditions;
import com.google.firebase.auth.FirebaseAuth;
Expand All @@ -22,6 +23,7 @@ public class AppInstance {
private User user;
private AdminInbox adminInbox;
private MealsListScreen mealsListScreen;
private PendingOrdersScreen pendingOrdersScreen;

public AppInstance() {
this.initializeApp();
Expand Down Expand Up @@ -91,6 +93,14 @@ public void setMealsListScreen(MealsListScreen mealsListScreen) {
this.mealsListScreen = mealsListScreen;
}

public PendingOrdersScreen getPendingOrdersScreen() {
return pendingOrdersScreen;
}

public void setPendingOrdersScreen(PendingOrdersScreen pendingOrdersScreen) {
this.pendingOrdersScreen = pendingOrdersScreen;
}

/**
* Method to get logged in client's cart
* @return Map containing order items if user is validated successfully, else an empty map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import com.example.mealer_project.utils.Response;
import com.example.mealer_project.utils.Result;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Orders {
Expand Down Expand Up @@ -52,6 +54,64 @@ public Result<Order, String> getOrder(@NonNull String orderID) {
}
}

/**
* Method to retrieve a list containing all Orders by the Chef
* @return a List containing Order objects
*/
public List<Order> getAllOrders() {
return new ArrayList<Order>(this.orders.values());
};

/**
* Method to retrieve a list containing all pending Orders by the Chef
* @return a List containing Order objects
*/
public List<Order> getPendingOrders() {

// Variable Declaration
ArrayList<Order> pendingList = new ArrayList<Order>();

// Process: looping through orders
for (Order order : this.orders.values()) {

// Process: checking if pending
if (order.getIsPending()) {

pendingList.add(order); //adding to list

}

}

// Output
return pendingList;

};

/**
* Method to retrieve a list containing all completed Orders by the Chef
* @return a List containing Order objects
*/
public List<Order> getCompletedOrders() {
// Variable Declaration
ArrayList<Order> completedList = new ArrayList<Order>();

// Process: looping through orders
for (Order order : this.orders.values()) {

// Process: checking if completed
if (order.getIsCompleted()) {

completedList.add(order); //adding to list

}

}

// Output
return completedList;
};

public Response removeOrder(@NonNull String orderId) {
// guard-clause
if (Preconditions.isNotEmptyString(orderId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public MealInfo (Meal meal) {

public void setPrice(double price) { this.price = price; }

public double getQuantity() { return quantity; }
public int getQuantity() { return quantity; }

public void setQuantity(int quantity) { this.quantity = quantity; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ public void onComplete(@NonNull Task<QuerySnapshot> task) {
loginScreen.showNextScreen();
} else {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.e("loadMeals", "creating meals");
//Log.e("loadMeals", "creating meals");
database.collection(MEALS_COLLECTION)
.document(document.getId())
.collection(CHEF_MEALS_COLLECTION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,19 @@ protected Order makeOrderFromFirebase(DocumentSnapshot document){
Log.e("DATE", "makeOrderFromFirebase: Error parsing date");
}

newOrder.setMeals((Map<String,MealInfo>)(document.getData().get("meals")));
Map<String,Object> mealsData = (Map<String, Object>) document.getData().get("meals");
Map<String,MealInfo> meals = new HashMap<>();

for (Map.Entry<String, Object> entry : mealsData.entrySet()) {
String key = entry.getKey();
Map<String,Object> value = (Map<String,Object>) entry.getValue();

MealInfo mealInfo = new MealInfo((String) value.get("name"), ((Number)value.get("price")).doubleValue(), ((Number)value.get("quantity")).intValue());

meals.put(key, mealInfo);
}

newOrder.setMeals(meals);

return newOrder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void onClick(View view) {
viewPendingOrder.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
displayErrorToast("No pending orders yet!");
startActivity(new Intent(getApplicationContext(), PendingOrdersScreen.class)); //show pending orders
}
});

Expand All @@ -108,15 +108,9 @@ public void updateUI() {

}

/**
* this method goes to the add new meal screen
*/
@Override
public void showNextScreen() {

Intent intent = new Intent(getApplicationContext(), NewMealScreen.class);
startActivity(intent);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.example.mealer_project.ui.screens;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.mealer_project.R;
import com.example.mealer_project.data.models.Order;
import com.example.mealer_project.data.models.orders.MealInfo;

import java.util.List;

public class PendingOrdersAdapter extends ArrayAdapter<Order> {

/**
* Constructor
* @param context The current context.
* @param resource The resource ID for a layout file containing a TextView to use when instantiating views.
* @param objects The objects to represent in the ListView.
*/
public PendingOrdersAdapter(@NonNull Context context, int resource, @NonNull List<Order> objects) {
super(context, resource, objects);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

// Variable Declaration: getting order data item for given position
Order order = getItem(position);
String mealNames = "";
String quantities = "";

// Process: checking if existing view is being reused
if (convertView == null) { //must inflate view
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_pending_orders_list_item, parent, false);
}

// Process: traversing entire meals map
for (MealInfo mI : order.getMeals().values()) {

mealNames += mI.getName() + "\n";
quantities += mI.getQuantity() + "\n";

}

// Process: setting the order info to appear on the screen
((TextView) convertView.findViewById(R.id.clientNameText)).setText(order.getClientInfo().getClientName());
((TextView) convertView.findViewById(R.id.mealNameText)).setText(mealNames);
((TextView) convertView.findViewById(R.id.quantityText)).setText(quantities);
((TextView) convertView.findViewById(R.id.dateText)).setText(order.getOrderDate().toString());
((Button) convertView.findViewById(R.id.rejectButton)).setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something

}
});
((Button) convertView.findViewById(R.id.acceptButton)).setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something

}
});

return convertView;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.example.mealer_project.ui.screens;

import android.os.Bundle;
import android.util.Log;
import android.widget.ImageButton;
import android.widget.ListView;

import com.example.mealer_project.R;
import com.example.mealer_project.app.App;
import com.example.mealer_project.data.models.Chef;
import com.example.mealer_project.data.models.Order;
import com.example.mealer_project.ui.core.UIScreen;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PendingOrdersScreen extends UIScreen {

// Variable Declaration
/**
* the map that contains the current CHEF's Orders
*/
private List<Order> ordersData;

/**
* the list that will hold the orders
*/
private List<Order> orders;

/**
* the array adapter for the list view of the pending orders
*/
private PendingOrdersAdapter pendingOrdersAdapter;

/**
* the back button icon
*/
ImageButton backButton;

//----------------------------------------------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pending_orders_screen);

// Initialization
ordersData = new ArrayList<Order>();
backButton = findViewById(R.id.backButton);

// Process: loading the Orders data
loadPendingOrdersData();

// Process: populating the Orders ListView
populatePendingOrdersList();

// Process: setting onClick method for back button
backButton.setOnClickListener(v -> finish());

}

/**
* this helper method retrieves the current CHEF's Orders
*/
private void loadPendingOrdersData() {

// Process: checking if current user is a CHEF
if (App.getUser() instanceof Chef) { //is CHEF
// Initialization: setting ordersData to the map of orderIDs & Orders
this.ordersData = ((Chef) App.getUser()).ORDERS.getPendingOrders();
}
else { //not a chef -> error-handling
Log.e("PendingOrdersScreen", "Can't show pending offered; Current logged-in user is not a CHEF");

// Output
displayErrorToast("No pending orders available to be displayed!");
}

}

/**
* this helper method populates the Orders list
*/
private void populatePendingOrdersList() {

// Variable Declaration
this.orders = new ArrayList<Order>();
ListView pendingOrdersList = findViewById(R.id.pendingListView);

// Initialization: setting the adapter
pendingOrdersAdapter = new PendingOrdersAdapter(this, R.layout.activity_pending_orders_list_item, this.orders);

// Process: attaching the adapter to the ListView
pendingOrdersList.setAdapter(pendingOrdersAdapter);

// Process: looping through the map of data
for (Order order: this.ordersData) {
pendingOrdersAdapter.add(order); //adding the orderData to the list
}

}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_chef_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="25dp"
android:text="@string/view_all_orders_label"
android:text="@string/view_completed_orders_label"
android:visibility="visible"
tools:ignore="TextContrastCheck" />

Expand Down
Loading

0 comments on commit 70ec22e

Please sign in to comment.