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
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' | |
} |
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
@Database(entities = arrayOf(Restaurant::class), version = 1) | |
abstract class Database : RoomDatabase() { | |
abstract fun restaurantsDao(): RestaurantsDao | |
} |
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
@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) |
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
@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 |
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 | |
} |
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
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
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
@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 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) | |
} |