Skip to content

Commit 82f0116

Browse files
author
Saksham-flutter
committed
Improve In Base Class With Network Communication,
Add Common Validation With Localization, EasyLocalization Added
1 parent 77ccb46 commit 82f0116

File tree

13 files changed

+433
-31
lines changed

13 files changed

+433
-31
lines changed

README.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@ success or any error found;
1919
4.) ResponseBody key which is object type, contain all type of data in object when data is not available it can be null;
2020
Example:-
2121
(Api Success):- {
22-
“ResponseCode”:200,
23-
“ResponseMessage”: “logged in Successfully”,
24-
“Succeeded”:true,
25-
“ResponseBody”:{
26-
“id”:id123455,
27-
“name”: “User Name”
28-
“Profile”: “www.profile.xn--com-9o0a
29-
}
30-
}
22+
“ResponseCode”:200,
23+
“ResponseMessage”: “logged in Successfully”,
24+
“Succeeded”:true,
25+
“ResponseBody”:{
26+
“id”:id123455,
27+
“name”: “User Name”
28+
“Profile”: “www.profile.xn--com-9o0a"
29+
}
30+
}
3131
(Api Error):- {
32-
“ResponseCode”:201,
33-
“ResponseMessage”: “User And Password not matched”,
34-
“Succeeded”:false,
35-
“ResponseBody”:null
36-
}
37-
}
32+
“ResponseCode”:201,
33+
“ResponseMessage”: “User And Password not matched”,
34+
“Succeeded”:false,
35+
“ResponseBody”:null
36+
}
3837

3938

