Android Kotlin Retrofit MVVM Get Request with Glide for User Profile.

Harshita Bambure
6 min readOct 20, 2021

--

Mostly we use retrofit with MVVM but sometimes we need images from API and for that, we will use the glide library to load the image from API.

For API I used the reqres website.

So, let's start

I am using get request of a single user from this website https://reqres.in/

Now we need to create JSON to POJO class so for that I am using this website.

first of all, add internet permission in the manifest file.

<uses-permission android:name="android.permission.INTERNET"/>

Add plugins to the build.Gradle(:app) file

plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'kotlin-android-extensions'
}

Now we need to add dependencies to the build.Gradle(:app) file

//RetroFit Dependencies
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.1'

//kotlin coroutine
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2-native-mt'
//Lifecycle

implementation 'androidx.lifecycle:lifecycle-common:2.3.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'

//glide
implementation 'com.github.bumptech.glide:glide:4.12.0'
kapt 'com.github.bumptech.glide:compiler:4.12.0'
implementation "androidx.activity:activity-ktx:1.3.1"

//size dp/sp
implementation 'com.intuit.sdp:sdp-android:1.0.6'
implementation 'com.intuit.ssp:ssp-android:1.0.6'

add build feature in build.Gradle(:app) file.

android {
buildFeatures {
viewBinding true
}
}

Let's start with the design part.

activity_profile.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:fillViewport="true"
android:fitsSystemWindows="true"
android:id="@+id/constraint">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/view"
android:background="@drawable/rectangle"
android:layout_width="match_parent"
android:layout_height="@dimen/_257sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text="Profile"
android:textColor="@color/white"
android:textSize="@dimen/_16ssp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.065"/>

<View
android:background="@drawable/circle"
android:layout_width="@dimen/_88sdp"
android:layout_height="@dimen/_89sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.145"/>
<ImageView
android:id="@+id/img_profile"
android:layout_width="@dimen/_84sdp"
android:layout_height="@dimen/_104sdp"
android:src="@drawable/profile"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.135"/>

<TextView
android:id="@+id/txt_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text="James Martin"
android:textStyle="bold"
android:textColor="@color/white"
android:textSize="@dimen/_14ssp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3"/>

<TextView
android:id="@+id/designation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text="Software Designer"
android:textColor="@color/color"
android:textSize="@dimen/_12ssp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.33"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text="1000"
android:textColor="@color/color"
android:textSize="@dimen/_14ssp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.15"
app:layout_constraintVertical_bias="0.39"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text=" Followers"
android:textColor="@color/color"
android:textSize="@dimen/_12ssp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.3"
app:layout_constraintVertical_bias="0.39"/>

<View
android:background="@color/color"
android:layout_width="0.2dp"
android:layout_height="@dimen/_22sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintVertical_bias="0.39"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text="1200"
android:textColor="@color/color"
android:textSize="@dimen/_14ssp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.65"
app:layout_constraintVertical_bias="0.39"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text=" Following"
android:textColor="@color/color"
android:textSize="@dimen/_12ssp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.842"
app:layout_constraintVertical_bias="0.39"/>

<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.065"
app:layout_constraintHeight_percent="0.03"
android:src="@drawable/ic_email"
android:layout_margin="@dimen/_8sdp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.49"
app:layout_constraintHorizontal_bias="0.01"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.15"
app:layout_constraintVertical_bias="0.49"
/>

<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="api email"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="#2B2929"
app:layout_constraintHorizontal_bias="0.22"
app:layout_constraintVertical_bias="0.52"
/>
<View
android:background="@color/gray"
android:layout_width="0dp"
app:layout_constraintWidth_percent="0.9"
android:layout_height="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.35"
app:layout_constraintVertical_bias="0.55"/>

<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.065"
app:layout_constraintHeight_percent="0.03"
android:src="@drawable/ic_phone"
android:layout_margin="@dimen/_8sdp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.6"
app:layout_constraintHorizontal_bias="0.01"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.15"
app:layout_constraintVertical_bias="0.595"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="145456"
android:textColor="#2B2929"
app:layout_constraintHorizontal_bias="0.15"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.63"
/>
<View
android:background="@color/gray"
android:layout_width="0dp"
app:layout_constraintWidth_percent="0.9"
android:layout_height="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.35"
app:layout_constraintVertical_bias="0.66"/>

<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.065"
app:layout_constraintHeight_percent="0.03"
android:src="@drawable/ic_twitter"
android:layout_margin="@dimen/_8sdp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.71"
app:layout_constraintHorizontal_bias="0.01"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Twitter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.15"
app:layout_constraintVertical_bias="0.699"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" @harshita"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="#2B2929"
app:layout_constraintHorizontal_bias="0.16"
app:layout_constraintVertical_bias="0.73"
/>
<View
android:background="@color/gray"
android:layout_width="0dp"
app:layout_constraintWidth_percent="0.9"
android:layout_height="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.35"
app:layout_constraintVertical_bias="0.76"/>

