Created
September 17, 2019 02:25
-
-
Save trunghq3101/378bd5abd1c1eec9653ae868ea0dc49a to your computer and use it in GitHub Desktop.
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
| 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) | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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