From a43e7070f4bf67072487bf27a52b30e2f33a472c Mon Sep 17 00:00:00 2001 From: sidhdhi canopas Date: Mon, 30 Sep 2024 15:16:10 +0530 Subject: [PATCH] fix dial code issue --- .../confirm_number_sheet.dart | 2 +- .../confirm_number_view_model.dart | 17 +++++++++---- .../contact_selection_screen.dart | 5 +++- .../contact_selection_view_model.dart | 18 +++++++------- .../contact_selection_view_model.freezed.dart | 24 ++++++++++++++++++- 5 files changed, 51 insertions(+), 15 deletions(-) diff --git a/khelo/lib/ui/flow/team/add_team_member/confirm_number_sheet/confirm_number_sheet.dart b/khelo/lib/ui/flow/team/add_team_member/confirm_number_sheet/confirm_number_sheet.dart index 1d981d1b..09473de9 100644 --- a/khelo/lib/ui/flow/team/add_team_member/confirm_number_sheet/confirm_number_sheet.dart +++ b/khelo/lib/ui/flow/team/add_team_member/confirm_number_sheet/confirm_number_sheet.dart @@ -63,7 +63,7 @@ class _ConfirmNumberSheetState extends ConsumerState { @override void initState() { notifier = ref.read(confirmNumberStateProvider.notifier); - runPostFrame(() => notifier.setDate( + runPostFrame(() => notifier.setData( widget.code, widget.defaultNumber, widget.isForCreateUser)); super.initState(); } diff --git a/khelo/lib/ui/flow/team/add_team_member/confirm_number_sheet/confirm_number_view_model.dart b/khelo/lib/ui/flow/team/add_team_member/confirm_number_sheet/confirm_number_view_model.dart index d30c26b4..bb7d03ff 100644 --- a/khelo/lib/ui/flow/team/add_team_member/confirm_number_sheet/confirm_number_view_model.dart +++ b/khelo/lib/ui/flow/team/add_team_member/confirm_number_sheet/confirm_number_view_model.dart @@ -21,7 +21,7 @@ class ConfirmNumberViewNotifier extends StateNotifier { ), )); - void setDate( + void setData( CountryCode? code, String? defaultNumber, bool isForCreateUser, @@ -37,14 +37,23 @@ class ConfirmNumberViewNotifier extends StateNotifier { } void onTextChange() { - final isButtonEnabled = state.phoneController.text.length > 3 && - (state.isForCreateUser ? state.nameController.text.length > 3 : true); - state = state.copyWith(isButtonEnable: isButtonEnabled); + final isNameNotEmpty = state.isForCreateUser + ? state.nameController.text.trim().isNotEmpty + : true; + final isPhoneNotEmpty = state.phoneController.text.trim().length > 3; + state = state.copyWith(isButtonEnable: isNameNotEmpty && isPhoneNotEmpty); } void onConfirmTap() { state = state.copyWith(isPop: true); } + + @override + void dispose() { + state.nameController.dispose(); + state.phoneController.dispose(); + super.dispose(); + } } @freezed diff --git a/khelo/lib/ui/flow/team/add_team_member/contact_selection/contact_selection_screen.dart b/khelo/lib/ui/flow/team/add_team_member/contact_selection/contact_selection_screen.dart index 19dac949..d6812d53 100644 --- a/khelo/lib/ui/flow/team/add_team_member/contact_selection/contact_selection_screen.dart +++ b/khelo/lib/ui/flow/team/add_team_member/contact_selection/contact_selection_screen.dart @@ -38,7 +38,7 @@ class _ContactSelectionScreenState @override void initState() { notifier = ref.read(contactSelectionStateProvider.notifier); - runPostFrame(() => notifier.setDate(widget.memberIds)); + runPostFrame(() => notifier.setData(widget.memberIds)); super.initState(); } @@ -70,6 +70,9 @@ class _ContactSelectionScreenState final confirmedNumber = await ConfirmNumberSheet.show< (String, CountryCode, String)>( context, + code: CountryCode.getCountryCodeByAlpha2( + countryAlpha2Code: state.deviceCountryCode, + ), isForCreateUser: true, ); if (context.mounted && confirmedNumber != null) { diff --git a/khelo/lib/ui/flow/team/add_team_member/contact_selection/contact_selection_view_model.dart b/khelo/lib/ui/flow/team/add_team_member/contact_selection/contact_selection_view_model.dart index 722d9c99..d58f4161 100644 --- a/khelo/lib/ui/flow/team/add_team_member/contact_selection/contact_selection_view_model.dart +++ b/khelo/lib/ui/flow/team/add_team_member/contact_selection/contact_selection_view_model.dart @@ -24,15 +24,15 @@ class ContactSelectionViewNotifier final DeviceService _deviceService; List fetchedContacts = []; List memberIds = []; - String? deviceCountryCode = - WidgetsBinding.instance.platformDispatcher.locale.countryCode; ContactSelectionViewNotifier(this._userService, this._deviceService) - : super(const ContactSelectionState()) { + : super(ContactSelectionState( + deviceCountryCode: WidgetsBinding + .instance.platformDispatcher.locale.countryCode)) { fetchCountryCode(); } - void setDate(List memberIds) { + void setData(List memberIds) { this.memberIds = memberIds; checkContactPermission(); } @@ -50,7 +50,8 @@ class ContactSelectionViewNotifier void fetchCountryCode() async { try { - deviceCountryCode = await _deviceService.countryCode; + final deviceCountryCode = await _deviceService.countryCode; + state = state.copyWith(deviceCountryCode: deviceCountryCode); } catch (e) { debugPrint( "ContactSelectionViewNotifier: Error in fetchCountryCode -> $e"); @@ -120,7 +121,7 @@ class ContactSelectionViewNotifier .where((element) => phoneNumber.startsWith(element.dialCode)) .firstOrNull ?.dialCode; - code = matchedCountryCode ?? deviceCountryCode; + code = matchedCountryCode ?? state.deviceCountryCode; final trimFrom = matchedCountryCode != null ? code?.length : 1; phoneNumber = phoneNumber.substring(trimFrom ?? 1); } else { @@ -130,14 +131,14 @@ class ContactSelectionViewNotifier CountryCode? countryCode; if (code == null) { countryCode = CountryCode.getCountryCodeByAlpha2( - countryAlpha2Code: deviceCountryCode, + countryAlpha2Code: state.deviceCountryCode, ); } else { countryCode = CountryCode.getCountryCodeByDialCode(dialCode: code); // handle default returned US code in case not found if (countryCode.dialCode == "+1" && code != "+1") { countryCode = CountryCode.getCountryCodeByAlpha2( - countryAlpha2Code: deviceCountryCode, + countryAlpha2Code: state.deviceCountryCode, ); } } @@ -162,6 +163,7 @@ class ContactSelectionState with _$ContactSelectionState { const factory ContactSelectionState({ Object? error, Object? actionError, + String? deviceCountryCode, UserModel? selectedUser, @Default(false) bool loading, @Default(false) bool isActionInProgress, diff --git a/khelo/lib/ui/flow/team/add_team_member/contact_selection/contact_selection_view_model.freezed.dart b/khelo/lib/ui/flow/team/add_team_member/contact_selection/contact_selection_view_model.freezed.dart index 6403f311..66cf049f 100644 --- a/khelo/lib/ui/flow/team/add_team_member/contact_selection/contact_selection_view_model.freezed.dart +++ b/khelo/lib/ui/flow/team/add_team_member/contact_selection/contact_selection_view_model.freezed.dart @@ -18,6 +18,7 @@ final _privateConstructorUsedError = UnsupportedError( mixin _$ContactSelectionState { Object? get error => throw _privateConstructorUsedError; Object? get actionError => throw _privateConstructorUsedError; + String? get deviceCountryCode => throw _privateConstructorUsedError; UserModel? get selectedUser => throw _privateConstructorUsedError; bool get loading => throw _privateConstructorUsedError; bool get isActionInProgress => throw _privateConstructorUsedError; @@ -41,6 +42,7 @@ abstract class $ContactSelectionStateCopyWith<$Res> { $Res call( {Object? error, Object? actionError, + String? deviceCountryCode, UserModel? selectedUser, bool loading, bool isActionInProgress, @@ -69,6 +71,7 @@ class _$ContactSelectionStateCopyWithImpl<$Res, $Res call({ Object? error = freezed, Object? actionError = freezed, + Object? deviceCountryCode = freezed, Object? selectedUser = freezed, Object? loading = null, Object? isActionInProgress = null, @@ -79,6 +82,10 @@ class _$ContactSelectionStateCopyWithImpl<$Res, return _then(_value.copyWith( error: freezed == error ? _value.error : error, actionError: freezed == actionError ? _value.actionError : actionError, + deviceCountryCode: freezed == deviceCountryCode + ? _value.deviceCountryCode + : deviceCountryCode // ignore: cast_nullable_to_non_nullable + as String?, selectedUser: freezed == selectedUser ? _value.selectedUser : selectedUser // ignore: cast_nullable_to_non_nullable @@ -133,6 +140,7 @@ abstract class _$$ContactSelectionStateImplCopyWith<$Res> $Res call( {Object? error, Object? actionError, + String? deviceCountryCode, UserModel? selectedUser, bool loading, bool isActionInProgress, @@ -160,6 +168,7 @@ class __$$ContactSelectionStateImplCopyWithImpl<$Res> $Res call({ Object? error = freezed, Object? actionError = freezed, + Object? deviceCountryCode = freezed, Object? selectedUser = freezed, Object? loading = null, Object? isActionInProgress = null, @@ -170,6 +179,10 @@ class __$$ContactSelectionStateImplCopyWithImpl<$Res> return _then(_$ContactSelectionStateImpl( error: freezed == error ? _value.error : error, actionError: freezed == actionError ? _value.actionError : actionError, + deviceCountryCode: freezed == deviceCountryCode + ? _value.deviceCountryCode + : deviceCountryCode // ignore: cast_nullable_to_non_nullable + as String?, selectedUser: freezed == selectedUser ? _value.selectedUser : selectedUser // ignore: cast_nullable_to_non_nullable @@ -204,6 +217,7 @@ class _$ContactSelectionStateImpl implements _ContactSelectionState { const _$ContactSelectionStateImpl( {this.error, this.actionError, + this.deviceCountryCode, this.selectedUser, this.loading = false, this.isActionInProgress = false, @@ -217,6 +231,8 @@ class _$ContactSelectionStateImpl implements _ContactSelectionState { @override final Object? actionError; @override + final String? deviceCountryCode; + @override final UserModel? selectedUser; @override @JsonKey() @@ -241,7 +257,7 @@ class _$ContactSelectionStateImpl implements _ContactSelectionState { @override String toString() { - return 'ContactSelectionState(error: $error, actionError: $actionError, selectedUser: $selectedUser, loading: $loading, isActionInProgress: $isActionInProgress, alreadyAdded: $alreadyAdded, hasContactPermission: $hasContactPermission, contacts: $contacts)'; + return 'ContactSelectionState(error: $error, actionError: $actionError, deviceCountryCode: $deviceCountryCode, selectedUser: $selectedUser, loading: $loading, isActionInProgress: $isActionInProgress, alreadyAdded: $alreadyAdded, hasContactPermission: $hasContactPermission, contacts: $contacts)'; } @override @@ -252,6 +268,8 @@ class _$ContactSelectionStateImpl implements _ContactSelectionState { const DeepCollectionEquality().equals(other.error, error) && const DeepCollectionEquality() .equals(other.actionError, actionError) && + (identical(other.deviceCountryCode, deviceCountryCode) || + other.deviceCountryCode == deviceCountryCode) && (identical(other.selectedUser, selectedUser) || other.selectedUser == selectedUser) && (identical(other.loading, loading) || other.loading == loading) && @@ -269,6 +287,7 @@ class _$ContactSelectionStateImpl implements _ContactSelectionState { runtimeType, const DeepCollectionEquality().hash(error), const DeepCollectionEquality().hash(actionError), + deviceCountryCode, selectedUser, loading, isActionInProgress, @@ -290,6 +309,7 @@ abstract class _ContactSelectionState implements ContactSelectionState { const factory _ContactSelectionState( {final Object? error, final Object? actionError, + final String? deviceCountryCode, final UserModel? selectedUser, final bool loading, final bool isActionInProgress, @@ -302,6 +322,8 @@ abstract class _ContactSelectionState implements ContactSelectionState { @override Object? get actionError; @override + String? get deviceCountryCode; + @override UserModel? get selectedUser; @override bool get loading;