Search in sources :

Example 1 with CafeteriaMenu

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

the class FavoriteDishAlarmScheduler method onReceive.

/**
 * Can either receive a date or a boolean cancelNotifications value. This way other activities
 * can close the currently opened notifications and it is possible to schedule dates, where the
 * alarm has to check for favorite dishes.
 *
 * @param context
 * @param extra   Extra can either be "cancelNotifications" or a date, when the alarm should check, if there are any
 *                favorite dishes at a given date.
 */
@Override
public void onReceive(Context context, Intent extra) {
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    cancelFoodNotifications(mNotificationManager);
    if (extra.getBooleanExtra("cancelNotifications", false)) {
        return;
    }
    String triggeredAt = extra.getStringExtra("triggeredAt");
    Calendar triggeredCal = Calendar.getInstance();
    triggeredCal.setTime(DateUtils.getDate(triggeredAt));
    CafeteriaMenuManager cm = new CafeteriaMenuManager(context);
    HashMap<Integer, HashSet<CafeteriaMenu>> scheduledNow = cm.getServedFavoritesAtDate(triggeredAt);
    if (scheduledNow == null) {
        Utils.log("FavoriteDishAlarmScheduler: Scheduled now is null, onReceived aborted");
        return;
    }
    CafeteriaDao dao = TcaDb.getInstance(context).cafeteriaDao();
    for (Integer mensaId : scheduledNow.keySet()) {
        StringBuilder message = new StringBuilder();
        int menuCount = 0;
        for (CafeteriaMenu menu : scheduledNow.get(mensaId)) {
            message.append(menu.getName()).append('\n');
            menuCount++;
        }
        ACTIVE_NOTIFICATIONS.add(mensaId);
        String mensaName = dao.getMensaNameFromId(mensaId);
        Intent intent = new Intent(context, CafeteriaActivity.class);
        intent.putExtra(Const.MENSA_FOR_FAVORITEDISH, mensaId);
        PendingIntent pi = PendingIntent.getActivity(context, mensaId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, Const.NOTIFICATION_CHANNEL_CAFETERIA).setSmallIcon(R.drawable.ic_notification).setContentTitle(mensaName + (menuCount > 1 ? " (" + menuCount + ")" : "")).setStyle(new NotificationCompat.BigTextStyle().bigText(message.toString())).setContentText(message.toString()).setAutoCancel(true).setLargeIcon(Utils.getLargeIcon(context, R.drawable.ic_cutlery)).setContentIntent(pi).setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(true);
        mNotificationManager.notify(IDENTIFIER_STRING, mensaId, mBuilder.build());
    }
}
Also used : CafeteriaMenu(de.tum.in.tumcampusapp.component.ui.cafeteria.model.CafeteriaMenu) NotificationManager(android.app.NotificationManager) Calendar(java.util.Calendar) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) CafeteriaMenuManager(de.tum.in.tumcampusapp.component.ui.cafeteria.controller.CafeteriaMenuManager) CafeteriaDao(de.tum.in.tumcampusapp.component.ui.cafeteria.CafeteriaDao) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent) HashSet(java.util.HashSet)

Example 2 with CafeteriaMenu

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

the class CafeteriaMenuManager method getServedFavoritesAtDate.

/**
 * This method returns all the mensas serving favorite dishes at a given day and their unique
 * dishes
 *
 * @param dayMonthYear String with ISO-Date (yyyy-mm-dd)
 * @return the favourite dishes at the given date
 */
public HashMap<Integer, HashSet<CafeteriaMenu>> getServedFavoritesAtDate(String dayMonthYear) {
    HashMap<Integer, HashSet<CafeteriaMenu>> cafeteriaServedDish = new HashMap<>();
    List<CafeteriaMenu> upcomingServings = favoriteDishDao.getFavouritedCafeteriaMenuOnDate(dayMonthYear);
    for (CafeteriaMenu upcomingServing : upcomingServings) {
        int mensaId = upcomingServing.getCafeteriaId();
        HashSet<CafeteriaMenu> servedAtCafeteria;
        if (cafeteriaServedDish.containsKey(mensaId)) {
            servedAtCafeteria = cafeteriaServedDish.get(mensaId);
        } else {
            servedAtCafeteria = new HashSet<>();
            cafeteriaServedDish.put(mensaId, servedAtCafeteria);
        }
        servedAtCafeteria.add(upcomingServing);
    }
    return cafeteriaServedDish;
}
Also used : CafeteriaMenu(de.tum.in.tumcampusapp.component.ui.cafeteria.model.CafeteriaMenu) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 3 with CafeteriaMenu

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

