account deletion added.

development
Sikander Saleem 3 years ago
parent 8d39f98a3d
commit d794945faf

@ -39,7 +39,7 @@ APIException _throwAPIException(Response response) {
case 404: case 404:
APIError apiError; APIError apiError;
if (response.body != null && response.body.isNotEmpty) { if (response.body != null && response.body.isNotEmpty) {
// var jsonError = jsonDecode(response.body); // var jsonError = jsonDecode(response.body);
apiError = APIError(404, response.body.toString()); apiError = APIError(404, response.body.toString());
} }
return APIException(APIException.BAD_REQUEST, error: apiError); return APIException(APIException.BAD_REQUEST, error: apiError);
@ -68,7 +68,7 @@ class ApiClient {
factory ApiClient() => _instance; factory ApiClient() => _instance;
Future<U> postJsonForObject<T, U>(FactoryConstructor<U> factoryConstructor, String url, T jsonObject, Future<U> postJsonForObject<T, U>(FactoryConstructor<U> factoryConstructor, String url, T jsonObject,
{String token, Map<String, dynamic> queryParameters, Map<String, String> headers, int retryTimes = 0}) async { {String token, Map<String, dynamic> queryParameters, Map<String, String> headers, int retryTimes = 0, bool isPutRequest = false}) async {
var _headers = {'Accept': 'application/json'}; var _headers = {'Accept': 'application/json'};
if (headers != null && headers.isNotEmpty) { if (headers != null && headers.isNotEmpty) {
_headers.addAll(headers); _headers.addAll(headers);
@ -77,7 +77,11 @@ class ApiClient {
print("Url:$url"); print("Url:$url");
print("body:${jsonEncode(jsonObject)}"); print("body:${jsonEncode(jsonObject)}");
} }
var response = await postJsonForResponse(url, jsonObject, token: token, queryParameters: queryParameters, headers: _headers, retryTimes: retryTimes); var response = await postJsonForResponse(url, jsonObject, token: token,
queryParameters: queryParameters,
headers: _headers,
retryTimes: retryTimes,
isPutRequest: isPutRequest);
try { try {
var jsonData = jsonDecode(response.body); var jsonData = jsonDecode(response.body);
return factoryConstructor(jsonData); return factoryConstructor(jsonData);
@ -86,7 +90,8 @@ class ApiClient {
} }
} }
Future<Response> postJsonForResponse<T>(String url, T jsonObject, {String token, Map<String, dynamic> queryParameters, Map<String, String> headers, int retryTimes = 0}) async { Future<Response> postJsonForResponse<T>(String url, T jsonObject,
{String token, Map<String, dynamic> queryParameters, Map<String, String> headers, int retryTimes = 0, bool isPutRequest = false}) async {
String requestBody; String requestBody;
if (jsonObject != null) { if (jsonObject != null) {
requestBody = jsonEncode(jsonObject); requestBody = jsonEncode(jsonObject);
@ -97,10 +102,14 @@ class ApiClient {
} }
} }
return await _postForResponse(url, requestBody, token: token, queryParameters: queryParameters, headers: headers, retryTimes: retryTimes); return await _postForResponse(url, requestBody, token: token,
queryParameters: queryParameters,
headers: headers,
retryTimes: retryTimes,
isPutRequest: isPutRequest);
} }
Future<Response> _postForResponse(String url, requestBody, {String token, Map<String, dynamic> queryParameters, Map<String, String> headers, int retryTimes = 0}) async { Future<Response> _postForResponse(String url, requestBody, {String token, Map<String, dynamic> queryParameters, Map<String, String> headers, int retryTimes = 0, bool isPutRequest = false}) async {
try { try {
var _headers = <String, String>{}; var _headers = <String, String>{};
if (token != null) { if (token != null) {
@ -115,7 +124,7 @@ class ApiClient {
var queryString = new Uri(queryParameters: queryParameters).query; var queryString = new Uri(queryParameters: queryParameters).query;
url = url + '?' + queryString; url = url + '?' + queryString;
} }
var response = await _post(Uri.parse(url), body: requestBody, headers: _headers).timeout(Duration(minutes: 2)); var response = await _post(Uri.parse(url), body: requestBody, headers: _headers, isPutRequest: isPutRequest).timeout(Duration(minutes: 2));
if (response.statusCode >= 200 && response.statusCode < 300) { if (response.statusCode >= 200 && response.statusCode < 300) {
return response; return response;
@ -126,7 +135,11 @@ class ApiClient {
if (retryTimes > 0) { if (retryTimes > 0) {
print('will retry after 3 seconds...'); print('will retry after 3 seconds...');
await Future.delayed(Duration(seconds: 3)); await Future.delayed(Duration(seconds: 3));
return await _postForResponse(url, requestBody, token: token, queryParameters: queryParameters, headers: headers, retryTimes: retryTimes - 1); return await _postForResponse(url, requestBody, token: token,
queryParameters: queryParameters,
headers: headers,
retryTimes: retryTimes - 1,
isPutRequest: isPutRequest);
} else { } else {
throw APIException(APIException.OTHER, arguments: e); throw APIException(APIException.OTHER, arguments: e);
} }
@ -134,7 +147,11 @@ class ApiClient {
if (retryTimes > 0) { if (retryTimes > 0) {
print('will retry after 3 seconds...'); print('will retry after 3 seconds...');
await Future.delayed(Duration(seconds: 3)); await Future.delayed(Duration(seconds: 3));
return await _postForResponse(url, requestBody, token: token, queryParameters: queryParameters, headers: headers, retryTimes: retryTimes - 1); return await _postForResponse(url, requestBody, token: token,
queryParameters: queryParameters,
headers: headers,
retryTimes: retryTimes - 1,
isPutRequest: isPutRequest);
} else { } else {
throw APIException(APIException.OTHER, arguments: e); throw APIException(APIException.OTHER, arguments: e);
} }
@ -144,7 +161,11 @@ class ApiClient {
if (retryTimes > 0) { if (retryTimes > 0) {
print('will retry after 3 seconds...'); print('will retry after 3 seconds...');
await Future.delayed(Duration(seconds: 3)); await Future.delayed(Duration(seconds: 3));
return await _postForResponse(url, requestBody, token: token, queryParameters: queryParameters, headers: headers, retryTimes: retryTimes - 1); return await _postForResponse(url, requestBody, token: token,
queryParameters: queryParameters,
headers: headers,
retryTimes: retryTimes - 1,
isPutRequest: isPutRequest);
} else { } else {
throw APIException(APIException.OTHER, arguments: e); throw APIException(APIException.OTHER, arguments: e);
} }
@ -154,7 +175,8 @@ class ApiClient {
bool _certificateCheck(X509Certificate cert, String host, int port) => true; bool _certificateCheck(X509Certificate cert, String host, int port) => true;
Future<T> _withClient<T>(Future<T> Function(Client) fn) async { Future<T> _withClient<T>(Future<T> Function(Client) fn) async {
var httpClient = HttpClient()..badCertificateCallback = _certificateCheck; var httpClient = HttpClient()
..badCertificateCallback = _certificateCheck;
var client = IOClient(httpClient); var client = IOClient(httpClient);
try { try {
return await fn(client); return await fn(client);
@ -163,5 +185,12 @@ class ApiClient {
} }
} }
Future<Response> _post(url, {Map<String, String> headers, body, Encoding encoding}) => _withClient((client) => client.post(url, headers: headers, body: body, encoding: encoding)); Future<Response> _post(url, {Map<String, String> headers, body, Encoding encoding, bool isPutRequest = false}) =>
_withClient((client) {
if (isPutRequest) {
return client.put(url, headers: headers, body: body, encoding: encoding);
} else {
return client.post(url, headers: headers, body: body, encoding: encoding);
}
});
} }

@ -46,6 +46,12 @@ class UserApiClient {
return await ApiClient().postJsonForObject((json) => GeneralResponseModel.fromJson(json), url, postParams); return await ApiClient().postJsonForObject((json) => GeneralResponseModel.fromJson(json), url, postParams);
} }
Future<GeneralResponseModel> deleteAccount(String userId) async {
String url = "${ApiConsts.user}UserRegistration_Delete";
var postParams = {"userId": userId};
return await ApiClient().postJsonForObject((json) => GeneralResponseModel.fromJson(json), url, postParams, isPutRequest: true);
}
Future<GeneralResponseModel> verifyOTP(String _email, int _otp) async { Future<GeneralResponseModel> verifyOTP(String _email, int _otp) async {
String url = "${ApiConsts.user}OTPVerification"; String url = "${ApiConsts.user}OTPVerification";
var postParams = {"email": _email, "opt": _otp}; var postParams = {"email": _email, "opt": _otp};

@ -41,6 +41,8 @@ class AppState {
String get token => _authenticationUser?.result?.data?.token; String get token => _authenticationUser?.result?.data?.token;
String get userId => _authenticationUser?.result?.data?.userId;
String get userEmail => _authenticationUser?.result?.data?.email; String get userEmail => _authenticationUser?.result?.data?.email;
void setAuthenticationModel(AuthenticationUserModel _authenticationUser) { void setAuthenticationModel(AuthenticationUserModel _authenticationUser) {

@ -102,6 +102,24 @@ class _CommonAppbarState extends State<CommonAppbar> {
} }
} }
void deleteUserAccount() async {
Utils.showLoading(context);
try {
await UserApiClient().deleteAccount(AppState().userId);
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.remove(GlobalConsts.userAuthData);
AppState().setAuthenticationModel(null);
Utils.showToast("تم حذف الحساب");
Navigator.pop(context);
Utils.hideLoading(context);
} catch (ex) {
if (mounted) {
Utils.hideLoading(context);
Utils.handleException(ex, null);
}
}
}
@override @override
void dispose() { void dispose() {
super.dispose(); super.dispose();
@ -325,35 +343,59 @@ class _CommonAppbarState extends State<CommonAppbar> {
), ),
); );
}); });
} else { } else if (index == 1) {
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.remove(GlobalConsts.userAuthData); await prefs.remove(GlobalConsts.userAuthData);
AppState().setAuthenticationModel(null); AppState().setAuthenticationModel(null);
Utils.showToast("تسجيل خروج المستخدم"); Utils.showToast("تسجيل خروج المستخدم");
Navigator.pop(context); Navigator.pop(context);
} else {
await showDialog(
context: context,
barrierColor: ColorConsts.secondaryWhite.withOpacity(0.8),
builder: (BuildContext context) => GeneralDialog(
message: 'بحذف حسابك ، ستتم إزالة بياناتك ولن تتمكن من استرداد حسابك. \ n انقر فوق "نعم" لحذف حسابك.',
onTap: () {
Navigator.pop(context);
deleteUserAccount();
},
showCancelButton: true,
),
);
} }
}, },
icon: SvgPicture.asset("assets/icons/fa_key.svg", height: 25, width: 30, color: ColorConsts.textGrey1), icon: SvgPicture.asset("assets/icons/fa_key.svg", height: 25, width: 30, color: ColorConsts.textGrey1),
itemBuilder: (_) => <PopupMenuItem<int>>[ itemBuilder: (_) => <PopupMenuItem<int>>[
PopupMenuItem( PopupMenuItem(
value: 0, value: 0,
padding: EdgeInsets.fromLTRB(0, 0, 0, 0), padding: EdgeInsets.fromLTRB(4, 0, 4, 0),
height: 24, height: 36,
child: Center( child: Center(
child: Text( child: Text(
'تغيير كلمة المرور', 'تغيير كلمة المرور',
style: TextStyle(color: ColorConsts.primaryBlack, fontSize: 14), style: TextStyle(color: ColorConsts.primaryBlack, fontSize: 16),
), ),
), ),
), ),
PopupMenuItem( PopupMenuItem(
value: 1, value: 1,
padding: EdgeInsets.fromLTRB(0, 0, 0, 0), padding: EdgeInsets.fromLTRB(4, 0, 4, 0),
height: 24, height: 36,
child: Center( child: Center(
child: Text( child: Text(
'تسجيل خروج', 'تسجيل خروج',
style: TextStyle(color: ColorConsts.primaryBlack, fontSize: 14), style: TextStyle(color: ColorConsts.primaryBlack, fontSize: 16),
),
),
),
PopupMenuItem(
value: 2,
padding: EdgeInsets.fromLTRB(4, 0, 4, 0),
height: 36,
child: Center(
child: Text(
'حذف الحساب',
style: TextStyle(color: ColorConsts.primaryBlack, fontSize: 16),
), ),
), ),
), ),

@ -3,7 +3,10 @@ import 'package:tangheem/classes/colors.dart';
class GeneralDialog extends StatelessWidget { class GeneralDialog extends StatelessWidget {
final String message; final String message;
GeneralDialog({Key key, this.message}) : super(key: key); final VoidCallback onTap;
final bool showCancelButton;
GeneralDialog({Key key, this.message, this.onTap, this.showCancelButton = false}) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -30,22 +33,49 @@ class GeneralDialog extends StatelessWidget {
style: TextStyle(color: Colors.white), style: TextStyle(color: Colors.white),
), ),
SizedBox(height: 32), SizedBox(height: 32),
SizedBox( Row(
width: double.infinity, children: [
height: 40, if (showCancelButton)
child: TextButton( Expanded(
onPressed: () => Navigator.pop(context), child: SizedBox(
style: TextButton.styleFrom( width: double.infinity,
primary: Colors.white, height: 40,
padding: EdgeInsets.all(2), child: TextButton(
backgroundColor: ColorConsts.secondaryPink, onPressed: () => Navigator.pop(context),
textStyle: TextStyle(fontSize: 14, fontFamily: "DroidKufi"), style: TextButton.styleFrom(
shape: RoundedRectangleBorder( primary: Colors.black87,
borderRadius: BorderRadius.circular(6.0), padding: EdgeInsets.all(2),
backgroundColor: ColorConsts.textHintGrey,
textStyle: TextStyle(fontSize: 14, fontFamily: "DroidKufi"),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6.0),
),
),
child: Text("لا"),
),
),
),
if (showCancelButton) SizedBox(width: 16),
Expanded(
child: SizedBox(
width: double.infinity,
height: 40,
child: TextButton(
onPressed: onTap ?? () => Navigator.pop(context),
style: TextButton.styleFrom(
primary: Colors.white,
padding: EdgeInsets.all(2),
backgroundColor: ColorConsts.secondaryPink,
textStyle: TextStyle(fontSize: 14, fontFamily: "DroidKufi"),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6.0),
),
),
child: Text("نعم"),
),
), ),
), ),
child: Text("نعم"), ],
),
), ),
], ],
), ),

Loading…
Cancel
Save