From 87ab31b9ed5e3079779f0bcd4982669364b1543a Mon Sep 17 00:00:00 2001 From: lizhongxin Date: Wed, 14 Aug 2019 16:25:55 +0800 Subject: [PATCH 1/8] this is a demo for reappear bug. --- android/build.gradle | 23 ++- android/src/main/AndroidManifest.xml | 3 +- .../io/android/CommonCache.java | 17 ++ .../io/android/MainActivity.java | 185 ++++++++++++++++++ .../victoralbertos/io/android/Providers.java | 16 ++ .../java/victoralbertos/io/android/User.java | 30 +++ android/src/main/res/layout/layout.xml | 25 +++ build.gradle | 4 +- config.gradle | 110 +++++++++++ 9 files changed, 409 insertions(+), 4 deletions(-) create mode 100644 android/src/main/java/victoralbertos/io/android/CommonCache.java create mode 100644 android/src/main/java/victoralbertos/io/android/Providers.java create mode 100644 android/src/main/java/victoralbertos/io/android/User.java create mode 100644 android/src/main/res/layout/layout.xml create mode 100644 config.gradle diff --git a/android/build.gradle b/android/build.gradle index 82f5150..4cca066 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -18,7 +18,7 @@ android { defaultConfig { applicationId "victoralbertos.io.android" minSdkVersion 16 - targetSdkVersion 23 + targetSdkVersion 22 versionCode 1 versionName "1.0" } @@ -44,6 +44,27 @@ dependencies { annotationProcessor "com.google.dagger:dagger-compiler:2.14.1" compile "com.google.dagger:dagger:2.14.1" provided "org.glassfish:javax.annotation:10.0-b28" + + implementation(rootProject.ext.dependencies["retrofit-converter-gson"]) { + exclude module: 'gson' + exclude module: 'okhttp' + exclude module: 'okio' + exclude module: 'retrofit' + } + implementation(rootProject.ext.dependencies["retrofit-adapter-rxjava2"]) { + exclude module: 'rxjava' + exclude module: 'okhttp' + exclude module: 'retrofit' + exclude module: 'okio' + } + api rootProject.ext.dependencies["okhttp3"] + api 'com.squareup.okhttp3:logging-interceptor:3.5.0' + api(rootProject.ext.dependencies["retrofit"]) { + exclude module: 'okhttp' + exclude module: 'okio' + } + + api rootProject.ext.dependencies["rxandroid2"] } diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index c1b3d44..6005238 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -1,6 +1,7 @@ - + + >> getUsers(Observable> users, DynamicKey idLastUserQueried, EvictProvider evictProvider); +} diff --git a/android/src/main/java/victoralbertos/io/android/MainActivity.java b/android/src/main/java/victoralbertos/io/android/MainActivity.java index ba8eca2..af5887a 100644 --- a/android/src/main/java/victoralbertos/io/android/MainActivity.java +++ b/android/src/main/java/victoralbertos/io/android/MainActivity.java @@ -1,10 +1,195 @@ package victoralbertos.io.android; import android.app.Activity; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.TextView; +import android.widget.Toast; + +import com.google.gson.Gson; + +import java.io.File; +import java.util.List; + +import io.reactivex.Observable; +import io.reactivex.ObservableSource; +import io.reactivex.Observer; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.Disposable; +import io.reactivex.functions.Consumer; +import io.reactivex.functions.Function; +import io.reactivex.schedulers.Schedulers; +import io.rx_cache2.DynamicKey; +import io.rx_cache2.EvictProvider; +import io.rx_cache2.Reply; +import io.rx_cache2.internal.RxCache; +import io.victoralbertos.jolyglot.GsonSpeaker; +import okhttp3.OkHttpClient; +import okhttp3.logging.HttpLoggingInterceptor; +import retrofit2.Retrofit; +import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; +import retrofit2.converter.gson.GsonConverterFactory; /** * Created by victor on 21/01/16. */ +/** + * TODO reappear steps: + * monitor Paging request! + * 1.onclick PageReFresh btn; + * 2.onclick LoadMore btn: + * i get some log: + * + * 08-14 16:17:35.297 25835-25854/victoralbertos.io.android D/okhttp_getUsers: lastIdQueried:1,source:CLOUD + * 08-14 16:17:37.990 25835-25853/victoralbertos.io.android D/okhttp_getUsers: lastIdQueried:20,source:CLOUD (loadmore has no data.so request from cloud is normal) + * + * 3.onclick PageReFresh btn; + * according to descriptions from readme-- EvictProvider allows to explicitly evict all the data associated with the provider. + * so the loadmore should request from cloud no matter EvictProvider.evict is true or no. + * + * but there are logs after onclick loadmore btn: + * D/okhttp_getUsers: lastIdQueried:20,source:PERSISTENCE + * + * so the is my pr reason. + * + * Thanks you read my pr! + * @param view + */ public class MainActivity extends Activity { + String TAG="Rxtest"; + private Providers mProvider; + private RxCache rxCache; + private int lastid=1; + private TextView mtv; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.layout); + mtv = (TextView) findViewById(R.id.tv); + HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); + httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);//ouput request basic message + OkHttpClient.Builder builder = new OkHttpClient.Builder().addNetworkInterceptor(httpLoggingInterceptor); + + Retrofit.Builder client = new Retrofit.Builder().baseUrl("https://api.github.com/").client(builder.build()); + client.addConverterFactory(GsonConverterFactory.create(new Gson())); + client.addCallAdapterFactory(RxJava2CallAdapterFactory.create()); + File cacheDir = getFilesDir(); + new RxCache.Builder() + .persistence(cacheDir, new GsonSpeaker()) + .using(Providers.class); + Retrofit mretrofit = client.build(); + mProvider = mretrofit.create(Providers.class); + + rxCache = new RxCache.Builder().persistence(getCacheDir(), new GsonSpeaker(new Gson())); + + } + public void ClearCache(View view){ + getFilesDir().deleteOnExit(); + } + + public void PageReFresh(View view){ + lastid=1; + Log.d(TAG, "PageReFresh: "); + Observable.just(mProvider.getUsers(lastid,10)) + .flatMap(new Function>, ObservableSource>>() { + @Override + public ObservableSource> apply(Observable> listObservable) throws Exception { + return rxCache.using(CommonCache.class).getUsers(listObservable,new DynamicKey(lastid),new EvictProvider(true)) + .map(new Function>, List>() { + @Override + public List apply(final Reply> listReply) throws Exception { + Log.d("okhttp_getUsers","lastIdQueried:"+lastid+",source:"+listReply.getSource()); + runOnUiThread(new Runnable() { + @Override + public void run() { + mtv.setText("lastIdQueried:"+lastid+",source:"+listReply.getSource()); + } + }); + + return listReply.getData(); + } + }); + } + }) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer>() { + @Override + public void onSubscribe(Disposable d) { + + } + + @Override + public void onNext(List users) { + lastid = users.get(users.size() - 1).getId(); + } + + @Override + public void onError(Throwable e) { + Log.d("okhttp_Requesterror",e.toString()); + + } + + @Override + public void onComplete() { + + } + }); + + + } + + + public void LoadMore(View view){ + if (lastid==1){ + Toast.makeText(this,"Please onclick PageRefresh btn first",Toast.LENGTH_LONG).show(); + return; + } + Log.d(TAG, "LoadMore: "); + Observable.just(mProvider.getUsers(lastid,10)) + .flatMap(new Function>, ObservableSource>>() { + @Override + public ObservableSource> apply(Observable> listObservable) throws Exception { + return rxCache.using(CommonCache.class).getUsers(listObservable,new DynamicKey(lastid),new EvictProvider(false)) + .map(new Function>, List>() { + @Override + public List apply(final Reply> listReply) throws Exception { + Log.d("okhttp_getUsers","lastIdQueried:"+lastid+",source:"+listReply.getSource()); + runOnUiThread(new Runnable() { + @Override + public void run() { + mtv.setText("lastIdQueried:"+lastid+",source:"+listReply.getSource()); + } + }); + return listReply.getData(); + } + }); + } + }) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer>() { + @Override + public void onSubscribe(Disposable d) { + + } + + @Override + public void onNext(List users) { + + } + + @Override + public void onError(Throwable e) { + Log.d("okhttp_Requesterror",e.toString()); + + } + + @Override + public void onComplete() { + + } + }); + } } diff --git a/android/src/main/java/victoralbertos/io/android/Providers.java b/android/src/main/java/victoralbertos/io/android/Providers.java new file mode 100644 index 0000000..d44fd3c --- /dev/null +++ b/android/src/main/java/victoralbertos/io/android/Providers.java @@ -0,0 +1,16 @@ +package victoralbertos.io.android; + +import java.util.List; + +import io.reactivex.Observable; +import retrofit2.http.GET; +import retrofit2.http.Headers; +import retrofit2.http.Query; + +public interface Providers { + String HEADER_API_VERSION = "Accept: application/vnd.github.v3+json"; + + @Headers({HEADER_API_VERSION}) + @GET("/users") + Observable> getUsers(@Query("since") int lastIdQueried, @Query("per_page") int perPage); +} diff --git a/android/src/main/java/victoralbertos/io/android/User.java b/android/src/main/java/victoralbertos/io/android/User.java new file mode 100644 index 0000000..4069bf2 --- /dev/null +++ b/android/src/main/java/victoralbertos/io/android/User.java @@ -0,0 +1,30 @@ +package victoralbertos.io.android; + +public class User { + private final int id; + private final String login; + private final String avatar_url; + + public User(int id, String login, String avatar_url) { + this.id = id; + this.login = login; + this.avatar_url = avatar_url; + } + + public String getAvatarUrl() { + if (avatar_url.isEmpty()) return avatar_url; + return avatar_url.split("\\?")[0]; + } + + + public int getId() { + return id; + } + + public String getLogin() { + return login; + } + + @Override public String toString() { + return "id -> " + id + " login -> " + login; + }} diff --git a/android/src/main/res/layout/layout.xml b/android/src/main/res/layout/layout.xml new file mode 100644 index 0000000..e4a172d --- /dev/null +++ b/android/src/main/res/layout/layout.xml @@ -0,0 +1,25 @@ + + +