Skip to content

Commit 3f202e5

Browse files
committed
Refactor view and presenter
1 parent 70767fb commit 3f202e5

File tree

3 files changed

+58
-118
lines changed

3 files changed

+58
-118
lines changed

raveandroid/src/main/java/com/flutterwave/raveandroid/banktransfer/BankTransferContract.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public interface BankTransferContract {
1313
interface View {
1414
void showProgressIndicator(boolean active);
1515
void showPollingIndicator(boolean active);
16-
void onPollingRoundComplete(String flwRef, String txRef, String encryptionKey);
1716
void onPaymentError(String message);
1817
void showToast(String message);
1918
void onPaymentSuccessful(String status, String flwRef, String responseAsString);
@@ -25,15 +24,16 @@ interface View {
2524
void onAmountValidationSuccessful(String valueOf);
2625
void showFieldError(int viewID, String message, Class<?> viewType);
2726
void onValidationSuccessful(HashMap<String, ViewObject> dataHashMap);
27+
void onAmountValidationFailed();
2828
}
2929

3030
interface UserActionsListener {
3131
void fetchFee(Payload payload);
32-
void requeryTx(String flwRef, String txRef, String publicKey);
32+
void requeryTx();
3333
void payWithBankTransfer(Payload body, String encryptionKey);
34-
void setRequeryCountdownTime(long currentTimeMillis);
3534
void init(RavePayInitializer ravePayInitializer);
3635
void processTransaction(HashMap<String, ViewObject> dataHashMap, RavePayInitializer ravePayInitializer);
3736
void onDataCollected(HashMap<String, ViewObject> dataHashMap);
37+
void startPaymentVerification();
3838
}
3939
}

raveandroid/src/main/java/com/flutterwave/raveandroid/banktransfer/BankTransferFragment.java

Lines changed: 38 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public class BankTransferFragment extends Fragment implements BankTransferContra
5757
private ProgressDialog progressDialog;
5858
private ProgressDialog pollingProgressDialog;
5959
BankTransferPresenter presenter;
60-
boolean canShowPollingIndicator = false;
6160

6261
public BankTransferFragment() {
6362
// Required empty public constructor
@@ -81,32 +80,25 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
8180

8281
presenter.init(ravePayInitializer);
8382

84-
double amountToPay = ravePayInitializer.getAmount();
85-
86-
if (amountToPay > 0) {
87-
amountTil.setVisibility(GONE);
88-
amountEt.setText(String.valueOf(amountToPay));
89-
}
90-
9183
return v;
9284
}
9385

9486
private void setListeners() {
95-
payButton.setOnClickListener(this);
87+
payButton.setOnClickListener(this);
9688
verifyPaymentButton.setOnClickListener(this);
9789

9890
}
9991

10092
@Override
10193
public void onClick(View view) {
102-
int i = view.getId();
103-
if (i == payButton.getId()) {
94+
int viewId = view.getId();
95+
if (viewId == payButton.getId()) {
10496
clearErrors();
10597
Utils.hide_keyboard(getActivity());
10698
collectData();
10799
}
108100

109-
if(i == verifyPaymentButton.getId()){
101+
if (viewId == verifyPaymentButton.getId()) {
110102
verifyPayment();
111103
}
112104
}
@@ -155,23 +147,11 @@ public void showFieldError(int viewID, String message, Class<?> viewType) {
155147

156148

157149
private void verifyPayment() {
158-
canShowPollingIndicator = true;
159150
showPollingIndicator(true);
160-
presenter.setRequeryCountdownTime(System.currentTimeMillis());
151+
presenter.startPaymentVerification();
161152

162153
}
163154

164-
@Override
165-
public void onPollingRoundComplete(String flwRef, String txRef, String publicKey) {
166-
167-
if (canShowPollingIndicator) {
168-
if (pollingProgressDialog != null && pollingProgressDialog.isShowing()) {
169-
presenter.requeryTx(flwRef, txRef, publicKey);
170-
}
171-
} else presenter.requeryTx(flwRef, txRef, publicKey);
172-
173-
174-
}
175155

176156
@Override
177157
public void showPollingIndicator(boolean active) {
@@ -180,27 +160,25 @@ public void showPollingIndicator(boolean active) {
180160
return;
181161
}
182162

183-
if (canShowPollingIndicator) {
184-
if (pollingProgressDialog == null) {
185-
pollingProgressDialog = new ProgressDialog(getActivity());
186-
pollingProgressDialog.setMessage("Checking transaction status. \nPlease wait");
187-
}
163+
if (pollingProgressDialog == null) {
164+
pollingProgressDialog = new ProgressDialog(getActivity());
165+
pollingProgressDialog.setMessage("Checking transaction status. \nPlease wait");
166+
}
188167

189-
if (active && !pollingProgressDialog.isShowing()) {
190-
pollingProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
191-
@Override
192-
public void onClick(DialogInterface dialog, int which) {
193-
pollingProgressDialog.dismiss();
194-
}
195-
});
196-
197-
pollingProgressDialog.show();
198-
} else if (active && pollingProgressDialog.isShowing()) {
199-
//pass
200-
} else {
201-
pollingProgressDialog.dismiss();
202-
}
168+
if (active && !pollingProgressDialog.isShowing()) {
169+
pollingProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
170+
@Override
171+
public void onClick(DialogInterface dialog, int which) {
172+
pollingProgressDialog.dismiss();
173+
}
174+
});
175+
pollingProgressDialog.show();
176+
} else if (active && pollingProgressDialog.isShowing()) {
177+
//pass
178+
} else {
179+
pollingProgressDialog.dismiss();
203180
}
181+
204182
}
205183

206184
private void clearErrors() {
@@ -289,66 +267,24 @@ public void showToast(String message) {
289267

290268
@Override
291269
public void onPaymentSuccessful(String status, String flwRef, final String responseAsString) {
270+
Intent intent = new Intent();
271+
intent.putExtra("response", responseAsString);
292272

293-
if (canShowPollingIndicator) { //Verify payment button has been clicked previously
294-
Intent intent = new Intent();
295-
intent.putExtra("response", responseAsString);
296-
297-
if (getActivity() != null) {
298-
getActivity().setResult(RavePayActivity.RESULT_SUCCESS, intent);
299-
getActivity().finish();
300-
}
301-
} else {
302-
verifyPaymentButton.setText(getString(R.string.proceed));
303-
transferStatusTv.setText(getString(R.string.transfer_received_successfully));
304-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
305-
transferStatusTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_check_circle_black_24dp, 0, 0, 0);
306-
} else transferStatusTv.setTextColor(Color.parseColor("#4BB543"));
307-
transferStatusTv.setVisibility(View.VISIBLE);
308-
309-
verifyPaymentButton.setOnClickListener(new View.OnClickListener() {
310-
@Override
311-
public void onClick(View view) {
312-
Intent intent = new Intent();
313-
intent.putExtra("response", responseAsString);
314-
315-
if (getActivity() != null) {
316-
getActivity().setResult(RavePayActivity.RESULT_SUCCESS, intent);
317-
getActivity().finish();
318-
}
319-
}
320-
});
273+
if (getActivity() != null) {
274+
getActivity().setResult(RavePayActivity.RESULT_SUCCESS, intent);
275+
getActivity().finish();
321276
}
322277
}
323278

324279
@Override
325280
public void onPaymentFailed(String message, final String responseAsJSONString) {
326-
if (canShowPollingIndicator) {// Verify Payment button has been clicked
327-
Intent intent = new Intent();
328-
intent.putExtra("response", responseAsJSONString);
329-
if (getActivity() != null) {
330-
getActivity().setResult(RavePayActivity.RESULT_ERROR, intent);
331-
getActivity().finish();
332-
}
333-
} else {
334-
transferStatusTv.setText(getString(R.string.payment_failed));
335-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
336-
transferStatusTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_error_black_24dp, 0, 0, 0);
337-
} else transferStatusTv.setTextColor(Color.parseColor("#FC100D"));
338-
transferStatusTv.setVisibility(View.VISIBLE);
339-
verifyPaymentButton.setText(getString(R.string.back_to_app));
340-
verifyPaymentButton.setOnClickListener(new View.OnClickListener() {
341-
@Override
342-
public void onClick(View view) {
343-
Intent intent = new Intent();
344-
intent.putExtra("response", responseAsJSONString);
345-
if (getActivity() != null) {
346-
getActivity().setResult(RavePayActivity.RESULT_ERROR, intent);
347-
getActivity().finish();
348-
}
349-
}
350-
});
281+
Intent intent = new Intent();
282+
intent.putExtra("response", responseAsJSONString);
283+
if (getActivity() != null) {
284+
getActivity().setResult(RavePayActivity.RESULT_ERROR, intent);
285+
getActivity().finish();
351286
}
287+
352288
}
353289

354290
@Override
@@ -387,4 +323,9 @@ public void onValidationSuccessful(HashMap<String, ViewObject> dataHashMap) {
387323

388324
}
389325

326+
@Override
327+
public void onAmountValidationFailed() {
328+
amountTil.setVisibility(View.VISIBLE);
329+
}
330+
390331
}