assets/translation/en.json

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"pleaseSelect": "Please select",
3+
"pleaseEnter": "Please enter",
4+
"isRequired": "is Required",
5+
"withoutCredential": "Please enter Email and Password",
6+
"blankEmail": "Please enter email",
7+
"invalidMail": "Please enter Valid Email",
8+
"blankMobile": "Please enter Mobile Number",
9+
"blankZipCode": "Please enter Zip Code",
10+
"zipCodeLengthError": "Zip Code should be 5 digit",
11+
"invalidMobile": "Please enter Valid Mobile Number",
12+
"blankUserName": "Please enter username",
13+
"blankPassword": "Please enter password",
14+
"blankConfirmPassword": "Please enter confirm password",
15+
"passwordNotMatched": "Password didn't matched",
16+
"signUpFieldsBlank": "Please fill all required fields",
17+
"blankName": "Please enter name",
18+
"blankVerificationCode": "Please enter Verification Code",
19+
"verificationCodeLengthError": "Verification Code should be 4 digit",
20+
"oldAndNewPasswordSame": "Old password and New password can't be same",
21+
"passwordRegExInvalid": "Password should include 1 Uppercase, 1 Lowercase, 1 Number and 1 Special Character",
22+
"passwordLengthError": "Password should be between 8 to 16 characters",
23+
"logOutTitle": "Are you sure you want to Logout?",
24+
"genericErrorMessage": "Something went wrong, Please try again later",
25+
"internetErrorMessage": "Please Check Your Internet Connection",
26+
"serverErrorMessage": "Server Error. Try again later",
27+
"formatExceptionMessage": "Format Exception Found :- ",
28+
"privacyError": "You Need To Agree With Our Privacy Policy",
29+
"enterMobileNumber": "Enter Mobile Number",
30+
"enterOtp": "Enter OTP",
31+
"continues": "Continue",
32+
"confirm": "Confirm",
33+
"countryCode": "Country Code",
34+
"details": "Details",
35+
"location": "Location",
36+
"log_in": "Log In",
37+
"sign_up": "Sign Up",
38+
"forgot_password": "Forgot Password?",
39+
"enter_the_verification_code_we_just_sent_you_on_your_email": "Enter the Verification code we just sent you on your Email",
40+
"name": "Name",
41+
"already_registered": "Already Registered?",
42+
"dontHaveAnAccount": "Don’t have an account?",
43+
"password": "Password",
44+
"old_password": "Old Password",
45+
"new_password": "New Password",
46+
"full_name": "Full Name",
47+
"email_address": "Email Address",
48+
"query": "Query",
49+
"submit": "Submit",
50+
"your_query": "Your Query",
51+
"first_name": "First Name",
52+
"email_not_editable": "Email field is non editable",
53+
"email": "Email",
54+
"logout": "Logout",
55+
"sur_name": "Surname",
56+
"confirmEmail": "Confirm Email",
57+
"mobile": "Mobile",
58+
"mobileNumber": "Mobile Number",
59+
"update_profile": "Update Profile",
60+
"profile": "Profile",
61+
"confirm_password": "Confirm Password",
62+
"create_password": "Create Password",
63+
"country": "Country",
64+
"privacy": "Privacy",
65+
"privacyPolicy": "Privacy Policy",
66+
"pleaseEnterYourEMail": "Please enter your Email",
67+
"send_OTP": "Send OTP",
68+
"update": "Update",
69+
"exit_log": "Double click to exit app",
70+
"termsOfService": "Terms of Service",
71+
"send": "Send",
72+
"yourMessage": "Your Message",
73+
"iAgreeWithThe": "I agree with the",
74+
"done": "Done",
75+
"verify": "Verify",
76+
"from": "From",
77+
"resendOtpIn": "Resend OTP In ",
78+
"didNotGetCode": "Didn't Get Code? ",
79+
"resendCode": "Resend Code",
80+
"noDetailsFound": "No Details Found",
81+
"enterMessage": "Enter a Message"
82+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
2+
import 'package:mvc_flutter/data/local/validations/constant_message.dart';
3+
import 'package:mvc_flutter/ui/utils/custom_extension.dart';
4+
5+
mixin CommonValidations {
6+
String? isValidPassword(String? password) {
7+
RegExp rex = ValidationConstant.passwordValidRegEx;
8+
if (password.isNull()) {
9+
return ValidationConstant.blankPassword;
10+
} else if (password!.trim().length < ValidationConstant.passwordMinLength) {
11+
return ValidationConstant.passwordLengthError;
12+
} else if (!rex.hasMatch(password)) {
13+
return ValidationConstant.passwordRegExInvalid;
14+
} else {
15+
return null;
16+
}
17+
}
18+
19+
String? isPasswordAndConfirmValid(String? password, String? confirmPassword) {
20+
if (confirmPassword.isNull()) {
21+
return ValidationConstant.blankConfirmPassword;
22+
} else if (confirmPassword != password) {
23+
return ValidationConstant.passwordNotMatched;
24+
} else {
25+
return null;
26+
}
27+
}
28+
29+
String? isOldPasswordAndNewValid(String? password, String? newPassword) {
30+
if (newPassword == password) {
31+
return ValidationConstant.oldAndNewPasswordSame;
32+
} else {
33+
return null;
34+
}
35+
}
36+
37+
String? isValidEmail(String? email) {
38+
if (email.isNull()) {
39+
return ValidationConstant.blankEmail;
40+
}
41+
final isValid = ValidationConstant.emailValidRegEx.hasMatch(email!.trim());
42+
if (isValid) {
43+
return null;
44+
} else {
45+
return ValidationConstant.invalidMail;
46+
}
47+
}
48+
49+
String? isValidMobile(String? value) {
50+
if (value.isNull()) {
51+
return ValidationConstant.blankMobile;
52+
} else if (/*value!.substring(0, 1) == "0" ||*/ int.tryParse(value!) == 0) {
53+
return ValidationConstant.invalidMobile;
54+
} else if (value.trim().length < ValidationConstant.mobileMinLength ||
55+
value.trim().length > ValidationConstant.mobileMaxLength) {
56+
return ValidationConstant.invalidMobile;
57+
} else {
58+
return null;
59+
}
60+
}
61+
62+
String? isValidCode(String? value) {
63+
if (value.isNull()) {
64+
return ValidationConstant.blankVerificationCode;
65+
} else if (value!.trim().length != ValidationConstant.otpLength) {
66+
return ValidationConstant.verificationCodeLengthError;
67+
} else {
68+
return null;
69+
}
70+
}
71+
72+
String? isRequiredField(String field, String? value) {
73+
if (value.isNull()) {
74+
return '$field ${ValidationConstant.isRequired}';
75+
} else {
76+
return null;
77+
}
78+
}
79+
80+
String? isEnteredField(String field, String? value) {
81+
if (value.isNull()) {
82+
return '${ValidationConstant.pleaseEnter} $field';
83+
} else {
84+
return null;
85+
}
86+
}
87+
88+
String? isValidZipCode(String? value) {
89+
if (value.isNull()) {
90+
return ValidationConstant.blankZipCode;
91+
} else if (value!.length < 5) {
92+
return ValidationConstant.zipCodeLengthError;
93+
} else {
94+
return null;
95+
}
96+
}
97+
98+
String? isSelectedField(String field, String? value) {
99+
if (value.isNull()) {
100+
return '${ValidationConstant.pleaseSelect} $field';
101+
} else {
102+
return null;
103+
}
104+
}
105+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
3+
import 'package:easy_localization/easy_localization.dart';
4+
5+
class ValidationConstant {
6+
static RegExp emailInputRegEx = RegExp(r'^[A-Za-z0-9_.@]+$');
7+
static RegExp nameInputRegEx = RegExp(r'^[a-zA-Z]*$');
8+
static RegExp emailValidRegEx = RegExp(
9+
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,}))$',
10+
);
11+
static RegExp passwordValidRegEx = RegExp(
12+
(r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[+×÷=/_€£₹₩!@#%$^&*()-:;,?`~\|<]).{8,}$'));
13+
static const int passwordMinLength = 8;
14+
static const int mobileMinLength = 5;
15+
static const int maxPassword = 16;
16+
static const int maxNames = 35;
17+
static const int maxZipCode = 6;
18+
static const int otpLength = 4;
19+
static const int zipLength = 5;
20+
static const int mobileMaxLength = 15;
21+
static const int emailMaxLength = 50;
22+
23+
static String pleaseSelect = 'pleaseSelect'.tr();
24+
static String pleaseEnter = 'pleaseEnter'.tr();
25+
static String isRequired = 'isRequired'.tr();
26+
static String withoutCredential = "withoutCredential".tr();
27+
static String blankEmail = "blankEmail".tr();
28+
static String invalidMail = "invalidMail".tr();
29+
static String blankMobile = "blankMobile".tr();
30+
static String blankZipCode = "blankZipCode".tr();
31+
static String zipCodeLengthError = "zipCodeLengthError".tr();
32+
static String invalidMobile = "invalidMobile".tr();
33+
static String blankUserName = "blankUserName".tr();
34+
static String blankPassword = "blankPassword".tr();
35+
static String blankConfirmPassword = "blankConfirmPassword".tr();
36+
static String passwordNotMatched = "passwordNotMatched".tr();
37+
static String signUpFieldsBlank = "signUpFieldsBlank".tr();
38+
static String blankName = "blankName".tr();
39+
static String blankVerificationCode = "blankVerificationCode".tr();
40+
static String verificationCodeLengthError =
41+
"verificationCodeLengthError".tr();
42+
static String oldAndNewPasswordSame = "oldAndNewPasswordSame".tr();
43+
static String passwordRegExInvalid = 'passwordRegExInvalid'.tr();
44+
static String passwordLengthError = 'passwordLengthError'.tr();
45+
static String logOutTitle = 'logOutTitle'.tr();
46+
static String enterMessage = 'enterMessage'.tr();
47+
static String genericErrorMessage = "genericErrorMessage".tr();
48+
static String internetErrorMessage = "internetErrorMessage".tr();
49+
static String serverErrorMessage = "serverErrorMessage".tr();
50+
static String formatExceptionMessage = "formatExceptionMessage".tr();
51+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import 'package:flutter/services.dart';
2+
import 'package:mvc_flutter/data/local/validations/constant_message.dart';
3+
4+
class EmailTextFormatter extends TextInputFormatter {
5+
EmailTextFormatter();
6+
7+
@override
8+
TextEditingValue formatEditUpdate(
9+
TextEditingValue oldValue, TextEditingValue newValue) {
10+
final String newText =
11+
ValidationConstant.emailInputRegEx.stringMatch(newValue.text) ?? "";
12+
TextEditingValue finalText = newText == newValue.text ? newValue : oldValue;
13+
if (finalText.text.length > 55) {
14+
finalText.text.substring(0, 55);
15+
}
16+
return finalText;
17+
}
18+
}
19+
20+
class NameTextFormatter extends TextInputFormatter {
21+
NameTextFormatter();
22+
23+
@override
24+
TextEditingValue formatEditUpdate(
25+
TextEditingValue oldValue, TextEditingValue newValue) {
26+
if (newValue.text.isNotEmpty) {
27+
if (ValidationConstant.nameInputRegEx
28+
.hasMatch(newValue.text.trim().replaceAll(" ", ""))) {
29+
return newValue;
30+
} else {
31+
return oldValue;
32+
}
33+
} else {
34+
return newValue;
35+
}
36+
}
37+
}

lib/data/utils/connectivity_status.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:collection';
23
import 'dart:developer';
34

45
import 'package:connectivity_plus/connectivity_plus.dart';
@@ -8,6 +9,8 @@ import 'package:mvc_flutter/my_app/my_app.dart';
89
import 'package:mvc_flutter/ui/utils/base_class/inetrnet_connection_interupt.dart';
910

1011
class NetworkConnection {
12+
NetworkConnection._();
13+
1114
static late StreamSubscription streamSubscription;
1215
static ValueNotifier<bool> isNetworkAvailable = ValueNotifier(false);
1316
static bool isAvailable = false;
@@ -46,3 +49,36 @@ class NetworkConnection {
4649
}
4750
}
4851
}
52+
53+
mixin InternetConnectionQueue {
54+
final Queue<Function> _functionQueue = Queue();
55+
56+
void addToQueue(Function function) {
57+
if (!_functionQueue.contains(function)) {
58+
_functionQueue.add(function);
59+
}
60+
log("addToQueue working", name: LogTags.internetQueue);
61+
if (_functionQueue.length == 1) {
62+
executeQueuedFunctions();
63+
}
64+
}
65+
66+
Future<void> executeQueuedFunctions() async {
67+
log(
68+
"Checking Function In Pending ${_functionQueue.length}",
69+
name: LogTags.internetQueue,
70+
);
71+
if (NetworkConnection.isNetworkAvailable.value) {
72+
while (_functionQueue.isNotEmpty) {
73+
final function = _functionQueue.first;
74+
try {
75+
await function();
76+
log("executeQueuedFunctions working", name: LogTags.internetQueue);
77+
} catch (e) {
78+
log('Error executing function: $e', name: LogTags.internetQueue);
79+
}
80+
_functionQueue.removeFirst();
81+
}
82+
}
83+
}
84+
}

lib/data/utils/log_tags.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
class LogTags {
2-
LogTags._();
3-
42
static const String apiCall = "API CALL";
53
static const String initialDataLoad = "INITIAL DATA LOAD";
64
static const String ui = "UI";
75
static const String location = "Location";
86
static const String network = "Network";
97
static const String chat = "Chat";
8+
static const String internetQueue = "Internet Queue";
109
}

0 commit comments

Comments
 (0)