Skip to main content

Multimap at its best

private void fetchTransactions() {
        transactionViewFlipper.setDisplayedChild(0);

        StringRequest stringRequest = new StringRequest(POST, GET_ALL_MERCHANT_TRANSACTIONS, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.getString("status").equals("SUCCESS")) {
                        JSONArray jsonArray = new JSONArray(jsonObject.getString("result"));
                        final Multimap<String, ArrayList<MerchantTransactionDetails>> multimap = LinkedListMultimap.create();
                        final ArrayList<String> monthNames = new ArrayList<>();
                        for (int i = 0; i < jsonArray.length(); i++) {
                            monthTransactions = new ArrayList<>();
                            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                            MerchantTransactionDetails merchantTransactionDetails = new MerchantTransactionDetails();
                            merchantTransactionDetails.setTransactionId(jsonObject1.getString("trans_id"));

                            merchantTransactionDetails.setTransactionType(jsonObject1.getString("transaction_type"));
                            merchantTransactionDetails.setTransactionAmount(jsonObject1.getString("transaction_amount"));
                            merchantTransactionDetails.setCashBackId(jsonObject1.getString("trans_cashback_id"));
                            merchantTransactionDetails.setOfferId(jsonObject1.getString("trans_offer_redeem_id"));
                            merchantTransactionDetails.setTransactionDate(jsonObject1.getString("transaction_created_date"));
                            merchantTransactionDetails.setTransactionCustomerName(jsonObject1.getString("v_uname"));
                            merchantTransactionDetails.setTransactionText(jsonObject1.getString("v_deal_text"));
                            merchantTransactionDetails.setTransactionDealImageUrl(jsonObject1.getString("markedasfeatured"));
                            merchantTransactionDetails.setCustomerId(jsonObject1.getString("customer_id"));
                            merchantTransactionDetails.setCustomerImage(jsonObject1.getString("v_image"));
                            merchantTransactionDetails.setCustomerContactNo(jsonObject1.getString("v_contact_no"));
                            merchantTransactionDetails.setCustomerEmail(jsonObject1.getString("v_email"));
                            merchantTransactionDetails.setTransactionBillNo(jsonObject1.getString("transaction_bill_number"));
                            merchantTransactionDetails.setBillImageUrl(jsonObject1.getString("transaction_bill_image"));
                            String key = Constants.getProperFormatDate(jsonObject1.getString("transaction_created_date")).split(" ")[0];
                            if (!monthNames.contains(key))
                                monthNames.add(key);
                            monthTransactions.add(merchantTransactionDetails);
                            multimap.put(key, monthTransactions);
                        }

                        JSONArray jsonArray1 = new JSONArray(jsonObject.getString("total_redeemed_cases"));
                        for (int i = 0; i < jsonArray1.length(); i++) {
                            monthTransactions = new ArrayList<>();
                            JSONObject jsonObject1 = jsonArray1.getJSONObject(i);
                            MerchantTransactionDetails merchantTransactionDetails = new MerchantTransactionDetails();
                            merchantTransactionDetails.setTransactionId(jsonObject1.getString("trans_id"));

                            merchantTransactionDetails.setTransactionType(jsonObject1.getString("transaction_type"));
                            merchantTransactionDetails.setTransactionAmount(jsonObject1.getString("transaction_amount"));
                            merchantTransactionDetails.setCashBackId(jsonObject1.getString("trans_cashback_id"));
                            merchantTransactionDetails.setOfferId(jsonObject1.getString("trans_offer_redeem_id"));
                            merchantTransactionDetails.setTransactionDate(jsonObject1.getString("transaction_created_date"));
                            merchantTransactionDetails.setTransactionCustomerName(jsonObject1.getString("v_uname"));
                            merchantTransactionDetails.setTransactionText("Redeemed");
                            merchantTransactionDetails.setTransactionDealImageUrl("");
                            merchantTransactionDetails.setCustomerId(jsonObject1.getString("customer_id"));
                            merchantTransactionDetails.setCustomerImage(jsonObject1.getString("v_image"));
                            merchantTransactionDetails.setCustomerContactNo(jsonObject1.getString("v_contact_no"));
                            merchantTransactionDetails.setCustomerEmail(jsonObject1.getString("v_email"));
                            merchantTransactionDetails.setTransactionBillNo(jsonObject1.getString("transaction_bill_number"));
                            merchantTransactionDetails.setAmountPaidThorughWishlo(jsonObject1.getString("paid_through_wishlo_amount"));
                            merchantTransactionDetails.setBillImageUrl(jsonObject1.getString("transaction_bill_image"));
                            String key = Constants.getProperFormatDate(jsonObject1.getString("transaction_created_date")).split(" ")[0];
                            if (!monthNames.contains(key))
                                monthNames.add(key);
                            monthTransactions.add(merchantTransactionDetails);
                            multimap.put(key, monthTransactions);
                        }
                        Collections.reverse(monthTransactions);
                        transactionViewFlipper.setDisplayedChild(1);
                        if(getContext()==null){
                            final Handler handler =new Handler();
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    if(getContext() == null){
                                        handler.postDelayed(this,400);
                                    }else{
                                        transactionRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                                        transactionRecyclerView.setAdapter(new MonthlyTransactionAdapter(getContext(), multimap, monthNames));
                                    }

                                }
                            },400);
                        }else{
                            transactionRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                            transactionRecyclerView.setAdapter(new MonthlyTransactionAdapter(getContext(), multimap, monthNames));
                        }

                        Log.d("TAG", multimap.toString());
                    } else {
                        transactionViewFlipper.setDisplayedChild(3);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getContext(), "Parse Exception", Toast.LENGTH_LONG).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                transactionViewFlipper.setDisplayedChild(2);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> stringStringHashMap = new HashMap<>();
                stringStringHashMap.put("Content-Type", "application/x-www-form-urlencoded");
                stringStringHashMap.put("X-API-KEY", "1234");
                stringStringHashMap.put("Accept", "application/json");
                return stringStringHashMap;
            }

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("merchant_id", getArguments().getString(MERCHANT_ID));
                Log.d(Constants.TAG, hashMap.toString());
                return hashMap;
            }
        };
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                10000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        AppController.getInstance().addToRequestQueue(stringRequest);
    }