<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.065"
app:layout_constraintHeight_percent="0.03"
android:src="@drawable/ic_behance"
android:layout_margin="@dimen/_8sdp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.82"
app:layout_constraintHorizontal_bias="0.01"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Behance"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.15"
app:layout_constraintVertical_bias="0.80"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="www.behance.net/harshita"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="#2B2929"
app:layout_constraintHorizontal_bias="0.24"
app:layout_constraintVertical_bias="0.83"
/>
<View
android:background="@color/gray"
android:layout_width="0dp"
app:layout_constraintWidth_percent="0.9"
android:layout_height="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.35"
app:layout_constraintVertical_bias="0.865"/>

<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.065"
app:layout_constraintHeight_percent="0.03"
android:src="@drawable/ic_facebook"
android:layout_margin="@dimen/_8sdp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.93"
app:layout_constraintHorizontal_bias="0.01"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Facebook"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.15"
app:layout_constraintVertical_bias="0.91"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="www.facebook.com/harshita"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="#2B2929"
app:layout_constraintHorizontal_bias="0.24"
app:layout_constraintVertical_bias="0.94"
/>

</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="gray">#C9C2C2</color>
<color name="tras_white">#66FFFFFF</color>
<color name="light_gray">#EFEFEF</color>
</resources>

In drawable, I am creating rectangle shape and circle shape.

rectangle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<solid android:color="#083EFD" />

<corners
android:bottomLeftRadius="32dp"
android:bottomRightRadius="32dp"
android:topLeftRadius="2dp"
android:topRightRadius="2dp" />
</shape>

circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid android:color="@color/white" />

<size
android:width="120dp"
android:height="120dp" />

</shape>

Now we need to create response class

ProfileResponse.kt

package com.example.apireqres.response

import com.google.gson.annotations.Expose

import com.google.gson.annotations.SerializedName

class ProfileResponse {
@SerializedName("data")
@Expose
private var data: Data? = null

@SerializedName("support")
@Expose
private var support: Support? = null

fun getData(): Data? {
return data
}

fun setData(data: Data?) {
this.data = data
}

fun getSupport(): Support? {
return support
}

fun setSupport(support: Support?) {
this.support = support
}
}

Data. kt

package com.example.apireqres.response

import com.google.gson.annotations.Expose

import com.google.gson.annotations.SerializedName

class Data {
@SerializedName("id")
@Expose
private var id: Int? = null

@SerializedName("email")
@Expose
private var email: String? = null

@SerializedName("first_name")
@Expose
private var firstName: String? = null

@SerializedName("last_name")
@Expose
private var lastName: String? = null

@SerializedName("avatar")
@Expose
private var avatar: String? = null

fun getId(): Int? {
return id
}

fun setId(id: Int?) {
this.id = id
}

fun getEmail(): String? {
return email
}

fun setEmail(email: String?) {
this.email = email
}

fun getFirstName(): String? {
return firstName
}

fun setFirstName(firstName: String?) {
this.firstName = firstName
}

fun getLastName(): String? {
return lastName
}

fun setLastName(lastName: String?) {
this.lastName = lastName
}

fun getAvatar(): String? {
return avatar
}

fun setAvatar(avatar: String?) {
this.avatar = avatar
}

}

Support. kt

package com.example.apireqres.response

import com.google.gson.annotations.Expose

import com.google.gson.annotations.SerializedName

class Support {
@SerializedName("url")
@Expose
private var url: String? = null

@SerializedName("text")
@Expose
private var text: String? = null

fun getUrl(): String? {
return url
}

fun setUrl(url: String?) {
this.url = url
}

fun getText(): String? {
return text
}

fun setText(text: String?) {
this.text = text
}

}

Now we will code for retrofit so first of all, I am creating a constants file

Constants.kt

package com.example.apireqres

object Constants {
const val BASE_URL = "https://reqres.in/"

}

ApiInterface.kt

package com.example.apireqres.retrofit

import com.example.apireqres.response.ProfileResponse
import retrofit2.http.GET

interface ApiInterface {
@GET("/api/users/2")
suspend fun getAllResult(): ProfileResponse
}

ApiHelper.kt

package com.example.apireqres.retrofit

class ApiHelper (private val apiInterface: ApiInterface) {
suspend fun getAllResult() = apiInterface.getAllResult()
}

RetrofitClient.kt

package com.example.apireqres.retrofit

import com.example.apireqres.Constants
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitClient {

var mHttpLoggingInterceptor = HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY)

