-
Notifications
You must be signed in to change notification settings - Fork 3
feat(navigation): auto-open invoice screens on daemon request #289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e9e504e
feat(orders): emit trade updates on daemon-driven status syncs
Catrya 62b77c4
feat(navigation): auto-open invoice screens on daemon request
Catrya 81db837
refactor(order): drop MyOrderScreen invoice navigation, now global
Catrya 274889a
docs(specs): document trade-update emissions in the orders contract
Catrya c9ea11d
fix(review): drop stale invoice navigation after the role lookup
Catrya eb2feb3
docs(review): state the real trade-update emission contract
Catrya 248ebd7
test(review): cover the buyer add-invoice navigation path
Catrya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import 'package:flutter/widgets.dart'; | ||
| import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
|
|
||
| import 'package:mostro/core/app_routes.dart'; | ||
| import 'package:mostro/features/order/providers/trade_state_provider.dart'; | ||
| import 'package:mostro/src/rust/api/orders.dart' as orders_api; | ||
| import 'package:mostro/src/rust/api/types.dart'; | ||
|
|
||
| /// Auto-opens the invoice screens when the daemon requests action. | ||
| /// | ||
| /// Both `add-invoice` and `pay-invoice` carry expiration timeouts, so the | ||
| /// user must learn about them no matter which screen is open. This widget | ||
| /// wraps the app root and listens to [tradeUpdatesProvider], pushed by the | ||
| /// Rust ingest after the in-memory book update and the DB persistence | ||
| /// attempt (a DB failure never suppresses the emission): the trade row may | ||
| /// therefore be missing or stale, which the role lookup tolerates — no | ||
| /// role, no navigation. `WaitingBuyerInvoice` sends the buyer to the | ||
| /// add-invoice screen, `WaitingPayment` sends the seller to the | ||
| /// pay-invoice screen. | ||
| /// | ||
| /// Only makers ever reach this path — a taker's first reply is consumed by | ||
| /// the take waiter in Rust and produces no emission (TakeOrderScreen | ||
| /// navigates locally instead). | ||
| class TradeActionListener extends ConsumerStatefulWidget { | ||
| const TradeActionListener({ | ||
| super.key, | ||
| required this.child, | ||
| this.resolveRole, | ||
| this.navigate, | ||
| }); | ||
|
|
||
| final Widget child; | ||
|
|
||
| /// Test seam — production uses the bridge's trade-role lookup. | ||
| final Future<TradeRole?> Function(String orderId)? resolveRole; | ||
|
|
||
| /// Test seam — production pushes on the global [appRouter] unless the | ||
| /// destination is already the current route. | ||
| final void Function(String destination)? navigate; | ||
|
|
||
| @override | ||
| ConsumerState<TradeActionListener> createState() => | ||
| _TradeActionListenerState(); | ||
| } | ||
|
|
||
| class _TradeActionListenerState extends ConsumerState<TradeActionListener> { | ||
| /// Updates whose role lookup is still in flight, keyed by | ||
| /// `orderId/status`, so a burst of identical emissions navigates once. | ||
| final Set<String> _inFlight = {}; | ||
|
|
||
| /// Latest status seen per order, recorded synchronously on every | ||
| /// emission. Emissions can arrive while a role lookup awaits (e.g. the | ||
| /// startup replay delivers WaitingPayment and Active milliseconds | ||
| /// apart); a handler whose status is no longer the latest must not | ||
| /// navigate to a screen the trade already left. | ||
| final Map<String, OrderStatus> _latest = {}; | ||
|
|
||
| static Future<TradeRole?> _bridgeRole(String orderId) => | ||
| orders_api.getTradeRole(orderId: orderId); | ||
|
|
||
| static void _routerNavigate(String destination) { | ||
| final current = | ||
| appRouter.routerDelegate.currentConfiguration.uri.toString(); | ||
| if (current == destination) return; | ||
| appRouter.push(destination); | ||
| } | ||
|
|
||
| Future<void> _handle(TradeUpdate update) async { | ||
| final destination = switch (update.status) { | ||
| OrderStatus.waitingBuyerInvoice => | ||
| AppRoute.addInvoicePath(update.orderId), | ||
| OrderStatus.waitingPayment => AppRoute.payInvoicePath(update.orderId), | ||
| _ => null, | ||
| }; | ||
| if (destination == null) return; | ||
|
|
||
| final key = '${update.orderId}/${update.status}'; | ||
| if (!_inFlight.add(key)) return; | ||
| try { | ||
| final role = await (widget.resolveRole ?? _bridgeRole)(update.orderId); | ||
| // A newer emission superseded this one during the lookup. | ||
| if (_latest[update.orderId] != update.status) return; | ||
| // The daemon addresses add-invoice to the buyer and pay-invoice to | ||
| // the seller, but the same statuses also reach the counterparty as | ||
| // informational syncs (waiting-seller-to-pay persists WaitingPayment | ||
| // on the buyer side too) — those must not navigate. | ||
| final actionable = switch (update.status) { | ||
| OrderStatus.waitingBuyerInvoice => role == TradeRole.buyer, | ||
| OrderStatus.waitingPayment => role == TradeRole.seller, | ||
| _ => false, | ||
| }; | ||
| if (!actionable || !mounted) return; | ||
| // Screens expect their role in this map before being navigated to | ||
| // (see tradeRoleProvider docs). | ||
| ref.read(tradeRoleProvider.notifier).state = { | ||
| ...ref.read(tradeRoleProvider), | ||
| update.orderId: role == TradeRole.buyer, | ||
| }; | ||
| (widget.navigate ?? _routerNavigate)(destination); | ||
| } catch (e, st) { | ||
| debugPrint( | ||
| '[TradeActionListener] failed to handle ${update.orderId}: $e\n$st'); | ||
| } finally { | ||
| _inFlight.remove(key); | ||
| } | ||
| } | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| ref.listen<AsyncValue<TradeUpdate>>(tradeUpdatesProvider, (prev, next) { | ||
| final update = next.valueOrNull; | ||
| if (update == null) return; | ||
| _latest[update.orderId] = update.status; | ||
| _handle(update); | ||
| }); | ||
| return widget.child; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.