prototype complete
This commit is contained in:
129
lib/screens/recipient/recipients_controller.dart
Normal file
129
lib/screens/recipient/recipients_controller.dart
Normal file
@@ -0,0 +1,129 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:qpay/http/http.dart';
|
||||
import 'package:qpay/screens/home/home_controller.dart' as hc;
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:qpay/screens/transaction_controller.dart';
|
||||
|
||||
part 'recipients_controller.freezed.dart';
|
||||
part 'recipients_controller.g.dart';
|
||||
|
||||
class RecipientModel {
|
||||
String? creditAccount;
|
||||
String? errorMessage;
|
||||
hc.BillProvider? selectedProvider;
|
||||
bool isLoading = false;
|
||||
List<Recipient> recipients = [];
|
||||
}
|
||||
|
||||
@freezed
|
||||
abstract class Recipient with _$Recipient {
|
||||
const factory Recipient({
|
||||
String? uid,
|
||||
String? name,
|
||||
String? email,
|
||||
String? phoneNumber,
|
||||
String? address,
|
||||
String? account,
|
||||
String? initials,
|
||||
String? latestProviderLabel,
|
||||
}) = _Recipient;
|
||||
|
||||
factory Recipient.fromJson(Map<String, dynamic> json) =>
|
||||
_$RecipientFromJson(json);
|
||||
}
|
||||
|
||||
class RecipientsController extends ChangeNotifier {
|
||||
final RecipientModel model = RecipientModel();
|
||||
final http = Http();
|
||||
BuildContext context;
|
||||
|
||||
RecipientsController(this.context) {
|
||||
model.selectedProvider =
|
||||
Provider.of<TransactionController>(
|
||||
context,
|
||||
listen: false,
|
||||
).model.selectedProvider;
|
||||
}
|
||||
|
||||
void _showErrorSnackBar(String? message) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(message.toString()),
|
||||
behavior: SnackBarBehavior.floating,
|
||||
backgroundColor: Colors.deepOrange,
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> getRecipients([Map<String, String>? params]) async {
|
||||
model.errorMessage = null;
|
||||
model.isLoading = true;
|
||||
model.recipients = getDummyRecipients();
|
||||
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
List<dynamic> response = await http.get(
|
||||
'/recipients/search?${buildQueryParameters(params ?? {})}',
|
||||
);
|
||||
model.recipients.clear();
|
||||
model.recipients.addAll(response.map((e) => Recipient.fromJson(e)));
|
||||
} catch (e) {
|
||||
_showErrorSnackBar(
|
||||
"Problem fetching recipients, are you connected to the internet?",
|
||||
);
|
||||
model.errorMessage = e.toString();
|
||||
model.recipients = [];
|
||||
logger.e(e);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void updateCreditAccount(String account) {
|
||||
model.creditAccount = account;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void addRecipient(Recipient recipient) {
|
||||
model.recipients.add(recipient);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void removeRecipient(String uid) {
|
||||
model.recipients.removeWhere((recipient) => recipient.uid == uid);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
List<Recipient> getDummyRecipients() {
|
||||
return [
|
||||
Recipient(
|
||||
uid: '1',
|
||||
name: 'Vusumuzi Khoza',
|
||||
phoneNumber: '+263773591219',
|
||||
email: 'vusumuzi@gmail.com',
|
||||
address: '123 Main St, Anytown, USA',
|
||||
account: '1234567890',
|
||||
initials: 'VK',
|
||||
latestProviderLabel: 'MTN',
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user