Skip to content

Commit

Permalink
Added bottom sheet dialog search
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuppan committed May 18, 2020
1 parent e3ccc31 commit e8c2966
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ deploy:
provider: releases
api-key: $GITHUB_API_KEY
file:
- $TRAVIS_BUILD_DIR/app/build/outputs/apk/release/example-release.apk
- $TRAVIS_BUILD_DIR/example/build/outputs/apk/release/example-release.apk
- $TRAVIS_BUILD_DIR/country/build/outputs/aar/country-release.aar
skip_cleanup: true
name: $TRAVIS_TAG
Expand Down
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Add this below in your app.gradle
```gradle
// app.gradle
dependencies {
implementation 'com.ancient.country:country:1.0.6'
implementation 'com.ancient.country:country:1.0.7'
}
```

Expand Down Expand Up @@ -65,15 +65,25 @@ override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)

Starting country selection as a dialog
```kotlin
val fragmentTransaction = supportFragmentManager.beginTransaction()
val dialogFragment = CountryListDialog()
dialogFragment.countrySelection = { aCountry ->
dialogFragment.dismiss()
//Handle your selection
val ft = supportFragmentManager.beginTransaction()
val countryListDialogFragment = CountryListDialogFragment()
countryListDialogFragment.countrySelection = {
changeValues(it)
countryListDialogFragment.dismiss()
}
dialogFragment.show(fragmentTransaction, "dialog")
countryListDialogFragment.show(ft, "dialog")
```

Starting country selection as a bottom sheet dialog
```kotlin
val ft = supportFragmentManager.beginTransaction()
val countryListBottomSheet = CountryListBottomSheet()
countryListBottomSheet.countrySelection = {
changeValues(it)
countryListBottomSheet.dismiss()
}
countryListBottomSheet.show(ft, "bottom_sheet_dialog")
```

## Buy Me a Coffee

Expand Down
2 changes: 1 addition & 1 deletion country/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ext {
libraryDescription = 'Search country list and pick one from it'
siteUrl = 'https://github.com/naveenkumarn27/country'
gitUrl = 'https://github.com/naveenkumarn27/country.git'
libraryVersion = '1.0.6'
libraryVersion = '1.0.7'
developerId = 'naveenkumarn27'
developerName = 'Naveen Kumar Kuppan'
developerEmail = '[email protected]'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package com.ancient.country.view.fragment

import android.content.ActivityNotFoundException
import android.content.Intent
import android.os.Bundle
import android.speech.RecognizerIntent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.ancient.country.BuildConfig
import com.ancient.country.R
import com.ancient.country.databinding.DialogCountrySearchBinding
import com.ancient.country.extention.autoCleared
import com.ancient.country.model.CountryModel
import com.ancient.country.view.adapter.CountryListAdapter
import com.ancient.country.view.viewmodel.CountryListViewModel
import com.ancient.country.view.viewmodel.SearchViewModel
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.floatingactionbutton.FloatingActionButton
import java.util.*

/**
* Created by ancientinc on 19/05/20.
**/
class CountryListBottomSheet : BottomSheetDialogFragment(), SwipeRefreshLayout.OnRefreshListener {

private var mRecyclerView by autoCleared<RecyclerView>()
private var mLoaderWidget by autoCleared<TextView>()
private var mSwipeRefreshLayout by autoCleared<SwipeRefreshLayout>()
private var mActionButton by autoCleared<FloatingActionButton>()

private var countrySearchModel: CountryListViewModel by autoCleared()
private var searchViewModel: SearchViewModel by autoCleared()

private var adapter: CountryListAdapter by autoCleared()

var countrySelection: ((CountryModel) -> Unit)? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

val view = inflater.inflate(R.layout.dialog_country_search, container, false)

searchViewModel = ViewModelProvider(this).get(SearchViewModel::class.java)

val binding = DialogCountrySearchBinding.bind(view)
binding.viewModel = searchViewModel

binding.lifecycleOwner = this.viewLifecycleOwner

return view
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

initializeView(view)

searchViewModel.searchText.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
processValue(it)
})

searchViewModel.backNavigation.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
dismiss()
})

searchViewModel.voiceSearch.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
promptSpeechInput()
})

searchViewModel.searchHintText.value = getSearchHintText()

createRequest(true)
}

private fun searchEntered(aSearchValue: String) {
countrySearchModel.loadCountryList(aSearchValue)
}

private fun getSearchHintText(): String {
return getString(R.string.search_country)
}

private fun processValue(it: String) {
searchEntered(it)
}

/**
* Showing google speech input dialog
*/
private fun promptSpeechInput() {
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.speech_prompt))
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT)
} catch (a: ActivityNotFoundException) {
if (BuildConfig.DEBUG) {
Log.d(TAG, a.localizedMessage ?: "")
}
}
}

