Search in sources :

Example 1 with PriceHistoryEntry

use of xyz.zedler.patrick.grocy.model.PriceHistoryEntry in project grocy-android by patzly.

the class ProductOverviewBottomSheet method loadPriceHistory.

private void loadPriceHistory() {
    if (!isFeatureEnabled(Constants.PREF.FEATURE_STOCK_PRICE_TRACKING)) {
        return;
    }
    dlHelper.get(activity.getGrocyApi().getPriceHistory(product.getId()), response -> {
        Type listType = new TypeToken<ArrayList<PriceHistoryEntry>>() {
        }.getType();
        ArrayList<PriceHistoryEntry> priceHistoryEntries;
        priceHistoryEntries = new Gson().fromJson(response, listType);
        if (priceHistoryEntries.isEmpty()) {
            return;
        }
        ArrayList<String> dates = new ArrayList<>();
        Collections.reverse(priceHistoryEntries);
        HashMap<String, ArrayList<BezierCurveChart.Point>> curveLists = new HashMap<>();
        for (PriceHistoryEntry priceHistoryEntry : priceHistoryEntries) {
            Store store = priceHistoryEntry.getStore();
            String storeName;
            if (store == null || store.getName().trim().isEmpty()) {
                storeName = activity.getString(R.string.property_store_unknown);
            } else {
                storeName = store.getName().trim();
            }
            if (!curveLists.containsKey(storeName)) {
                curveLists.put(storeName, new ArrayList<>());
            }
            ArrayList<BezierCurveChart.Point> curveList = curveLists.get(storeName);
            String date = new DateUtil(activity).getLocalizedDate(priceHistoryEntry.getDate(), DateUtil.FORMAT_SHORT);
            if (!dates.contains(date)) {
                dates.add(date);
            }
            assert curveList != null;
            curveList.add(new BezierCurveChart.Point(dates.indexOf(date), (float) priceHistoryEntry.getPrice()));
        }
        binding.itemPriceHistory.init(curveLists, dates);
        animateLinearPriceHistory();
    }, error -> {
    });
}
Also used : PriceHistoryEntry(xyz.zedler.patrick.grocy.model.PriceHistoryEntry) HashMap(java.util.HashMap) BezierCurveChart(xyz.zedler.patrick.grocy.view.BezierCurveChart) DateUtil(xyz.zedler.patrick.grocy.util.DateUtil) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) Store(xyz.zedler.patrick.grocy.model.Store) Type(java.lang.reflect.Type)

Aggregations

Gson (com.google.gson.Gson)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 PriceHistoryEntry (xyz.zedler.patrick.grocy.model.PriceHistoryEntry)1 Store (xyz.zedler.patrick.grocy.model.Store)1 DateUtil (xyz.zedler.patrick.grocy.util.DateUtil)1 BezierCurveChart (xyz.zedler.patrick.grocy.view.BezierCurveChart)1