From a476d101510ae4be4d63689d332512e0613104c2 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 1 Dec 2020 17:51:43 +0800 Subject: Fix ArrayIndexOutOfBoundsException in FollowedUsersRecyclerViewAdapter. Minor bugs fixed. --- .../activities/SubredditSelectionActivity.java | 4 + .../activities/ViewSubredditDetailActivity.java | 8 +- .../adapters/FollowedUsersRecyclerViewAdapter.java | 255 +++++++++++---------- .../services/DownloadRedditVideoService.java | 2 +- 4 files changed, 147 insertions(+), 122 deletions(-) (limited to 'app/src/main/java/ml/docilealligator') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditSelectionActivity.java index 3a81c202..8742ad7c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditSelectionActivity.java @@ -174,6 +174,10 @@ public class SubredditSelectionActivity extends BaseActivity implements Activity } private void bindView(boolean initializeFragment) { + if (isFinishing() || isDestroyed()) { + return; + } + loadSubscriptions(); if (initializeFragment) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java index ff6b6875..c62d4ae2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java @@ -901,9 +901,11 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp if (mMenu != null) { mMenu.findItem(R.id.action_lazy_mode_view_subreddit_detail_activity).setTitle(R.string.action_start_lazy_mode); } - sectionsPagerAdapter.refresh(); - mFetchSubredditInfoSuccess = false; - fetchSubredditData(); + if (sectionsPagerAdapter != null) { + sectionsPagerAdapter.refresh(); + mFetchSubredditInfoSuccess = false; + fetchSubredditData(); + } return true; case R.id.action_lazy_mode_view_subreddit_detail_activity: if (sectionsPagerAdapter != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/FollowedUsersRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/FollowedUsersRecyclerViewAdapter.java index 05c85ed1..40107a03 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/FollowedUsersRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/FollowedUsersRecyclerViewAdapter.java @@ -102,11 +102,6 @@ public class FollowedUsersRecyclerViewAdapter extends RecyclerView.Adapter 0) ? mFavoriteSubscribedUserData.size() + 2 : 0; - viewHolder.itemView.setOnClickListener(view -> { - Intent intent = new Intent(mContext, ViewUserDetailActivity.class); - intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mSubscribedUserData.get(viewHolder.getAdapterPosition() - offset).getName()); - mContext.startActivity(intent); - }); if (!mSubscribedUserData.get(viewHolder.getAdapterPosition() - offset).getIconUrl().equals("")) { glide.load(mSubscribedUserData.get(viewHolder.getAdapterPosition() - offset).getIconUrl()) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) @@ -125,66 +120,7 @@ public class FollowedUsersRecyclerViewAdapter extends RecyclerView.Adapter { - if(mSubscribedUserData.get(viewHolder.getAdapterPosition() - offset).isFavorite()) { - ((UserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); - mSubscribedUserData.get(viewHolder.getAdapterPosition() - offset).setFavorite(false); - FavoriteThing.unfavoriteUser(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, - mSubscribedUserData.get(viewHolder.getAdapterPosition() - offset), - new FavoriteThing.FavoriteThingListener() { - @Override - public void success() { - int position = viewHolder.getAdapterPosition() - offset; - if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { - mFavoriteSubscribedUserData.get(position).setFavorite(false); - } - ((UserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); - } - - @Override - public void failed() { - Toast.makeText(mContext, R.string.thing_unfavorite_failed, Toast.LENGTH_SHORT).show(); - int position = viewHolder.getAdapterPosition() - offset; - if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { - mFavoriteSubscribedUserData.get(position).setFavorite(true); - } - ((UserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); - } - }); - } else { - ((UserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); - mSubscribedUserData.get(viewHolder.getAdapterPosition() - offset).setFavorite(true); - FavoriteThing.favoriteUser(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, - mSubscribedUserData.get(viewHolder.getAdapterPosition() - offset), - new FavoriteThing.FavoriteThingListener() { - @Override - public void success() { - int position = viewHolder.getAdapterPosition() - offset; - if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { - mFavoriteSubscribedUserData.get(position).setFavorite(true); - } - ((UserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); - } - - @Override - public void failed() { - Toast.makeText(mContext, R.string.thing_favorite_failed, Toast.LENGTH_SHORT).show(); - int position = viewHolder.getAdapterPosition() - offset; - if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { - mFavoriteSubscribedUserData.get(position).setFavorite(false); - } - ((UserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); - } - }); - } - }); } else if (viewHolder instanceof FavoriteUserViewHolder) { - viewHolder.itemView.setOnClickListener(view -> { - Intent intent = new Intent(mContext, ViewUserDetailActivity.class); - intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mFavoriteSubscribedUserData.get(viewHolder.getAdapterPosition() - 1).getName()); - mContext.startActivity(intent); - }); if (!mFavoriteSubscribedUserData.get(viewHolder.getAdapterPosition() - 1).getIconUrl().equals("")) { glide.load(mFavoriteSubscribedUserData.get(viewHolder.getAdapterPosition() - 1).getIconUrl()) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) @@ -203,60 +139,6 @@ public class FollowedUsersRecyclerViewAdapter extends RecyclerView.Adapter { - if(mFavoriteSubscribedUserData.get(viewHolder.getAdapterPosition() - 1).isFavorite()) { - ((FavoriteUserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); - mFavoriteSubscribedUserData.get(viewHolder.getAdapterPosition() - 1).setFavorite(false); - FavoriteThing.unfavoriteUser(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, - mFavoriteSubscribedUserData.get(viewHolder.getAdapterPosition() - 1), - new FavoriteThing.FavoriteThingListener() { - @Override - public void success() { - int position = viewHolder.getAdapterPosition() - 1; - if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { - mFavoriteSubscribedUserData.get(position).setFavorite(false); - } - ((FavoriteUserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); - } - - @Override - public void failed() { - Toast.makeText(mContext, R.string.thing_unfavorite_failed, Toast.LENGTH_SHORT).show(); - int position = viewHolder.getAdapterPosition() - 1; - if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { - mFavoriteSubscribedUserData.get(position).setFavorite(true); - } - ((FavoriteUserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); - } - }); - } else { - ((FavoriteUserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); - mFavoriteSubscribedUserData.get(viewHolder.getAdapterPosition() - 1).setFavorite(true); - FavoriteThing.favoriteUser(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, - mFavoriteSubscribedUserData.get(viewHolder.getAdapterPosition() - 1), - new FavoriteThing.FavoriteThingListener() { - @Override - public void success() { - int position = viewHolder.getAdapterPosition() - 1; - if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { - mFavoriteSubscribedUserData.get(position).setFavorite(true); - } - ((FavoriteUserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); - } - - @Override - public void failed() { - Toast.makeText(mContext, R.string.thing_favorite_failed, Toast.LENGTH_SHORT).show(); - int position = viewHolder.getAdapterPosition() - 1; - if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { - mFavoriteSubscribedUserData.get(position).setFavorite(false); - } - ((FavoriteUserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); - } - }); - } - }); } } @@ -317,6 +199,72 @@ public class FollowedUsersRecyclerViewAdapter extends RecyclerView.Adapter { + int position = getAdapterPosition() - 1; + if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { + Intent intent = new Intent(mContext, ViewUserDetailActivity.class); + intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mFavoriteSubscribedUserData.get(position).getName()); + mContext.startActivity(intent); + } + }); + + favoriteImageView.setOnClickListener(view -> { + int position = getAdapterPosition() - 1; + if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { + if(mFavoriteSubscribedUserData.get(position).isFavorite()) { + favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); + mFavoriteSubscribedUserData.get(position).setFavorite(false); + FavoriteThing.unfavoriteUser(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, + mFavoriteSubscribedUserData.get(position), + new FavoriteThing.FavoriteThingListener() { + @Override + public void success() { + int position = getAdapterPosition() - 1; + if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { + mFavoriteSubscribedUserData.get(position).setFavorite(false); + } + favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); + } + + @Override + public void failed() { + Toast.makeText(mContext, R.string.thing_unfavorite_failed, Toast.LENGTH_SHORT).show(); + int position = getAdapterPosition() - 1; + if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { + mFavoriteSubscribedUserData.get(position).setFavorite(true); + } + favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); + } + }); + } else { + favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); + mFavoriteSubscribedUserData.get(position).setFavorite(true); + FavoriteThing.favoriteUser(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, + mFavoriteSubscribedUserData.get(position), + new FavoriteThing.FavoriteThingListener() { + @Override + public void success() { + int position = getAdapterPosition() - 1; + if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { + mFavoriteSubscribedUserData.get(position).setFavorite(true); + } + favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); + } + + @Override + public void failed() { + Toast.makeText(mContext, R.string.thing_favorite_failed, Toast.LENGTH_SHORT).show(); + int position = getAdapterPosition() - 1; + if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { + mFavoriteSubscribedUserData.get(position).setFavorite(false); + } + favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); + } + }); + } + } + }); } } @@ -332,6 +280,77 @@ public class FollowedUsersRecyclerViewAdapter extends RecyclerView.Adapter { + int offset = (mFavoriteSubscribedUserData != null && mFavoriteSubscribedUserData.size() > 0) ? + mFavoriteSubscribedUserData.size() + 2 : 0; + int position = getAdapterPosition() - offset; + if(position >= 0 && mSubscribedUserData.size() > position) { + Intent intent = new Intent(mContext, ViewUserDetailActivity.class); + intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mSubscribedUserData.get(position).getName()); + mContext.startActivity(intent); + } + }); + + favoriteImageView.setOnClickListener(view -> { + int offset = (mFavoriteSubscribedUserData != null && mFavoriteSubscribedUserData.size() > 0) ? + mFavoriteSubscribedUserData.size() + 2 : 0; + int position = getAdapterPosition() - offset; + + if(position >= 0 && mSubscribedUserData.size() > position) { + if(mSubscribedUserData.get(position).isFavorite()) { + favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); + mSubscribedUserData.get(position).setFavorite(false); + FavoriteThing.unfavoriteUser(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, + mSubscribedUserData.get(position), + new FavoriteThing.FavoriteThingListener() { + @Override + public void success() { + int position = getAdapterPosition() - offset; + if(position >= 0 && mSubscribedUserData.size() > position) { + mSubscribedUserData.get(position).setFavorite(false); + } + favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); + } + + @Override + public void failed() { + Toast.makeText(mContext, R.string.thing_unfavorite_failed, Toast.LENGTH_SHORT).show(); + int position = getAdapterPosition() - offset; + if(position >= 0 && mSubscribedUserData.size() > position) { + mSubscribedUserData.get(position).setFavorite(true); + } + favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); + } + }); + } else { + favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); + mSubscribedUserData.get(position).setFavorite(true); + FavoriteThing.favoriteUser(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, + mSubscribedUserData.get(position), + new FavoriteThing.FavoriteThingListener() { + @Override + public void success() { + int position = getAdapterPosition() - offset; + if(position >= 0 && mSubscribedUserData.size() > position) { + mSubscribedUserData.get(position).setFavorite(true); + } + favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); + } + + @Override + public void failed() { + Toast.makeText(mContext, R.string.thing_favorite_failed, Toast.LENGTH_SHORT).show(); + int position = getAdapterPosition() - offset; + if(position >= 0 && mSubscribedUserData.size() > position) { + mSubscribedUserData.get(position).setFavorite(false); + } + favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); + } + }); + } + } + }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java index 091877cd..e661488c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java @@ -567,7 +567,7 @@ public class DownloadRedditVideoService extends Service { muxer.stop(); muxer.release(); - } catch (IllegalArgumentException ignore) { + } catch (IllegalArgumentException | IllegalStateException ignore) { } catch (IOException e) { e.printStackTrace(); return false; -- cgit v1.2.3