Skip to content
K M Rejowan Ahmmed edited this page May 13, 2024 · 2 revisions

Utils

This is a collection of utility functions that I use in my projects. This will add some functionality to your project that you might find useful.

Automatic Country Detection

This will detect the country based on the user's device.

    var country by remember {
        mutableStateOf(Country.Bangladesh)
    }

    if (!LocalInspectionMode.current) {
        CCPUtils.getCountryAutomatically(context = LocalContext.current).let {
            it?.let {
                country = it
            }
        }
    }

Phone Number Validation

This will validate the phone number based on the selected country.

    
    val validatePhoneNumber = remember(context) {
        CCPValidator(context = context)
    }

    var isNumberValid: Boolean by rememberSaveable(country, text) {
        mutableStateOf(
            validatePhoneNumber(
                number = text, countryCode = country.countryCode
            ),
        )
    }

     OutlinedTextField(
        value = text,
        onValueChange = {
            isNumberValid = validatePhoneNumber(
                number = it, countryCode = country.countryCode
            )

        },

        // Other properties

     )

Visual Transformation

This will transform the phone number based on the selected country. Attach this to text field to see the transformation. It's already integrated with CountryCodePickerTextField.

    OutlinedTextField(
        // Other properties
        visualTransformation = CCPTransformer(context, country.countryIso)
        // Other properties
    )

Visual Transformation

This will transform the phone number based on the selected country. Attach this to text field to see the transformation. It's already integrated with CountryCodePickerTextField.

    OutlinedTextField(
        // Other properties
        visualTransformation = CCPTransformer(context, country.countryIso)
        // Other properties
    )

View Customization

data class ViewCustomization(
    var showFlag: Boolean = true,
    var showCountryIso: Boolean = false,
    var showCountryName: Boolean = false,
    var showCountryCode: Boolean = true,
    var showArrow: Boolean = true,
    var clipToFull: Boolean = false,
)