@Override
    public void onBindViewHolder(MonthlyTransactionAdapterViewHolder holder, int position) {
        Collection<ArrayList<MerchantTransactionDetails>> albumDataArrayList1 = multimap.get(monthName.get(position));

        Iterator it = albumDataArrayList1.iterator();

        ArrayList<MerchantTransactionDetails> albumDataArrayList2 = new ArrayList<>();
        while (it.hasNext()) {
            albumDataArrayList2.addAll((Collection<? extends MerchantTransactionDetails>) it.next());
        }
        holder.monthsRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        IndividualMonthDetailAdapter individualMonthDetailAdapter = new IndividualMonthDetailAdapter(context, albumDataArrayList2, mFont);
        holder.monthsRecyclerView.setAdapter(individualMonthDetailAdapter);
        holder.monthName.setText(monthName.get(position));
        holder.monthName.setTypeface(mFont);
    }


Comments

Popular posts from this blog

Meteor JS REST API

WRITE A POST Shahid Shaikh FOLLOW Engineer, Blogger from Mumbai. RESTful CRUD Operations in Meteor.js Published Dec 31, 2015 Last updated Jan 19, 2017 Introduction Meteor is a popular framework for building real-time web applications. We have already covered a tutorial which explains  how to build chat system using Meteor.js . In this tutorial, we will learn how to develop a RESTful API using Meteor which perform CRUD operations. Creating new project Create a new meteor project using the following command: meteor create appName Choose an application name of your choice. Once Meteor is done creating the project, you will see the directories and files created by Meteor in the folder name. We will not need them as of now, so we will delete all of those files and create whatever we need in the coming section. Installing iron-router In order to perform routing in Meteor, we will use  this awesome...

COnfigure Orthanc

cmake -DSTATIC_BUILD:BOOL=ON -DCMAKE_BUILD_TYPE=Debug cmake "-DDCMTK_LIBRARIES=wrap;oflog" -DSTATIC_BUILD=OFF -DDEBIAN_USE_GTEST_SOURCE_PACKAGE:BOOL=ON -DCMAKE_BUILD_TYPE=Debug SRC https://bitbucket.org/sjodogne/orthanc/src/default/LinuxCompilation.txt?fileviewer=file-view-default # How to compile, install and run newest version of Orthanc DICOM server # from Github source in Ubuntu 16.04 LTS cd sudo apt-get update sudo apt-get install build-essential unzip cmake mercurial uuid-dev libcurl4-openssl-dev liblua5.1-0-dev libgtest-dev libpng-dev libsqlite3-dev libssl-dev libjpeg-dev zlib1g-dev libdcmtk2-dev libboost-all-dev libwrap0-dev libcharls-dev libjsoncpp-dev libpugixml-dev doxygen qupzilla sudo rm -rf Orthanc* sudo apt-get purge orthanc git clone https://github.com/jodogne/OrthancMirror.git mkdir ~/OrthancBuild cd ~/OrthancBuild/ cmake -DSTATIC_BUILD=ON -DCMAKE_BUILD_TYPE=Release ~/OrthancMirror/ sudo make sudo make doc ./Orthanc --version ./Orthanc & qupzill...