the class MensaRemoteViewFactory method getViewAt.

@Override
public RemoteViews getViewAt(int position) {
    CafeteriaMenu currentItem = mensaMenu.get(position);
    if (currentItem == null) {
        return null;
    }
    RemoteViews rv = new RemoteViews(applicationContext.getPackageName(), R.layout.mensa_widget_item);
    String menuContent = COMPILE.matcher(currentItem.getName()).replaceAll("").trim();
    rv.setTextViewText(R.id.menu_content, menuContent + " (" + currentItem.getTypeShort() + ")");
    String price = CafeteriaPrices.INSTANCE.getPrice(applicationContext, currentItem.getTypeLong());
    if (price == null) {
        price = "____";
    }
    rv.setTextViewText(R.id.menu_price, price + " €");
    return rv;
}
Also used : CafeteriaMenu(de.tum.in.tumcampusapp.component.ui.cafeteria.model.CafeteriaMenu) RemoteViews(android.widget.RemoteViews)

Example 4 with CafeteriaMenu

use of de.tum.in.tumcampusapp.component.ui.cafeteria.model.CafeteriaMenu 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)

Example 5 with CafeteriaMenu

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

the class CafeteriaMenuCard method fillNotification.

@Override
protected Notification fillNotification(NotificationCompat.Builder notificationBuilder) {
    Map<String, String> rolePrices = CafeteriaPrices.INSTANCE.getRolePrices(mContext);
    NotificationCompat.WearableExtender morePageNotification = new NotificationCompat.WearableExtender();
    StringBuilder allContent = new StringBuilder();
    StringBuilder firstContent = new StringBuilder();
    for (CafeteriaMenu menu : mMenus) {
        if ("bei".equals(menu.getTypeShort())) {
            continue;
        }
        NotificationCompat.Builder pageNotification = new NotificationCompat.Builder(mContext, Const.NOTIFICATION_CHANNEL_CAFETERIA).setContentTitle(PATTERN.matcher(menu.getTypeLong()).replaceAll("").trim());
        pageNotification.setSmallIcon(R.drawable.ic_notification);
        pageNotification.setLargeIcon(Utils.getLargeIcon(mContext, R.drawable.ic_cutlery));
        StringBuilder content = new StringBuilder(menu.getName());
        if (rolePrices.containsKey(menu.getTypeLong())) {
            content.append('\n').append(rolePrices.get(menu.getTypeLong())).append(" €");
        }
        String contentString = COMPILE.matcher(content.toString()).replaceAll("").trim();
        pageNotification.setContentText(contentString);
        if ("tg".equals(menu.getTypeShort())) {
            if (!allContent.toString().isEmpty()) {
                allContent.append('\n');
            }
            allContent.append(contentString);
        }
        if (firstContent.toString().isEmpty()) {
            firstContent.append(COMPILE.matcher(menu.getName()).replaceAll("").trim()).append('…');
        } else {
            morePageNotification.addPage(pageNotification.build());
        }
    }
    notificationBuilder.setWhen(mDate.getTime());
    notificationBuilder.setContentText(firstContent);
    notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(allContent));
    Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.wear_cafeteria);
    morePageNotification.setBackground(bm);
    return morePageNotification.extend(notificationBuilder).build();
}
Also used : CafeteriaMenu(de.tum.in.tumcampusapp.component.ui.cafeteria.model.CafeteriaMenu) Bitmap(android.graphics.Bitmap) NotificationCompat(android.support.v4.app.NotificationCompat)

Aggregations

CafeteriaMenu (de.tum.in.tumcampusapp.component.ui.cafeteria.model.CafeteriaMenu)5 NotificationCompat (android.support.v4.app.NotificationCompat)2 HashSet (java.util.HashSet)2 SuppressLint (android.annotation.SuppressLint)1 NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 Context (android.content.Context)1 Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1 SpannableString (android.text.SpannableString)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 RemoteViews (android.widget.RemoteViews)1 TextView (android.widget.TextView)1 CafeteriaDao (de.tum.in.tumcampusapp.component.ui.cafeteria.CafeteriaDao)1 FavoriteDishDao (de.tum.in.tumcampusapp.component.ui.cafeteria.FavoriteDishDao)1 CafeteriaMenuManager (de.tum.in.tumcampusapp.component.ui.cafeteria.controller.CafeteriaMenuManager)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