65 lines
1.8 KiB
Dart
65 lines
1.8 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
|
|
import 'models.dart';
|
|
import 'service.dart';
|
|
|
|
class DestinationSelectionProvider extends ChangeNotifier {
|
|
DestinationSelectionProvider(this.service, this.context);
|
|
final DestinationService service;
|
|
final PayoutContext context;
|
|
final assessments = <String, PayoutEligibility>{};
|
|
bool busy = false, _disposed = false;
|
|
DestinationIssue? error;
|
|
Future<void> assess(List<SavedDestination> methods) async {
|
|
for (final d in methods) {
|
|
try {
|
|
final assessment = await service.assess(d.input, context);
|
|
if (_disposed) return;
|
|
assessments[d.id] = assessment;
|
|
} on DestinationException catch (e) {
|
|
if (_disposed) return;
|
|
assessments[d.id] = PayoutEligibility(
|
|
EligibilityState.requiresAction,
|
|
reason: e.issue,
|
|
);
|
|
} catch (_) {
|
|
if (_disposed) return;
|
|
assessments[d.id] = const PayoutEligibility(
|
|
EligibilityState.temporarilyUnavailable,
|
|
reason: DestinationIssue.unavailable,
|
|
);
|
|
}
|
|
}
|
|
if (!_disposed) notifyListeners();
|
|
}
|
|
|
|
Future<SavedReference?> select(SavedDestination d) async {
|
|
if (busy) return null;
|
|
busy = true;
|
|
error = null;
|
|
notifyListeners();
|
|
SavedReference? result;
|
|
try {
|
|
final current = await service.get(d.id);
|
|
final a = await service.assess(current.input, context);
|
|
if (_disposed) return null;
|
|
assessments[d.id] = a;
|
|
if (a.eligible) result = current.input;
|
|
} on DestinationException catch (e) {
|
|
error = e.issue;
|
|
} catch (_) {
|
|
error = DestinationIssue.unavailable;
|
|
}
|
|
if (_disposed) return null;
|
|
busy = false;
|
|
notifyListeners();
|
|
return result;
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_disposed = true;
|
|
super.dispose();
|
|
}
|
|
}
|