Search in sources :

Example 1 with FavoriteDishDao

use of de.tum.in.tumcampusapp.component.ui.cafeteria.FavoriteDishDao in project TumCampusApp by TCA-Team.

the class CafeteriaDetailsSectionFragment method showMenu.

/**
 * Inflates the cafeteria menu layout.
 * This is put into an extra static method to be able to
 * reuse it in {@link CafeteriaMenuCard}
 *
 * @param rootView    Parent layout
 * @param cafeteriaId Cafeteria id
 * @param dateStr     Date in yyyy-mm-dd format
 * @param big         True to show big lines
 */
@SuppressLint("ShowToast")
public static List<View> showMenu(LinearLayout rootView, int cafeteriaId, String dateStr, boolean big, List<CafeteriaMenu> cafeteriaMenus) {
    // initialize a few often used things
    final Context context = rootView.getContext();
    final Map<String, String> rolePrices = CafeteriaPrices.INSTANCE.getRolePrices(context);
    final int padding = (int) context.getResources().getDimension(R.dimen.card_text_padding);
    List<View> addedViews = new ArrayList<>(32);
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    TcaDb db = TcaDb.getInstance(context);
    final FavoriteDishDao favoriteDishDao = db.favoriteDishDao();
    TextView textview;
    if (!big) {
        // Show opening hours
        OpenHoursHelper lm = new OpenHoursHelper(context);
        textview = new TextView(context);
        textview.setText(lm.getHoursByIdAsString(context, cafeteriaId, DateUtils.getDate(dateStr)));
        textview.setTextColor(ContextCompat.getColor(context, R.color.sections_green));
        rootView.addView(textview);
        addedViews.add(textview);
    }
    // Show cafeteria menu
    String curShort = "";
    for (CafeteriaMenu cafeteriaMenu : cafeteriaMenus) {
        String typeShort = cafeteriaMenu.getTypeShort();
        String typeLong = cafeteriaMenu.getTypeLong();
        // Skip unchecked categories if showing card
        boolean shouldShow = Utils.getSettingBool(context, "card_cafeteria_" + typeShort, "tg".equals(typeShort) || "ae".equals(typeShort));
        if (!big && !shouldShow) {
            continue;
        }
        // Add header if we start with a new category
        if (!typeShort.equals(curShort)) {
            curShort = typeShort;
            View view = inflater.inflate(big ? R.layout.list_header_big : R.layout.card_list_header, rootView, false);
            textview = view.findViewById(R.id.list_header);
            textview.setText(typeLong.replaceAll("[0-9]", "").trim());
            rootView.addView(view);
            addedViews.add(view);
        }
        // Show menu item
        String menuName = cafeteriaMenu.getName();
        final SpannableString text = menuToSpan(context, big ? menuName : prepare(menuName));
        if (rolePrices.containsKey(typeLong)) {
            // If price is available
            View view = inflater.inflate(big ? R.layout.price_line_big : R.layout.card_price_line, rootView, false);
            textview = view.findViewById(R.id.line_name);
            TextView priceView = view.findViewById(R.id.line_price);
            final View favDish = view.findViewById(R.id.favoriteDish);
            favDish.setTag(menuName + "__" + cafeteriaId);
            /*
                         * saved dish id in the favoriteDishButton tag.
                         * onButton checked getTag->DishID and mark it as favorite locally (favorite=1)
                         */
            textview.setText(text);
            priceView.setText(String.format("%s €", rolePrices.get(typeLong)));
            rootView.addView(view);
            addedViews.add(view);
            Object tag = favDish.getTag();
            List<FavoriteDish> isFavourite = favoriteDishDao.checkIfFavoriteDish(tag.toString());
            favDish.setSelected(!isFavourite.isEmpty());
            favDish.setOnClickListener(view1 -> {
                String id = view1.getTag().toString();
                String[] data = id.split("__");
                String dishName = data[0];
                int mensaId = Integer.parseInt(data[1]);
                if (!view1.isSelected()) {
                    DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MM-yyyy");
                    String currentDate = DateTime.now().toString(formatter);
                    favoriteDishDao.insertFavouriteDish(FavoriteDish.Companion.create(mensaId, dishName, currentDate, tag.toString()));
                    view1.setSelected(true);
                } else {
                    favoriteDishDao.deleteFavoriteDish(mensaId, dishName);
                    view1.setSelected(false);
                }
            });
        } else {
            // Without price
            textview = new TextView(context);
            textview.setText(text);
            textview.setPadding(padding, padding, padding, padding);
            rootView.addView(textview);
            addedViews.add(textview);
        }
    }
    return addedViews;
}
Also used : Context(android.content.Context) FavoriteDishDao(de.tum.in.tumcampusapp.component.ui.cafeteria.FavoriteDishDao) CafeteriaMenu(de.tum.in.tumcampusapp.component.ui.cafeteria.model.CafeteriaMenu) FavoriteDish(de.tum.in.tumcampusapp.component.ui.cafeteria.model.FavoriteDish) ArrayList(java.util.ArrayList) SpannableString(android.text.SpannableString) View(android.view.View) TextView(android.widget.TextView) SuppressLint(android.annotation.SuppressLint) SpannableString(android.text.SpannableString) LayoutInflater(android.view.LayoutInflater) TextView(android.widget.TextView) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) TcaDb(de.tum.in.tumcampusapp.database.TcaDb) SuppressLint(android.annotation.SuppressLint)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 Context (android.content.Context)1 SpannableString (android.text.SpannableString)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 TextView (android.widget.TextView)1 FavoriteDishDao (de.tum.in.tumcampusapp.component.ui.cafeteria.FavoriteDishDao)1 CafeteriaMenu (de.tum.in.tumcampusapp.component.ui.cafeteria.model.CafeteriaMenu)1 FavoriteDish (de.tum.in.tumcampusapp.component.ui.cafeteria.model.FavoriteDish)1 TcaDb (de.tum.in.tumcampusapp.database.TcaDb)1 ArrayList (java.util.ArrayList)1 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)1