Skip to content

Commit

Permalink
chore: remove polling widgets (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-tbd authored Aug 30, 2024
1 parent 5823ac4 commit 0d1bb59
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 294 deletions.
2 changes: 0 additions & 2 deletions lib/features/dap/dap_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ class DapForm extends HookConsumerWidget {
dap.value = const AsyncValue.loading();
final result = await DapResolver().resolve(parsedDap);

await Future.delayed(const Duration(milliseconds: 500));

dap.value = AsyncValue.data(result.dap);
await onSubmit(result.dap, result.moneyAddresses);
} on Exception catch (e) {
Expand Down
115 changes: 74 additions & 41 deletions lib/features/payment/payment_details_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import 'package:didpay/features/dap/dap_state.dart';
import 'package:didpay/features/did/did_provider.dart';
import 'package:didpay/features/kcc/kcc_consent_page.dart';
import 'package:didpay/features/payment/payment_details_state.dart';
import 'package:didpay/features/payment/payment_fetch_quote_widget.dart';
import 'package:didpay/features/payment/payment_method.dart';
import 'package:didpay/features/payment/payment_methods_page.dart';
import 'package:didpay/features/payment/payment_review_page.dart';
import 'package:didpay/features/payment/payment_state.dart';
import 'package:didpay/features/payment/payment_types_page.dart';
import 'package:didpay/features/pfis/pfi.dart';
Expand Down Expand Up @@ -38,8 +38,7 @@ class PaymentDetailsPage extends HookConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final quote = useState<AsyncValue<Quote?>>(ref.watch(quoteProvider));
final rfq = useState<AsyncValue<Rfq>?>(null);
final quote = useState<AsyncValue<Quote?>?>(null);
final state = useState<PaymentDetailsState>(
paymentState.paymentDetailsState ?? PaymentDetailsState(),
);
Expand All @@ -63,41 +62,32 @@ class PaymentDetailsPage extends HookConsumerWidget {
[state.value.selectedPaymentType],
);

final isAwaiting =
(rfq.value?.isLoading ?? false) || (quote.value.isLoading);
final isAwaiting = quote.value?.isLoading ?? false;

return PopScope(
canPop: !isAwaiting,
onPopInvoked: (_) {
if (isAwaiting) {
rfq.value = null;
quote.value = const AsyncData(null);
ref.read(quoteProvider.notifier).stopPolling();
quote.value = null;
}
},
child: Scaffold(
appBar: AppBar(),
body: SafeArea(
child: rfq.value != null
? rfq.value!.when(
data: (sentRfq) => PaymentFetchQuoteWidget(
paymentState: paymentState.copyWith(
paymentDetailsState: state.value
.copyWith(exchangeId: sentRfq.metadata.exchangeId),
),
quote: quote,
rfq: rfq,
ref: ref,
),
child: quote.value != null
? quote.value!.when(
data: (_) => Container(),
loading: () => LoadingMessage(
message: Loc.of(context).sendingYourRequest,
message: Loc.of(context).fetchingYourQuote,
),
error: (error, _) => ErrorMessage(
message: error.toString(),
onRetry: () => _sendRfq(
context,
ref,
state.value,
rfq,
state,
quote,
),
),
)
Expand Down Expand Up @@ -125,12 +115,7 @@ class PaymentDetailsPage extends HookConsumerWidget {
availableMethods,
state,
),
_buildPaymentForm(
context,
ref,
state,
rfq,
),
_buildPaymentForm(context, ref, quote, state),
],
),
),
Expand All @@ -141,14 +126,15 @@ class PaymentDetailsPage extends HookConsumerWidget {
Widget _buildPaymentForm(
BuildContext context,
WidgetRef ref,
ValueNotifier<AsyncValue<Quote?>?> quote,
ValueNotifier<PaymentDetailsState> state,
ValueNotifier<AsyncValue<Rfq>?> rfq,
) =>
Expanded(
child: JsonSchemaForm(
state: state.value,
dapState: dapState,
onSubmit: (formData) async {
quote.value = const AsyncLoading();
state.value = state.value.copyWith(formData: formData);

final presentationDefinition = paymentState
Expand Down Expand Up @@ -186,10 +172,19 @@ class PaymentDetailsPage extends HookConsumerWidget {

if (context.mounted) {
await _sendRfq(
context,
ref,
state,
quote,
);
}

if (context.mounted && quote.value != null) {
await _pollForQuote(
context,
ref,
state.value,
rfq,
quote,
);
}
},
Expand Down Expand Up @@ -282,32 +277,70 @@ class PaymentDetailsPage extends HookConsumerWidget {
Future<void> _sendRfq(
BuildContext context,
WidgetRef ref,
PaymentDetailsState state,
ValueNotifier<AsyncValue<Rfq>?> rfq,
ValueNotifier<PaymentDetailsState> state,
ValueNotifier<AsyncValue<Quote?>?> quote,
) async {
rfq.value = const AsyncLoading();

try {
final updatedPaymentState =
paymentState.copyWith(paymentDetailsState: state);
paymentState.copyWith(paymentDetailsState: state.value);

final sentRfq = await ref.read(tbdexServiceProvider).sendRfq(
ref.read(didProvider),
updatedPaymentState.paymentAmountState?.pfiDid ?? '',
updatedPaymentState.paymentAmountState?.offeringId ?? '',
updatedPaymentState.paymentAmountState?.payinAmount ?? '',
paymentState.paymentAmountState?.pfiDid ?? '',
paymentState.paymentAmountState?.offeringId ?? '',
paymentState.paymentAmountState?.payinAmount ?? '',
updatedPaymentState.selectedPayinKind ?? '',
updatedPaymentState.selectedPayoutKind ?? '',
updatedPaymentState.payinDetails,
updatedPaymentState.payoutDetails,
claims: updatedPaymentState.paymentDetailsState?.credentialsJwt,
claims: state.value.credentialsJwt,
);

if (context.mounted && rfq.value != null) {
rfq.value = AsyncData(sentRfq);
state.value = state.value.copyWith(exchangeId: sentRfq.metadata.id);
} on Exception catch (e) {
quote.value = AsyncError(e, StackTrace.current);
}
}

Future<void> _pollForQuote(
BuildContext context,
WidgetRef ref,
PaymentDetailsState state,
ValueNotifier<AsyncValue<Quote?>?> quote,
) async {
quote.value = const AsyncLoading();
final quoteNotifier = ref.read(quoteProvider.notifier);

try {
final fetchedQuote = await quoteNotifier.startPolling(
paymentState.paymentAmountState?.pfiDid ?? '',
state.exchangeId ?? '',
);

if (context.mounted) {
quoteNotifier.stopPolling();

if (fetchedQuote != null && quote.value != null) {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => PaymentReviewPage(
paymentState: paymentState.copyWith(
paymentDetailsState: state.copyWith(quote: fetchedQuote),
),
),
),
);
}

if (context.mounted) {
quote.value = null;
}
}
} on Exception catch (e) {
rfq.value = AsyncError(e, StackTrace.current);
if (context.mounted) {
quoteNotifier.stopPolling();
quote.value = AsyncError(e, StackTrace.current);
}
}
}
}
5 changes: 5 additions & 0 deletions lib/features/payment/payment_details_state.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:didpay/features/payment/payment_method.dart';
import 'package:tbdex/tbdex.dart';

class PaymentDetailsState {
final String? paymentCurrency;
Expand All @@ -9,6 +10,7 @@ class PaymentDetailsState {
final List<PaymentMethod>? paymentMethods;
final List<String>? credentialsJwt;
final Map<String, String>? formData;
final Quote? quote;

PaymentDetailsState({
this.paymentCurrency,
Expand All @@ -19,6 +21,7 @@ class PaymentDetailsState {
this.paymentMethods,
this.credentialsJwt,
this.formData,
this.quote,
});

Set<String>? get paymentTypes =>
Expand All @@ -43,6 +46,7 @@ class PaymentDetailsState {
List<PaymentMethod>? paymentMethods,
List<String>? credentialsJwt,
Map<String, String>? formData,
Quote? quote,
}) {
return PaymentDetailsState(
paymentCurrency: paymentCurrency ?? this.paymentCurrency,
Expand All @@ -54,6 +58,7 @@ class PaymentDetailsState {
paymentMethods: paymentMethods ?? this.paymentMethods,
credentialsJwt: credentialsJwt ?? this.credentialsJwt,
formData: formData ?? this.formData,
quote: quote ?? this.quote,
);
}
}
87 changes: 0 additions & 87 deletions lib/features/payment/payment_fetch_instructions_widget.dart

This file was deleted.

Loading

0 comments on commit 0d1bb59

Please sign in to comment.