raveandroid/src/main/java/com/flutterwave/raveandroid/banktransfer/BankTransferPresenter.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class BankTransferPresenter implements BankTransferContract.UserActionsLi
3030
private Context context;
3131
private BankTransferContract.View mView;
3232
private AmountValidator amountValidator = new AmountValidator();
33+
private String txRef = null, flwRef = null, publicKey = null;
3334
private long requeryCountdownTime = 0;
3435

3536
BankTransferPresenter(Context context, BankTransferContract.View mView) {
@@ -90,9 +91,9 @@ public void onSuccess(ChargeResponse response, String responseAsJSONString) {
9091
if (response.getData() != null) {
9192
Log.d("resp", responseAsJSONString);
9293

93-
String flwRef = response.getData().getFlw_reference();
94-
String txRef = response.getData().getTx_ref();
95-
requeryTx(flwRef, txRef, payload.getPBFPubKey());
94+
flwRef = response.getData().getFlw_reference();
95+
txRef = response.getData().getTx_ref();
96+
publicKey = payload.getPBFPubKey();
9697
mView.onTransferDetailsReceived(response);
9798
} else {
9899
mView.onPaymentError("No response data was returned");
@@ -109,36 +110,33 @@ public void onError(String message, String responseAsJSONString) {
109110
}
110111

111112
@Override
112-
public void setRequeryCountdownTime(long currentTimeMillis) {
113-
requeryCountdownTime = currentTimeMillis;
113+
public void startPaymentVerification() {
114+
requeryCountdownTime = System.currentTimeMillis();
115+
mView.showPollingIndicator(true);
116+
requeryTx();
114117
}
115118

116-
117119
@Override
118-
public void requeryTx(final String flwRef, final String txRef, final String publicKey) {
120+
public void requeryTx() {
119121

120122
RequeryRequestBody body = new RequeryRequestBody();
121123
body.setFlw_ref(flwRef);
122124
body.setPBFPubKey(publicKey);
123125

124-
mView.showPollingIndicator(true);
125-
126126
new NetworkRequestImpl().requeryPayWithBankTx(body, new Callbacks.OnRequeryRequestComplete() {
127127
@Override
128128
public void onSuccess(RequeryResponse response, String responseAsJSONString) {
129129
if (response.getData() == null) {
130130
mView.onPaymentFailed(response.getStatus(), responseAsJSONString);
131131
} else if (response.getData().getChargeResponseCode().equals("01")) {
132-
if (requeryCountdownTime != 0) {
133-
if ((System.currentTimeMillis() - requeryCountdownTime) < 300000) {
134-
mView.onPollingRoundComplete(flwRef, txRef, publicKey);
135-
} else {
136-
mView.showPollingIndicator(false);
137-
mView.onPollingTimeout(flwRef, txRef, responseAsJSONString);
138-
}
132+
133+
if ((System.currentTimeMillis() - requeryCountdownTime) < 300000) {
134+
requeryTx();
139135
} else {
140-
mView.onPollingRoundComplete(flwRef, txRef, publicKey);
136+
mView.showPollingIndicator(false);
137+
mView.onPollingTimeout(flwRef, txRef, responseAsJSONString);
141138
}
139+
142140
} else if (response.getData().getChargeResponseCode().equals("00")) {
143141
mView.showPollingIndicator(false);
144142
mView.onPaymentSuccessful(flwRef, txRef, responseAsJSONString);
@@ -164,7 +162,8 @@ public void init(RavePayInitializer ravePayInitializer) {
164162
boolean isAmountValid = amountValidator.isAmountValid(ravePayInitializer.getAmount());
165163
if (isAmountValid) {
166164
mView.onAmountValidationSuccessful(String.valueOf(ravePayInitializer.getAmount()));
167-
}
165+
} else mView.onAmountValidationFailed();
166+
168167
}
169168
}
170169

0 commit comments

Comments
 (0)