var mOkHttpClient = OkHttpClient
.Builder()
.addInterceptor(mHttpLoggingInterceptor)
.build()

var mRetrofit: Retrofit? = null


val client: Retrofit?
get() {
if(mRetrofit == null){
mRetrofit = Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.client(mOkHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return mRetrofit
}

}

AppCreator.kt

package com.example.apireqres.retrofit

import android.app.Application
import com.example.apireqres.retrofit.RetrofitClient.client


class AppCreator : Application() {
companion object {

private var mApiHelper:ApiHelper? = null
fun getApiHelperInstance():ApiHelper{
if(mApiHelper==null){
mApiHelper = ApiHelper(client!!.create(ApiInterface::class.java))
}
return mApiHelper!!
}

}
}

Now we will create a repository file.

ProfileRepository.kt

package com.example.apireqres.repository

import com.example.apireqres.retrofit.ApiHelper


class ProfileRepository (private val apiHelper: ApiHelper) {

suspend fun getUserProfile() = apiHelper.getAllResult()

}

Status.kt

package com.example.apireqres

enum class Status {
SUCCESS,
FAILURE,
LOADING
}

Resource. kt

package com.example.apireqres

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.FAILURE, data = data, message = message)

fun <T> loading(data:T?):Resource<T> =
Resource(status = Status.LOADING, data = data, message = null)
}

}

Now we will create a view model class.

ProfileViewModel.kt

package com.example.apireqres.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import com.example.apireqres.Resource
import com.example.apireqres.repository.ProfileRepository

class ProfileViewModel(private val mProfileRepository: ProfileRepository) : ViewModel(){

fun getAllProfile() = liveData {
emit(Resource.loading(null))
try{
emit(Resource.success(mProfileRepository.getUserProfile()))
} catch (e:Exception){
emit(Resource.error(null,e.message.toString()))
}
}

}

ViewModelFactory.kt

package com.example.apireqres.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.example.apireqres.repository.ProfileRepository
import com.example.apireqres.retrofit.ApiHelper
import java.lang.IllegalArgumentException

@Suppress("UNCHECKED_CAST")
class ViewModelFactory(private val apiHelper: ApiHelper) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if(modelClass.isAssignableFrom(ProfileViewModel::class.java)){
return ProfileViewModel(ProfileRepository(apiHelper)) as T
}
throw IllegalArgumentException("Class not found")
}
}

Now we will create an app glide module class for the glide library.

package com.example.apireqres


import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.module.AppGlideModule

@GlideModule
class AppGlideModule : AppGlideModule()

at the last, we need to code for Profile Activity

ProfileActivity.kt

package com.example.apireqres

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.WindowManager

import android.os.Build
import android.view.Window
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.example.apireqres.response.ProfileResponse
import com.example.apireqres.retrofit.AppCreator
import com.example.apireqres.viewmodel.ProfileViewModel
import com.example.apireqres.viewmodel.ViewModelFactory
import kotlinx.android.synthetic.main.activity_profile.*


class ProfileActivity : AppCompatActivity() {

private lateinit var mProfileViewModel: ProfileViewModel
private lateinit var mProfileResponse : ProfileResponse

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_profile)
// this code for statusbar just like instagram
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val w: Window = window
w.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
)
}
//...
initData()
obtainListFromServer()
}
private fun obtainListFromServer() {

mProfileViewModel.getAllProfile().observe(this)


{
when(it.status){
Status.SUCCESS -> {

mProfileResponse = it.data!!

getData()

Toast.makeText(
this,
"Sucessfully load the data",
Toast.LENGTH_LONG
).show()

}

Status.FAILURE -> {
Toast.makeText(
this,
"Failed to load the data ${it.message}",
Toast.LENGTH_LONG
).show()
}
Status.LOADING -> {
Toast.makeText(
this,
"Loading...",
Toast.LENGTH_LONG
).show()
}
}
}
}
private fun getData() {

val data = mProfileResponse.getData()!!

txt_username.text = data.getFirstName().toString()
email.text = data.getEmail().toString()


Glide.with(this@ProfileActivity).load(data.getAvatar()).apply(RequestOptions.circleCropTransform()).into(img_profile)


}

private fun initData() {
//initialization of viewmodel instance,
mProfileViewModel = ViewModelProvider(
this,
ViewModelFactory(AppCreator.getApiHelperInstance())
).get(ProfileViewModel::class.java)


}
}
User Profile using a retrofit.

The source code.

That's it. Happy Coding.

--

--

Harshita Bambure
Harshita Bambure

Written by Harshita Bambure

Android Developer || WomenTech Global Ambassador at WomenTech Network. || Yoga Teacher || Member @WTM .

Responses (1)