import 'package:flutter/material.dart'; import 'package:qpay/http/http.dart'; import 'package:qpay/models/pageable_model.dart'; class HistoryModel { PageableModel? pageableModel; List> transactions = []; bool isLoading = false; String? errorMessage; } class HistoryController extends ChangeNotifier { final HistoryModel model = HistoryModel(); final Http http = Http(); BuildContext context; HistoryController(this.context); void _showErrorSnackBar(String? message) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(message.toString()), behavior: SnackBarBehavior.floating, backgroundColor: Colors.deepOrange, ), ); } Future getTransactions(Map params) async { model.isLoading = true; if (params['page'] == '0') { model.transactions = getFakeTransactions(); } notifyListeners(); try { params['type'] = 'REQUEST'; Map response = await http.get( '/transaction?${buildQueryParameters(params)}', ); if (params['page'] == '0') { model.transactions = []; } model.pageableModel = PageableModel.fromJson(response); // pagination adds to current list instead of replacing it model.transactions = model.transactions + model.pageableModel!.content .map((e) => e as Map) .toList(); } catch (e) { logger.e(e); _showErrorSnackBar( "Problem fetching transactions, are you connected to the internet?", ); model.errorMessage = e.toString(); model.transactions = []; } finally { model.isLoading = false; notifyListeners(); } } String buildQueryParameters(Map params) { if (params.isEmpty) { return ''; } String query = ''; params.forEach((key, value) { query += '$key=$value&'; }); return query.substring(0, query.length - 1); } List> getFakeTransactions() { return [ { "id": "77b2c479-b803-49fc-b5c3-acbf52cba633", "trace": "22a3044b-8f53-4e43-b4f5-c77dcec48d2d", "amount": 25.0, "charge": 0.0, "gatewayCharge": 0.25, "tax": 0.5, "totalAmount": 25.75, "reference": "c838136c-c015-4a0b-9c7c-52f127c8c109", "paymentProcessorLabel": "MPGS", "paymentProcessorImage": "visa-mastercard.png", "debitPhone": "", "debitAccount": "", "debitCurrency": "USD", "debitRef": "peak", "creditPhone": "", "creditAccount": "07088597534", "billClientId": "powertel_zesa", "billName": "Zesa", "type": "REQUEST", "authType": "WEB", "errorMessage": "", "responseCode": "00", "status": "SUCCESS", "sdkActionId": "0aa0d90d-6132-48a7-a5d8-e796f757c73f", }, ]; } }