You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
car_common_app/lib/extensions/string_extensions.dart

371 lines
9.4 KiB
Dart

import 'package:auto_size_text/auto_size_text.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:mc_common_app/theme/colors.dart';
import 'package:mc_common_app/utils/enums.dart';
import 'package:mc_common_app/utils/enums.dart';
extension EmailValidator on String {
Widget toText(
{Color? color,
bool isBold = false,
double? fontSize,
bool isUnderLine = false,
bool isItalic = false,
TextDecoration? textDecoration,
double letterSpacing = -0.4,
TextAlign? textAlign,
FontWeight? fontWeight,
double? height,
int? maxLines}) =>
AutoSizeText(
this,
textAlign: textAlign,
maxLines: maxLines,
style: TextStyle(
fontStyle: isItalic ? FontStyle.italic : null,
height: height,
decoration: isUnderLine ? TextDecoration.underline : textDecoration ?? TextDecoration.none,
fontSize: fontSize ?? 10,
fontWeight: isBold ? FontWeight.bold : fontWeight ?? FontWeight.w600,
color: color ?? MyColors.darkTextColor,
letterSpacing: letterSpacing,
),
);
bool isValidEmail() {
return RegExp(r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$').hasMatch(this);
}
bool isNum() {
return RegExp(r'^[0-9]+$').hasMatch(this);
}
getMonth(int month) {
switch (month) {
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
}
}
}
extension FormatDate on String {
/// get month by
/// [month] convert month number in to month name
/// get month by
/// [month] convert month number in to month name in Arabic
static String getMonthArabic(int month) {
switch (month) {
case 1:
return "يناير";
case 2:
return " فبراير";
case 3:
return "مارس";
case 4:
return "أبريل";
case 5:
return "مايو";
case 6:
return "يونيو";
case 7:
return "يوليو";
case 8:
return "أغسطس";
case 9:
return "سبتمبر";
case 10:
return " اكتوبر";
case 11:
return " نوفمبر";
case 12:
return "ديسمبر";
default:
return "";
}
}
static String getMonth(int month) {
switch (month) {
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
default:
return "";
}
}
String toFormattedDate() {
String date = split("T")[0];
String time = split("T")[1];
var dates = date.split("-");
return "${dates[2]} ${getMonth(int.parse(dates[1]))} ${dates[0]} ${DateFormat('hh:mm a', "en_US").format(DateFormat('hh:mm:ss', "en_US").parse(time))}";
}
String toFormattedDateWithoutTime() {
String date = split("T")[0];
var dates = date.split("-");
return "${dates[2]} ${getMonth(int.parse(dates[1]))}, ${dates[0]}";
}
}
extension AdPostEnum on int {
AdPostStatus toAdPostEnum() {
if (this == 1) {
return AdPostStatus.pendingForReview;
} else if (this == 2) {
return AdPostStatus.pendingForPayment;
} else if (this == 3) {
return AdPostStatus.rejected;
} else if (this == 4) {
return AdPostStatus.cancelled;
} else if (this == 5) {
return AdPostStatus.pendingForPost;
} else if (this == 6) {
return AdPostStatus.active;
} else if (this == 7) {
return AdPostStatus.expired;
} else if (this == 8) {
return AdPostStatus.sold;
} else if (this == 9) {
return AdPostStatus.reserved;
} else if (this == 10) {
return AdPostStatus.buyingService;
} else if (this == 11) {
return AdPostStatus.reserveCancel;
} else if (this == -1) {
return AdPostStatus.allAds;
} else {
return AdPostStatus.pendingForPost;
}
}
}
extension AdPostStatusToInt on AdPostStatus {
int getIdFromAdPostStatusEnum() {
switch (this) {
case AdPostStatus.pendingForReview:
return 1;
case AdPostStatus.pendingForPayment:
return 2;
case AdPostStatus.rejected:
return 3;
case AdPostStatus.cancelled:
return 4;
case AdPostStatus.pendingForPost:
return 5;
case AdPostStatus.active:
return 6;
case AdPostStatus.expired:
return 7;
case AdPostStatus.sold:
return 8;
case AdPostStatus.reserved:
return 9;
case AdPostStatus.buyingService:
return 10;
case AdPostStatus.reserveCancel:
return 11;
default:
return -1;
}
}
}
extension CreatedByRoleEnumToInt on CreatedByRoleEnum {
int getIdFromCreatedByRoleEnum() {
switch (this) {
case CreatedByRoleEnum.admin:
return 1;
case CreatedByRoleEnum.customer:
return 2;
case CreatedByRoleEnum.provider:
return 3;
case CreatedByRoleEnum.allAds:
return -1;
default:
return 1;
}
}
}
extension AdReserveStatusEnum on int {
AdReserveStatus toAdRserveStatusEnum() {
if (this == 0) {
return AdReserveStatus.defaultStatus;
} else if (this == 1) {
return AdReserveStatus.reserved;
} else if (this == 2) {
return AdReserveStatus.cancelledByOwner;
} else if (this == 3) {
return AdReserveStatus.cancelledByAdmin;
} else {
return AdReserveStatus.defaultStatus;
}
}
}
extension AdOwnerEnum on int {
CreatedByRoleEnum toCreatedByRoleEnum() {
if (this == -1) {
return CreatedByRoleEnum.allAds;
} else if (this == 1) {
return CreatedByRoleEnum.admin;
} else if (this == 2) {
return CreatedByRoleEnum.customer;
} else if (this == 3) {
return CreatedByRoleEnum.provider;
}
return CreatedByRoleEnum.customer;
}
}
extension BranchsEnum on int {
BranchStatusEnum toBranchStatusEnum() {
if (this == 1) {
return BranchStatusEnum.pending;
} else if (this == 2) {
return BranchStatusEnum.review;
} else if (this == 3) {
return BranchStatusEnum.approvedOrActive;
} else if (this == 4) {
return BranchStatusEnum.rejected;
} else if (this == 5) {
return BranchStatusEnum.blocked;
} else if (this == 6) {
return BranchStatusEnum.deactivated;
} else {
return BranchStatusEnum.pending;
}
}
}
extension ServiceEnum on int {
ServiceStatusEnum toServiceStatusEnum() {
if (this == 1) {
return ServiceStatusEnum.pending;
} else if (this == 2) {
return ServiceStatusEnum.review;
} else if (this == 3) {
return ServiceStatusEnum.approvedOrActive;
} else if (this == 4) {
return ServiceStatusEnum.rejected;
} else if (this == 5) {
return ServiceStatusEnum.blocked;
} else if (this == 6) {
return ServiceStatusEnum.deactivated;
} else {
return ServiceStatusEnum.pending;
}
}
}
extension DateTimeConversions on DateTime {
String getTimeAgo({bool numericDates = true}) {
final date2 = DateTime.now();
final difference = date2.difference(this);
if ((difference.inDays / 7).floor() >= 1) {
return (numericDates) ? '1 week ago' : 'Last week';
} else if (difference.inDays >= 2) {
return '${difference.inDays} days ago';
} else if (difference.inDays >= 1) {
return (numericDates) ? '1 day ago' : 'Yesterday';
} else if (difference.inHours >= 2) {
return '${difference.inHours} hours ago';
} else if (difference.inHours >= 1) {
return (numericDates) ? '1 hour ago' : 'An hour ago';
} else if (difference.inMinutes >= 2) {
return '${difference.inMinutes} minutes ago';
} else if (difference.inMinutes >= 1) {
return (numericDates) ? '1 minute ago' : 'A minute ago';
} else if (difference.inSeconds >= 3) {
return '${difference.inSeconds} seconds ago';
} else {
return 'Just now';
}
}
}
extension VehicleAdTypeEnum on int {
VehicleType toVehicleTypeEnum() {
if (this == 1) {
return VehicleType.car;
} else if (this == 2) {
return VehicleType.motorCycle;
} else if (this == 3) {
return VehicleType.golfCart;
} else if (this == 4) {
return VehicleType.buggy;
} else {
return VehicleType.car;
}
}
String toVehicleTypeString() {
if (this == 1) {
return "Car";
} else if (this == 2) {
return "Motorcycle";
} else if (this == 3) {
return "Golf Cart";
} else if (this == 4) {
return "Buggy";
} else {
return "Car";
}
}
}