aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java190
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java43
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java13
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java65
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java25
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java23
6 files changed, 205 insertions, 154 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
index bfc88685..4e97a934 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
@@ -56,11 +56,16 @@ public class MainActivity extends AppCompatActivity {
private static final String FETCH_USER_INFO_STATE = "FUIS";
private static final String INSERT_SUBSCRIBED_SUBREDDIT_STATE = "ISSS";
- @BindView(R.id.subscribed_subreddit_recycler_view_main_activity) RecyclerView subscribedSubredditRecyclerView;
- @BindView(R.id.subscriptions_label_main_activity) TextView subscriptionsLabelTextView;
- @BindView(R.id.subscribed_user_recycler_view_main_activity) RecyclerView subscribedUserRecyclerView;
- @BindView(R.id.following_label_main_activity) TextView followingLabelTextView;
- @BindView(R.id.profile_linear_layout_main_activity) LinearLayout profileLinearLayout;
+ @BindView(R.id.subscribed_subreddit_recycler_view_main_activity)
+ RecyclerView subscribedSubredditRecyclerView;
+ @BindView(R.id.subscriptions_label_main_activity)
+ TextView subscriptionsLabelTextView;
+ @BindView(R.id.subscribed_user_recycler_view_main_activity)
+ RecyclerView subscribedUserRecyclerView;
+ @BindView(R.id.following_label_main_activity)
+ TextView followingLabelTextView;
+ @BindView(R.id.profile_linear_layout_main_activity)
+ LinearLayout profileLinearLayout;
private TextView mNameTextView;
private TextView mKarmaTextView;
@@ -82,13 +87,16 @@ public class MainActivity extends AppCompatActivity {
private SubscribedSubredditViewModel mSubscribedSubredditViewModel;
private SubscribedUserViewModel mSubscribedUserViewModel;
- @Inject @Named("user_info")
+ @Inject
+ @Named("user_info")
SharedPreferences mUserInfoSharedPreferences;
- @Inject @Named("auth_info")
+ @Inject
+ @Named("auth_info")
SharedPreferences mAuthInfoSharedPreferences;
- @Inject @Named("oauth")
+ @Inject
+ @Named("oauth")
Retrofit mOauthRetrofit;
@Override
@@ -109,11 +117,11 @@ public class MainActivity extends AppCompatActivity {
toggle.syncState();
String accessToken = getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
- if(accessToken.equals("")) {
+ if (accessToken.equals("")) {
Intent loginIntent = new Intent(this, LoginActivity.class);
startActivity(loginIntent);
} else {
- if(savedInstanceState == null) {
+ if (savedInstanceState == null) {
mFragment = new PostFragment();
Bundle bundle = new Bundle();
bundle.putInt(PostFragment.POST_TYPE_KEY, PostDataSource.TYPE_FRONT_PAGE);
@@ -124,7 +132,7 @@ public class MainActivity extends AppCompatActivity {
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit();
}
- loadUserData(savedInstanceState);
+ loadUserData();
View header = findViewById(R.id.nav_header_main_activity);
mNameTextView = header.findViewById(R.id.name_text_view_nav_header_main);
@@ -146,10 +154,10 @@ public class MainActivity extends AppCompatActivity {
mNameTextView.setText(mName);
mKarmaTextView.setText(mKarma);
glide = Glide.with(this);
- if(!mProfileImageUrl.equals("")) {
+ if (!mProfileImageUrl.equals("")) {
glide.load(mProfileImageUrl).into(mProfileImageView);
}
- if(!mBannerImageUrl.equals("")) {
+ if (!mBannerImageUrl.equals("")) {
glide.load(mBannerImageUrl).into(mBannerImageView);
}
@@ -189,83 +197,76 @@ public class MainActivity extends AppCompatActivity {
}
}
- private void loadUserData(Bundle savedInstanceState) {
- if (savedInstanceState == null) {
- if (!mFetchUserInfoSuccess) {
- FetchMyInfo.fetchMyInfo(mOauthRetrofit, mAuthInfoSharedPreferences, new FetchMyInfo.FetchUserMyListener() {
- @Override
- public void onFetchMyInfoSuccess(String response) {
- ParseMyInfo.parseMyInfo(response, new ParseMyInfo.ParseMyInfoListener() {
- @Override
- public void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) {
- mNameTextView.setText(name);
- if (!mProfileImageUrl.equals("")) {
- glide.load(profileImageUrl).into(mProfileImageView);
- }
- if (!mBannerImageUrl.equals("")) {
- glide.load(bannerImageUrl).into(mBannerImageView);
- }
-
- mName = name;
- mProfileImageUrl = profileImageUrl;
- mBannerImageUrl = bannerImageUrl;
- mKarma = getString(R.string.karma_info, karma);
-
- mKarmaTextView.setText(mKarma);
-
- SharedPreferences.Editor editor = mUserInfoSharedPreferences.edit();
- editor.putString(SharedPreferencesUtils.USER_KEY, name);
- editor.putString(SharedPreferencesUtils.PROFILE_IMAGE_URL_KEY, profileImageUrl);
- editor.putString(SharedPreferencesUtils.BANNER_IMAGE_URL_KEY, bannerImageUrl);
- editor.putString(SharedPreferencesUtils.KARMA_KEY, mKarma);
- editor.apply();
- mFetchUserInfoSuccess = true;
+ private void loadUserData() {
+ if (!mFetchUserInfoSuccess) {
+ FetchMyInfo.fetchMyInfo(mOauthRetrofit, mAuthInfoSharedPreferences, new FetchMyInfo.FetchUserMyListener() {
+ @Override
+ public void onFetchMyInfoSuccess(String response) {
+ ParseMyInfo.parseMyInfo(response, new ParseMyInfo.ParseMyInfoListener() {
+ @Override
+ public void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) {
+ mNameTextView.setText(name);
+ if (!mProfileImageUrl.equals("")) {
+ glide.load(profileImageUrl).into(mProfileImageView);
}
-
- @Override
- public void onParseMyInfoFail() {
- mFetchUserInfoSuccess = false;
+ if (!mBannerImageUrl.equals("")) {
+ glide.load(bannerImageUrl).into(mBannerImageView);
}
- });
- }
-
- @Override
- public void onFetchMyInfoFail() {
- mFetchUserInfoSuccess = false;
- }
- });
- }
- if (!mInsertSuccess) {
- FetchSubscribedThing.fetchSubscribedThing(mOauthRetrofit, mAuthInfoSharedPreferences, null,
- new ArrayList<SubscribedSubredditData>(), new ArrayList<SubscribedUserData>(),
- new ArrayList<SubredditData>(),
- new FetchSubscribedThing.FetchSubscribedThingListener() {
- @Override
- public void onFetchSubscribedThingSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData,
- ArrayList<SubscribedUserData> subscribedUserData,
- ArrayList<SubredditData> subredditData) {
- new InsertSubscribedThingsAsyncTask(
- SubscribedSubredditRoomDatabase.getDatabase(MainActivity.this),
- SubscribedUserRoomDatabase.getDatabase(MainActivity.this),
- SubredditRoomDatabase.getDatabase(MainActivity.this),
- subscribedSubredditData,
- subscribedUserData,
- subredditData,
- new InsertSubscribedThingsAsyncTask.InsertSubscribedThingListener() {
- @Override
- public void insertSuccess() {
- mInsertSuccess = true;
- }
- }).execute();
- }
+ mName = name;
+ mProfileImageUrl = profileImageUrl;
+ mBannerImageUrl = bannerImageUrl;
+ mKarma = getString(R.string.karma_info, karma);
+
+ mKarmaTextView.setText(mKarma);
+
+ SharedPreferences.Editor editor = mUserInfoSharedPreferences.edit();
+ editor.putString(SharedPreferencesUtils.USER_KEY, name);
+ editor.putString(SharedPreferencesUtils.PROFILE_IMAGE_URL_KEY, profileImageUrl);
+ editor.putString(SharedPreferencesUtils.BANNER_IMAGE_URL_KEY, bannerImageUrl);
+ editor.putString(SharedPreferencesUtils.KARMA_KEY, mKarma);
+ editor.apply();
+ mFetchUserInfoSuccess = true;
+ }
+
+ @Override
+ public void onParseMyInfoFail() {
+ mFetchUserInfoSuccess = false;
+ }
+ });
+ }
- @Override
- public void onFetchSubscribedThingFail() {
- mInsertSuccess = false;
- }
- });
- }
+ @Override
+ public void onFetchMyInfoFail() {
+ mFetchUserInfoSuccess = false;
+ }
+ });
+ }
+
+ if (!mInsertSuccess) {
+ FetchSubscribedThing.fetchSubscribedThing(mOauthRetrofit, mAuthInfoSharedPreferences, null,
+ new ArrayList<>(), new ArrayList<>(),
+ new ArrayList<>(),
+ new FetchSubscribedThing.FetchSubscribedThingListener() {
+ @Override
+ public void onFetchSubscribedThingSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData,
+ ArrayList<SubscribedUserData> subscribedUserData,
+ ArrayList<SubredditData> subredditData) {
+ new InsertSubscribedThingsAsyncTask(
+ SubscribedSubredditRoomDatabase.getDatabase(MainActivity.this),
+ SubscribedUserRoomDatabase.getDatabase(MainActivity.this),
+ SubredditRoomDatabase.getDatabase(MainActivity.this),
+ subscribedSubredditData,
+ subscribedUserData,
+ subredditData,
+ () -> mInsertSuccess = true).execute();
+ }
+
+ @Override
+ public void onFetchSubscribedThingFail() {
+ mInsertSuccess = false;
+ }
+ });
}
}
@@ -279,8 +280,11 @@ public class MainActivity extends AppCompatActivity {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh_main_activity:
- if(mFragment instanceof FragmentCommunicator) {
+ if (mFragment instanceof FragmentCommunicator) {
((FragmentCommunicator) mFragment).refresh();
+ mFetchUserInfoSuccess = false;
+ mInsertSuccess = false;
+ loadUserData();
}
return true;
}
@@ -300,7 +304,7 @@ public class MainActivity extends AppCompatActivity {
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
- if(mFragment != null) {
+ if (mFragment != null) {
getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment);
}
outState.putString(NAME_STATE, mName);
@@ -322,10 +326,10 @@ public class MainActivity extends AppCompatActivity {
mInsertSuccess = savedInstanceState.getBoolean(INSERT_SUBSCRIBED_SUBREDDIT_STATE);
mNameTextView.setText(mName);
mKarmaTextView.setText(mKarma);
- if(!mProfileImageUrl.equals("")) {
+ if (!mProfileImageUrl.equals("")) {
glide.load(mProfileImageUrl).into(mProfileImageView);
}
- if(!mBannerImageUrl.equals("")) {
+ if (!mBannerImageUrl.equals("")) {
glide.load(mBannerImageUrl).into(mBannerImageView);
}
}
@@ -362,13 +366,13 @@ public class MainActivity extends AppCompatActivity {
@Override
protected Void doInBackground(final Void... params) {
- for(SubscribedSubredditData s : subscribedSubredditData) {
+ for (SubscribedSubredditData s : subscribedSubredditData) {
mSubscribedSubredditDao.insert(s);
}
- for(SubscribedUserData s : subscribedUserData) {
+ for (SubscribedUserData s : subscribedUserData) {
mUserDao.insert(s);
}
- for(SubredditData s : subredditData) {
+ for (SubredditData s : subredditData) {
mSubredditDao.insert(s);
}
return null;
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java
index 29e78b07..615ceb9f 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java
@@ -13,6 +13,11 @@ import retrofit2.Callback;
import retrofit2.Retrofit;
class PostDataSource extends PageKeyedDataSource<String, Post> {
+ interface OnPostFetchedCallback {
+ void hasPost();
+ void noPost();
+ }
+
static final int TYPE_FRONT_PAGE = 0;
static final int TYPE_SUBREDDIT = 1;
static final int TYPE_USER = 2;
@@ -22,6 +27,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
private Locale locale;
private String name;
private int postType;
+ private OnPostFetchedCallback onPostFetchedCallback;
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
private MutableLiveData<NetworkState> initialLoadStateLiveData;
@@ -31,22 +37,24 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
private LoadParams<String> params;
private LoadCallback<String, Post> callback;
- PostDataSource(Retrofit retrofit, String accessToken, Locale locale, int postType) {
+ PostDataSource(Retrofit retrofit, String accessToken, Locale locale, int postType, OnPostFetchedCallback onPostFetchedCallback) {
this.retrofit = retrofit;
this.accessToken = accessToken;
this.locale = locale;
paginationNetworkStateLiveData = new MutableLiveData();
initialLoadStateLiveData = new MutableLiveData();
this.postType = postType;
+ this.onPostFetchedCallback = onPostFetchedCallback;
}
- PostDataSource(Retrofit retrofit, Locale locale, String name, int postType) {
+ PostDataSource(Retrofit retrofit, Locale locale, String name, int postType, OnPostFetchedCallback onPostFetchedCallback) {
this.retrofit = retrofit;
this.locale = locale;
this.name = name;
paginationNetworkStateLiveData = new MutableLiveData();
initialLoadStateLiveData = new MutableLiveData();
this.postType = postType;
+ this.onPostFetchedCallback = onPostFetchedCallback;
}
MutableLiveData getPaginationNetworkStateLiveData() {
@@ -72,7 +80,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
loadSubredditPostsInitial(callback);
break;
case TYPE_USER:
- loadUserPostsInitial(callback);
+ loadUserPostsInitial(callback, null);
break;
}
}
@@ -117,6 +125,12 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
new ParsePost.ParsePostListener() {
@Override
public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) {
+ if(newPosts.size() == 0) {
+ onPostFetchedCallback.noPost();
+ } else {
+ onPostFetchedCallback.hasPost();
+ }
+
callback.onResult(newPosts, null, lastItem);
initialLoadStateLiveData.postValue(NetworkState.LOADED);
}
@@ -187,6 +201,12 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
new ParsePost.ParsePostListener() {
@Override
public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) {
+ if(newPosts.size() == 0) {
+ onPostFetchedCallback.noPost();
+ } else {
+ onPostFetchedCallback.hasPost();
+ }
+
callback.onResult(newPosts, null, lastItem);
initialLoadStateLiveData.postValue(NetworkState.LOADED);
}
@@ -245,9 +265,9 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
});
}
- private void loadUserPostsInitial(@NonNull final LoadInitialCallback<String, Post> callback) {
+ private void loadUserPostsInitial(@NonNull final LoadInitialCallback<String, Post> callback, String lastItem) {
RedditAPI api = retrofit.create(RedditAPI.class);
- Call<String> getPost = api.getUserBestPosts(name, null);
+ Call<String> getPost = api.getUserBestPosts(name, lastItem);
getPost.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, retrofit2.Response<String> response) {
@@ -256,8 +276,17 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
new ParsePost.ParsePostListener() {
@Override
public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) {
- callback.onResult(newPosts, null, lastItem);
- initialLoadStateLiveData.postValue(NetworkState.LOADED);
+ if(newPosts.size() == 0 && lastItem.equals("null")) {
+ callback.onResult(newPosts, null, lastItem);
+ initialLoadStateLiveData.postValue(NetworkState.LOADED);
+ onPostFetchedCallback.noPost();
+ } else if(newPosts.size() == 0) {
+ loadUserPostsInitial(callback, lastItem);
+ } else {
+ callback.onResult(newPosts, null, lastItem);
+ initialLoadStateLiveData.postValue(NetworkState.LOADED);
+ onPostFetchedCallback.hasPost();
+ }
}
@Override
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java
index 19fe9a06..848799bf 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java
@@ -13,32 +13,37 @@ class PostDataSourceFactory extends DataSource.Factory {
private Locale locale;
private String subredditName;
private int postType;
+ private PostDataSource.OnPostFetchedCallback onPostFetchedCallback;
private PostDataSource postDataSource;
private MutableLiveData<PostDataSource> postDataSourceLiveData;
- PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, int postType) {
+ PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, int postType,
+ PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
this.retrofit = retrofit;
this.accessToken = accessToken;
this.locale = locale;
postDataSourceLiveData = new MutableLiveData<>();
this.postType = postType;
+ this.onPostFetchedCallback = onPostFetchedCallback;
}
- PostDataSourceFactory(Retrofit retrofit, Locale locale, String subredditName, int postType) {
+ PostDataSourceFactory(Retrofit retrofit, Locale locale, String subredditName, int postType,
+ PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
this.retrofit = retrofit;
this.locale = locale;
this.subredditName = subredditName;
postDataSourceLiveData = new MutableLiveData<>();
this.postType = postType;
+ this.onPostFetchedCallback = onPostFetchedCallback;
}
@Override
public DataSource create() {
if(postType == PostDataSource.TYPE_FRONT_PAGE) {
- postDataSource = new PostDataSource(retrofit, accessToken, locale, postType);
+ postDataSource = new PostDataSource(retrofit, accessToken, locale, postType, onPostFetchedCallback);
} else {
- postDataSource = new PostDataSource(retrofit, locale, subredditName, postType);
+ postDataSource = new PostDataSource(retrofit, locale, subredditName, postType, onPostFetchedCallback);
}
postDataSourceLiveData.postValue(postDataSource);
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
index 995cee9a..e4023fd0 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
@@ -7,7 +7,6 @@ import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
-import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
@@ -16,6 +15,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
+import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;
@@ -38,8 +38,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
private RecyclerView mPostRecyclerView;
private LinearLayoutManager mLinearLayoutManager;
private CircleProgressBar mProgressBar;
- private LinearLayout mFetchPostErrorLinearLayout;
- private ImageView mFetchPostErrorImageView;
+ private LinearLayout mFetchPostInfoLinearLayout;
+ private ImageView mFetchPostInfoImageView;
+ private TextView mFetchPostInfoTextView;
private String mName;
private int mPostType;
@@ -82,8 +83,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mLinearLayoutManager = new LinearLayoutManager(getActivity());
mPostRecyclerView.setLayoutManager(mLinearLayoutManager);
mProgressBar = rootView.findViewById(R.id.progress_bar_post_fragment);
- mFetchPostErrorLinearLayout = rootView.findViewById(R.id.fetch_post_error_linear_layout_post_fragment);
- mFetchPostErrorImageView = rootView.findViewById(R.id.fetch_post_error_image_view_post_fragment);
+ mFetchPostInfoLinearLayout = rootView.findViewById(R.id.fetch_post_info_linear_layout_post_fragment);
+ mFetchPostInfoImageView = rootView.findViewById(R.id.fetch_post_info_image_view_post_fragment);
+ mFetchPostInfoTextView = rootView.findViewById(R.id.fetch_post_info_text_view_post_fragment);
/*FloatingActionButton fab = rootView.findViewById(R.id.fab_post_fragment);
fab.setOnClickListener(new View.OnClickListener() {
@Override
@@ -97,8 +99,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
if(mPostType != PostDataSource.TYPE_FRONT_PAGE) {
mName = getArguments().getString(NAME_KEY);
- } else {
- mFetchPostErrorLinearLayout.setOnClickListener(view -> mPostViewModel.retry());
}
if(mPostType == PostDataSource.TYPE_FRONT_PAGE) {
@@ -116,11 +116,38 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
PostViewModel.Factory factory;
if(mPostType == PostDataSource.TYPE_FRONT_PAGE) {
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
- getResources().getConfiguration().locale, mPostType);
+ getResources().getConfiguration().locale, mPostType, new PostDataSource.OnPostFetchedCallback() {
+ @Override
+ public void hasPost() {
+ mFetchPostInfoLinearLayout.setVisibility(View.GONE);
+ }
+
+ @Override
+ public void noPost() {
+ mFetchPostInfoLinearLayout.setOnClickListener(view -> {
+ //Do nothing
+ });
+ showErrorView(R.string.no_posts);
+ }
+ });
} else {
factory = new PostViewModel.Factory(mRetrofit,
- getResources().getConfiguration().locale, mName, mPostType);
+ getResources().getConfiguration().locale, mName, mPostType, new PostDataSource.OnPostFetchedCallback() {
+ @Override
+ public void hasPost() {
+ mFetchPostInfoLinearLayout.setVisibility(View.GONE);
+ }
+
+ @Override
+ public void noPost() {
+ mFetchPostInfoLinearLayout.setOnClickListener(view -> {
+ //Do nothing
+ });
+ showErrorView(R.string.no_posts);
+ }
+ });
}
+
mPostViewModel = ViewModelProviders.of(this, factory).get(PostViewModel.class);
mPostViewModel.getPosts().observe(this, posts -> mAdapter.submitList(posts));
@@ -128,9 +155,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mProgressBar.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
- showErrorView();
+ mFetchPostInfoLinearLayout.setOnClickListener(view -> mPostViewModel.retry());
+ showErrorView(R.string.load_posts_error);
} else {
- mFetchPostErrorLinearLayout.setVisibility(View.GONE);
+ mFetchPostInfoLinearLayout.setVisibility(View.GONE);
mProgressBar.setVisibility(View.VISIBLE);
}
});
@@ -147,17 +175,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mPostViewModel.refresh();
}
- private void showErrorView() {
+ private void showErrorView(int stringResId) {
mProgressBar.setVisibility(View.GONE);
- if(mPostType == PostDataSource.TYPE_FRONT_PAGE) {
- if(getActivity() != null && isAdded()) {
- mFetchPostErrorLinearLayout.setVisibility(View.VISIBLE);
- Glide.with(this).load(R.drawable.load_post_error_indicator).into(mFetchPostErrorImageView);
- }
- } else {
- Snackbar snackbar = Snackbar.make(mCoordinatorLayout, "Error getting post", Snackbar.LENGTH_INDEFINITE);
- snackbar.setAction(R.string.retry, view -> mPostViewModel.retry());
- snackbar.show();
+ if(getActivity() != null && isAdded()) {
+ mFetchPostInfoLinearLayout.setVisibility(View.VISIBLE);
+ mFetchPostInfoTextView.setText(stringResId);
+ Glide.with(this).load(R.drawable.load_post_error_indicator).into(mFetchPostInfoImageView);
}
}
} \ No newline at end of file
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java
index da60c5a5..7028c81b 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java
@@ -23,6 +23,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
+import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
@@ -212,20 +213,10 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
.into(((DataViewHolder) holder).subredditIconGifImageView);
}
- ((DataViewHolder) holder).subredditIconGifImageView.setOnClickListener(view -> {
- if(canStartActivity) {
- canStartActivity = false;
- Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
- intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY,
- post.getSubredditNamePrefixed().substring(2));
- mContext.startActivity(intent);
- }
- });
-
((DataViewHolder) holder).subredditNameTextView.setTextColor(mContext.getResources().getColor(R.color.colorAccent));
((DataViewHolder) holder).subredditNameTextView.setText(subredditNamePrefixed);
- ((DataViewHolder) holder).subredditNameTextView.setOnClickListener(view -> {
+ ((DataViewHolder) holder).subredditIconNameLinearLayout.setOnClickListener(view -> {
if(canStartActivity) {
canStartActivity = false;
Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
@@ -297,19 +288,10 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
.into(((DataViewHolder) holder).subredditIconGifImageView);
}
- ((DataViewHolder) holder).subredditIconGifImageView.setOnClickListener(view -> {
- if(canStartActivity) {
- canStartActivity = false;
- Intent intent = new Intent(mContext, ViewUserDetailActivity.class);
- intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, post.getAuthor());
- mContext.startActivity(intent);
- }
- });
-
((DataViewHolder) holder).subredditNameTextView.setTextColor(mContext.getResources().getColor(R.color.colorPrimaryDark));
((DataViewHolder) holder).subredditNameTextView.setText(author);
- ((DataViewHolder) holder).subredditNameTextView.setOnClickListener(view -> {
+ ((DataViewHolder) holder).subredditIconNameLinearLayout.setOnClickListener(view -> {
if(canStartActivity) {
canStartActivity = false;
Intent intent = new Intent(mContext, ViewUserDetailActivity.class);
@@ -645,6 +627,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
class DataViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.card_view_view_post_detail) MaterialCardView cardView;
+ @BindView(R.id.subreddit_icon_name_linear_layout_view_item_best_post) LinearLayout subredditIconNameLinearLayout;
@BindView(R.id.subreddit_icon_gif_image_view_best_post_item) AspectRatioGifImageView subredditIconGifImageView;
@BindView(R.id.subreddit_text_view_best_post_item) TextView subredditNameTextView;
@BindView(R.id.stickied_post_image_view_best_post_item) ImageView stickiedPostImageView;
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java
index 939ca68c..69df991f 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java
@@ -18,8 +18,9 @@ public class PostViewModel extends ViewModel {
private LiveData<NetworkState> initialLoadingState;
private LiveData<PagedList<Post>> posts;
- public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType) {
- postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, postType);
+ public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType,
+ PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
+ postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, postType, onPostFetchedCallback);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
dataSource -> dataSource.getInitialLoadStateLiveData());
@@ -34,8 +35,9 @@ public class PostViewModel extends ViewModel {
posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
}
- public PostViewModel(Retrofit retrofit, Locale locale, String subredditName, int postType) {
- postDataSourceFactory = new PostDataSourceFactory(retrofit, locale, subredditName, postType);
+ public PostViewModel(Retrofit retrofit, Locale locale, String subredditName, int postType,
+ PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
+ postDataSourceFactory = new PostDataSourceFactory(retrofit, locale, subredditName, postType, onPostFetchedCallback);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
dataSource -> dataSource.getInitialLoadStateLiveData());
@@ -81,28 +83,33 @@ public class PostViewModel extends ViewModel {
private Locale locale;
private String subredditName;
private int postType;
+ private PostDataSource.OnPostFetchedCallback onPostFetchedCallback;
- public Factory(Retrofit retrofit, String accessToken, Locale locale, int postType) {
+ public Factory(Retrofit retrofit, String accessToken, Locale locale, int postType,
+ PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
this.retrofit = retrofit;
this.accessToken = accessToken;
this.locale = locale;
this.postType = postType;
+ this.onPostFetchedCallback = onPostFetchedCallback;
}
- public Factory(Retrofit retrofit, Locale locale, String subredditName, int postType) {
+ public Factory(Retrofit retrofit, Locale locale, String subredditName, int postType,
+ PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
this.retrofit = retrofit;
this.locale = locale;
this.subredditName = subredditName;
this.postType = postType;
+ this.onPostFetchedCallback = onPostFetchedCallback;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
if(postType == PostDataSource.TYPE_FRONT_PAGE) {
- return (T) new PostViewModel(retrofit, accessToken, locale, postType);
+ return (T) new PostViewModel(retrofit, accessToken, locale, postType, onPostFetchedCallback);
} else {
- return (T) new PostViewModel(retrofit, locale, subredditName, postType);
+ return (T) new PostViewModel(retrofit, locale, subredditName, postType, onPostFetchedCallback);
}
}
}