From 6d5548362c505b83165990a36d60abb9beaf5924 Mon Sep 17 00:00:00 2001 From: MohammadTaha Basiri Date: Fri, 11 Feb 2022 20:43:01 +0330 Subject: [PATCH] D1APP-78 delete user profile --- lib/models/user.dart | 6 +- lib/models/view/action_sheet_data.dart | 2 + .../profile/widgets/profile_photo.dart | 57 +++++++++++++------ lib/providers/user_provider.dart | 16 +++++- lib/services/network/request_helper.dart | 2 +- lib/utils/action_sheet.dart | 25 ++++---- 6 files changed, 74 insertions(+), 34 deletions(-) diff --git a/lib/models/user.dart b/lib/models/user.dart index 01feaf8..92011db 100644 --- a/lib/models/user.dart +++ b/lib/models/user.dart @@ -43,11 +43,11 @@ class User { }) { return User( id: id ?? this.id, - username: username ?? this.username, + username: this.username, phoneNumber: phoneNumber ?? this.phoneNumber, - photo: photo ?? this.photo, + photo: photo, fullName: fullName ?? this.fullName, - email: email ?? this.email, + email: email, ); } } diff --git a/lib/models/view/action_sheet_data.dart b/lib/models/view/action_sheet_data.dart index a95a4a2..6166bb6 100644 --- a/lib/models/view/action_sheet_data.dart +++ b/lib/models/view/action_sheet_data.dart @@ -10,6 +10,7 @@ class ActionSheetData { final IconData? titleIcon; final Color? titleColor; final bool hasDismissButton; + final bool hasConfirmButton; final bool withoutButtonMode; final bool smallDismissButton; @@ -20,6 +21,7 @@ class ActionSheetData { this.onConfirmed, this.titleColor, this.hasDismissButton = true, + this.hasConfirmButton = true, this.titleIcon, this.dismissTitle, this.onDismissed, diff --git a/lib/pages/home/settings/profile/widgets/profile_photo.dart b/lib/pages/home/settings/profile/widgets/profile_photo.dart index 3b307ee..42c15ee 100644 --- a/lib/pages/home/settings/profile/widgets/profile_photo.dart +++ b/lib/pages/home/settings/profile/widgets/profile_photo.dart @@ -1,6 +1,7 @@ import 'package:didvan/config/theme_data.dart'; import 'package:didvan/constants/app_icons.dart'; import 'package:didvan/models/enums.dart'; +import 'package:didvan/models/view/action_sheet_data.dart'; import 'package:didvan/models/view/alert_data.dart'; import 'package:didvan/pages/home/widgets/menu_item.dart'; import 'package:didvan/providers/user_provider.dart'; @@ -78,37 +79,62 @@ class _ProfilePhotoState extends State { Future _openImagePickerSheet() async { FocusScope.of(context).unfocus(); - await showModalBottomSheet( - context: context, - isScrollControlled: true, - backgroundColor: Colors.transparent, - builder: (context) => DidvanCard( - enableBorder: false, - child: Column( + await ActionSheetUtils.showBottomSheet( + data: ActionSheetData( + content: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ const DidvanText('بارگذاری تصویر از:'), const SizedBox(height: 16), - MenuItem( - title: 'دوربین', - onTap: () => _setProfilePhoto(ImageSource.camera), - icon: DidvanIcons.camera_regular, + Padding( + padding: const EdgeInsets.only(right: 20), + child: Column( + children: [ + MenuItem( + title: 'دوربین', + onTap: () => _setProfilePhoto(ImageSource.camera), + icon: DidvanIcons.camera_regular, + ), + const DidvanDivider(), + MenuItem( + title: 'گالری', + onTap: () => _setProfilePhoto(ImageSource.gallery), + icon: DidvanIcons.gallery_file_regular, + ), + ], + ), ), const DidvanDivider(), MenuItem( - title: 'گالری', - onTap: () => _setProfilePhoto(ImageSource.gallery), - icon: DidvanIcons.gallery_file_regular, + title: 'حذف تصویر', + onTap: () => _setProfilePhoto(null), + icon: DidvanIcons.trash_solid, + color: Theme.of(context).colorScheme.secondary, ), ], ), + hasConfirmButton: false, + hasDismissButton: false, + title: 'بارگذاری تصویر از:', ), ); } - Future _setProfilePhoto(ImageSource source) async { + Future _setProfilePhoto(ImageSource? source) async { ActionSheetUtils.pop(); + final state = context.read(); + if (source == null) { + final result = await state.deleteProfilePhoto(); + ActionSheetUtils.showAlert( + AlertData( + message: + result ? 'تصویر پروفایل حذف شد.' : 'حذف تصویر موفقیت آمیز نبود.', + aLertType: result ? ALertType.success : ALertType.error, + ), + ); + return; + } final pickedFile = await MediaService.pickImage(source: source); if (pickedFile != null) { ActionSheetUtils.showLogoLoadingIndicator(); @@ -119,7 +145,6 @@ class _ProfilePhotoState extends State { arguments: { 'bytes': bytes, 'onCropped': () async { - final state = context.read(); final result = await state.setProfilePhoto(pickedFile); ActionSheetUtils.showAlert( AlertData( diff --git a/lib/providers/user_provider.dart b/lib/providers/user_provider.dart index 3c1da30..9d47e85 100644 --- a/lib/providers/user_provider.dart +++ b/lib/providers/user_provider.dart @@ -36,7 +36,7 @@ class UserProvider extends CoreProvier { Future setProfilePhoto(dynamic file) async { appState = AppState.isolatedBusy; final RequestService service = - RequestService(RequestHelper.updateUserProfile); + RequestService(RequestHelper.updateProfilePhoto); await service.multipart(file, 'PUT'); if (service.isSuccess) { user = user.copyWith(photo: service.result['photo']); @@ -47,6 +47,20 @@ class UserProvider extends CoreProvier { return false; } + Future deleteProfilePhoto() async { + appState = AppState.isolatedBusy; + final RequestService service = + RequestService(RequestHelper.updateProfilePhoto); + await service.delete(); + if (service.isSuccess) { + user = user.copyWith(photo: null); + appState = AppState.idle; + return true; + } + appState = AppState.failed; + return false; + } + Future checkUsername(String username) async { if (user.username == username) return true; final RequestService service = RequestService( diff --git a/lib/services/network/request_helper.dart b/lib/services/network/request_helper.dart index bc2278c..ba7e262 100644 --- a/lib/services/network/request_helper.dart +++ b/lib/services/network/request_helper.dart @@ -12,7 +12,7 @@ class RequestHelper { static const String login = _baseUserUrl + '/login'; static const String directs = _baseUserUrl + '/direct'; static const String userInfo = _baseUserUrl + '/info'; - static const String updateUserProfile = _baseUserUrl + '/profile/photo'; + static const String updateProfilePhoto = _baseUserUrl + '/profile/photo'; static const String checkUsername = _baseUserUrl + '/CheckUsername'; static const String updateProfile = _baseUserUrl + '/profile/edit'; static String bookmarks({String? type}) => diff --git a/lib/utils/action_sheet.dart b/lib/utils/action_sheet.dart index 5d61a82..7d4a98e 100644 --- a/lib/utils/action_sheet.dart +++ b/lib/utils/action_sheet.dart @@ -83,9 +83,7 @@ class ActionSheetUtils { decoration: BoxDecoration( color: Theme.of(context).colorScheme.surface, borderRadius: const BorderRadius.vertical( - top: Radius.circular( - 10, - ), + top: Radius.circular(10), ), ), child: SingleChildScrollView( @@ -136,17 +134,18 @@ class ActionSheetUtils { ), ), if (data.hasDismissButton) const SizedBox(width: 20), - Expanded( - flex: data.smallDismissButton ? 2 : 1, - child: DidvanButton( - style: ButtonStyleMode.primary, - onPressed: () { - Navigator.of(context).pop(); - data.onConfirmed?.call(); - }, - title: data.confrimTitle ?? 'تایید', + if (data.hasConfirmButton) + Expanded( + flex: data.smallDismissButton ? 2 : 1, + child: DidvanButton( + style: ButtonStyleMode.primary, + onPressed: () { + Navigator.of(context).pop(); + data.onConfirmed?.call(); + }, + title: data.confrimTitle ?? 'تایید', + ), ), - ), ], ), ],