prototype complete
This commit is contained in:
94
lib/screens/history/history_controller.dart
Normal file
94
lib/screens/history/history_controller.dart
Normal file
@@ -0,0 +1,94 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:qpay/http/http.dart';
|
||||
|
||||
class HistoryModel {
|
||||
List<Map<String, dynamic>> 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<void> getTransactions(Map<String, String> params) async {
|
||||
model.isLoading = true;
|
||||
model.transactions = getFakeTransactions();
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
params['type'] = 'REQUEST';
|
||||
List<dynamic> response = await http.get(
|
||||
'/transaction?${buildQueryParameters(params)}',
|
||||
);
|
||||
model.transactions =
|
||||
response.map((e) => e as Map<String, dynamic>).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<String, String> params) {
|
||||
if (params.isEmpty) {
|
||||
return '';
|
||||
}
|
||||
|
||||
String query = '';
|
||||
params.forEach((key, value) {
|
||||
query += '$key=$value&';
|
||||
});
|
||||
return query.substring(0, query.length - 1);
|
||||
}
|
||||
|
||||
List<Map<String, dynamic>> 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",
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user