Skip to content

Commit

Permalink
Use View Binding
Browse files Browse the repository at this point in the history
Signed-off-by: Rafael Chagas <[email protected]>
  • Loading branch information
rtchagas committed Oct 18, 2022
1 parent db3963f commit 542f4b4
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 105 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ buildscript {
google()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.2.2'
classpath 'com.google.gms:google-services:4.3.14'
Expand Down
6 changes: 5 additions & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-parcelize'
apply plugin: 'maven-publish'

android {
Expand All @@ -18,6 +18,10 @@ android {
freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
}

buildFeatures {
viewBinding true
}

defaultConfig {
minSdkVersion 19
targetSdkVersion 32
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<application>

<activity
android:name=".ui.PlacePickerActivity"
android:name=".ui.activity.PlacePickerActivity"
android:launchMode="singleTop"
android:theme="@style/PingTheme.NoStatus" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.google.android.gms.common.GooglePlayServicesNotAvailableException
import com.google.android.gms.maps.model.LatLng
import com.google.android.libraries.places.api.model.Place
import com.rtchagas.pingplacepicker.inject.PingKoinContext
import com.rtchagas.pingplacepicker.ui.PlacePickerActivity
import com.rtchagas.pingplacepicker.ui.activity.PlacePickerActivity

object PingPlacePicker {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.net.Uri
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.LatLngBounds
import com.google.android.libraries.places.api.model.*
import kotlinx.android.parcel.Parcelize
import kotlinx.parcelize.Parcelize

@Parcelize
internal class CustomPlace(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.rtchagas.pingplacepicker.ui.activity

import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding

typealias ActivityInflater<T> = (LayoutInflater) -> T

abstract class BaseActivity<T : ViewBinding>(
private val inflate: ActivityInflater<T>
) : AppCompatActivity() {

private var _binding: T? = null

/**
* The view binding for this fragment.
*
* [https://developer.android.com/topic/libraries/view-binding]
*
* This property is only valid between [onCreate] and [onDestroy].
*/
val binding: T get() = _binding!!

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = inflate(layoutInflater)
setContentView(binding.root)
}

override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rtchagas.pingplacepicker.ui
package com.rtchagas.pingplacepicker.ui.activity

import android.annotation.SuppressLint
import android.app.Activity
Expand All @@ -11,7 +11,6 @@ import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.DrawableCompat
Expand Down Expand Up @@ -39,18 +38,23 @@ import com.karumi.dexter.listener.PermissionGrantedResponse
import com.karumi.dexter.listener.single.BasePermissionListener
import com.rtchagas.pingplacepicker.PingPlacePicker
import com.rtchagas.pingplacepicker.R
import com.rtchagas.pingplacepicker.databinding.ActivityPlacePickerBinding
import com.rtchagas.pingplacepicker.helper.PermissionsHelper
import com.rtchagas.pingplacepicker.inject.PingKoinComponent
import com.rtchagas.pingplacepicker.ui.UiUtils
import com.rtchagas.pingplacepicker.ui.adapter.PlacePickerAdapter
import com.rtchagas.pingplacepicker.ui.fragment.PlaceConfirmDialogFragment
import com.rtchagas.pingplacepicker.ui.onclick
import com.rtchagas.pingplacepicker.viewmodel.PlacePickerViewModel
import com.rtchagas.pingplacepicker.viewmodel.Resource
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.activity_place_picker.*
import kotlinx.coroutines.delay
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.viewModel
import kotlin.math.abs

internal class PlacePickerActivity : AppCompatActivity(),
internal class PlacePickerActivity :
BaseActivity<ActivityPlacePickerBinding>(ActivityPlacePickerBinding::inflate),
PingKoinComponent,
OnMapReadyCallback,
GoogleMap.OnMarkerClickListener,
Expand Down Expand Up @@ -97,10 +101,9 @@ internal class PlacePickerActivity : AppCompatActivity(),

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_place_picker)

// Configure the toolbar
setSupportActionBar(toolbar)
setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)

// Check whether a pre-defined location was set.
Expand Down Expand Up @@ -208,13 +211,16 @@ internal class PlacePickerActivity : AppCompatActivity(),
UiUtils.getColorAttr(this, R.attr.colorPrimarySurface),
resources.getDimension(R.dimen.material_elevation_app_bar)
)
collapsingToolbarLayout.setContentScrimColor(scrimColor)

// Set the correct elevation to the content container
val containerColor = elevationOverlayProvider.compositeOverlayWithThemeSurfaceColorIfNeeded(
resources.getDimension(R.dimen.material_elevation_app_bar)
)
mapContainer.setBackgroundColor(containerColor)

with(binding) {
collapsingToolbarLayout.setContentScrimColor(scrimColor)
mapContainer.setBackgroundColor(containerColor)
}
}

private fun bindPlaces(places: List<Place>) {
Expand All @@ -227,7 +233,7 @@ internal class PlacePickerActivity : AppCompatActivity(),
placeAdapter?.swapData(places)
}

rvNearbyPlaces.adapter = placeAdapter
binding.rvNearbyPlaces.adapter = placeAdapter

// Bind to the map

Expand Down Expand Up @@ -350,53 +356,44 @@ internal class PlacePickerActivity : AppCompatActivity(),

private fun handlePlaceByLocation(result: Resource<Place?>) {

binding.pbLoading.hide()

when (result.status) {
Resource.Status.LOADING -> {
pbLoading.show()
}
Resource.Status.SUCCESS -> {
Resource.Status.LOADING ->
binding.pbLoading.show()
Resource.Status.SUCCESS ->
result.data?.run { showConfirmPlacePopup(this) }
pbLoading.hide()
}
Resource.Status.ERROR -> {
Resource.Status.ERROR ->
toast(R.string.picker_load_this_place_error)
pbLoading.hide()
}
Resource.Status.NO_DATA -> {
Resource.Status.NO_DATA ->
Log.d(TAG, "No places data found...")
}
}

}

private fun handlePlacesLoaded(result: Resource<List<Place>>) {

binding.pbLoading.hide()

when (result.status) {
Resource.Status.LOADING -> {
pbLoading.show()
}
Resource.Status.SUCCESS -> {
Resource.Status.LOADING ->
binding.pbLoading.show()
Resource.Status.SUCCESS ->
bindPlaces((result.data ?: listOf()))
pbLoading.hide()
}
Resource.Status.ERROR -> {
Resource.Status.ERROR ->
toast(R.string.picker_load_places_error)
pbLoading.hide()
}
Resource.Status.NO_DATA -> {
Resource.Status.NO_DATA ->
Log.d(TAG, "No places data found...")
}
}
}

private fun initializeUi() {
private fun initializeUi() = with(binding) {

// Some material components still don't support setting the correct
// elevation for dark themes, so we should handle that
adjustElevationOverlayColors()

// Initialize the recycler view
rvNearbyPlaces.layoutManager = LinearLayoutManager(this)
rvNearbyPlaces.layoutManager = LinearLayoutManager(this@PlacePickerActivity)

// Bind the click listeners
disposables.addAll(
Expand Down Expand Up @@ -541,7 +538,7 @@ internal class PlacePickerActivity : AppCompatActivity(),
// Location is not available. Give up...
setDefaultLocation()
Snackbar
.make(coordinator, R.string.picker_location_unavailable, Snackbar.LENGTH_INDEFINITE)
.make(binding.root, R.string.picker_location_unavailable, Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.places_try_again) { getDeviceLocation(animate) }
.show()
}
Expand Down Expand Up @@ -600,9 +597,9 @@ internal class PlacePickerActivity : AppCompatActivity(),

if (isLocationPermissionGranted) {
it.isMyLocationEnabled = true
btnMyLocation.visibility = View.VISIBLE
binding.btnMyLocation.visibility = View.VISIBLE
} else {
btnMyLocation.visibility = View.GONE
binding.btnMyLocation.visibility = View.GONE
it.isMyLocationEnabled = false
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,46 @@
package com.rtchagas.pingplacepicker.ui
package com.rtchagas.pingplacepicker.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.google.android.libraries.places.api.model.Place
import com.rtchagas.pingplacepicker.R
import kotlinx.android.synthetic.main.item_place.view.*
import com.rtchagas.pingplacepicker.databinding.ItemPlaceBinding
import com.rtchagas.pingplacepicker.ui.UiUtils

internal class PlacePickerAdapter(
private var placeList: List<Place>,
private val clickListener: (Place) -> Unit
) : RecyclerView.Adapter<PlacePickerAdapter.PlaceViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlaceViewHolder {

val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_place, parent, false)

return PlaceViewHolder(view)
val inflater = LayoutInflater.from(parent.context)
val binding = ItemPlaceBinding.inflate(inflater, parent, false)
return PlaceViewHolder(binding)
}

override fun onBindViewHolder(holder: PlaceViewHolder, position: Int) {
holder.bind(placeList[position], clickListener)
}

override fun getItemCount(): Int {
return placeList.size
}
override fun getItemCount(): Int =
placeList.size

fun swapData(newPlaceList: List<Place>) {
placeList = newPlaceList
notifyDataSetChanged()
}

inner class PlaceViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
inner class PlaceViewHolder(private val binding: ItemPlaceBinding) :
RecyclerView.ViewHolder(binding.root) {

fun bind(place: Place, listener: (Place) -> Unit) {

with(itemView) {
setOnClickListener { listener(place) }
with(binding) {
root.setOnClickListener { listener(place) }
ivPlaceType.setImageResource(UiUtils.getPlaceDrawableRes(itemView.context, place))
tvPlaceName.text = place.name
tvPlaceAddress.text = place.address
}
}
}
}

Loading

0 comments on commit 542f4b4

Please sign in to comment.