Skip to content

Commit

Permalink
manage state from viewmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-ishita-g committed Dec 23, 2024
1 parent 70483e7 commit 30fbf97
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
12 changes: 5 additions & 7 deletions app/lib/ui/flow/setting/profile/profile_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class _ProfileScreenState extends ConsumerState<ProfileScreen> {
_observePop();
_observeAccountDeleted();
_observeError();
_observeIsCurrentUserAdminOfAnyGroup();
_observeIsUserAdminOfAnyGroup();

return AppPage(
title: context.l10n.edit_profile_title,
Expand Down Expand Up @@ -279,10 +279,8 @@ class _ProfileScreenState extends ConsumerState<ProfileScreen> {
background: context.colorScheme.containerLow,
progress: state.deletingAccount,
onPressed: () async {
if (state.currentUserSpace.isEmpty) {
await notifier.isCurrentUserIsAdminOfAnyGroup();
}
if (!state.isCurrentUserAdminOfAnyGroup) {
await notifier.isCurrentUserIsAdminOfAnyGroup();
if (!state.isUserAdminOfAnyGroup) {
showDeleteAccountConfirmation();
}
},
Expand Down Expand Up @@ -333,8 +331,8 @@ class _ProfileScreenState extends ConsumerState<ProfileScreen> {
});
}

void _observeIsCurrentUserAdminOfAnyGroup() {
ref.listen(editProfileViewStateProvider.select((state) => state.isCurrentUserAdminOfAnyGroup), (previous, next) {
void _observeIsUserAdminOfAnyGroup() {
ref.listen(editProfileViewStateProvider.select((state) => state.isUserAdminOfAnyGroup), (previous, next) {
if (next) {
showChangeAdminConfirmation();
}
Expand Down
6 changes: 3 additions & 3 deletions app/lib/ui/flow/setting/profile/profile_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class EditProfileViewNotifier extends StateNotifier<EditProfileViewState> {

Future<void> isCurrentUserIsAdminOfAnyGroup() async {
try {
if (state.isCurrentUserAdminOfAnyGroup) return;
if (state.isUserAdminOfAnyGroup || state.currentUserSpace.isNotEmpty) return;
final spaces = await spaceService.getUserSpaces(user?.id ?? '');
final adminSpaces = spaces
.where((space) => space?.admin_id == user?.id)
Expand All @@ -55,7 +55,7 @@ class EditProfileViewNotifier extends StateNotifier<EditProfileViewState> {
for (final space in adminSpaces) {
final members = await spaceService.getMemberBySpaceId(space.id);
if (members.length > 1) {
state = state.copyWith(isCurrentUserAdminOfAnyGroup: true);
state = state.copyWith(isUserAdminOfAnyGroup: true);
}
}
} catch (error, stack) {
Expand Down Expand Up @@ -162,7 +162,7 @@ class EditProfileViewState with _$EditProfileViewState {
@Default(false) bool accountDeleted,
@Default(false) bool uploadingImage,
@Default(false) bool deletingAccount,
@Default(false) bool isCurrentUserAdminOfAnyGroup,
@Default(false) bool isUserAdminOfAnyGroup,
@Default([]) List<ApiSpace> currentUserSpace,
required TextEditingController firstName,
required TextEditingController lastName,
Expand Down
40 changes: 19 additions & 21 deletions app/lib/ui/flow/setting/profile/profile_view_model.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mixin _$EditProfileViewState {
bool get accountDeleted => throw _privateConstructorUsedError;
bool get uploadingImage => throw _privateConstructorUsedError;
bool get deletingAccount => throw _privateConstructorUsedError;
bool get isCurrentUserAdminOfAnyGroup => throw _privateConstructorUsedError;
bool get isUserAdminOfAnyGroup => throw _privateConstructorUsedError;
List<ApiSpace> get currentUserSpace => throw _privateConstructorUsedError;
TextEditingController get firstName => throw _privateConstructorUsedError;
TextEditingController get lastName => throw _privateConstructorUsedError;
Expand Down Expand Up @@ -52,7 +52,7 @@ abstract class $EditProfileViewStateCopyWith<$Res> {
bool accountDeleted,
bool uploadingImage,
bool deletingAccount,
bool isCurrentUserAdminOfAnyGroup,
bool isUserAdminOfAnyGroup,
List<ApiSpace> currentUserSpace,
TextEditingController firstName,
TextEditingController lastName,
Expand Down Expand Up @@ -83,7 +83,7 @@ class _$EditProfileViewStateCopyWithImpl<$Res,
Object? accountDeleted = null,
Object? uploadingImage = null,
Object? deletingAccount = null,
Object? isCurrentUserAdminOfAnyGroup = null,
Object? isUserAdminOfAnyGroup = null,
Object? currentUserSpace = null,
Object? firstName = null,
Object? lastName = null,
Expand Down Expand Up @@ -124,9 +124,9 @@ class _$EditProfileViewStateCopyWithImpl<$Res,
? _value.deletingAccount
: deletingAccount // ignore: cast_nullable_to_non_nullable
as bool,
isCurrentUserAdminOfAnyGroup: null == isCurrentUserAdminOfAnyGroup
? _value.isCurrentUserAdminOfAnyGroup
: isCurrentUserAdminOfAnyGroup // ignore: cast_nullable_to_non_nullable
isUserAdminOfAnyGroup: null == isUserAdminOfAnyGroup
? _value.isUserAdminOfAnyGroup
: isUserAdminOfAnyGroup // ignore: cast_nullable_to_non_nullable
as bool,
currentUserSpace: null == currentUserSpace
? _value.currentUserSpace
Expand Down Expand Up @@ -170,7 +170,7 @@ abstract class _$$EditProfileViewStateImplCopyWith<$Res>
bool accountDeleted,
bool uploadingImage,
bool deletingAccount,
bool isCurrentUserAdminOfAnyGroup,
bool isUserAdminOfAnyGroup,
List<ApiSpace> currentUserSpace,
TextEditingController firstName,
TextEditingController lastName,
Expand Down Expand Up @@ -198,7 +198,7 @@ class __$$EditProfileViewStateImplCopyWithImpl<$Res>
Object? accountDeleted = null,
Object? uploadingImage = null,
Object? deletingAccount = null,
Object? isCurrentUserAdminOfAnyGroup = null,
Object? isUserAdminOfAnyGroup = null,
Object? currentUserSpace = null,
Object? firstName = null,
Object? lastName = null,
Expand Down Expand Up @@ -239,9 +239,9 @@ class __$$EditProfileViewStateImplCopyWithImpl<$Res>
? _value.deletingAccount
: deletingAccount // ignore: cast_nullable_to_non_nullable
as bool,
isCurrentUserAdminOfAnyGroup: null == isCurrentUserAdminOfAnyGroup
? _value.isCurrentUserAdminOfAnyGroup
: isCurrentUserAdminOfAnyGroup // ignore: cast_nullable_to_non_nullable
isUserAdminOfAnyGroup: null == isUserAdminOfAnyGroup
? _value.isUserAdminOfAnyGroup
: isUserAdminOfAnyGroup // ignore: cast_nullable_to_non_nullable
as bool,
currentUserSpace: null == currentUserSpace
? _value._currentUserSpace
Expand Down Expand Up @@ -280,7 +280,7 @@ class _$EditProfileViewStateImpl implements _EditProfileViewState {
this.accountDeleted = false,
this.uploadingImage = false,
this.deletingAccount = false,
this.isCurrentUserAdminOfAnyGroup = false,
this.isUserAdminOfAnyGroup = false,
final List<ApiSpace> currentUserSpace = const [],
required this.firstName,
required this.lastName,
Expand Down Expand Up @@ -315,7 +315,7 @@ class _$EditProfileViewStateImpl implements _EditProfileViewState {
final bool deletingAccount;
@override
@JsonKey()
final bool isCurrentUserAdminOfAnyGroup;
final bool isUserAdminOfAnyGroup;
final List<ApiSpace> _currentUserSpace;
@override
@JsonKey()
Expand All @@ -339,7 +339,7 @@ class _$EditProfileViewStateImpl implements _EditProfileViewState {

@override
String toString() {
return 'EditProfileViewState(saving: $saving, saved: $saved, allowSave: $allowSave, enablePhone: $enablePhone, enableEmail: $enableEmail, accountDeleted: $accountDeleted, uploadingImage: $uploadingImage, deletingAccount: $deletingAccount, isCurrentUserAdminOfAnyGroup: $isCurrentUserAdminOfAnyGroup, currentUserSpace: $currentUserSpace, firstName: $firstName, lastName: $lastName, email: $email, profileUrl: $profileUrl, error: $error)';
return 'EditProfileViewState(saving: $saving, saved: $saved, allowSave: $allowSave, enablePhone: $enablePhone, enableEmail: $enableEmail, accountDeleted: $accountDeleted, uploadingImage: $uploadingImage, deletingAccount: $deletingAccount, isUserAdminOfAnyGroup: $isUserAdminOfAnyGroup, currentUserSpace: $currentUserSpace, firstName: $firstName, lastName: $lastName, email: $email, profileUrl: $profileUrl, error: $error)';
}

@override
Expand All @@ -361,10 +361,8 @@ class _$EditProfileViewStateImpl implements _EditProfileViewState {
other.uploadingImage == uploadingImage) &&
(identical(other.deletingAccount, deletingAccount) ||
other.deletingAccount == deletingAccount) &&
(identical(other.isCurrentUserAdminOfAnyGroup,
isCurrentUserAdminOfAnyGroup) ||
other.isCurrentUserAdminOfAnyGroup ==
isCurrentUserAdminOfAnyGroup) &&
(identical(other.isUserAdminOfAnyGroup, isUserAdminOfAnyGroup) ||
other.isUserAdminOfAnyGroup == isUserAdminOfAnyGroup) &&
const DeepCollectionEquality()
.equals(other._currentUserSpace, _currentUserSpace) &&
(identical(other.firstName, firstName) ||
Expand All @@ -388,7 +386,7 @@ class _$EditProfileViewStateImpl implements _EditProfileViewState {
accountDeleted,
uploadingImage,
deletingAccount,
isCurrentUserAdminOfAnyGroup,
isUserAdminOfAnyGroup,
const DeepCollectionEquality().hash(_currentUserSpace),
firstName,
lastName,
Expand All @@ -415,7 +413,7 @@ abstract class _EditProfileViewState implements EditProfileViewState {
final bool accountDeleted,
final bool uploadingImage,
final bool deletingAccount,
final bool isCurrentUserAdminOfAnyGroup,
final bool isUserAdminOfAnyGroup,
final List<ApiSpace> currentUserSpace,
required final TextEditingController firstName,
required final TextEditingController lastName,
Expand All @@ -440,7 +438,7 @@ abstract class _EditProfileViewState implements EditProfileViewState {
@override
bool get deletingAccount;
@override
bool get isCurrentUserAdminOfAnyGroup;
bool get isUserAdminOfAnyGroup;
@override
List<ApiSpace> get currentUserSpace;
@override
Expand Down

0 comments on commit 30fbf97

Please sign in to comment.