Skip to content

Commit

Permalink
feat: Disable close position button when no orders in orderbook
Browse files Browse the repository at this point in the history
  • Loading branch information
klochowicz committed Jul 11, 2023
1 parent 062d498 commit c14c168
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions mobile/lib/features/trade/position_list_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import 'dart:async';
import 'package:expandable/expandable.dart';
import 'package:flutter/material.dart';
import 'package:get_10101/common/value_data_row.dart';
import 'package:get_10101/features/trade/domain/direction.dart';
import 'package:get_10101/features/trade/domain/position.dart';
import 'package:get_10101/features/trade/position_change_notifier.dart';
import 'package:get_10101/features/trade/trade_theme.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';

import 'contract_symbol_icon.dart';

Expand Down Expand Up @@ -45,10 +48,18 @@ class _PositionListItemState extends State<PositionListItem> {
formatter.maximumFractionDigits = 2;

TradeTheme tradeTheme = Theme.of(context).extension<TradeTheme>()!;
PositionChangeNotifier positionChangeNotifier = context.watch<PositionChangeNotifier>();

// We're a bit conservative, we only enable action when we have both bid and ask

// dart cannot promote...
Position notNullPosition = widget.position!;

// XXX: Not sure whether ask or bid price here
bool hasPriceInOrderBook = notNullPosition.direction == Direction.long
? positionChangeNotifier.price?.ask != null
: positionChangeNotifier.price?.bid != null;

if (!isPositionExpired) {
Timer(notNullPosition.expiry.difference(DateTime.now().toUtc()), () {
setState(() {
Expand Down Expand Up @@ -157,12 +168,13 @@ class _PositionListItemState extends State<PositionListItem> {
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ElevatedButton(
onPressed:
notNullPosition.positionState == PositionState.closing || isPositionExpired
? null
: () async {
await widget.onClose();
},
onPressed: notNullPosition.positionState == PositionState.closing ||
isPositionExpired ||
!hasPriceInOrderBook
? null
: () async {
await widget.onClose();
},
child: notNullPosition.positionState == PositionState.closing || isPositionExpired
? const Row(
children: [
Expand Down

0 comments on commit c14c168

Please sign in to comment.