Skip to content

Commit

Permalink
fix dial code issue
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sidhdhi-p committed Sep 30, 2024
1 parent f531111 commit a43e707
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class _ConfirmNumberSheetState extends ConsumerState<ConfirmNumberSheet> {
@override
void initState() {
notifier = ref.read(confirmNumberStateProvider.notifier);
runPostFrame(() => notifier.setDate(
runPostFrame(() => notifier.setData(
widget.code, widget.defaultNumber, widget.isForCreateUser));
super.initState();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ConfirmNumberViewNotifier extends StateNotifier<ConfirmNumberViewState> {
),
));

void setDate(
void setData(
CountryCode? code,
String? defaultNumber,
bool isForCreateUser,
Expand All @@ -37,14 +37,23 @@ class ConfirmNumberViewNotifier extends StateNotifier<ConfirmNumberViewState> {
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ class ContactSelectionViewNotifier
final DeviceService _deviceService;
List<Contact> fetchedContacts = [];
List<String> 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<String> memberIds) {
void setData(List<String> memberIds) {
this.memberIds = memberIds;
checkContactPermission();
}
Expand All @@ -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");
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
);
}
}
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,6 +42,7 @@ abstract class $ContactSelectionStateCopyWith<$Res> {
$Res call(
{Object? error,
Object? actionError,
String? deviceCountryCode,
UserModel? selectedUser,
bool loading,
bool isActionInProgress,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -133,6 +140,7 @@ abstract class _$$ContactSelectionStateImplCopyWith<$Res>
$Res call(
{Object? error,
Object? actionError,
String? deviceCountryCode,
UserModel? selectedUser,
bool loading,
bool isActionInProgress,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -204,6 +217,7 @@ class _$ContactSelectionStateImpl implements _ContactSelectionState {
const _$ContactSelectionStateImpl(
{this.error,
this.actionError,
this.deviceCountryCode,
this.selectedUser,
this.loading = false,
this.isActionInProgress = false,
Expand All @@ -217,6 +231,8 @@ class _$ContactSelectionStateImpl implements _ContactSelectionState {
@override
final Object? actionError;
@override
final String? deviceCountryCode;
@override
final UserModel? selectedUser;
@override
@JsonKey()
Expand All @@ -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
Expand All @@ -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) &&
Expand All @@ -269,6 +287,7 @@ class _$ContactSelectionStateImpl implements _ContactSelectionState {
runtimeType,
const DeepCollectionEquality().hash(error),
const DeepCollectionEquality().hash(actionError),
deviceCountryCode,
selectedUser,
loading,
isActionInProgress,
Expand All @@ -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,
Expand All @@ -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;
Expand Down

0 comments on commit a43e707

Please sign in to comment.