Skip to content

Commit

Permalink
Refactor country code picker (#10)
Browse files Browse the repository at this point in the history
* Refactor picker

* Upgrade dependencies

* Cleanup

* Update README.md

* Upgrade gradle
  • Loading branch information
cp-radhika-s authored Jun 2, 2023
1 parent 398fd81 commit 9d0a91e
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 249 deletions.
6 changes: 0 additions & 6 deletions .idea/compiler.xml

This file was deleted.

20 changes: 0 additions & 20 deletions .idea/inspectionProfiles/Project_Default.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/misc.xml

This file was deleted.

55 changes: 27 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# JetCountryPicker

Country code bottomsheet picker in Jetpack Compose with Search functionality.
Country code bottom sheet picker in Jetpack Compose with Search functionality.

<img src="https://github.com/canopas/JetCountrypicker/blob/main/gif/Peek%202022-04-11%2011-46.gif" />

Expand All @@ -10,47 +10,46 @@ Available on [Maven Central](https://repo1.maven.org/maven2/com/canopas/jetcount

Add the dependency
```gradle
implementation 'com.canopas.jetcountrypicker:jetcountrypicker:1.0.4'
implementation 'com.canopas.jetcountrypicker:jetcountrypicker:1.0.5'
```

## How to use ?
```kotlin
Box {
var expanded by remember { mutableStateOf(false) }
var selectedCountry by remember { mutableStateOf<Country?>(null) }
val focusManager = LocalFocusManager.current

CountryPickerBottomSheet(title = {
```kotlin
Box {
var selectedCountry by remember { mutableStateOf<Country?>(null) }
val modalBottomSheetState = rememberModalBottomSheetState(
initialValue = ModalBottomSheetValue.Hidden
)

CountryPickerBottomSheet(
sheetState = modalBottomSheetState,
bottomSheetTitle = {
Text(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
text = "Select Country", textAlign = TextAlign.Center,
text = stringResource(R.string.select_country_text),
textAlign = TextAlign.Center,
fontWeight = FontWeight.Bold,
fontSize = 20.sp
)
}, expanded, onDismissRequest = {
expanded = false
}, onItemSelected = {
},
onItemSelected = {
selectedCountry = it
expanded = false
focusManager.clearFocus()
}) {
CountryTextField(
label = "Select country",
modifier = Modifier
.padding(top = 50.dp)
.align(Alignment.TopCenter),
expanded,
defaultSelectedCountry = countryList(LocalContext.current).single { it.code == "IN" },
selectedCountry
) {
expanded = !expanded
}

}

) {
CountryTextField(
sheetState = modalBottomSheetState,
label = stringResource(R.string.select_country_text),
modifier = Modifier
.padding(top = 50.dp)
.align(Alignment.TopCenter),
selectedCountry = selectedCountry,
defaultCountry = countryList(LocalContext.current).firstOrNull { it.code == "IN" }
)
}
}
```

# Demo
Expand Down
31 changes: 17 additions & 14 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ plugins {
}

android {
compileSdk 31
compileSdk 33

defaultConfig {
applicationId "com.canopas.campose.jetcountypicker"
minSdk 21
targetSdk 31
targetSdk 33
versionCode 1
versionName "1.0"

Expand All @@ -25,41 +25,44 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}

kotlinOptions {
jvmTarget = '11'
useIR = true
}

buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}

packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
composeOptions {
kotlinCompilerExtensionVersion compose_compiler_version
}
}

dependencies {
implementation project(":countrypicker")
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.core:core-ktx:1.10.1'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"

implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
implementation 'androidx.activity:activity-compose:1.4.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
implementation 'androidx.activity:activity-compose:1.7.2'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.MaterialTheme
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -17,7 +20,6 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
Expand All @@ -43,45 +45,41 @@ class MainActivity : ComponentActivity() {
}
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun SampleCountryPicker() {
Box {
var expanded by remember { mutableStateOf(false) }
var selectedCountry by remember { mutableStateOf<Country?>(null) }
val focusManager = LocalFocusManager.current
val modalBottomSheetState = rememberModalBottomSheetState(
initialValue = ModalBottomSheetValue.Hidden
)

CountryPickerBottomSheet(
title = {
sheetState = modalBottomSheetState,
bottomSheetTitle = {
Text(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
text = stringResource(R.string.select_country_text), textAlign = TextAlign.Center,
text = stringResource(R.string.select_country_text),
textAlign = TextAlign.Center,
fontWeight = FontWeight.Bold,
fontSize = 20.sp
)
},
expanded,
onDismissRequest = {
expanded = false
},
onItemSelected = {
selectedCountry = it
expanded = false
focusManager.clearFocus()
}
) {
CountryTextField(
sheetState = modalBottomSheetState,
label = stringResource(R.string.select_country_text),
modifier = Modifier
.padding(top = 50.dp)
.align(Alignment.TopCenter),
expanded = expanded,
selectedCountry = selectedCountry,
defaultSelectedCountry = countryList(LocalContext.current).single { it.code == "IN" }
) {
expanded = !expanded
}
defaultCountry = countryList(LocalContext.current).firstOrNull { it.code == "IN" }
)
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext {
compose_version = '1.1.0-beta04'
compose_version = '1.4.3'
kotlin_version = '1.8.10'
compose_compiler_version = "1.4.3"
}
}
plugins {
id 'com.android.application' version '7.1.0' apply false
id 'com.android.library' version '7.1.0' apply false
id 'org.jetbrains.kotlin.android' version '1.6.0' apply false
id 'com.android.application' version '7.4.0' apply false
id 'com.android.library' version '7.4.0' apply false
id 'org.jetbrains.kotlin.android' version '1.8.10' apply false
id 'io.github.gradle-nexus.publish-plugin' version "1.1.0"
}
apply from: "${rootDir}/scripts/publish-root.gradle"
Expand Down
20 changes: 10 additions & 10 deletions countrypicker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ ext {
apply from: "${rootDir}/scripts/publish-module.gradle"

android {
compileSdk 31
compileSdk 33

defaultConfig {
minSdk 21
targetSdk 31
targetSdk 33
versionCode 1
versionName "1.0"

Expand All @@ -40,22 +40,22 @@ android {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
kotlinCompilerExtensionVersion compose_compiler_version
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'androidx.core:core-ktx:1.10.1'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation "androidx.compose.material:material-icons-extended:$compose_version"
implementation 'com.google.code.gson:gson:2.8.7'
implementation 'com.google.code.gson:gson:2.10.1'

}
Loading

0 comments on commit 9d0a91e

Please sign in to comment.