Skip to content

Instantly share code, notes, and snippets.

View carolinemusyoka's full-sized avatar
:octocat:
so?.let { it -> be (it) }

Carol carolinemusyoka

:octocat:
so?.let { it -> be (it) }
View GitHub Profile
<?xml version="1.0" encoding="utf-8"?>
<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.view.MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/superhero_recycler"
enum class Status {
SUCCESS,
ERROR,
LOADING
}
data class Resource<out T>(val status: Status, val data: T?, val message: String?) {
companion object {
fun <T> success(data: T): Resource<T> = Resource(status = Status.SUCCESS, data = data, message = null)
fun <T> error(data: T?, message: String): Resource<T> =
Resource(status = Status.ERROR, data = data, message = message)
fun <T> loading(data: T?): Resource<T> = Resource(status = Status.LOADING, data = data, message = null)
}
}
class MainRepository(private val apiHelper: ApiHelper) {
suspend fun getSuperHeroes() = apiHelper.getSuperHeroes()
}
object RetrofitBuilder {
private const val BASE_URL = "https://akabab.github.io/superhero-api/api/"
private fun getRetrofit(): Retrofit{
return Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
class ApiHelper(private val apiService: ApiService) {
suspend fun getSuperHeroes() = apiService.getSuperHeroes()
}
interface ApiService {
@GET("all.json")
suspend fun getSuperHeroes(): List<Hero>
}
import com.google.gson.annotations.SerializedName
data class Hero(
@SerializedName("id")
val id: Long,
@SerializedName("name")
val name: String,
@SerializedName("slug")
<uses-permission android:name="android.permission.INTERNET" />
implementation 'androidx.gridlayout:gridlayout:1.0.0'
implementation 'com.google.android.material:material:1.2.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.github.bumptech.glide:glide:4.11.0'
//LifeCycle
implementation 'androidx.lifecycle:lifecycle-common:2.2.0'
implementation 'androidx.lifecycle:lifecycle-runtime:2.2.0'
implementation 'android.arch.lifecycle:extensions:2.2.0'