private fun initializeView(aView: View) {

mRecyclerView = aView.findViewById(R.id.recyclerview)
mLoaderWidget = aView.findViewById(R.id.loader_widget)
mSwipeRefreshLayout = aView.findViewById(R.id.refresh_layout)
mActionButton = aView.findViewById(R.id.action_button)

mRecyclerView.layoutManager = GridLayoutManager(context, 1)
mRecyclerView.itemAnimator = DefaultItemAnimator()

mSwipeRefreshLayout.setOnRefreshListener(this)

hideActionButton()
}

fun setHasFixedSize(aFixedSize: Boolean) {
mRecyclerView.setHasFixedSize(aFixedSize)
}

protected fun setActionIconDrawable(@DrawableRes aDrawable: Int) {
mActionButton.setImageResource(aDrawable)
}

protected fun setActionClickListener(aClickListener: View.OnClickListener) {
mActionButton.visibility = View.VISIBLE
mActionButton.setOnClickListener(aClickListener)
}

protected fun hideActionButton() {
mActionButton.visibility = View.GONE
}

protected fun startLoadingRequest() {
mSwipeRefreshLayout.isRefreshing = true
}

protected fun setNoResult() {
mSwipeRefreshLayout.isRefreshing = false
mLoaderWidget.visibility = View.VISIBLE
}

protected fun setResult() {
mSwipeRefreshLayout.isRefreshing = false
mLoaderWidget.visibility = View.GONE
}

protected fun setAdapter(aAdapter: RecyclerView.Adapter<*>) {
mRecyclerView.adapter = aAdapter
}

override fun onRefresh() {
createRequest(isStart = false)
}

private fun createRequest(isStart: Boolean) {

if (isStart) {

countrySearchModel = ViewModelProvider(this).get(CountryListViewModel::class.java)

adapter = CountryListAdapter {
countrySelection?.invoke(it)
}

setAdapter(adapter)

countrySearchModel.countryList.observe(viewLifecycleOwner, androidx.lifecycle.Observer {

if (it != null && it.isNotEmpty()) {
adapter.submitList(it)
setResult()
} else {
adapter.submitList(mutableListOf())
setNoResult()
}
})
}

startLoadingRequest()

countrySearchModel.loadCountryList()
}

companion object {
private const val REQ_CODE_SPEECH_INPUT = 100
private const val TAG = "Search"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import java.util.*
/**
* Created by ancientinc on 11/05/20.
**/
class CountryListDialog : DialogFragment(), SwipeRefreshLayout.OnRefreshListener {
class CountryListDialogFragment : DialogFragment(), SwipeRefreshLayout.OnRefreshListener {

private var mRecyclerView by autoCleared<RecyclerView>()
private var mLoaderWidget by autoCleared<TextView>()
Expand Down
21 changes: 16 additions & 5 deletions example/src/main/java/com/ancient/example/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import com.ancient.country.model.CountryModel
import com.ancient.country.utils.RequestCode
import com.ancient.country.utils.RequestParam
import com.ancient.country.view.activity.CountrySearchActivity
import com.ancient.country.view.fragment.CountryListDialog
import com.ancient.country.view.fragment.CountryListBottomSheet
import com.ancient.country.view.fragment.CountryListDialogFragment
import com.ancient.example.databinding.MainActivityBinding
import com.google.android.material.snackbar.Snackbar

Expand All @@ -32,12 +33,22 @@ class MainActivity : AppCompatActivity() {

dataBinding.searchDialog.setOnClickListener {
val ft = supportFragmentManager.beginTransaction()
val dialogFragment = CountryListDialog()
dialogFragment.countrySelection = {
val countryListDialogFragment = CountryListDialogFragment()
countryListDialogFragment.countrySelection = {
changeValues(it)
dialogFragment.dismiss()
countryListDialogFragment.dismiss()
}
dialogFragment.show(ft, "dialog")
countryListDialogFragment.show(ft, "dialog")
}

dataBinding.searchBottomSheet.setOnClickListener {
val ft = supportFragmentManager.beginTransaction()
val countryListBottomSheet = CountryListBottomSheet()
countryListBottomSheet.countrySelection = {
changeValues(it)
countryListBottomSheet.dismiss()
}
countryListBottomSheet.show(ft, "bottom_sheet_dialog")
}
}

Expand Down
8 changes: 8 additions & 0 deletions example/src/main/res/layout/main_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,13 @@
android:text="@string/country_search_as_dialog"
android:textColor="@color/icon_white_color" />

<Button
android:id="@+id/search_bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorAccent"
android:text="@string/country_search_as_bottom_sheet"
android:textColor="@color/icon_white_color" />

</LinearLayout>
</layout>
1 change: 1 addition & 0 deletions example/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<string name="country_dial_code">[Country Dial Code]</string>
<string name="country_currency_code">[Country Currency Code]</string>
<string name="country_symbol">[Currency Symbol]</string>
<string name="country_search_as_bottom_sheet">Country Search As Bottom Sheet</string>
</resources>
Binary file modified screenshots/country.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e8c2966

Please sign in to comment.