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);
}
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
Post a Comment