Skip to content

Instantly share code, notes, and snippets.

@trunghq3101
Created September 17, 2019 02:25
Show Gist options
  • Select an option

  • Save trunghq3101/378bd5abd1c1eec9653ae868ea0dc49a to your computer and use it in GitHub Desktop.

Select an option

Save trunghq3101/378bd5abd1c1eec9653ae868ea0dc49a to your computer and use it in GitHub Desktop.
package com.miller.futurechat
import android.content.Context
import android.util.Log
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.signature.ObjectKey
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import okhttp3.OkHttpClient
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.http.HEAD
/**
* Created by Miller on 17/09/2019
*/
interface ApiService {
@HEAD("http://lorempixel.com/400/200/")
fun getImageDate(): Single<Response<Void>>
}
// Put in other class
fun showGlideImage(context: Context, target: ImageView) {
val client = OkHttpClient()
val retrofit = Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.baseUrl("http://mock.api")
.build()
val apiService = retrofit.create(ApiService::class.java)
apiService.getImageDate()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
val date = it.headers()["date"]
Log.d("----->","MainActivity - onResponse : $date")
Glide.with(context)
.load("http://lorempixel.com/400/200/")
.signature(ObjectKey(date ?: ""))
.into(target)
},
{
it.printStackTrace()
}
)
.apply {
CompositeDisposable().add(this)
}
}
@trunghq3101
Copy link
Author

trunghq3101 commented Sep 17, 2019

Response header from http://lorempixel.com/400/200/ doesn't have Etag and Content-Length, so I have to use date instead for demo purpose

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment