Skip to content

Commit

Permalink
Merge pull request #93 from pennlabs/dining-fragment
Browse files Browse the repository at this point in the history
Dining fragment
  • Loading branch information
adelq committed Nov 23, 2014
2 parents a9dd12c + 635fea0 commit 582d9c4
Show file tree
Hide file tree
Showing 13 changed files with 439 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -31,13 +34,15 @@ public class DiningFragment extends ListFragment {
private ArrayList<DiningHall> mDiningHalls;
private DiningAdapter mAdapter;
private Activity mActivity;
public static Fragment mFragment;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAPI = new DiningAPI();
mActivity = getActivity();
mDiningHalls = new ArrayList<DiningHall>();
mFragment = this;
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);

View view = getActivity().getCurrentFocus();
Expand All @@ -59,6 +64,24 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
return inflater.inflate(R.layout.fragment_dining, container, false);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
DiningHall diningHall = (DiningHall) v.getTag();
if (diningHall.hasMenu()) {
Fragment fragment = new MenuFragment();

Bundle args = new Bundle();
args.putParcelable("DiningHall", (Parcelable) v.getTag());
fragment.setArguments(args);

FragmentManager fragmentManager = this.getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.dining_fragment, fragment)
.addToBackStack(null)
.commit();
onResume();
}
}
private class GetOpenTask extends AsyncTask<Void, Void, Void> {

@Override
Expand All @@ -72,7 +95,7 @@ protected Void doInBackground(Void... params) {
int id = venue.getInt("id");
String name = venue.getString("name");
boolean isResidential = venue.getString("venueType").equals("residential") && !name.equals("Cafe at McClelland");
boolean hasMenu = !venue.getString("dailyMenuURL").isEmpty();
boolean hasMenu = hasMenu(venue);
JSONArray hours = venue.getJSONArray("dateHours");
mDiningHalls.add(new DiningHall(id, name, isResidential, hasMenu, hours));
}
Expand All @@ -90,6 +113,25 @@ protected void onPostExecute(Void params) {
}
}

private boolean hasMenu(JSONObject venue) {
try {
if (venue.getString("dailyMenuURL").isEmpty()) {
return false;
} else {
JSONObject meals = mAPI.getDailyMenu(venue.getInt("id")).getJSONObject("Document")
.getJSONObject("tblMenu");
if (meals.length() == 0) {
return false;
}
}
} catch (JSONException ignored) {
return false;
} catch (NullPointerException ignored) {
return false;
}
return true;
}

private class GetMenusTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pennapps.labs.pennmobile;

