haveno-app/lib/tabs/sell/sale_market_offers_tab.dart

75 lines
2.8 KiB
Dart
Raw Normal View History

2024-07-15 19:14:30 +00:00
import 'dart:convert';
2024-07-14 15:34:24 +00:00
import 'package:flutter/material.dart';
2024-09-20 17:16:54 +00:00
import 'package:haveno/providers/prices_provider.dart';
import 'package:haveno/widgets/offer_card_widget.dart';
2024-07-14 15:34:24 +00:00
import 'package:provider/provider.dart';
2024-09-20 17:16:54 +00:00
import 'package:haveno/providers/offers_provider.dart';
import 'package:haveno/proto/compiled/grpc.pbgrpc.dart'; // Ensure you have the correct import for OfferInfo
2024-07-14 15:34:24 +00:00
class SaleMarketOffersTab extends StatelessWidget {
@override
Widget build(BuildContext context) {
final offersProvider = Provider.of<OffersProvider>(context, listen: false);
2024-07-15 19:14:30 +00:00
final pricesProvider = Provider.of<PricesProvider>(context, listen: false);
Future<void> fetchData() async {
if (pricesProvider.prices.isEmpty) {
await pricesProvider.getXmrMarketPrices();
}
await offersProvider.getOffers();
}
2024-07-14 15:34:24 +00:00
return FutureBuilder<void>(
2024-07-15 19:14:30 +00:00
future: fetchData(),
2024-07-14 15:34:24 +00:00
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
} else {
final offers = offersProvider.marketSellOffers;
2024-07-15 19:14:30 +00:00
final marketPrices = pricesProvider.prices;
2024-07-14 15:34:24 +00:00
if (offers == null || offers.isEmpty) {
return const Center(child: Text('No offers available'));
} else {
2024-07-15 19:14:30 +00:00
// Calculate value score for each offer
final List<Map<String, dynamic>> sortedOffers = offers.map((offer) {
final marketPriceInfo = marketPrices.firstWhere(
(marketInfo) => marketInfo.currencyCode == offer.counterCurrencyCode,
orElse: () => MarketPriceInfo()..price = 0.0); // Provide a default value if not found
final marketPrice = marketPriceInfo.price;
final offerPrice = double.tryParse(offer.price) ?? 0.0;
double valueScore = 0;
if (marketPrice > 0) {
valueScore = (offerPrice - marketPrice) / marketPrice * 100;
}
return {
'offer': offer,
'valueScore': valueScore,
};
}).toList();
// Sort offers by value score (highest value first)
sortedOffers.sort((a, b) => (b['valueScore'] as double).compareTo(a['valueScore'] as double));
2024-07-14 15:34:24 +00:00
return Padding(
2024-07-15 19:14:30 +00:00
padding: const EdgeInsets.only(top: 2.0), // Add 2 pixels of padding at the top
2024-07-14 15:34:24 +00:00
child: ListView.builder(
2024-07-15 19:14:30 +00:00
itemCount: sortedOffers.length,
2024-07-14 15:34:24 +00:00
itemBuilder: (context, index) {
2024-07-15 19:14:30 +00:00
final offer = sortedOffers[index]['offer'] as OfferInfo;
2024-07-14 15:34:24 +00:00
return OfferCard(offer: offer);
},
),
);
}
}
},
);
}
}