aboutsummaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2018-09-28 01:18:28 +0000
committerAlex Ning <chineseperson5@gmail.com>2018-09-28 01:18:28 +0000
commitcdcd5923fa2d581173badff9346f998605127732 (patch)
tree43ae9be2295aecc575643656dda0bbbd0fdee465 /app
parentbfb311d75fbe704e9db5aae04ceeb91315fb3ba1 (diff)
downloadinfinity-for-reddit-cdcd5923fa2d581173badff9346f998605127732.tar
infinity-for-reddit-cdcd5923fa2d581173badff9346f998605127732.tar.gz
infinity-for-reddit-cdcd5923fa2d581173badff9346f998605127732.tar.bz2
infinity-for-reddit-cdcd5923fa2d581173badff9346f998605127732.tar.lz
infinity-for-reddit-cdcd5923fa2d581173badff9346f998605127732.tar.xz
infinity-for-reddit-cdcd5923fa2d581173badff9346f998605127732.tar.zst
infinity-for-reddit-cdcd5923fa2d581173badff9346f998605127732.zip
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.
Diffstat (limited to '')
-rw-r--r--app/build.gradle2
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Network.java8
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java29
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PaginationSynchronizer.java45
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java29
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java21
6 files changed, 75 insertions, 59 deletions
diff --git a/app/build.gradle b/app/build.gradle
index 6286dcd4..13bb3cf8 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -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'
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Network.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Network.java
new file mode 100644
index 00000000..47ad2e38
--- /dev/null
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Network.java
@@ -0,0 +1,8 @@
+package ml.docilealligator.infinityforreddit;
+
+import dagger.Component;
+
+@Component(modules = NetworkModule.class)
+interface Network {
+
+}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java
new file mode 100644
index 00000000..e8cc9fea
--- /dev/null
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java
@@ -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();
+ }
+}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PaginationSynchronizer.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PaginationSynchronizer.java
index 15346dfc..c0e61184 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PaginationSynchronizer.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PaginationSynchronizer.java
@@ -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));
- }
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
index 8ddbd717..afad78ca 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
@@ -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(
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java
index a647a60c..30308dd2 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java
@@ -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);
}
} \ No newline at end of file