import android.os.StrictMode;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
Expand Down Expand Up @@ -29,6 +30,9 @@ protected void onCreate(Bundle savedInstanceState) {
Crashlytics.start(this);
setContentView(R.layout.activity_main);

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

mFeatureTitles = new String[]{"Home", "Registrar", "Directory", "Dining", "Transit", "News", "About"};
int[] icons = new int[]{R.drawable.ic_home, R.drawable.ic_book, R.drawable.ic_contacts,
R.drawable.ic_restaurant, R.drawable.ic_directions_bus, R.drawable.ic_announcement,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package com.pennapps.labs.pennmobile;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pennapps.labs.pennmobile.api.DiningAPI;
import com.pennapps.labs.pennmobile.classes.DiningHall;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class MenuFragment extends Fragment {

private DiningAPI mAPI;
private TextView diningHallNameTV;
private DiningHall mDiningHall;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAPI = new DiningAPI();
mDiningHall = getArguments().getParcelable("DiningHall");
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_menu, container, false);
v.setBackgroundColor(Color.WHITE);
diningHallNameTV = (TextView) v.findViewById(R.id.dining_hall_name);
diningHallNameTV.setText(mDiningHall.getName());
diningHallNameTV.setGravity(Gravity.LEFT);
LinearLayout menu_layout = (LinearLayout) v.findViewById(R.id.main_menu_layout);
fillDescriptions(v);
try {
if (mDiningHall.isResidential() && mDiningHall.hasMenu()) {
JSONObject resultObj = mAPI.getDailyMenu(mDiningHall.getId());

JSONArray meals = resultObj.getJSONObject("Document")
.getJSONObject("tblMenu")
.getJSONArray("tblDayPart");

for (int i = 0; i < meals.length(); i++) {
JSONObject meal = meals.getJSONObject(i);
parseMeal(meal, mDiningHall);
}
}
} catch (JSONException e) {
}
return v;
}

private void parseMeal(JSONObject meal, DiningHall diningHall) {
try {
String mealName = meal.getString("txtDayPartDescription");

JSONArray stations = new JSONArray();
try {
stations = meal.getJSONArray("tblStation");
} catch (JSONException e) {
JSONObject stationsObject = meal.getJSONObject("tblStation");
stations.put(stationsObject);
}
HashMap<String, String> currentMenu = new HashMap<String, String>();
for (int j = 0; j < stations.length(); j++) {
JSONObject station = stations.getJSONObject(j);
parseStation(station, currentMenu);
}

if (mealName != null) {
diningHall.menus.put(mealName, currentMenu);
}
} catch (JSONException ignored) {

}
}

private void parseStation(JSONObject station, HashMap<String, String> menu) {
try {
String stationName = station.getString("txtStationDescription");
JSONArray stationItems = new JSONArray();
try {
stationItems = station.getJSONArray("tblItem");
} catch (JSONException e) {
JSONObject stationItem = station.getJSONObject("tblItem");
stationItems.put(stationItem);
}
for (int k = 0; k < stationItems.length(); k++) {
JSONObject foodItem = stationItems.getJSONObject(k);
String foodName = foodItem.getString("txtTitle");
foodName = StringEscapeUtils.unescapeHtml4(foodName);
if (menu.containsKey(stationName)) {
menu.put(stationName, menu.get(stationName) + ", " + foodName);
} else {
menu.put(stationName, foodName);
}
}
} catch (JSONException ignored) {

}
}

public void fillDescriptions(View view) {
DiningHall diningHall = mDiningHall;
TextView hallNameTV = (TextView) view.findViewById(R.id.dining_hall_name);
TextView hallStatus = (TextView) view.findViewById(R.id.dining_hall_status);
TextView breakfastMenuTV = (TextView) view.findViewById(R.id.dining_hall_breakfast);
TextView brunchMenuTV = (TextView) view.findViewById(R.id.dining_hall_brunch);
TextView lunchMenuTV = (TextView) view.findViewById(R.id.dining_hall_lunch);
TextView dinnerMenuTV = (TextView) view.findViewById(R.id.dining_hall_dinner);

hallNameTV.setText(WordUtils.capitalizeFully(diningHall.getName()));
view.setTag(diningHall);
if (diningHall.isOpen()) {
hallStatus.setText("Open");
hallStatus.setBackground(view.getContext().getResources().getDrawable(R.drawable.label_green));
} else {
hallStatus.setText("Closed");
hallStatus.setBackground(view.getContext().getResources().getDrawable(R.drawable.label_red));
}

for (Map.Entry<String, HashMap<String, String>> menu : diningHall.menus.entrySet()) {
String mealName = StringUtils.capitalize(menu.getKey());
String menuText = "";
for (Map.Entry<String, String> menuItem : menu.getValue().entrySet()) {
String key = StringUtils.capitalize(menuItem.getKey());
String value = menuItem.getValue();
menuText += key + ": " + value + "\n";
}
if (mealName.equals("Breakfast")) {
view.findViewById(R.id.dining_hall_breakfast_title).setVisibility(View.VISIBLE);
breakfastMenuTV.setVisibility(View.VISIBLE);
breakfastMenuTV.setText(menuText);
} else if (mealName.equals("Brunch")) {
view.findViewById(R.id.dining_hall_brunch_title).setVisibility(View.VISIBLE);
brunchMenuTV.setVisibility(View.VISIBLE);
brunchMenuTV.setText(menuText);
} else if (mealName.equals("Lunch")) {
view.findViewById(R.id.dining_hall_lunch_title).setVisibility(View.VISIBLE);
lunchMenuTV.setVisibility(View.VISIBLE);
lunchMenuTV.setText(menuText);
} else if (mealName.equals("Dinner")) {
view.findViewById(R.id.dining_hall_dinner_title).setVisibility(View.VISIBLE);
dinnerMenuTV.setVisibility(View.VISIBLE);
dinnerMenuTV.setText(menuText);
}
}
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}

@Override
public void onResume() {
super.onResume();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.pennapps.labs.pennmobile.classes.DiningHall;
import com.pennapps.labs.pennmobile.R;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;

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

public class DiningAdapter extends ArrayAdapter<DiningHall> {

Expand All @@ -32,46 +30,46 @@ public View getView(int position, View convertView, ViewGroup parent) {

TextView hallNameTV = (TextView) view.findViewById(R.id.dining_hall_name);
TextView hallStatus = (TextView) view.findViewById(R.id.dining_hall_status);
TextView breakfastMenuTV = (TextView) view.findViewById(R.id.dining_hall_breakfast);
TextView brunchMenuTV = (TextView) view.findViewById(R.id.dining_hall_brunch);
TextView lunchMenuTV = (TextView) view.findViewById(R.id.dining_hall_lunch);
TextView dinnerMenuTV = (TextView) view.findViewById(R.id.dining_hall_dinner);
TextView openMeal = (TextView) view.findViewById(R.id.dining_hall_open_meal);
TextView openClose = (TextView) view.findViewById(R.id.dining_hall_open_close);
ImageView menuArrow = (ImageView) view.findViewById(R.id.dining_hall_menu_indicator);
menuArrow.setVisibility(View.GONE);
view.findViewById(R.id.dining_hall_open_meal).setVisibility(View.VISIBLE);
view.findViewById(R.id.dining_hall_open_close).setVisibility(View.VISIBLE);

hallNameTV.setText(WordUtils.capitalizeFully(diningHall.getName()));
view.setTag(diningHall);

if (diningHall.isOpen()) {
hallStatus.setText("Open");
hallStatus.setBackground(getContext().getResources().getDrawable(R.drawable.label_green));
if (!diningHall.openMeal().equals("all")) {
openMeal.setText("Currently serving " + diningHall.openMeal());
} else {
view.findViewById(R.id.dining_hall_open_meal).setVisibility(View.GONE);
}
openClose.setText("Closes at " + diningHall.closingTime());
} else {
hallStatus.setText("Closed");
hallStatus.setBackground(getContext().getResources().getDrawable(R.drawable.label_red));
}

for (Map.Entry<String, HashMap<String, String>> menu : diningHall.menus.entrySet()) {
String mealName = StringUtils.capitalize(menu.getKey());
String menuText = "";
for (Map.Entry<String, String> menuItem : menu.getValue().entrySet()) {
String key = StringUtils.capitalize(menuItem.getKey());
String value = menuItem.getValue();
menuText += key + ": " + value + "\n";
String meal = diningHall.nextMeal();
if (meal.equals("") || meal.equals("all")) {
view.findViewById(R.id.dining_hall_open_meal).setVisibility(View.GONE);
} else {
openMeal.setText("Next serving " + meal);
}
if (mealName.equals("Breakfast")) {
view.findViewById(R.id.dining_hall_breakfast_title).setVisibility(View.VISIBLE);
breakfastMenuTV.setVisibility(View.VISIBLE);
breakfastMenuTV.setText(menuText);
} else if (mealName.equals("Brunch")) {
view.findViewById(R.id.dining_hall_brunch_title).setVisibility(View.VISIBLE);
brunchMenuTV.setVisibility(View.VISIBLE);
brunchMenuTV.setText(menuText);
} else if (mealName.equals("Lunch")) {
view.findViewById(R.id.dining_hall_lunch_title).setVisibility(View.VISIBLE);
lunchMenuTV.setVisibility(View.VISIBLE);
lunchMenuTV.setText(menuText);
} else if (mealName.equals("Dinner")) {
view.findViewById(R.id.dining_hall_dinner_title).setVisibility(View.VISIBLE);
dinnerMenuTV.setVisibility(View.VISIBLE);
dinnerMenuTV.setText(menuText);
String openingTime = diningHall.openingTime();
if (openingTime.equals("")) {
view.findViewById(R.id.dining_hall_open_close).setVisibility(View.GONE);
} else {
openClose.setText("Opens at " + diningHall.openingTime());
}
}

if (diningHall.hasMenu()) {
menuArrow.setVisibility(View.VISIBLE);
}

this.sort(new MenuComparator());
return view;
}
Expand Down
Loading

0 comments on commit 582d9c4

Please sign in to comment.