completed group batch feature
This commit is contained in:
@@ -8,54 +8,37 @@ class HistoryModel {
|
||||
|
||||
List<Map<String, dynamic>> transactions = [];
|
||||
bool isLoading = false;
|
||||
bool isLoadingMore = false;
|
||||
bool isActing = false;
|
||||
String? errorMessage;
|
||||
|
||||
int currentPage = 0;
|
||||
int totalPages = 0;
|
||||
|
||||
String searchQuery = '';
|
||||
String? statusFilter;
|
||||
String? typeFilter;
|
||||
|
||||
bool selectMode = false;
|
||||
Set<String> selectedTransactionIds = {};
|
||||
}
|
||||
|
||||
class HistoryController extends ChangeNotifier {
|
||||
final HistoryModel model = HistoryModel();
|
||||
final Http http = Http();
|
||||
BuildContext context;
|
||||
bool _disposed = false;
|
||||
|
||||
HistoryController(this.context);
|
||||
|
||||
Future<ApiResponse<List<Map<String, dynamic>>>> getTransactions(Map<String, String> params) async {
|
||||
model.isLoading = true;
|
||||
if (params['page'] == '0') {
|
||||
model.transactions = getFakeTransactions();
|
||||
}
|
||||
notifyListeners();
|
||||
@override
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
try {
|
||||
Map<String, dynamic> response = await http.get(
|
||||
'/public/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<String, dynamic>)
|
||||
.toList();
|
||||
|
||||
model.isLoading = false;
|
||||
notifyListeners();
|
||||
return ApiResponse.success(List<Map<String, dynamic>>.from(model.transactions));
|
||||
} catch (e) {
|
||||
logger.e(e);
|
||||
model.errorMessage = e.toString();
|
||||
model.transactions = [];
|
||||
model.isLoading = false;
|
||||
notifyListeners();
|
||||
return ApiResponse.failure(
|
||||
"Problem fetching transactions, are you connected to the internet?",
|
||||
);
|
||||
}
|
||||
void _safeNotify() {
|
||||
if (!_disposed) notifyListeners();
|
||||
}
|
||||
|
||||
String buildQueryParameters(Map<String, String> params) {
|
||||
@@ -70,6 +53,152 @@ class HistoryController extends ChangeNotifier {
|
||||
return query.substring(0, query.length - 1);
|
||||
}
|
||||
|
||||
Future<ApiResponse<List<Map<String, dynamic>>>> getTransactions(
|
||||
Map<String, String> params,
|
||||
) async {
|
||||
final isFirstPage = params['page'] == '0' || params['page'] == null;
|
||||
if (isFirstPage) {
|
||||
model.isLoading = true;
|
||||
model.transactions = [];
|
||||
model.currentPage = 0;
|
||||
} else {
|
||||
model.isLoadingMore = true;
|
||||
}
|
||||
_safeNotify();
|
||||
|
||||
params['partyType'] = 'INDIVIDUAL';
|
||||
|
||||
try {
|
||||
Map<String, dynamic> response = await http.get(
|
||||
'/public/transaction?${buildQueryParameters(params)}',
|
||||
);
|
||||
|
||||
model.pageableModel = PageableModel.fromJson(response);
|
||||
model.currentPage = model.pageableModel!.number;
|
||||
model.totalPages = model.pageableModel!.totalPages;
|
||||
|
||||
final newItems = model.pageableModel!.content
|
||||
.map((e) => Map<String, dynamic>.from(e as Map))
|
||||
.toList();
|
||||
|
||||
if (isFirstPage) {
|
||||
model.transactions = newItems;
|
||||
} else {
|
||||
model.transactions.addAll(newItems);
|
||||
}
|
||||
|
||||
model.isLoading = false;
|
||||
model.isLoadingMore = false;
|
||||
_safeNotify();
|
||||
return ApiResponse.success(model.transactions);
|
||||
} catch (e) {
|
||||
logger.e(e);
|
||||
model.errorMessage = e.toString();
|
||||
if (isFirstPage) {
|
||||
model.transactions = [];
|
||||
}
|
||||
model.isLoading = false;
|
||||
model.isLoadingMore = false;
|
||||
_safeNotify();
|
||||
return ApiResponse.failure(
|
||||
"Problem fetching transactions, are you connected to the internet?",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> searchTransactions(
|
||||
String userId, {
|
||||
String? search,
|
||||
String? status,
|
||||
String? type,
|
||||
}) async {
|
||||
model.searchQuery = search ?? '';
|
||||
model.statusFilter = status;
|
||||
model.typeFilter = type;
|
||||
final params = <String, String>{
|
||||
'userId': userId,
|
||||
'page': '0',
|
||||
'size': '20',
|
||||
'sort': 'createdAt,desc',
|
||||
};
|
||||
if (search != null && search.isNotEmpty) {
|
||||
params['creditAccount'] = search;
|
||||
params['creditEmail'] = search;
|
||||
params['creditPhone'] = search;
|
||||
params['creditName'] = search;
|
||||
params['billName'] = search;
|
||||
params['amount'] = search;
|
||||
}
|
||||
if (status != null && status.isNotEmpty) {
|
||||
params['status'] = status;
|
||||
}
|
||||
if (type != null && type.isNotEmpty) {
|
||||
params['type'] = type;
|
||||
}
|
||||
await getTransactions(params);
|
||||
}
|
||||
|
||||
Future<void> loadNextPage(String userId) async {
|
||||
if (model.isLoadingMore || model.currentPage + 1 >= model.totalPages) {
|
||||
return;
|
||||
}
|
||||
final params = <String, String>{
|
||||
'userId': userId,
|
||||
'page': (model.currentPage + 1).toString(),
|
||||
'size': '20',
|
||||
'sort': 'createdAt,desc',
|
||||
};
|
||||
if (model.searchQuery.isNotEmpty) {
|
||||
params['creditAccount'] = model.searchQuery;
|
||||
params['creditEmail'] = model.searchQuery;
|
||||
params['creditPhone'] = model.searchQuery;
|
||||
params['creditName'] = model.searchQuery;
|
||||
params['billName'] = model.searchQuery;
|
||||
params['amount'] = model.searchQuery;
|
||||
}
|
||||
if (model.statusFilter != null && model.statusFilter!.isNotEmpty) {
|
||||
params['status'] = model.statusFilter!;
|
||||
}
|
||||
if (model.typeFilter != null && model.typeFilter!.isNotEmpty) {
|
||||
params['type'] = model.typeFilter!;
|
||||
}
|
||||
await getTransactions(params);
|
||||
}
|
||||
|
||||
void toggleSelectMode() {
|
||||
model.selectMode = !model.selectMode;
|
||||
if (!model.selectMode) {
|
||||
model.selectedTransactionIds.clear();
|
||||
}
|
||||
_safeNotify();
|
||||
}
|
||||
|
||||
void toggleTransactionSelection(String id) {
|
||||
if (id.isEmpty) return;
|
||||
if (model.selectedTransactionIds.contains(id)) {
|
||||
model.selectedTransactionIds.remove(id);
|
||||
} else {
|
||||
model.selectedTransactionIds.add(id);
|
||||
}
|
||||
_safeNotify();
|
||||
}
|
||||
|
||||
/// Removes selected transactions from the local list. The history
|
||||
/// endpoint does not support deletes, so this is a client-side action.
|
||||
void deleteSelectedTransactions() {
|
||||
final ids = List<String>.from(model.selectedTransactionIds);
|
||||
model.transactions.removeWhere((t) => ids.contains(t['id'] as String?));
|
||||
model.selectMode = false;
|
||||
model.selectedTransactionIds.clear();
|
||||
_safeNotify();
|
||||
}
|
||||
|
||||
void clearFilters() {
|
||||
model.statusFilter = null;
|
||||
model.typeFilter = null;
|
||||
_safeNotify();
|
||||
}
|
||||
|
||||
List<Map<String, dynamic>> getFakeTransactions() {
|
||||
return [
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user