Skip to content

Instantly share code, notes, and snippets.

View cdmunoz's full-sized avatar

Carlos Daniel cdmunoz

View GitHub Profile
interface ApiService {
@GET("photos")
suspend fun getPhotos(@Query("sol") sol: Int, @Query("api_key") apiKey: String, @Query("page") page: Int): Response<PhotosResponse>?
}
object RetrofitService {
var client: OkHttpClient = OkHttpClient.Builder().build()
@cdmunoz
cdmunoz / Photo.kt
Created April 22, 2020 20:57
Photo data class to model rover photo object
@Parcelize
data class Photo(@SerializedName("camera") val camera: Camera,
@SerializedName("earth_date") val earthDate: String,
@SerializedName("id") val id: String,
@SerializedName("img_src") val imgSrc: String,
@SerializedName("rover") val rover: Rover,
@SerializedName("sol") val sol: String) : Parcelable
@cdmunoz
cdmunoz / Result.kt
Created April 22, 2020 20:54
Generic Result sealed class to UI state management for a network result
sealed class Result<out T : Any> {
data class Success<out T : Any>(val data: T) : Result<T>()
data class Error(val exception: Exception) : Result<Nothing>()
object InProgress : Result<Nothing>()
val extractData: T?
get() = when (this) {
is Success -> data
is Error -> null
is InProgress -> null
@cdmunoz
cdmunoz / build.gradle
Created April 22, 2020 20:52
New gradle dependencies
def glide_version = "4.9.0"
implementation "com.github.bumptech.glide:glide:$glide_version"
implementation "com.github.bumptech.glide:okhttp3-integration:$glide_version"
kapt "com.github.bumptech.glide:compiler:$glide_version"
def retrofit_version = "2.6.0"
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
def coroutines_version = "1.3.0"
/**
* Manages the various graphs needed for a [BottomNavigationView].
* This sample is a workaround until the Navigation Component supports multiple back stacks.
* Taken from:
* https://github.com/android/architecture-components-samples/blob/master/NavigationAdvancedSample
* /app/src/main/java/com/example/android/navigationadvancedsample/NavigationExtensions.kt
*/
fun BottomNavigationView.setupWithNavController(
navGraphIds: List<Int>,
fragmentManager: FragmentManager,
@cdmunoz
cdmunoz / PhotosViewHolder.kt
Created March 16, 2020 03:42
bind method with navigation
class PhotosViewHolder(rowBinding: RowPhotoHomeBinding) :
RecyclerView.ViewHolder(rowBinding.root) {
private val binding = rowBinding
fun bind(str: String) {
binding.camera = str
val bundle = bundleOf("PHOTO_NAME" to str)
binding.root.setOnClickListener { view ->
Navigation.findNavController(view).navigate(R.id.action_home_to_details, bundle)
}
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph_info"
app:startDestination="@id/generalInfo_dest">
<fragment
android:id="@+id/generalInfo_dest"
android:name="co.cdmunoz.nasaroverphotos.GeneralInfoFragment"
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph_home"
app:startDestination="@id/home_dest">
<fragment
android:id="@+id/home_dest"
android:name="co.cdmunoz.nasaroverphotos.HomeFragment"
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_graph_home"
android:icon="@drawable/ic_home"
android:title="@string/title_home" />
<item
android:id="@+id/nav_graph_info"
android:icon="@drawable/ic_info"
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"