Update: 2026-06-11 18:22:57
This commit is contained in:
94
siro_driver/lib/controller/functions/country_logic.dart
Normal file
94
siro_driver/lib/controller/functions/country_logic.dart
Normal file
@@ -0,0 +1,94 @@
|
||||
import 'package:siro_driver/constant/box_name.dart';
|
||||
import 'package:siro_driver/main.dart';
|
||||
|
||||
class CountryLogic {
|
||||
/// Formats the phone number based on the country's dialing rules.
|
||||
static String formatPhone(String phone, String country) {
|
||||
phone = phone.replaceAll(RegExp(r'[ \-\(\)\+]'), '').trim();
|
||||
|
||||
if (country == 'Egypt') {
|
||||
// Rule: 010, 011, 012, 015 -> 2010, 2011, 2012, 2015
|
||||
if (phone.startsWith('0020')) phone = phone.replaceFirst('0020', '20');
|
||||
if (phone.startsWith('01') && phone.length >= 10) {
|
||||
return '20${phone.substring(1)}';
|
||||
}
|
||||
if (phone.startsWith('1') &&
|
||||
phone.length >= 9 &&
|
||||
!phone.startsWith('20')) {
|
||||
return '20$phone';
|
||||
}
|
||||
if (!phone.startsWith('20') && phone.length > 8) return '20$phone';
|
||||
} else if (country == 'Jordan') {
|
||||
// Rule: 07x -> 9627x
|
||||
if (phone.startsWith('00962')) phone = phone.replaceFirst('00962', '962');
|
||||
if (phone.startsWith('07') && phone.length >= 9) {
|
||||
return '962${phone.substring(1)}';
|
||||
}
|
||||
if (phone.startsWith('7') &&
|
||||
phone.length >= 8 &&
|
||||
!phone.startsWith('962')) {
|
||||
return '962$phone';
|
||||
}
|
||||
if (!phone.startsWith('962') && phone.length > 7) return '962$phone';
|
||||
} else {
|
||||
// Default to Syria
|
||||
if (phone.startsWith('00963')) phone = phone.replaceFirst('00963', '963');
|
||||
if (phone.startsWith('0963')) phone = phone.replaceFirst('0963', '963');
|
||||
if (phone.startsWith('096309')) {
|
||||
phone = phone.replaceFirst('096309', '963');
|
||||
}
|
||||
if (phone.startsWith('96309')) phone = '9639${phone.substring(5)}';
|
||||
if (phone.startsWith('9630')) phone = '9639${phone.substring(4)}';
|
||||
if (phone.startsWith('9639') && phone.length == 12) return phone;
|
||||
if (phone.startsWith('963') &&
|
||||
phone.length > 3 &&
|
||||
!phone.startsWith('9639')) {
|
||||
phone = '9639${phone.substring(3)}';
|
||||
}
|
||||
if (phone.startsWith('09')) return '963${phone.substring(1)}';
|
||||
if (phone.startsWith('9') && phone.length == 9) return '963$phone';
|
||||
if (phone.startsWith('0') && phone.length == 10) {
|
||||
return '963${phone.substring(1)}';
|
||||
}
|
||||
}
|
||||
return phone;
|
||||
}
|
||||
|
||||
/// Returns the default country prefix (EG, JO, SY) for UI initial selection.
|
||||
static String getCountryPrefix(String country) {
|
||||
if (country == 'Egypt') return 'EG';
|
||||
if (country == 'Jordan') return 'JO';
|
||||
return 'SY';
|
||||
}
|
||||
|
||||
/// Returns the default emergency number for the country.
|
||||
static String getEmergencyNumber(String country) {
|
||||
if (country == 'Egypt') return '122';
|
||||
if (country == 'Jordan') return '911';
|
||||
return '112'; // Syria
|
||||
}
|
||||
|
||||
/// Returns the hint text for phone inputs based on the country.
|
||||
static String getPhoneHint(String country) {
|
||||
if (country == 'Egypt') return 'e.g. 01012345678 (Default +20)';
|
||||
if (country == 'Jordan') return 'e.g. 0791234567 (Default +962)';
|
||||
return 'e.g. 0912345678 (Default +963)'; // Syria
|
||||
}
|
||||
|
||||
/// Helper to format phone using the current country in box.
|
||||
static String formatCurrentCountryPhone(String phone) {
|
||||
String cleanPhone = phone.replaceAll(RegExp(r'[ \-\(\)]'), '').trim();
|
||||
if (cleanPhone.startsWith('+963') || cleanPhone.startsWith('00963')) {
|
||||
return formatPhone(cleanPhone, 'Syria');
|
||||
}
|
||||
if (cleanPhone.startsWith('+20') || cleanPhone.startsWith('0020')) {
|
||||
return formatPhone(cleanPhone, 'Egypt');
|
||||
}
|
||||
if (cleanPhone.startsWith('+962') || cleanPhone.startsWith('00962')) {
|
||||
return formatPhone(cleanPhone, 'Jordan');
|
||||
}
|
||||
|
||||
final country = box.read(BoxName.countryCode) ?? 'Syria';
|
||||
return formatPhone(cleanPhone, country);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
import 'dart:io';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:siro_driver/views/widgets/error_snakbar.dart';
|
||||
import 'package:siro_driver/controller/functions/country_logic.dart';
|
||||
|
||||
void showInBrowser(String url) async {
|
||||
if (await canLaunchUrl(Uri.parse(url))) {
|
||||
@@ -10,18 +9,11 @@ void showInBrowser(String url) async {
|
||||
}
|
||||
|
||||
String cleanAndFormatPhoneNumber(String phoneNumber) {
|
||||
// 1. Clean the number
|
||||
String formattedNumber = phoneNumber.replaceAll(RegExp(r'\s+'), '');
|
||||
|
||||
// 2. Format logic (Syria/Egypt/International)
|
||||
if (formattedNumber.length > 6) {
|
||||
if (formattedNumber.startsWith('09')) {
|
||||
formattedNumber = '+963${formattedNumber.substring(1)}';
|
||||
} else if (formattedNumber.startsWith('01') && formattedNumber.length == 11) {
|
||||
formattedNumber = '+20${formattedNumber.substring(1)}';
|
||||
} else if (formattedNumber.startsWith('00')) {
|
||||
formattedNumber = '+${formattedNumber.substring(2)}';
|
||||
} else if (!formattedNumber.startsWith('+')) {
|
||||
formattedNumber = CountryLogic.formatCurrentCountryPhone(formattedNumber);
|
||||
if (!formattedNumber.startsWith('+')) {
|
||||
formattedNumber = '+$formattedNumber';
|
||||
}
|
||||
}
|
||||
@@ -30,11 +22,6 @@ String cleanAndFormatPhoneNumber(String phoneNumber) {
|
||||
|
||||
Future<void> makePhoneCall(String phoneNumber) async {
|
||||
String formattedNumber = cleanAndFormatPhoneNumber(phoneNumber);
|
||||
|
||||
if (!formattedNumber.startsWith('+963')) {
|
||||
mySnackeBarError("Calling non-Syrian numbers is not supported".tr);
|
||||
return;
|
||||
}
|
||||
|
||||
// Create URI directly from String to avoid double encoding '+' as '%2B'
|
||||
final Uri launchUri = Uri.parse('tel:$formattedNumber');
|
||||
@@ -63,10 +50,6 @@ void launchCommunication(
|
||||
if (Platform.isIOS) {
|
||||
switch (method) {
|
||||
case 'phone':
|
||||
if (!formattedContact.startsWith('+963')) {
|
||||
mySnackeBarError("Calling non-Syrian numbers is not supported".tr);
|
||||
return;
|
||||
}
|
||||
url = 'tel:$formattedContact';
|
||||
break;
|
||||
case 'sms':
|
||||
@@ -86,10 +69,6 @@ void launchCommunication(
|
||||
} else if (Platform.isAndroid) {
|
||||
switch (method) {
|
||||
case 'phone':
|
||||
if (!formattedContact.startsWith('+963')) {
|
||||
mySnackeBarError("Calling non-Syrian numbers is not supported".tr);
|
||||
return;
|
||||
}
|
||||
url = 'tel:$formattedContact';
|
||||
break;
|
||||
case 'sms':
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'package:siro_driver/constant/currency.dart';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:siro_driver/constant/links.dart';
|
||||
@@ -35,7 +36,7 @@ Future<void> showDriverGiftClaim(BuildContext context) async {
|
||||
if (box.read(BoxName.is_claimed).toString() == '0' ||
|
||||
box.read(BoxName.is_claimed) == null) {
|
||||
MyDialog().getDialog(
|
||||
'You have gift 300 SYP'.tr, 'This for new registration'.tr, () async {
|
||||
'You have gift 300 ${CurrencyHelper.currency}'.tr, 'This for new registration'.tr, () async {
|
||||
Get.back();
|
||||
var res = await CRUD().post(link: AppLink.updateDriverClaim, payload: {
|
||||
'driverId': box.read(BoxName.driverID),
|
||||
|
||||
Reference in New Issue
Block a user