Skip to content

Commit b754dc7

Browse files
author
“TechnourceDeveloper”
committed
Add comments in functions
1 parent 529eeff commit b754dc7

File tree

4 files changed

+103
-6
lines changed

4 files changed

+103
-6
lines changed

app/src/main/java/com/technource/android/utils/Extension.kt

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ fun Toast.successToast(message: String, activity: Activity) {
9898
}
9999
}
100100

101+
/**
102+
Checks if a password is valid based on the following criteria:
103+
Contains at least one uppercase letter
104+
Contains at least one lowercase letter
105+
Contains at least one digit
106+
Contains at least one special character from the set [!@#$&*~]
107+
Has a minimum length of 8 characters
108+
@param password The password to be validated.
109+
@return true if the password is valid, false otherwise.
110+
*/
101111
fun isValidPassword(password: String): Boolean {
102112
val pattern: Pattern
103113
val PASSWORD_PATTERN = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#\\\$&*~]).{8,}\$"
@@ -106,11 +116,21 @@ fun isValidPassword(password: String): Boolean {
106116
return matcher.matches()
107117
}
108118

119+
/**
120+
Checks if an email address is valid.
121+
@param email The email address to be validated.
122+
@return true if the email address is valid, false otherwise.
123+
*/
109124
fun isValidEmail(email: String): Boolean {
110125
val pattern = Patterns.EMAIL_ADDRESS
111126
return pattern.matcher(email).matches()
112127
}
113128

129+
/**
130+
Validates an email address and returns the validation status.
131+
@param email The email address to be validated.
132+
@return The ValidationStatus enum indicating the validation status of the email.
133+
*/
114134
fun validateEmail(email: String?): ValidationStatus {
115135
return when {
116136
email.isNullOrEmpty() -> ValidationStatus.EMPTY_EMAIL
@@ -119,6 +139,11 @@ fun validateEmail(email: String?): ValidationStatus {
119139
}
120140
}
121141

142+
/**
143+
Validates a password and returns the validation status.
144+
@param password The password to be validated.
145+
@return The ValidationStatus enum indicating the validation status of the password.
146+
*/
122147
fun validatePassword(password: String?): ValidationStatus {
123148
return when {
124149
password.isNullOrEmpty() -> ValidationStatus.EMPTY_PASSWORD
@@ -127,48 +152,84 @@ fun validatePassword(password: String?): ValidationStatus {
127152
}
128153
}
129154

155+
/**
156+
Validates a first name and returns the validation status.
157+
@param v The first name to be validated.
158+
@return The ValidationStatus enum indicating the validation status of the first name.
159+
*/
130160
fun validFirstName(v: String?): ValidationStatus {
131161
return when {
132162
v.isNullOrEmpty() -> ValidationStatus.EMPTY_FIRSTNAME
133163
else -> ValidationStatus.VALID
134164
}
135165
}
136166

167+
/**
168+
Validates a last name and returns the validation status.
169+
@param v The last name to be validated.
170+
@return The ValidationStatus enum indicating the validation status of the last name.
171+
*/
137172
fun validLastName(v: String?): ValidationStatus {
138173
return when {
139174
v.isNullOrEmpty() -> ValidationStatus.EMPTY_LASTNAME
140175
else -> ValidationStatus.VALID
141176
}
142177
}
143178

179+
/**
180+
Validates a username and returns the validation status.
181+
@param v The username to be validated.
182+
@return The ValidationStatus enum indicating the validation status of the username.
183+
*/
144184
fun validUsername(v: String?): ValidationStatus {
145185
return when {
146186
v.isNullOrEmpty() -> ValidationStatus.EMPTY_USERNAME
147187
else -> ValidationStatus.VALID
148188
}
149189
}
150190

191+
/**
192+
Validates a home address and returns the validation status.
193+
@param v The home address to be validated.
194+
@return The ValidationStatus enum indicating the validation status of the home address.
195+
*/
151196
fun validHomeAddress(v: String?): ValidationStatus {
152197
return when {
153198
v.isNullOrEmpty() -> ValidationStatus.EMPTY_USERNAME
154199
else -> ValidationStatus.VALID
155200
}
156201
}
157202

203+
/**
204+
Validates an office address and returns the validation status.
205+
@param v The office address to be validated.
206+
@return The ValidationStatus enum indicating the validation status of the office address.
207+
*/
158208
fun validOfficeAddress(v: String?): ValidationStatus {
159209
return when {
160210
v.isNullOrEmpty() -> ValidationStatus.EMPTY_USERNAME
161211
else -> ValidationStatus.VALID
162212
}
163213
}
164214

215+
/**
216+
Validates a mobile number and returns the validation status.
217+
@param v The mobile number to be validated.
218+
@return The ValidationStatus enum indicating the validation status of the mobile number.
219+
*/
165220
fun validMobile(v: String?): ValidationStatus {
166221
return when {
167222
v.isNullOrEmpty() -> ValidationStatus.EMPTY_MOBILENO
168223
else -> ValidationStatus.VALID
169224
}
170225
}
171226

227+
/**
228+
Validates the confirm password and returns the validation status.
229+
@param password The password entered by the user.
230+
@param confirmPassword The confirm password entered by the user.
231+
@return The ValidationStatus enum indicating the validation status of the confirm password.
232+
*/
172233
fun validConfirmPassword(password: String?, confirmPassword: String?): ValidationStatus {
173234
// validate confirm password
174235
if (TextUtils.isEmpty(confirmPassword)) {
@@ -180,12 +241,21 @@ fun validConfirmPassword(password: String?, confirmPassword: String?): Validatio
180241
return ValidationStatus.VALID
181242
}
182243

244+
/**
245+
Retrieves the error message for a specific ValidationStatus.
246+
@param context The context used to retrieve the string resources.
247+
@return The error message corresponding to the ValidationStatus.
248+
*/
183249
fun ValidationStatus.getErrorMessage(context: Context): String? {
184250
val errorMessageResId = ValidationConstants.validationStatusErrorMap[this]
185251
return errorMessageResId?.let { context.getString(it) }
186252
}
187253

188-
// Function to hide the keyboard
254+
/**
255+
Hides the keyboard.
256+
@param context The context used to retrieve the input method manager.
257+
@param view The view from which to retrieve the window token.
258+
*/
189259
fun hideKeyboard(context: Context, view: View) {
190260
val inputMethodManager =
191261
context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

app/src/main/java/com/technource/android/utils/LocalHelper.kt

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ import android.os.Build
88
import android.preference.PreferenceManager
99
import java.util.*
1010

11-
11+
/**
12+
Helper class to manage the application's locale and language settings.
13+
*/
1214
object LocaleHelper {
1315
private const val SELECTED_LANGUAGE = "Locale.Helper.Selected.Language"
1416

15-
// the method is used to set the language at runtime
17+
/**
18+
Sets the locale for the application.
19+
@param context The context.
20+
@param language The language code (e.g., "en", "fr").
21+
@return The updated context with the new locale.
22+
*/
1623
fun setLocale(context: Context, language: String): Context {
1724
persist(context, language)
1825

@@ -23,15 +30,24 @@ object LocaleHelper {
2330
// for devices having lower version of android os
2431
}
2532

33+
/**
34+
Persists the selected language in the shared preferences.
35+
@param context The context.
36+
@param language The selected language.
37+
*/
2638
private fun persist(context: Context, language: String) {
2739
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
2840
val editor = preferences.edit()
2941
editor.putString(SELECTED_LANGUAGE, language)
3042
editor.apply()
3143
}
3244

33-
// the method is used update the language of application by creating
34-
// object of inbuilt Locale class and passing language argument to it
45+
/**
46+
Updates the resources configuration with the specified language for devices running on Android Nougat (API level 24) or higher.
47+
@param context The context.
48+
@param language The language code (e.g., "en", "fr").
49+
@return The updated context with the new resources configuration.
50+
*/
3551
@TargetApi(Build.VERSION_CODES.N)
3652
private fun updateResources(context: Context, language: String): Context {
3753
val locale = Locale(language)
@@ -42,6 +58,12 @@ object LocaleHelper {
4258
return context.createConfigurationContext(configuration)
4359
}
4460

61+
/**
62+
Updates the resources configuration with the specified language for devices running on Android versions lower than Nougat (API level 24).
63+
@param context The context.
64+
@param language The language code (e.g., "en", "fr").
65+
@return The updated context with the new resources configuration.
66+
*/
4567
private fun updateResourcesLegacy(context: Context, language: String): Context {
4668
val locale = Locale(language)
4769
Locale.setDefault(locale)

app/src/main/java/com/technource/android/utils/ValidationConstants.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package com.technource.android.utils
22

33
import com.example.android_kotlin_boilerplate.R
44

5-
5+
/**
6+
Object containing constants related to validation.
7+
*/
68
object ValidationConstants {
79
// The validationStatusErrorMap is a map that maps each validation status to its corresponding error message resource ID.
810
val validationStatusErrorMap = mapOf(

app/src/main/java/com/technource/android/utils/ValidationStatus.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.technource.android.utils
22

3+
/**
4+
Enum class representing the validation status for various fields.
5+
*/
36
enum class ValidationStatus {
47
VALID,
58
EMPTY_EMAIL,

0 commit comments

Comments
 (0)