Fixed a bug which causes the LastItemSynchronizer object in PaginationSynchronizer becomes null when the app is reopened after it was killed. Prepare to use Dagger 2 to manage Retrofit singleton.

This commit is contained in:
Alex Ning 2018-09-28 09:18:28 +08:00
parent bfb311d75f
commit cdcd5923fa
8 changed files with 76 additions and 60 deletions

View File

@ -25,7 +25,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View File

@ -60,4 +60,6 @@ dependencies {
implementation 'com.squareup.retrofit2:converter-scalars:2.4.0'
implementation 'jp.wasabeef:glide-transformations:3.3.0'
implementation 'com.muditsen.multilevelrecyclerview:multilevelview:1.0.0'
implementation 'com.google.dagger:dagger:2.17'
annotationProcessor 'com.google.dagger:dagger-compiler:2.17'
}

View File

@ -0,0 +1,8 @@
package ml.docilealligator.infinityforreddit;
import dagger.Component;
@Component(modules = NetworkModule.class)
interface Network {
}

View File

@ -0,0 +1,29 @@
package ml.docilealligator.infinityforreddit;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;
@Module
class NetworkModule {
@Provides
@Singleton
Retrofit provideOauthRetrofit() {
return new Retrofit.Builder()
.baseUrl(RedditUtils.OAUTH_API_BASE_URI)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
@Provides
@Singleton
Retrofit provideRetrofit() {
return new Retrofit.Builder()
.baseUrl(RedditUtils.API_BASE_URI)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
}

View File

@ -1,9 +1,6 @@
package ml.docilealligator.infinityforreddit;
import android.os.Parcel;
import android.os.Parcelable;
class PaginationSynchronizer implements Parcelable {
class PaginationSynchronizer {
private boolean loadingState;
private boolean loadSuccess;
private PaginationNotifier paginationNotifier;
@ -16,24 +13,7 @@ class PaginationSynchronizer implements Parcelable {
this. lastItemSynchronizer = lastItemSynchronizer;
}
protected PaginationSynchronizer(Parcel in) {
loadingState = in.readByte() != 0;
loadSuccess = in.readByte() != 0;
}
public static final Creator<PaginationSynchronizer> CREATOR = new Creator<PaginationSynchronizer>() {
@Override
public PaginationSynchronizer createFromParcel(Parcel in) {
return new PaginationSynchronizer(in);
}
@Override
public PaginationSynchronizer[] newArray(int size) {
return new PaginationSynchronizer[size];
}
};
public void setLoading(boolean isLoading) {
public void setLoadingState(boolean isLoading) {
this.loadingState = isLoading;
}
@ -41,7 +21,7 @@ class PaginationSynchronizer implements Parcelable {
return loadingState;
}
public void setLoadingState(boolean state) {
public void loadSuccess(boolean state) {
loadSuccess = state;
if(loadSuccess) {
paginationNotifier.LoadMorePostSuccess();
@ -50,6 +30,10 @@ class PaginationSynchronizer implements Parcelable {
}
}
public void setLoadSuccess(boolean loadSuccess) {
this.loadSuccess = loadSuccess;
}
public boolean isLoadSuccess() {
return loadSuccess;
}
@ -66,23 +50,8 @@ class PaginationSynchronizer implements Parcelable {
return paginationRetryNotifier;
}
public void setLastItemSynchronizer(LastItemSynchronizer lastItemSynchronizer) {
this.lastItemSynchronizer = lastItemSynchronizer;
}
public LastItemSynchronizer getLastItemSynchronizer() {
return lastItemSynchronizer;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeByte((byte) (loadingState ? 1 : 0));
parcel.writeByte((byte) (loadSuccess ? 1 : 0));
}
}

View File

@ -37,9 +37,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
static final String SUBREDDIT_NAME_KEY = "SNK";
static final String IS_BEST_POST_KEY = "IBPK";
private static final String PostDataParcelableState = "BPDPS";
private static final String lastItemState = "LIS";
private static final String paginationSynchronizerState = "PSS";
private static final String POST_DATA_PARCELABLE_STATE = "PDPS";
private static final String LAST_ITEM_STATE = "LIS";
private static final String LOADING_STATE_STATE = "LSS";
private static final String LOAD_SUCCESS_STATE = "LOSS";
private CoordinatorLayout mCoordinatorLayout;
private RecyclerView mPostRecyclerView;
@ -63,9 +64,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
if(mPostData != null) {
outState.putParcelableArrayList(PostDataParcelableState, mPostData);
outState.putString(lastItemState, mLastItem);
outState.putParcelable(paginationSynchronizerState, mPaginationSynchronizer);
outState.putParcelableArrayList(POST_DATA_PARCELABLE_STATE, mPostData);
outState.putString(LAST_ITEM_STATE, mLastItem);
outState.putBoolean(LOADING_STATE_STATE, mPaginationSynchronizer.isLoading());
outState.putBoolean(LOAD_SUCCESS_STATE, mPaginationSynchronizer.isLoadSuccess());
}
}
@ -114,10 +116,17 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
});
}
if(savedInstanceState != null && savedInstanceState.containsKey(PostDataParcelableState)) {
mPostData = savedInstanceState.getParcelableArrayList(PostDataParcelableState);
mLastItem = savedInstanceState.getString(lastItemState);
mPaginationSynchronizer = savedInstanceState.getParcelable(paginationSynchronizerState);
if(savedInstanceState != null && savedInstanceState.containsKey(POST_DATA_PARCELABLE_STATE)) {
mPostData = savedInstanceState.getParcelableArrayList(POST_DATA_PARCELABLE_STATE);
mLastItem = savedInstanceState.getString(LAST_ITEM_STATE);
mPaginationSynchronizer = new PaginationSynchronizer(new LastItemSynchronizer() {
@Override
public void lastItemChanged(String lastItem) {
mLastItem = lastItem;
}
});
mPaginationSynchronizer.setLoadSuccess(savedInstanceState.getBoolean(LOAD_SUCCESS_STATE));
mPaginationSynchronizer.setLoadingState(savedInstanceState.getBoolean(LOADING_STATE_STATE));
PostRecyclerViewAdapter adapter = new PostRecyclerViewAdapter(getActivity(), mPostData, mPaginationSynchronizer);
mPostRecyclerView.setAdapter(adapter);
mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener(

View File

@ -27,7 +27,6 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener {
private PostRecyclerViewAdapter mAdapter;
private ArrayList<PostData> mPostData;
private PaginationSynchronizer mPaginationSynchronizer;
private LastItemSynchronizer mLastItemSynchronizer;
private String mSubredditName;
private boolean isBestPost;
@ -64,7 +63,7 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener {
}
};
mPaginationSynchronizer.setPaginationRetryNotifier(paginationRetryNotifier);
mLastItemSynchronizer = mPaginationSynchronizer.getLastItemSynchronizer();
//mLastItemSynchronizer = mPaginationSynchronizer.getLastItemSynchronizer();
}
}
@ -95,7 +94,7 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener {
isLoading = true;
loadSuccess = false;
mPaginationSynchronizer.setLoading(true);
mPaginationSynchronizer.setLoadingState(true);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(RedditUtils.OAUTH_API_BASE_URI)
@ -120,12 +119,12 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener {
if(mAdapter != null) {
mAdapter.notifyItemRangeInserted(mPostData.size(), postData.size());
mLastItem = lastItem;
mLastItemSynchronizer.lastItemChanged(lastItem);
mPaginationSynchronizer.getLastItemSynchronizer().lastItemChanged(lastItem);
isLoading = false;
loadSuccess = true;
mPaginationSynchronizer.setLoading(false);
mPaginationSynchronizer.setLoadingState(true);
mPaginationSynchronizer.setLoadingState(false);
mPaginationSynchronizer.loadSuccess(true);
}
}
@ -171,7 +170,7 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener {
isLoading = true;
loadSuccess = false;
mPaginationSynchronizer.setLoading(true);
mPaginationSynchronizer.setLoadingState(true);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(RedditUtils.API_BASE_URI)
@ -193,12 +192,12 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener {
if(mAdapter != null) {
mAdapter.notifyItemRangeInserted(mPostData.size(), postData.size());
mLastItem = lastItem;
mLastItemSynchronizer.lastItemChanged(lastItem);
mPaginationSynchronizer.getLastItemSynchronizer().lastItemChanged(lastItem);
isLoading = false;
loadSuccess = true;
mPaginationSynchronizer.setLoading(false);
mPaginationSynchronizer.setLoadingState(true);
mPaginationSynchronizer.setLoadingState(false);
mPaginationSynchronizer.loadSuccess(true);
}
}
@ -227,7 +226,7 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener {
private void loadFailed() {
isLoading = false;
loadSuccess = false;
mPaginationSynchronizer.setLoading(false);
mPaginationSynchronizer.setLoadingState(false);
mPaginationSynchronizer.loadSuccess(false);
}
}