prototype complete
This commit is contained in:
114
lib/screens/receipt/receipt_controller.dart
Normal file
114
lib/screens/receipt/receipt_controller.dart
Normal file
@@ -0,0 +1,114 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:qpay/http/http.dart';
|
||||
import 'package:qpay/screens/pay/pay_controller.dart' as pc;
|
||||
import 'package:qpay/screens/transaction_controller.dart' as tc;
|
||||
import 'package:qpay/screens/home/home_controller.dart' as hc;
|
||||
|
||||
class ReceiptModel {
|
||||
Map<String, dynamic>? receiptData;
|
||||
bool isLoading = false;
|
||||
String? errorMessage;
|
||||
}
|
||||
|
||||
class ReceiptController extends ChangeNotifier {
|
||||
final ReceiptModel model = ReceiptModel();
|
||||
final Http http = Http();
|
||||
late tc.TransactionController transactionController;
|
||||
late SharedPreferences prefs;
|
||||
|
||||
BuildContext context;
|
||||
|
||||
ReceiptController(this.context) {
|
||||
transactionController = Provider.of<tc.TransactionController>(
|
||||
context,
|
||||
listen: false,
|
||||
);
|
||||
}
|
||||
|
||||
void _showErrorSnackBar(String? message) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(message.toString()),
|
||||
behavior: SnackBarBehavior.floating,
|
||||
backgroundColor: Colors.deepOrange,
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
void setLoading(bool value) {
|
||||
model.isLoading = value;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> repeatTransaction(BuildContext context) async {
|
||||
setLoading(true);
|
||||
// fetch provider & update tran controller
|
||||
String providerId = model.receiptData?['billClientId'] ?? '';
|
||||
Map<String, dynamic> provider = await http.get(
|
||||
'/providers/client/$providerId',
|
||||
);
|
||||
transactionController.model.selectedProvider = hc.BillProvider.fromJson(
|
||||
provider,
|
||||
);
|
||||
|
||||
// update tran controller formdata with credit account, email, name, phone & providerLabel
|
||||
transactionController.model.formData = transactionController.model.formData
|
||||
.copyWith(
|
||||
creditAccount: model.receiptData?['creditAccount'] ?? '',
|
||||
creditPhone: model.receiptData?['creditPhone'] ?? '',
|
||||
creditName: model.receiptData?['creditName'] ?? '',
|
||||
creditEmail: model.receiptData?['creditEmail'] ?? '',
|
||||
);
|
||||
|
||||
// fetch product and update tran controller formdata
|
||||
String providerUid =
|
||||
transactionController.model.selectedProvider?.uid ?? '';
|
||||
String productUid = model.receiptData?['productUid'] ?? '';
|
||||
|
||||
if (productUid.isNotEmpty) {
|
||||
Map<String, dynamic> product = await http.get(
|
||||
'/providers/$providerUid/products/$productUid',
|
||||
);
|
||||
transactionController.model.selectedProduct = pc.BillProduct.fromJson(
|
||||
product,
|
||||
);
|
||||
}
|
||||
|
||||
// update credit amount, notification phone number, payment processor
|
||||
transactionController
|
||||
.model
|
||||
.formData = transactionController.model.formData.copyWith(
|
||||
amount: model.receiptData?['amount'].toStringAsFixed(2) ?? '',
|
||||
creditPhone: model.receiptData?['creditPhone'] ?? '',
|
||||
paymentProcessorLabel: model.receiptData?['paymentProcessorLabel'] ?? '',
|
||||
paymentProcessorName: model.receiptData?['paymentProcessorName'] ?? '',
|
||||
);
|
||||
|
||||
setLoading(false);
|
||||
context.push('/make-payment');
|
||||
}
|
||||
|
||||
Future<void> getReceiptData(String id) async {
|
||||
setLoading(true);
|
||||
try {
|
||||
Map<String, dynamic> response = await http.get('/transaction/$id');
|
||||
model.receiptData = response;
|
||||
} catch (e) {
|
||||
_showErrorSnackBar(
|
||||
"Problem fetching transaction data, are you connected to the internet?",
|
||||
);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
void updateReceiptData(Map<String, dynamic> data) {
|
||||
model.receiptData = data;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user