Skip to content

Commit

Permalink
fix: NPE on generating Individual collectionsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
tarun0 committed Aug 27, 2017
1 parent fd5dee9 commit 430792a
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class IndividualCollectionSheetAdapter extends
RecyclerView.Adapter<IndividualCollectionSheetAdapter.ViewHolder> {

private List<String> paymentTypeList;
private List<LoanAndClientName> loans;
private List<LoanAndClientName> loanAndClientNames;
private List<PaymentTypeOptions> paymentTypeOptionsList;
private Context c;

Expand All @@ -61,8 +61,8 @@ public void setPaymentTypeList(List<String> paymentTypeList) {
this.paymentTypeList.add(c.getString(R.string.payment_type));
}

public void setLoans(List<LoanAndClientName> loans) {
this.loans = loans;
public void setLoans(List<LoanAndClientName> loanAndClientNameList) {
this.loanAndClientNames = loanAndClientNameList;
}


Expand All @@ -81,33 +81,39 @@ public void onBindViewHolder(final IndividualCollectionSheetAdapter.ViewHolder h
int position) {
if (holder != null) {

LoanAndClientName item = loans.get(position);
final LoanCollectionSheet loanItem = item.getLoan();
holder.tvClientName.setText(item.getClientName());
LoanAndClientName loanAndClientNameItem = loanAndClientNames.get(position);
final LoanCollectionSheet loanCollectionSheetItem = loanAndClientNameItem.getLoan();
holder.tvClientName.setText(loanAndClientNameItem.getClientName());

holder.tvProductCode.setText(concatProductWithAccount(loanItem.getProductShortName()
, loanItem.getAccountId()));
holder.tvProductCode.setText(concatProductWithAccount(loanCollectionSheetItem
.getProductShortName(), loanCollectionSheetItem.getAccountId()));

if (loanItem.getChargesDue() != null) {
if (loanCollectionSheetItem.getChargesDue() != null) {
holder.etCharges.setText(
String.format(Locale.getDefault(), "%f", loanItem.getChargesDue()));
String.format(Locale.getDefault(), "%f",
loanCollectionSheetItem.getChargesDue()));
}

if (loanItem.getTotalDue() != null) {
if (loanCollectionSheetItem.getTotalDue() != null) {
holder.etTotalDues.setText(
String.format(Locale.getDefault(), "%f", loanItem.getTotalDue()));
String.format(Locale.getDefault(), "%f",
loanCollectionSheetItem.getTotalDue()));
}


//Add default value of transaction irrespective of they are 'saved' or 'cancelled'
// manually by the user.
BulkRepaymentTransactions singleTransaction = new BulkRepaymentTransactions();
singleTransaction.setLoanId(loanItem.getLoanId());
singleTransaction.setTransactionAmount(loanItem.getChargesDue() != null ?
loanItem.getChargesDue() + loanItem.getTotalDue() :
loanItem.getTotalDue());

sheetItemClickListener.onShowSheetMandatoryItem(singleTransaction, position);
BulkRepaymentTransactions defaultBulkRepaymentTransaction = new
BulkRepaymentTransactions();
defaultBulkRepaymentTransaction.setLoanId(loanCollectionSheetItem.getLoanId());
defaultBulkRepaymentTransaction.setTransactionAmount(
loanCollectionSheetItem.getChargesDue() != null ?
loanCollectionSheetItem.getChargesDue() +
loanCollectionSheetItem.getTotalDue() :
loanCollectionSheetItem.getTotalDue());

sheetItemClickListener.onShowSheetMandatoryItem(defaultBulkRepaymentTransaction,
position);
}
}

Expand All @@ -117,7 +123,7 @@ private String concatProductWithAccount(String productCode, String accountNo) {

@Override
public int getItemCount() {
return loans.size();
return loanAndClientNames.size();
}

@Override
Expand All @@ -128,7 +134,7 @@ public long getItemId(int i) {
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,
Spinner.OnItemSelectedListener {
int position;
BulkRepaymentTransactions transaction = new BulkRepaymentTransactions();
BulkRepaymentTransactions bulkRepaymentTransaction = new BulkRepaymentTransactions();

public ViewHolder(View v) {
super(v);
Expand All @@ -140,9 +146,9 @@ public ViewHolder(View v) {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

if (i != paymentTypeList.size() - 1) {
transaction.setPaymentTypeId(paymentTypeOptionsList.get(i).getId());
bulkRepaymentTransaction.setPaymentTypeId(paymentTypeOptionsList.get(i).getId());
} else {
transaction.setPaymentTypeId(null);
bulkRepaymentTransaction.setPaymentTypeId(null);
}
}

Expand Down Expand Up @@ -179,33 +185,35 @@ public void onClick(View view) {
*/
private void saveAdditional() {

transaction.setLoanId(loans.get(position).getLoan().getLoanId());
transaction.setTransactionAmount((!etCharges.getText().toString().isEmpty() ?
bulkRepaymentTransaction.setLoanId(loanAndClientNames.get(position)
.getLoan().getLoanId());
bulkRepaymentTransaction.setTransactionAmount((!etCharges
.getText().toString().isEmpty() ?
Double.parseDouble(etCharges.getText().toString()) : 0)
+ (!etTotalDues.getText().toString().isEmpty() ?
Double.parseDouble(etTotalDues.getText().toString()) : 0));

if (!etAccountNumber.getText().toString().isEmpty()) {
transaction.setAccountNumber(etAccountNumber.getText().toString());
bulkRepaymentTransaction.setAccountNumber(etAccountNumber.getText().toString());
}

if (!etChequeNumber.getText().toString().isEmpty()) {
transaction.setCheckNumber(etChequeNumber.getText().toString());
bulkRepaymentTransaction.setCheckNumber(etChequeNumber.getText().toString());
}

if (!etRoutingCode.getText().toString().isEmpty()) {
transaction.setRoutingCode(etRoutingCode.getText().toString());
bulkRepaymentTransaction.setRoutingCode(etRoutingCode.getText().toString());
}

if (!etReceiptNumber.getText().toString().isEmpty()) {
transaction.setReceiptNumber(etReceiptNumber.getText().toString());
bulkRepaymentTransaction.setReceiptNumber(etReceiptNumber.getText().toString());
}

if (!etBankNumber.getText().toString().isEmpty()) {
transaction.setBankNumber(etBankNumber.getText().toString());
bulkRepaymentTransaction.setBankNumber(etBankNumber.getText().toString());
}

sheetItemClickListener.onSaveAdditionalItem(transaction, position);
sheetItemClickListener.onSaveAdditionalItem(bulkRepaymentTransaction, position);
tableAdditional.setVisibility(View.GONE);
}

Expand All @@ -214,20 +222,22 @@ private void saveAdditional() {
* the EditTexts and sets the remaining fields to null.
*/
private void cancelAdditional() {
transaction.setLoanId(loans.get(position).getLoan().getLoanId());
transaction.setTransactionAmount((!etCharges.getText().toString().isEmpty() ?
bulkRepaymentTransaction.setLoanId(loanAndClientNames.get(position)
.getLoan().getLoanId());
bulkRepaymentTransaction.setTransactionAmount((!etCharges.getText()
.toString().isEmpty() ?
Double.parseDouble(etCharges.getText().toString()) : 0)
+ (!etTotalDues.getText().toString().isEmpty() ?
Double.parseDouble(etTotalDues.getText().toString()) : 0));

tableAdditional.setVisibility(View.GONE);
transaction.setPaymentTypeId(null);
transaction.setAccountNumber(null);
transaction.setCheckNumber(null);
transaction.setRoutingCode(null);
transaction.setReceiptNumber(null);
transaction.setBankNumber(null);
sheetItemClickListener.onSaveAdditionalItem(transaction, position);
bulkRepaymentTransaction.setPaymentTypeId(null);
bulkRepaymentTransaction.setAccountNumber(null);
bulkRepaymentTransaction.setCheckNumber(null);
bulkRepaymentTransaction.setRoutingCode(null);
bulkRepaymentTransaction.setReceiptNumber(null);
bulkRepaymentTransaction.setBankNumber(null);
sheetItemClickListener.onSaveAdditionalItem(bulkRepaymentTransaction, position);
}

private void showAdditional() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,17 @@ private void setUpUi() {
}

@Override
public void onShowSheetMandatoryItem(BulkRepaymentTransactions transaction, int position) {
public void onShowSheetMandatoryItem(BulkRepaymentTransactions bulkRepaymentTransactions,
int position) {
//Add the data as received
payload.getBulkRepaymentTransactions().set(position, transaction);
payload.getBulkRepaymentTransactions().set(position, bulkRepaymentTransactions);
}

@Override
public void onSaveAdditionalItem(BulkRepaymentTransactions transaction, int position) {
public void onSaveAdditionalItem(BulkRepaymentTransactions bulkRepaymentTransactions,
int position) {
//Add or amend the data as changed by the user
payload.getBulkRepaymentTransactions().set(position, transaction);
payload.getBulkRepaymentTransactions().set(position, bulkRepaymentTransactions);
}


Expand All @@ -216,12 +218,15 @@ private void showCollectionSheetViews(IndividualCollectionSheet sheet) {
//methods.

payload = new IndividualCollectionSheetPayload();
for (LoanAndClientName l : presenter.filterLoanAndClientNames(sheet.getClients())) {
LoanCollectionSheet loan = l.getLoan();
for (LoanAndClientName loanAndClientName :
presenter.filterLoanAndClientNames(sheet.getClients())) {
LoanCollectionSheet loanCollectionSheet = loanAndClientName.getLoan();
payload.getBulkRepaymentTransactions().add(new BulkRepaymentTransactions(
loan.getLoanId(),
loan.getChargesDue() == null ? loan.getTotalDue() :
loan.getTotalDue() + loan.getChargesDue()));
loanCollectionSheet.getLoanId(),
loanCollectionSheet.getChargesDue() == null ?
loanCollectionSheet.getTotalDue() :
loanCollectionSheet.getTotalDue() +
loanCollectionSheet.getChargesDue()));
}

LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
Expand Down Expand Up @@ -250,8 +255,8 @@ private void submitSheet() {


@Override
public void setOfficeSpinner(List<Office> list) {
officeList = list;
public void setOfficeSpinner(List<Office> offices) {
officeList = offices;
officeNameList.clear();
officeNameList.addAll(presenter.filterOffices(officeList));
officeNameList.add(getString(R.string.spinner_office));
Expand All @@ -277,10 +282,10 @@ public void setTvRepaymentDate() {


@Override
public void setStaffSpinner(List<Staff> list) {
public void setStaffSpinner(List<Staff> staffs) {

spStaff.setOnItemSelectedListener(this);
staffList = list;
staffList = staffs;
staffNameList.clear();
staffNameList.addAll(presenter.filterStaff(staffList));
staffNameList.add(getString(R.string.spinner_staff));
Expand Down Expand Up @@ -318,8 +323,8 @@ public void onNothingSelected(AdapterView<?> adapterView) {
}

@Override
public void showSheet(IndividualCollectionSheet collectionSheet) {
sheet = collectionSheet;
public void showSheet(IndividualCollectionSheet individualCollectionSheet) {
sheet = individualCollectionSheet;
if (recyclerSheets.getVisibility() == View.GONE) {
recyclerSheets.setVisibility(View.VISIBLE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ public void detachView() {
}
}

void submitIndividualCollectionSheet(IndividualCollectionSheetPayload payload) {
void submitIndividualCollectionSheet(IndividualCollectionSheetPayload
individualCollectionSheetPayload) {
checkViewAttached();
getMvpView().showProgressbar(true);
mSubscription.add(mDataManagerCollection.saveIndividualCollectionSheet(payload)
mSubscription.add(mDataManagerCollection
.saveIndividualCollectionSheet(individualCollectionSheetPayload)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber<GenericResponse>() {
Expand All @@ -90,18 +92,20 @@ public void onError(Throwable e) {
}

@Override
public void onNext(GenericResponse response) {
public void onNext(GenericResponse genericResponse) {
getMvpView().showProgressbar(false);
getMvpView().showSuccess();
}
}));
}

void fetchIndividualCollectionSheet(RequestCollectionSheetPayload payload) {
void fetchIndividualCollectionSheet(RequestCollectionSheetPayload
requestCollectionSheetPayload) {

checkViewAttached();
getMvpView().showProgressbar(true);
mSubscription.add(mDataManagerCollection.getIndividualCollectionSheet(payload)
mSubscription.add(mDataManagerCollection
.getIndividualCollectionSheet(requestCollectionSheetPayload)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber<IndividualCollectionSheet>() {
Expand Down Expand Up @@ -129,10 +133,10 @@ public void onError(Throwable e) {
}

@Override
public void onNext(IndividualCollectionSheet collectionSheet) {
public void onNext(IndividualCollectionSheet individualCollectionSheet) {
getMvpView().showProgressbar(false);
if (collectionSheet.getClients().size() > 0) {
getMvpView().showSheet(collectionSheet);
if (individualCollectionSheet.getClients().size() > 0) {
getMvpView().showSheet(individualCollectionSheet);
} else {
getMvpView().showNoSheetFound();
}
Expand Down Expand Up @@ -222,9 +226,9 @@ public void call(Office office) {
return officesList;
}

List<String> filterStaff(List<Staff> staff) {
List<String> filterStaff(List<Staff> staffs) {
final List<String> staffList = new ArrayList<>();
Observable.from(staff)
Observable.from(staffs)
.subscribe(new Action1<Staff>() {
@Override
public void call(Staff staff) {
Expand All @@ -246,16 +250,20 @@ public void call(PaymentTypeOptions paymentTypeOption) {
return paymentList;
}

List<LoanAndClientName> filterLoanAndClientNames(List<ClientCollectionSheet> clients) {
List<LoanAndClientName> filterLoanAndClientNames(List<ClientCollectionSheet>
clientCollectionSheets) {
final List<LoanAndClientName> loansAndClientNames = new ArrayList<>();
Observable.from(clients)
Observable.from(clientCollectionSheets)
.subscribe(new Action1<ClientCollectionSheet>() {
@Override
public void call(ClientCollectionSheet client) {
for (LoanCollectionSheet loan :
client.getLoans()) {
loansAndClientNames.add(new
LoanAndClientName(loan, client.getClientName()));
public void call(ClientCollectionSheet clientCollectionSheet) {
if (clientCollectionSheet.getLoans() != null) {
for (LoanCollectionSheet loanCollectionSheet :
clientCollectionSheet.getLoans()) {
loansAndClientNames.add(new
LoanAndClientName(loanCollectionSheet,
clientCollectionSheet.getClientName()));
}
}
}
});
Expand Down
Loading

0 comments on commit 430792a

Please sign in to comment.