102 lines
2.9 KiB
Dart
102 lines
2.9 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:qpay/http/http.dart';
|
|
import 'package:qpay/models/api_response.dart';
|
|
import 'package:qpay/screens/accounts/models/account_model.dart';
|
|
import 'package:qpay/screens/accounts/models/statement_model.dart';
|
|
|
|
class AccountProvider extends ChangeNotifier {
|
|
final Http http = Http();
|
|
|
|
AccountProvider();
|
|
|
|
AccountModel? _account;
|
|
StatementModel? _statement;
|
|
bool _isLoadingAccount = false;
|
|
bool _isLoadingStatement = false;
|
|
String? _accountError;
|
|
String? _statementError;
|
|
|
|
AccountModel? get account => _account;
|
|
StatementModel? get statement => _statement;
|
|
bool get isLoadingAccount => _isLoadingAccount;
|
|
bool get isLoadingStatement => _isLoadingStatement;
|
|
String? get accountError => _accountError;
|
|
String? get statementError => _statementError;
|
|
|
|
Future<ApiResponse<AccountModel>> fetchAccount(String phoneNumber) async {
|
|
_isLoadingAccount = true;
|
|
_accountError = null;
|
|
notifyListeners();
|
|
|
|
try {
|
|
Map<String, dynamic> response = await http.get(
|
|
'/accounts/phone/$phoneNumber',
|
|
);
|
|
|
|
_account = AccountModel.fromJson(response);
|
|
_isLoadingAccount = false;
|
|
notifyListeners();
|
|
return ApiResponse.success(_account!);
|
|
} on DioException catch (e, s) {
|
|
logger.e(s);
|
|
_isLoadingAccount = false;
|
|
_accountError = 'Failed to fetch account. Please try again.';
|
|
notifyListeners();
|
|
return ApiResponse.failure(_accountError!);
|
|
} catch (e, s) {
|
|
logger.e(s);
|
|
_isLoadingAccount = false;
|
|
_accountError = 'An unexpected error occurred. Please try again.';
|
|
notifyListeners();
|
|
return ApiResponse.failure(_accountError!);
|
|
}
|
|
}
|
|
|
|
Future<ApiResponse<StatementModel>> fetchStatement({
|
|
required String phoneNumber,
|
|
required String startDate,
|
|
required String endDate,
|
|
}) async {
|
|
_isLoadingStatement = true;
|
|
_statementError = null;
|
|
notifyListeners();
|
|
|
|
try {
|
|
Map<String, dynamic> response = await http.get(
|
|
'/accounts/phone/$phoneNumber/statement'
|
|
'?startDate=$startDate&endDate=$endDate',
|
|
);
|
|
|
|
_statement = StatementModel.fromJson(response);
|
|
_isLoadingStatement = false;
|
|
notifyListeners();
|
|
return ApiResponse.success(_statement!);
|
|
} on DioException catch (e, s) {
|
|
logger.e(s);
|
|
_isLoadingStatement = false;
|
|
_statementError = 'Failed to fetch statement. Please try again.';
|
|
notifyListeners();
|
|
return ApiResponse.failure(_statementError!);
|
|
} catch (e, s) {
|
|
logger.e(s);
|
|
_isLoadingStatement = false;
|
|
_statementError = 'An unexpected error occurred. Please try again.';
|
|
notifyListeners();
|
|
return ApiResponse.failure(_statementError!);
|
|
}
|
|
}
|
|
|
|
void clearAccount() {
|
|
_account = null;
|
|
_accountError = null;
|
|
notifyListeners();
|
|
}
|
|
|
|
void clearStatement() {
|
|
_statement = null;
|
|
_statementError = null;
|
|
notifyListeners();
|
|
}
|
|
}
|