From d733be6e55eccc6fef34b0065f6044e8402aa3ad Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 22 Jan 2019 18:45:02 +0800 Subject: Fixed the user's posts cannot be loaded. Refreshing in MainActivity also refreshes user info, subscribed users and subreddits. Enlarge the area which contains subreddit or user name and icon for easier touching. --- .../infinityforreddit/MainActivity.java | 190 +++++++++++---------- .../infinityforreddit/PostDataSource.java | 43 ++++- .../infinityforreddit/PostDataSourceFactory.java | 13 +- .../infinityforreddit/PostFragment.java | 65 ++++--- .../infinityforreddit/PostRecyclerViewAdapter.java | 25 +-- .../infinityforreddit/PostViewModel.java | 23 ++- .../res/layout/activity_view_subreddit_detail.xml | 8 +- app/src/main/res/layout/fragment_post.xml | 8 +- app/src/main/res/layout/item_post.xml | 63 ++++--- app/src/main/res/values/strings.xml | 3 + 10 files changed, 253 insertions(+), 188 deletions(-) (limited to 'app') 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(), new ArrayList(), - new ArrayList(), - new FetchSubscribedThing.FetchSubscribedThingListener() { - @Override - public void onFetchSubscribedThingSuccess(ArrayList subscribedSubredditData, - ArrayList subscribedUserData, - ArrayList 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, + ArrayList subscribedUserData, + ArrayList 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 { + 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 { private Locale locale; private String name; private int postType; + private OnPostFetchedCallback onPostFetchedCallback; private MutableLiveData paginationNetworkStateLiveData; private MutableLiveData initialLoadStateLiveData; @@ -31,22 +37,24 @@ class PostDataSource extends PageKeyedDataSource { private LoadParams params; private LoadCallback 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 { loadSubredditPostsInitial(callback); break; case TYPE_USER: - loadUserPostsInitial(callback); + loadUserPostsInitial(callback, null); break; } } @@ -117,6 +125,12 @@ class PostDataSource extends PageKeyedDataSource { new ParsePost.ParsePostListener() { @Override public void onParsePostSuccess(ArrayList 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 { new ParsePost.ParsePostListener() { @Override public void onParsePostSuccess(ArrayList 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 { }); } - private void loadUserPostsInitial(@NonNull final LoadInitialCallback callback) { + private void loadUserPostsInitial(@NonNull final LoadInitialCallback callback, String lastItem) { RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost = api.getUserBestPosts(name, null); + Call getPost = api.getUserBestPosts(name, lastItem); getPost.enqueue(new Callback() { @Override public void onResponse(Call call, retrofit2.Response response) { @@ -256,8 +276,17 @@ class PostDataSource extends PageKeyedDataSource { new ParsePost.ParsePostListener() { @Override public void onParsePostSuccess(ArrayList 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 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 { - 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 { - 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 initialLoadingState; private LiveData> 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 create(@NonNull Class 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); } } } diff --git a/app/src/main/res/layout/activity_view_subreddit_detail.xml b/app/src/main/res/layout/activity_view_subreddit_detail.xml index 6492e140..1460efac 100644 --- a/app/src/main/res/layout/activity_view_subreddit_detail.xml +++ b/app/src/main/res/layout/activity_view_subreddit_detail.xml @@ -49,7 +49,6 @@ android:layout_height="wrap_content" android:orientation="vertical" android:paddingTop="36dp" - android:paddingBottom="16dp" android:paddingStart="16dp" android:paddingEnd="16dp" android:layout_below="@id/banner_image_view_view_subreddit_detail_activity" @@ -75,7 +74,8 @@ + android:layout_marginTop="16dp" + android:layout_marginBottom="16dp"> + android:layout_toStartOf="@id/online_subscriber_count_text_view_view_subreddit_detail_activity" /> diff --git a/app/src/main/res/layout/fragment_post.xml b/app/src/main/res/layout/fragment_post.xml index 9493bb8f..1c78fcad 100644 --- a/app/src/main/res/layout/fragment_post.xml +++ b/app/src/main/res/layout/fragment_post.xml @@ -22,7 +22,7 @@ android:layout_height="match_parent" /> + android:gravity="center" /> diff --git a/app/src/main/res/layout/item_post.xml b/app/src/main/res/layout/item_post.xml index 1d686e52..585c8931 100644 --- a/app/src/main/res/layout/item_post.xml +++ b/app/src/main/res/layout/item_post.xml @@ -13,49 +13,60 @@ android:layout_height="wrap_content" android:orientation="vertical"> - - - + android:layout_height="wrap_content"> - + android:layout_height="wrap_content" + android:padding="16dp" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent"> + + + + + + + android:visibility="gone" + app:layout_constraintEnd_toStartOf="@+id/post_time_text_view_best_post_item" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintTop_toTopOf="parent"/> + android:layout_gravity="center" + android:layout_marginEnd="16dp" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintTop_toTopOf="parent" /> - + Error loading image. Tap to retry. Error loading posts.\nTap to retry. + No posts here. Cannot load posts Error loading comments @@ -47,4 +48,6 @@ Unfollowing Failed Subreddit Banner Image + + Error loading post -- cgit v1.2.3