From b26948527eab8e62647647e199f2f0d6af39ef0d Mon Sep 17 00:00:00 2001 From: amin Date: Fri, 6 Dec 2024 18:17:45 +0330 Subject: [PATCH] delete account screen --- lib/drawer_navigation_bar.dart | 10 ++- lib/l10n/app_en.arb | 5 +- lib/l10n/app_fa.arb | 4 +- lib/router/router.dart | 1 + lib/screens/delete_account/screen.dart | 101 +++++++++++++++++++++++++ lib/screens/home/screen.dart | 17 +++-- lib/screens/home/state.dart | 34 +++++++++ lib/services/home/home.dart | 29 +++++++ lib/utils/hive/user_local_db.dart | 14 +++- 9 files changed, 203 insertions(+), 12 deletions(-) create mode 100644 lib/screens/delete_account/screen.dart diff --git a/lib/drawer_navigation_bar.dart b/lib/drawer_navigation_bar.dart index a01469e..7ac0b1a 100644 --- a/lib/drawer_navigation_bar.dart +++ b/lib/drawer_navigation_bar.dart @@ -9,6 +9,7 @@ import 'package:qadirneyriz/config/config.dart'; import 'package:qadirneyriz/screens/aboutUs/screen.dart'; import 'package:qadirneyriz/screens/auth/state/state.dart'; import 'package:qadirneyriz/screens/change_pass/screen.dart'; +import 'package:qadirneyriz/screens/delete_account/screen.dart'; import 'package:qadirneyriz/screens/home/screen.dart'; import 'package:qadirneyriz/screens/home/state.dart'; import 'package:qadirneyriz/screens/meeting/screen.dart'; @@ -47,6 +48,7 @@ class _CustomDrawerNavigationState extends State { const PrivateMeetingsScreen(), const ReportScreen(), const ChangePassScreen(), + const DeleteAccountScreen(), const AboutUsScreen() // Add more screens here ]; @@ -145,10 +147,16 @@ class _CustomDrawerNavigationState extends State { text: AppLocalizations.of(context)!.profile, index: 4, ), + if (userRole == 1) + _buildDrawerItem( + icon: Icons.delete_outlined, + text: AppLocalizations.of(context)!.deleteaccount, + index: 5, + ), _buildDrawerItem( icon: FontAwesomeIcons.info, text: AppLocalizations.of(context)!.aboutus, - index: 5, + index: 6, ), ], ), diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 658e303..57ea574 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -121,5 +121,8 @@ "profile":"User Account", "doyouredit":"You must make at least one change", "changename":"Change Username", "newpass":"Your New Password", - "textaboutus":"The Mizban meeting and appointment management software has been designed and developed with the aim of facilitating and optimizing the processes of organizing organizational and personal meetings under the leadership of Dr. Mohsen Mostafapour. This innovative and user-centric software serves as an efficient tool to enhance coordination and realize the motto **The Codeword of Empathy** within the esteemed **Foulad Ghadir Neyriz** organization. The Mizban project was initiated and launched with the invaluable support and backing of the esteemed CEO, Dr. Mohsen Mostafapour, representing a significant step forward in the organization's path toward growth and excellence." +"deleteaccount": "Delete Account", +"suretodelelteaccount": "Are you sure you want to delete your account?", +"actioncantundo": "This action cannot be undone!", +"textaboutus":"The Mizban meeting and appointment management software has been designed and developed with the aim of facilitating and optimizing the processes of organizing organizational and personal meetings under the leadership of Dr. Mohsen Mostafapour. This innovative and user-centric software serves as an efficient tool to enhance coordination and realize the motto **The Codeword of Empathy** within the esteemed **Foulad Ghadir Neyriz** organization. The Mizban project was initiated and launched with the invaluable support and backing of the esteemed CEO, Dr. Mohsen Mostafapour, representing a significant step forward in the organization's path toward growth and excellence." } diff --git a/lib/l10n/app_fa.arb b/lib/l10n/app_fa.arb index 503ac39..172097b 100644 --- a/lib/l10n/app_fa.arb +++ b/lib/l10n/app_fa.arb @@ -118,7 +118,9 @@ "profile":"حساب کاربری", "doyouredit":"باید حداقل یکی از تغییرات را انجام دهید!", "changename":"تغییر نام کاربری", - "newpass":"رمزعبور جدید شما", + "newpass":"رمزعبور جدید شما", "deleteaccount": "پاک کردن حساب کاربری", + "suretodelelteaccount": "آیا اطمینان دارید که می‌خواهید حساب کاربری خود را حذف کنید؟", + "actioncantundo": "این عمل قابل بازگشت نیست!", "areusuretodeletfile":"آیا اطمینان دارید که میخواهید این فایل را حذف کنید؟", "textaboutus":"نرم‌افزار مدیریت جلسات و ملاقات‌های “میزبان” با هدف تسهیل و بهینه‌سازی فرآیندهای برگزاری جلسات سازمانی و شخصی جناب آقای دکتر محسن مصطفی پور طراحی و توسعه یافته تا ابزاری کارآمد و نوآورانه برای تحقق بیشتر اسم رمز همدلی در مجموعه معظم فولاد غدیر نی ریز باشد . این نرم افزار با حمایت ، همت و پشتیبانی بی‌دریغ مدیریت محترم عامل ( دکتر محسن مصطفی پور ) ایجاد ، توسعه و راه اندازی شده است." } \ No newline at end of file diff --git a/lib/router/router.dart b/lib/router/router.dart index a528122..cd04eba 100644 --- a/lib/router/router.dart +++ b/lib/router/router.dart @@ -47,6 +47,7 @@ final GoRouter router = GoRouter( ), GoRoute( path: '/route', + name: 'route', builder: (BuildContext context, GoRouterState state) { return const SplashScreen(); }, diff --git a/lib/screens/delete_account/screen.dart b/lib/screens/delete_account/screen.dart new file mode 100644 index 0000000..d5a9457 --- /dev/null +++ b/lib/screens/delete_account/screen.dart @@ -0,0 +1,101 @@ +import 'package:flutter/material.dart'; +import 'package:go_router/go_router.dart'; +import 'package:provider/provider.dart'; +import 'package:qadirneyriz/screens/home/state.dart'; +import 'package:qadirneyriz/setting/setting.dart'; +import 'package:qadirneyriz/utils/enums/status.dart'; +import 'package:qadirneyriz/utils/tools/tools.dart'; +import 'package:qadirneyriz/widgets/custom_appbar.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; +import 'package:qadirneyriz/widgets/custom_button.dart'; + +class DeleteAccountScreen extends StatefulWidget { + const DeleteAccountScreen({super.key}); + + @override + State createState() => _DeleteAccountScreenState(); +} + +class _DeleteAccountScreenState extends State { + @override + Widget build(BuildContext context) { + return Consumer( + builder: (context, value, child) { + return CustomScrollView( + slivers: [ + CustomAppbar(), + SliverFillRemaining( + hasScrollBody: false, + child: Padding( + padding: const EdgeInsets.all(16.0), + child: Column( + children: [ + const Icon( + Icons.warning_amber_rounded, + size: 100, + color: Colors.red, + ), + const SizedBox(height: 20), + Text( + AppLocalizations.of(context)!.suretodelelteaccount, + textAlign: TextAlign.center, + style: + TextStyle(fontSize: 18, fontWeight: FontWeight.bold), + ), + const SizedBox(height: 20), + Text( + AppLocalizations.of(context)!.actioncantundo, + textAlign: TextAlign.center, + style: TextStyle(fontSize: 16, color: Colors.grey[600]), + ), + const SizedBox(height: 40), + deleteButton(value), + ], + ), + ), + ), + ], + ); + }, + ); + } + + CustomButton deleteButton(HomeState state) { + switch (state.statusDeleteAccount) { + case Status.loading: + return CustomButton( + onPressed: null, + text: AppLocalizations.of(context)!.loading, + hieght: 50, + ); + + default: + return CustomButton( + onPressed: () async { + final status = await state.deleteAccount(); + if (status == Status.ready) { + final logOut = await setting.userLocalDb.logOut(); + if (logOut) { + context.goNamed('route'); + Tools.showCustomSnackBar(context, + text: state.messageDeleteAccount ?? 'Done successfully', + isError: false); + } else { + Tools.showCustomSnackBar(context, + text: state.messageDeleteAccount ?? + AppLocalizations.of(context)!.error, + isError: true); + } + } else { + Tools.showCustomSnackBar(context, + text: state.messageDeleteAccount ?? + AppLocalizations.of(context)!.error, + isError: true); + } + }, + text: AppLocalizations.of(context)!.deleteaccount, + hieght: 50, + ); + } + } +} diff --git a/lib/screens/home/screen.dart b/lib/screens/home/screen.dart index fb7892e..a7b1775 100644 --- a/lib/screens/home/screen.dart +++ b/lib/screens/home/screen.dart @@ -435,11 +435,18 @@ class _HomeScreenState extends State { AppLocalizations.of(context)!.error, isError: true); } else if (status == Status.ready) { - setting.userLocalDb.logOut(); - context.pushReplacementNamed('login'); - Tools.showCustomSnackBar(context, - text: value.messageLogOut ?? 'Done successfully', - isError: false); + final logOut = await setting.userLocalDb.logOut(); + if (logOut) { + context.goNamed('login'); + Tools.showCustomSnackBar(context, + text: value.messageLogOut ?? 'Done successfully', + isError: false); + } else { + Tools.showCustomSnackBar(context, + text: value.messageLogOut ?? + AppLocalizations.of(context)!.error, + isError: true); + } } }, ), diff --git a/lib/screens/home/state.dart b/lib/screens/home/state.dart index a2d5299..e7627a5 100644 --- a/lib/screens/home/state.dart +++ b/lib/screens/home/state.dart @@ -80,6 +80,40 @@ class HomeState extends ChangeNotifier { return statusLogOut; } + // delete account + Status statusDeleteAccount = Status.empty; + String? messageDeleteAccount; + Map? errorsDeleteAccount; + + Future deleteAccount() async { + statusDeleteAccount = Status.loading; + notifyListeners(); + try { + final result = await homeApi.deleteAccountApi(); + if (result == null) { + statusDeleteAccount = Status.error; + } else { + if (result.isOk) { + statusDeleteAccount = Status.ready; + messageDeleteAccount = result.message; + } else if (result.isOk == false) { + errorsDeleteAccount = result.errors; + messageDeleteAccount = result.message; + statusDeleteAccount = Status.error; + } else { + statusDeleteAccount = Status.error; + } + } + notifyListeners(); + } catch (e) { + statusDeleteAccount = Status.error; + // print(e); + } + notifyListeners(); + // print(statusLogOut); + return statusDeleteAccount; + } + // edit profile Status statusEditProfile = Status.empty; String? messageEditProfile; diff --git a/lib/services/home/home.dart b/lib/services/home/home.dart index 19352e9..b3eb168 100644 --- a/lib/services/home/home.dart +++ b/lib/services/home/home.dart @@ -53,6 +53,35 @@ class HomeApi { return const Result(isOk: false); } +// delete account + Future deleteAccountApi() async { + try { + Map headers = {"Accept": "application/json"}; + + String dataToken = setting.userLocalDb.getUser().token!; + if (dataToken != '') { + headers['Authorization'] = "Bearer $dataToken"; + } + + final res = await Dio().post( + "${config.network.baseUrl}user/deleteaccount", + options: Options(headers: headers)); + + if (res.statusCode == 200 || res.statusCode == 201) { + setting.userLocalDb.logOut(); + return const Result(isOk: true); + } + } on DioException catch (e) { + // print(e); + // print(e.response!.data); + return Result( + isOk: false, + errors: e.response!.data['errors'], + message: e.response!.data['message']); + } + return const Result(isOk: false); + } + // edit profile Future editProfile({String? name, String? password}) async { diff --git a/lib/utils/hive/user_local_db.dart b/lib/utils/hive/user_local_db.dart index f6d411f..a5100bb 100644 --- a/lib/utils/hive/user_local_db.dart +++ b/lib/utils/hive/user_local_db.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:hive/hive.dart'; import 'package:path_provider/path_provider.dart'; +import 'package:qadirneyriz/utils/enums/status.dart'; import 'package:qadirneyriz/utils/hive/local_user.dart'; class UserLocalDb { @@ -56,10 +57,15 @@ class UserLocalDb { await box!.put(key, value); } - void logOut() async { - await openBox(); - await box!.clear(); // حذف تمامی داده‌ها از داخل box - // Navigate to login screen + Future logOut() async { + await openBox(); // باز کردن Box + await box!.clear(); // حذف تمامی داده‌ها از Box + + // بررسی تعداد آیتم‌های باقی‌مانده + final int remainingItems = box!.length; + + // بازگرداندن true اگر داده‌ها حذف شدند، false در غیر این صورت + return remainingItems == 0; } // Future updateUser(LocalUser user) async {