Skip to content

Instantly share code, notes, and snippets.

View tolmachevroman's full-sized avatar

Roman Tolmachev tolmachevroman

  • Santiago, Chile
View GitHub Profile
@tolmachevroman
tolmachevroman / URL, build.gradle
Created November 25, 2017 07:39
Medium Post 1. URL, build.gradle
buildTypes {
//
debug {
buildConfigField 'String', 'URL', '"http://192.168.1.195:8080/"'
}
release {
buildConfigField 'String', 'URL', '"https://my-json-server.typicode.com/tolmachevroman/demo/"'
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
@tolmachevroman
tolmachevroman / Database.kt
Last active November 26, 2017 05:11
Medium Post 1. Database
@Database(entities = arrayOf(Restaurant::class), version = 1)
abstract class Database : RoomDatabase() {
abstract fun restaurantsDao(): RestaurantsDao
}
@tolmachevroman
tolmachevroman / RestaurantsDao.kt
Last active November 26, 2017 05:12
Medium Post 1. RestaurantsDao
@Dao
interface RestaurantsDao {
@Query("SELECT * FROM restaurant")
fun queryRestaurants(): LiveData<List<Restaurant>>
@Query("SELECT * FROM restaurant WHERE cuisine LIKE :cuisine")
fun queryRestaurantsByCuisine(cuisine: Int): LiveData<List<Restaurant>>
@Insert(onConflict = REPLACE)
@tolmachevroman
tolmachevroman / Restaurant.kt
Last active November 26, 2017 05:12
Medium Post 1. Restaurant entity
@Entity
data class Restaurant(@PrimaryKey val id: Int, val cuisine: Int?, val name: String?,
val lat: Double, val lng: Double, val price: Int,
val image: String = "", val description: String = "") : Serializable
@tolmachevroman
tolmachevroman / RestaurantsMapActivity.kt
Last active November 26, 2017 05:13
Medium Post 2. constructMarkerOptions
@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
}
@tolmachevroman
tolmachevroman / Utils.kt
Last active November 26, 2017 05:13
Medium Post 2. Utils
@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")
}
@tolmachevroman
tolmachevroman / build.gradle
Last active November 26, 2017 05:14
Medium Post 2. vectorDrawables.useSupportLibrary
android {
// ...
defaultConfig {
// ...
vectorDrawables.useSupportLibrary = true
}
}
@tolmachevroman
tolmachevroman / RestaurantsMapActivity.kt
Last active November 26, 2017 05:14
Medium Post 2. ViewModel instantiation
class RestaurantsMapActivity : AppCompatActivity() {
private lateinit var restaurantsViewModel: RestaurantsViewModel
override fun onCreate(savedInstanceState: Bundle?) {
// ...
restaurantsViewModel = ViewModelProviders.of(this, viewModelFactory)
.get(RestaurantsViewModel::class.java)
}
@tolmachevroman
tolmachevroman / ViewModelFactory.kt
Last active November 26, 2017 05:14
Medium Post 2. ViewModelFactory
@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)) {
@tolmachevroman
tolmachevroman / RestaurantsViewModel.kt
Last active November 26, 2017 05:15
Medium Post 2. RestaurantsViewModel
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)
}