Add the methods to hide/show both merchants and tellers markers depending on the current switches selection in the corresponding popup menu.

This commit is contained in:
Severiano Jaramillo 2019-01-25 22:33:20 -06:00
parent 0c41c76097
commit 227a52d032

View file

@ -15,6 +15,7 @@ import android.preference.PreferenceManager
import android.view.* import android.view.*
import android.widget.PopupWindow import android.widget.PopupWindow
import android.widget.TextView import android.widget.TextView
import androidx.appcompat.widget.SwitchCompat
import androidx.appcompat.widget.Toolbar import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
@ -64,6 +65,12 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback {
private var mToolbar: Toolbar? = null private var mToolbar: Toolbar? = null
private var screenWidth: Int = 0 private var screenWidth: Int = 0
// Variables used to decide whether or not to display the merchants and tellers markers on the map
private var merchants = ArrayList<Merchant>()
private var tellers = ArrayList<Teller>()
private var showMerchantsMarkers = true
private var showTellerMarkers = false
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
setHasOptionsMenu(true) setHasOptionsMenu(true)
@ -92,7 +99,19 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback {
private fun setupPopupWindow() { private fun setupPopupWindow() {
val popupView = layoutInflater?.inflate(R.layout.popup_menu_merchants, null) val popupView = layoutInflater?.inflate(R.layout.popup_menu_merchants, null)
// TODO get references to the popup menu items val switchMerchants = popupView?.findViewById<SwitchCompat>(R.id.switchMerchants)
switchMerchants?.isChecked = showMerchantsMarkers
switchMerchants?.setOnCheckedChangeListener { _, isChecked ->
showMerchantsMarkers = isChecked
showHideMerchantsMarkers()
}
val switchTellers = popupView?.findViewById<SwitchCompat>(R.id.switchTellers)
switchTellers?.isChecked = showTellerMarkers
switchTellers?.setOnCheckedChangeListener { _, isChecked ->
showTellerMarkers = isChecked
showHideTellersMarkers()
}
val tvAbout = popupView?.findViewById<TextView>(R.id.tvAbout) val tvAbout = popupView?.findViewById<TextView>(R.id.tvAbout)
tvAbout?.setOnClickListener { tvAbout?.setOnClickListener {
@ -225,9 +244,9 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback {
mMerchantClusterManager?.markerCollection?.setOnInfoWindowAdapter(MerchantInfoWindowAdapter()) mMerchantClusterManager?.markerCollection?.setOnInfoWindowAdapter(MerchantInfoWindowAdapter())
mMerchantViewModel.getAllMerchants().observe(this, Observer<List<Merchant>> {merchants -> mMerchantViewModel.getAllMerchants().observe(this, Observer<List<Merchant>> {merchants ->
mMerchantClusterManager?.clearItems() this.merchants.clear()
mMerchantClusterManager?.addItems(merchants) this.merchants.addAll(merchants)
mMerchantClusterManager?.cluster() showHideMerchantsMarkers()
}) })
} }
@ -251,10 +270,28 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback {
mTellerClusterManager?.markerCollection?.setOnInfoWindowAdapter(TellerInfoWindowAdapter()) mTellerClusterManager?.markerCollection?.setOnInfoWindowAdapter(TellerInfoWindowAdapter())
mMerchantViewModel.getAllTellers().observe(this, Observer<List<Teller>> {tellers -> mMerchantViewModel.getAllTellers().observe(this, Observer<List<Teller>> {tellers ->
this.tellers.clear()
this.tellers.addAll(tellers)
showHideTellersMarkers()
})
}
private fun showHideMerchantsMarkers() {
mMerchantClusterManager?.clearItems()
mMerchantClusterManager?.cluster()
if (showMerchantsMarkers) {
mMerchantClusterManager?.addItems(merchants)
mMerchantClusterManager?.cluster()
}
}
private fun showHideTellersMarkers() {
mTellerClusterManager?.clearItems() mTellerClusterManager?.clearItems()
mTellerClusterManager?.cluster()
if (showTellerMarkers) {
mTellerClusterManager?.addItems(tellers) mTellerClusterManager?.addItems(tellers)
mTellerClusterManager?.cluster() mTellerClusterManager?.cluster()
}) }
} }
/** Animates the camera update to focus on an area that shows all the items from the cluster that was tapped. */ /** Animates the camera update to focus on an area that shows all the items from the cluster that was tapped. */