This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cornershopapp.shopper.android.utils; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Paint; | |
import android.text.style.ReplacementSpan; | |
import com.cornershopapp.shopper.android.R; | |
/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onMapLoaded() { | |
restaurantsViewModel.restaurants | |
.observe(this, Observer<Resource<List<Restaurant>>> { resource -> | |
when (resource?.status) { | |
Resource.Status.SUCCESS -> { | |
hideLoading() | |
if (resource.data != null && resource.data.isNotEmpty()) { | |
showMarkers(resource.data) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
abstract class BuildersModule { | |
@ContributesAndroidInjector | |
abstract fun contributeRestaurantsMapActivity(): RestaurantsMapActivity | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RestaurantsMapActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
AndroidInjection.inject(this) | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_restaurants_map) | |
// ... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RestaurantsViewModel @Inject constructor(private val repository: RestaurantsRepository) : ViewModel() { | |
var initialized = false | |
var cuisineInput: MutableLiveData<Int> = MutableLiveData() | |
val restaurants: LiveData<Resource<List<Restaurant>>> = Transformations | |
.switchMap(cuisineInput) { cuisine -> | |
initialized = true; repository.getRestaurants(cuisine) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Singleton | |
class ViewModelFactory @Inject constructor(private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>?) : ViewModelProvider.Factory { | |
@Suppress("UNCHECKED_CAST") | |
override fun <T : ViewModel?> create(modelClass: Class<T>): T { | |
var creator = creators!![modelClass] | |
if (creator == null) { | |
for (entry in creators.entries) { | |
if (modelClass.isAssignableFrom(entry.key)) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RestaurantsMapActivity : AppCompatActivity() { | |
private lateinit var restaurantsViewModel: RestaurantsViewModel | |
override fun onCreate(savedInstanceState: Bundle?) { | |
// ... | |
restaurantsViewModel = ViewModelProviders.of(this, viewModelFactory) | |
.get(RestaurantsViewModel::class.java) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
android { | |
// ... | |
defaultConfig { | |
// ... | |
vectorDrawables.useSupportLibrary = true | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Singleton | |
class Utils @Inject constructor(private val context: Context) { | |
fun getBitmap(drawableId: Int): Bitmap { | |
val drawable = ContextCompat.getDrawable(context, drawableId) | |
return when (drawable) { | |
is BitmapDrawable -> BitmapFactory.decodeResource(context.resources, drawableId) | |
is VectorDrawable -> getBitmap(drawable) | |
else -> throw IllegalArgumentException("unsupported drawable type") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Inject lateinit var utils: Utils | |
private fun constructMarkerOptions(restaurant: Restaurant): MarkerOptions { | |
val point = LatLng(restaurant.lat, restaurant.lng) | |
val icon = when (restaurant.cuisine) { | |
1 -> BitmapDescriptorFactory.fromBitmap(utils.getBitmap(R.drawable.ic_peru)) | |
2 -> BitmapDescriptorFactory.fromBitmap(utils.getBitmap(R.drawable.ic_italy)) | |
3 -> BitmapDescriptorFactory.fromBitmap(utils.getBitmap(R.drawable.ic_chile)) | |
else -> null | |
} |