From a249aee87cd67a0e7cfee04e2d1e0a8a866a0759 Mon Sep 17 00:00:00 2001 From: Richard Holzeis Date: Wed, 24 Jan 2024 10:58:36 +0100 Subject: [PATCH] feat: Add polling position change notifier --- webapp/frontend/lib/main.dart | 3 ++ .../lib/trade/order_and_position_table.dart | 32 ++++++++-------- .../lib/trade/position_change_notifier.dart | 38 +++++++++++++++++++ ...ion_service.dart => position_service.dart} | 6 +-- 4 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 webapp/frontend/lib/trade/position_change_notifier.dart rename webapp/frontend/lib/trade/{open_position_service.dart => position_service.dart} (94%) diff --git a/webapp/frontend/lib/main.dart b/webapp/frontend/lib/main.dart index 81989eb0d..6d15b678c 100644 --- a/webapp/frontend/lib/main.dart +++ b/webapp/frontend/lib/main.dart @@ -3,6 +3,8 @@ import 'package:get_10101/auth/auth_service.dart'; import 'package:get_10101/common/version_service.dart'; import 'package:get_10101/logger/logger.dart'; import 'package:get_10101/routes.dart'; +import 'package:get_10101/trade/position_change_notifier.dart'; +import 'package:get_10101/trade/position_service.dart'; import 'package:get_10101/trade/quote_change_notifier.dart'; import 'package:get_10101/trade/quote_service.dart'; import 'package:get_10101/settings/settings_service.dart'; @@ -22,6 +24,7 @@ void main() { Provider(create: (context) => const VersionService()), ChangeNotifierProvider(create: (context) => WalletChangeNotifier(const WalletService())), ChangeNotifierProvider(create: (context) => QuoteChangeNotifier(const QuoteService())), + ChangeNotifierProvider(create: (context) => PositionChangeNotifier(const PositionService())), Provider(create: (context) => const SettingsService()), Provider(create: (context) => AuthService()) ]; diff --git a/webapp/frontend/lib/trade/order_and_position_table.dart b/webapp/frontend/lib/trade/order_and_position_table.dart index 8211499ad..fb6d2a00d 100644 --- a/webapp/frontend/lib/trade/order_and_position_table.dart +++ b/webapp/frontend/lib/trade/order_and_position_table.dart @@ -1,8 +1,9 @@ import 'package:flutter/material.dart'; import 'package:get_10101/common/color.dart'; -import 'package:get_10101/logger/logger.dart'; -import 'package:get_10101/trade/open_position_service.dart'; +import 'package:get_10101/trade/position_change_notifier.dart'; +import 'package:get_10101/trade/position_service.dart'; import 'package:intl/intl.dart'; +import 'package:provider/provider.dart'; class OrderAndPositionTable extends StatefulWidget { const OrderAndPositionTable({super.key}); @@ -53,21 +54,18 @@ class OpenPositionTable extends StatelessWidget { @override Widget build(BuildContext context) { - return FutureBuilder>( - future: OpenPositionsService.fetchOpenPositions(), - builder: (context, snapshot) { - if (snapshot.connectionState == ConnectionState.waiting) { - return const Center(child: CircularProgressIndicator()); - } else if (snapshot.hasError) { - logger.i("received ${snapshot.error}"); - return const Center(child: Text('Error loading data')); - } else if (!snapshot.hasData || snapshot.data!.isEmpty) { - return const Center(child: Text('No data available')); - } else { - return buildTable(snapshot.data!); - } - }, - ); + final positionChangeNotifier = context.watch(); + final positions = positionChangeNotifier.getPositions(); + + if (positions == null) { + return const Center(child: CircularProgressIndicator()); + } + + if (positions.isEmpty) { + return const Center(child: Text('No data available')); + } else { + return buildTable(positions); + } } Widget buildTable(List positions) { diff --git a/webapp/frontend/lib/trade/position_change_notifier.dart b/webapp/frontend/lib/trade/position_change_notifier.dart new file mode 100644 index 000000000..dff6fbd3f --- /dev/null +++ b/webapp/frontend/lib/trade/position_change_notifier.dart @@ -0,0 +1,38 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; +import 'package:get_10101/logger/logger.dart'; +import 'package:get_10101/trade/position_service.dart'; + +class PositionChangeNotifier extends ChangeNotifier { + final PositionService service; + late Timer timer; + + List? _positions; + + PositionChangeNotifier(this.service) { + _refresh(); + Timer.periodic(const Duration(seconds: 2), (timer) async { + _refresh(); + }); + } + + void _refresh() async { + try { + final positions = await service.fetchOpenPositions(); + _positions = positions; + + super.notifyListeners(); + } catch (error) { + logger.e(error); + } + } + + List? getPositions() => _positions; + + @override + void dispose() { + super.dispose(); + timer.cancel(); + } +} diff --git a/webapp/frontend/lib/trade/open_position_service.dart b/webapp/frontend/lib/trade/position_service.dart similarity index 94% rename from webapp/frontend/lib/trade/open_position_service.dart rename to webapp/frontend/lib/trade/position_service.dart index a0222df38..54f79fd50 100644 --- a/webapp/frontend/lib/trade/open_position_service.dart +++ b/webapp/frontend/lib/trade/position_service.dart @@ -3,10 +3,10 @@ import 'package:flutter/cupertino.dart'; import 'package:get_10101/common/http_client.dart'; import 'package:get_10101/common/model.dart'; -class OpenPositionsService { - const OpenPositionsService(); +class PositionService { + const PositionService(); - static Future> fetchOpenPositions() async { + Future> fetchOpenPositions() async { final response = await HttpClientManager.instance.get(Uri(path: '/api/positions')); if (response.statusCode == 200) {