import 'package:fixnum/fixnum.dart' as fixnum; import 'package:flutter/material.dart'; import 'package:haveno_flutter_app/proto/compiled/grpc.pbgrpc.dart'; import 'package:haveno_flutter_app/proto/compiled/pb.pb.dart'; import 'package:haveno_flutter_app/services/haveno_service.dart'; class TradesProvider with ChangeNotifier { final HavenoService _havenoService; List? _trades; TradeInfo? _currentTrade; Map> _chatMessages = {}; TradesProvider(this._havenoService); List? get trades => _trades; TradeInfo? get currentTrade => _currentTrade; Map> get chatMessages => _chatMessages; Future getTrades() async { try { final getTradesReply = await _havenoService.tradesClient.getTrades(GetTradesRequest()); _trades = getTradesReply.trades; _trades?.forEach((trade) { print("Trade: $trade"); }); notifyListeners(); } catch (e) { print("Failed to get trades: $e"); } } Future getTrade(String? tradeId) async { try { final getTradeReply = await _havenoService.tradesClient .getTrade(GetTradeRequest(tradeId: tradeId)); return getTradeReply.trade; } catch (e) { debugPrint("Failed to get trade: $e"); } return null; } Future takeOffer( String? offerId, String? paymentAccountId, fixnum.Int64 amount) async { try { final takeOfferReply = await _havenoService.tradesClient.takeOffer( TakeOfferRequest( offerId: offerId, paymentAccountId: paymentAccountId, amount: amount)); _currentTrade = takeOfferReply.trade; notifyListeners(); } catch (e) { print("Failed to take trades: $e"); rethrow; } } Future sendChatMessage(String? tradeId, String? message) async { try { await _havenoService.tradesClient.sendChatMessage( SendChatMessageRequest(tradeId: tradeId, message: message)); } catch (e) { debugPrint("Failed to send trade chat message: $e"); rethrow; } } Future getChatMessages(String tradeId) async { try { final getChatMessagesReply = await _havenoService.tradesClient .getChatMessages(GetChatMessagesRequest(tradeId: tradeId)); _chatMessages[tradeId] = getChatMessagesReply.message; notifyListeners(); } catch (e) { debugPrint("Failed to get trade chat messages: $e"); } } Future confirmPaymentSent(String tradeId) async { try { await _havenoService.tradesClient .confirmPaymentSent(ConfirmPaymentSentRequest(tradeId: tradeId)); } catch (e) { debugPrint("Failed to confirm payment sent: $e"); rethrow; } } Future confirmPaymentReceived(String tradeId) async { try { await _havenoService.tradesClient.confirmPaymentReceived( ConfirmPaymentReceivedRequest(tradeId: tradeId)); } catch (e) { debugPrint("Failed to confirm payment sent: $e"); rethrow; } } Future completeTrade(String? tradeId) async { try { await _havenoService.tradesClient .completeTrade(CompleteTradeRequest(tradeId: tradeId)); } catch (e) { debugPrint("Failed to complete trade: $e"); rethrow; } } }