Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compose support #56

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

[*.kt]
# Recommended is 100 but some action names are very long that can not fit in the limit.
# 120 seems to be a sweet point for this project
# In general length should be 100 so added visual guide should help
max_line_length=120
ij_visual_guides =100,120
ktlint_function_naming_ignore_when_annotated_with=Composable
ktlint_standard_no-wildcard-imports = disabled


[*Test.kt]
max_line_length=off
ktlint_function_naming_ignore_when_annotated_with=Composable
ktlint_standard_no-wildcard-imports = disabled

2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
not find required country in the preferred countries so that section is of no use when searching.
Although, preferred country will continue to show up in regular filtered list if matches for
query. @hbb20
- Add support to JetPack Compose @hbb20
- Out of box composables for Material2 @hbb20

## v0.0.7 2022-01

Expand Down
48 changes: 29 additions & 19 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,49 @@ android {
versionCode = BuildData.versionCode
versionName = BuildData.versionName
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}
buildFeatures {
dataBinding = true
viewBinding = true
compose = true
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = BuildData.appJavaVersion
targetCompatibility = BuildData.appJavaVersion
}

composeOptions {
kotlinCompilerExtensionVersion = Versions.COMPOSE_COMPILER
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
jvmTarget = BuildData.appJavaVersion.toString()
}
packagingOptions {

packaging {
jniLibs {
excludes += setOf("META-INF/licenses/**")
}
resources {
excludes += setOf(
"META-INF/DEPENDENCIES",
"META-INF/LICENSE",
"META-INF/LICENSE.txt",
"META-INF/license.txt",
"META-INF/licenses/**",
"META-INF/NOTICE",
"META-INF/NOTICE.txt",
"META-INF/notice.txt",
"META-INF/ASL2.0",
"META-INF/AL2.0",
"META-INF/*.kotlin_module",
"META-INF/LGPL2.1",
"**/attach_hotspot_windows.dll"
)
excludes +=
setOf(
"META-INF/DEPENDENCIES",
"META-INF/LICENSE",
"META-INF/LICENSE.txt",
"META-INF/license.txt",
"META-INF/licenses/**",
"META-INF/NOTICE",
"META-INF/NOTICE.txt",
"META-INF/notice.txt",
"META-INF/ASL2.0",
"META-INF/AL2.0",
"META-INF/*.kotlin_module",
"META-INF/LGPL2.1",
"**/attach_hotspot_windows.dll",
)
}
}
namespace = "com.hbb20.androidcountrypicker"
Expand All @@ -73,6 +82,7 @@ dependencies {
implementation(Deps.viewModels)
implementation(Deps.timber)
implementArchitectureComponents()
implementCompose()
implementEpoxy()
implementTesting()
implementProject("countrypicker")
Expand Down

This file was deleted.

10 changes: 8 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".CountryPickerViewDemoActivity"></activity>
<activity
android:name=".compose.ComposeDemoActivity"
android:exported="false"
android:label="@string/title_activity_compose_demo"
android:theme="@style/AppTheme" />
<activity android:name=".CountryPickerViewDemoActivity" />
<activity android:name=".OpenDialogDirectlyActivity" />
<activity android:name=".CustomRecyclerViewActivity" />
<activity android:name=".MainActivity"
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.hbb20.countrypicker.view.prepareCustomCountryPickerView

class CountryPickerViewDemoActivity : AppCompatActivity() {
lateinit var binding: ActivityCountryPickerViewDemoBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityCountryPickerViewDemoBinding.inflate(layoutInflater)
Expand All @@ -41,8 +42,8 @@ class CountryPickerViewDemoActivity : AppCompatActivity() {
binding.cpFlagPack.changeFlagProvider(
CPFlagImageProvider(
FlagPack1.alpha2ToFlag,
FlagPack1.missingFlagPlaceHolder
)
FlagPack1.missingFlagPlaceHolder,
),
)
binding.cpNoFlag.changeFlagProvider(null)
}
Expand All @@ -58,7 +59,7 @@ class CountryPickerViewDemoActivity : AppCompatActivity() {
containerViewGroup = customCPContainer,
tvSelectedCountryInfo = customCPSelectedCountryTextView,
tvSelectedCountryEmojiFlag = customCPEmojiTextView,
initialSelection = CPViewConfig.InitialSelection.AutoDetectCountry()
initialSelection = CPViewConfig.InitialSelection.AutoDetectCountry(),
) { selectedCountry: CPCountry? ->
// listen to change through callback
// your code to handle selected country
Expand All @@ -74,19 +75,20 @@ class CountryPickerViewDemoActivity : AppCompatActivity() {
val customCPSelectedCountryTextView = findViewById<TextView>(R.id.tvSelectedCountry)
val customCPEmojiTextView = findViewById<TextView>(R.id.tvSelectedCountryEmojiFlag)

val cpViewHelper = prepareCustomCountryPickerView(
containerViewGroup = customCPContainer,
tvSelectedCountryInfo = customCPSelectedCountryTextView,
tvSelectedCountryEmojiFlag = customCPEmojiTextView
)
val cpViewHelper =
prepareCustomCountryPickerView(
containerViewGroup = customCPContainer,
tvSelectedCountryInfo = customCPSelectedCountryTextView,
tvSelectedCountryEmojiFlag = customCPEmojiTextView,
)

// observe live data
cpViewHelper.selectedCountry.observe(
lifecycleOwner,
{ selectedCountry: CPCountry? ->
// observe live data
// your code to handle selected country
}
},
)

// Modify CPViewConfig if you need. Access cpViewConfig through `cpViewHelper`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.hbb20.countrypicker.recyclerview.loadCountries

class CustomRecyclerViewActivity : AppCompatActivity() {
lateinit var binding: ActivityCustomRecyclerViewBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityCustomRecyclerViewBinding.inflate(layoutInflater)
Expand All @@ -22,7 +23,6 @@ class CustomRecyclerViewActivity : AppCompatActivity() {
}

private fun setupRecyclerView() {

val primaryTextGenerator = { cpCountry: CPCountry ->
"${cpCountry.name} (${cpCountry.alpha3})"
}
Expand All @@ -35,21 +35,22 @@ class CustomRecyclerViewActivity : AppCompatActivity() {
"+${cpCountry.phoneCode}"
}

val cpListConfig = CPListConfig(
preferredCountryCodes = "IN,US,NZ,AU"
)
val cpListConfig =
CPListConfig(
preferredCountryCodes = "IN,US,NZ,AU",
)

binding.recyclerView.loadCountries { selectedCountry: CPCountry ->
// your code to handle selected country
}

binding.recyclerView.loadCountries(
preferredCountryCodes = "IN,US,NZ,AU"
preferredCountryCodes = "IN,US,NZ,AU",
) { selectedCountry ->
Toast.makeText(this, selectedCountry.name, Toast.LENGTH_SHORT).show()
}

val dataFileReader = CPDataStoreGenerator.defaultCountryFileReader
val dataFileReader = CPDataStoreGenerator.DEFAULT_FILE_READER
val customFlagImageProvider = CPRowConfig.defaultFlagProvider

// binding.recyclerView.loadCountries { selectedCountry: CPCountry ->
Expand Down Expand Up @@ -80,13 +81,17 @@ class CustomRecyclerViewActivity : AppCompatActivity() {
// See CPDataStore for available configuration
val cpDataStore = CPDataStoreGenerator.generate(this)

val cpRecyclerViewHelper = CPRecyclerViewHelper(
cpDataStore = cpDataStore, // required
cpListConfig = cpListConfig, // Default: CPListConfig()
cpRowConfig = cpRowConfig // Default: CPRowConfig()
) { selectedCountry: CPCountry ->
// required: handle selected country
}
val cpRecyclerViewHelper =
CPRecyclerViewHelper(
// required
cpDataStore = cpDataStore,
// Default: CPListConfig()
cpListConfig = cpListConfig,
// Default: CPRowConfig()
cpRowConfig = cpRowConfig,
) { selectedCountry: CPCountry ->
// required: handle selected country
}

// attach recyclerView to show list in recyclerView
cpRecyclerViewHelper.attachRecyclerView(binding.recyclerView)
Expand All @@ -107,13 +112,17 @@ class CustomRecyclerViewActivity : AppCompatActivity() {
// Check CPRowConfig for available configuration
val cpRowConfig: CPRowConfig = CPRowConfig()

val cpRecyclerViewHelper = CPRecyclerViewHelper(
cpDataStore = cpDataStore, // required
cpListConfig = cpListConfig, // Default: CPListConfig()
cpRowConfig = cpRowConfig // Default: CPRowConfig()
) { selectedCountry: CPCountry ->
// required: handle selected country
}
val cpRecyclerViewHelper =
CPRecyclerViewHelper(
// required
cpDataStore = cpDataStore,
// Default: CPListConfig()
cpListConfig = cpListConfig,
// Default: CPRowConfig()
cpRowConfig = cpRowConfig,
) { selectedCountry: CPCountry ->
// required: handle selected country
}

// attach recyclerView to show list in recyclerView
cpRecyclerViewHelper.attachRecyclerView(binding.recyclerView)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.hbb20.androidcountrypicker

import android.app.Application
import timber.log.Timber

class DemoApplication : Application() {

override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
}
// if (BuildConfig.DEBUG) {
// Timber.plant(Timber.DebugTree())
// }
}
}
10 changes: 8 additions & 2 deletions app/src/main/java/com/hbb20/androidcountrypicker/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.hbb20.CountryPickerView
import com.hbb20.androidcountrypicker.compose.ComposeDemoActivity
import com.hbb20.androidcountrypicker.databinding.ActivityMainBinding
import com.hbb20.contrypicker.flagpack1.FlagPack1
import com.hbb20.countrypicker.flagprovider.CPFlagImageProvider
import com.hbb20.countrypicker.models.CPCountry

class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
Expand All @@ -29,8 +31,8 @@ class MainActivity : AppCompatActivity() {
countryPicker.changeFlagProvider(
CPFlagImageProvider(
FlagPack1.alpha2ToFlag,
FlagPack1.missingFlagPlaceHolder
)
FlagPack1.missingFlagPlaceHolder,
),
)
}

Expand All @@ -48,4 +50,8 @@ class MainActivity : AppCompatActivity() {
fun openCpViewDemo(view: View) {
startActivity(Intent(this, CountryPickerViewDemoActivity::class.java))
}

fun openComposeDemo(view: View) {
startActivity(Intent(this, ComposeDemoActivity::class.java))
}
}
Loading
Loading