Skip to content

Commit

Permalink
[#3] - Use chips to filter regions
Browse files Browse the repository at this point in the history
  • Loading branch information
jhg3410 committed Sep 5, 2022
1 parent 54c86a0 commit dcd6ba1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ class GdgListFragment : Fragment() {
ViewModelProvider(this).get(GdgListViewModel::class.java)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = FragmentGdgListBinding.inflate(inflater)

// Allows Data Binding to Observe LiveData with the lifecycle of this Fragment
Expand All @@ -65,7 +67,7 @@ class GdgListFragment : Fragment() {
// Sets the adapter of the RecyclerView
binding.gdgChapterList.adapter = adapter

viewModel.showNeedLocation.observe(viewLifecycleOwner, object: Observer<Boolean> {
viewModel.showNeedLocation.observe(viewLifecycleOwner, object : Observer<Boolean> {
override fun onChanged(show: Boolean?) {
// Snackbar is like Toast but it lets us show forever
if (show == true) {
Expand All @@ -79,6 +81,28 @@ class GdgListFragment : Fragment() {
})

setHasOptionsMenu(true)

viewModel.regionList.observe(viewLifecycleOwner, object : Observer<List<String>> {
override fun onChanged(data: List<String>?) {
data ?: return
val chipGroup = binding.regionList
val inflator = LayoutInflater.from(chipGroup.context)
val children = data.map { regionName ->
val chip = inflator.inflate(R.layout.region, chipGroup, false) as Chip
chip.text = regionName
chip.tag = regionName
chip.setOnCheckedChangeListener { button, isChecked ->
viewModel.onFilterChanged(button.tag as String, isChecked)
}
chip
}
chipGroup.removeAllViews()
for (chip in children) {
chipGroup.addView(chip)
}
}
})

return binding.root
}

Expand All @@ -102,7 +126,11 @@ class GdgListFragment : Fragment() {
*/
private fun requestLastLocationOrStartLocationUpdates() {
// if we don't have permission ask for it and wait until the user grants it
if (ContextCompat.checkSelfPermission(requireContext(), LOCATION_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(
requireContext(),
LOCATION_PERMISSION
) != PackageManager.PERMISSION_GRANTED
) {
requestLocationPermission()
return
}
Expand All @@ -123,14 +151,18 @@ class GdgListFragment : Fragment() {
*/
private fun startLocationUpdates(fusedLocationClient: FusedLocationProviderClient) {
// if we don't have permission ask for it and wait until the user grants it
if (ContextCompat.checkSelfPermission(requireContext(), LOCATION_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(
requireContext(),
LOCATION_PERMISSION
) != PackageManager.PERMISSION_GRANTED
) {
requestLocationPermission()
return
}


val request = LocationRequest().setPriority(LocationRequest.PRIORITY_LOW_POWER)
val callback = object: LocationCallback() {
val callback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
val location = locationResult?.lastLocation ?: return
viewModel.onLocationUpdated(location)
Expand All @@ -144,9 +176,13 @@ class GdgListFragment : Fragment() {
*
* If granted, continue with the operation that the user gave us permission to do.
*/
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when(requestCode) {
when (requestCode) {
LOCATION_PERMISSION_REQUEST -> {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
requestLastLocationOrStartLocationUpdates()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimaryVariant" android:state_selected="true" />

<item android:alpha="0.18" android:color="?attr/colorOnSurface" />

</selector>
12 changes: 11 additions & 1 deletion GDGFinder-Starter/app/src/main/res/layout/fragment_gdg_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></HorizontalScrollView>
app:layout_constraintTop_toTopOf="parent">

<com.google.android.material.chip.ChipGroup
android:id="@+id/region_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleLine="true"
app:singleSelection="true"
android:padding="@dimen/spacing_normal"/>

</HorizontalScrollView>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/gdg_chapter_list"
Expand Down
12 changes: 12 additions & 0 deletions GDGFinder-Starter/app/src/main/res/layout/region.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>

<com.google.android.material.chip.Chip
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.Chip.Choice"
app:chipBackgroundColor="@color/selected_highlight"
app:checkedIconVisible="true"
tools:checked="true"/>

0 comments on commit dcd6ba1

Please sign in to comment.