Skip to content

Commit

Permalink
feat: Add polling position change notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
holzeis committed Jan 24, 2024
1 parent aa563ad commit 3af7e32
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 20 deletions.
3 changes: 3 additions & 0 deletions webapp/frontend/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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())
];
Expand Down
32 changes: 15 additions & 17 deletions webapp/frontend/lib/trade/order_and_position_table.dart
Original file line number Diff line number Diff line change
@@ -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});
Expand Down Expand Up @@ -53,21 +54,18 @@ class OpenPositionTable extends StatelessWidget {

@override
Widget build(BuildContext context) {
return FutureBuilder<List<Position>>(
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<PositionChangeNotifier>();
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<Position> positions) {
Expand Down
38 changes: 38 additions & 0 deletions webapp/frontend/lib/trade/position_change_notifier.dart
Original file line number Diff line number Diff line change
@@ -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<Position>? _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<Position>? getPositions() => _positions;

@override
void dispose() {
super.dispose();
timer.cancel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Position>> fetchOpenPositions() async {
Future<List<Position>> fetchOpenPositions() async {
final response = await HttpClientManager.instance.get(Uri(path: '/api/positions'));

if (response.statusCode == 200) {
Expand Down

0 comments on commit 3af7e32

Please sign in to comment.