improving VMC flow
This commit is contained in:
@@ -8,10 +8,13 @@ import 'package:provider/provider.dart';
|
||||
|
||||
class GatewayModel {
|
||||
bool isLoading = false;
|
||||
String status = '';
|
||||
String status = 'PENDING';
|
||||
String pollStatus = 'PENDING';
|
||||
String? errorMessage;
|
||||
bool isCancelled = false;
|
||||
int count = 0;
|
||||
int pollAttempt = 0;
|
||||
int pollMaxAttempts = 20;
|
||||
}
|
||||
|
||||
class GatewayController extends ChangeNotifier {
|
||||
@@ -84,19 +87,26 @@ class GatewayController extends ChangeNotifier {
|
||||
|
||||
Future<ApiResponse<Map<String, dynamic>>> pollTransaction(String uid) async {
|
||||
// poll up to 15 times (~45 seconds at 3-second intervals)
|
||||
int maxAttempts = 15;
|
||||
int attempt = 0;
|
||||
|
||||
while (!model.isCancelled && attempt < maxAttempts) {
|
||||
model.pollAttempt = 0;
|
||||
notifyListeners();
|
||||
|
||||
while (!model.isCancelled && attempt < model.pollMaxAttempts) {
|
||||
attempt++;
|
||||
model.pollAttempt = attempt;
|
||||
notifyListeners();
|
||||
|
||||
ApiResponse<Map<String, dynamic>> result = await poll(uid);
|
||||
|
||||
// Only stop on success or failure
|
||||
if (result.isSuccess) {
|
||||
String status = model.status;
|
||||
// If status is SUCCESS, we're done
|
||||
if (status == 'SUCCESS') {
|
||||
if (model.status == 'SUCCESS') {
|
||||
model.isCancelled = true;
|
||||
model.pollStatus = model.status;
|
||||
finishLoading();
|
||||
notifyListeners();
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -107,18 +117,15 @@ class GatewayController extends ChangeNotifier {
|
||||
} else {
|
||||
await Future.delayed(const Duration(seconds: 3));
|
||||
if (model.isCancelled) break;
|
||||
|
||||
// Network error — show failure UI and stop
|
||||
model.isCancelled = true;
|
||||
model.status = 'FAILED';
|
||||
model.errorMessage = result.error;
|
||||
return result;
|
||||
model.status = 'PENDING';
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
// Timed out after max attempts
|
||||
model.isCancelled = true;
|
||||
model.status = 'FAILED';
|
||||
model.pollStatus = 'FAILED';
|
||||
model.errorMessage = 'We failed to check your transaction status';
|
||||
finishLoading();
|
||||
notifyListeners();
|
||||
|
||||
Reference in New Issue
Block a user