diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-04-19 04:01:41 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-04-19 04:01:41 +0000 |
commit | d8ddad9522596a6f8fb2e5a5d46ba74f6ed1e234 (patch) | |
tree | ea156c1d09928890530225a609a982b4002e12d7 /app/src/main/java/ml | |
parent | cd4730c6492eae869e74182383c35be16c70ae25 (diff) | |
download | infinity-for-reddit-d8ddad9522596a6f8fb2e5a5d46ba74f6ed1e234.tar infinity-for-reddit-d8ddad9522596a6f8fb2e5a5d46ba74f6ed1e234.tar.gz infinity-for-reddit-d8ddad9522596a6f8fb2e5a5d46ba74f6ed1e234.tar.bz2 infinity-for-reddit-d8ddad9522596a6f8fb2e5a5d46ba74f6ed1e234.tar.lz infinity-for-reddit-d8ddad9522596a6f8fb2e5a5d46ba74f6ed1e234.tar.xz infinity-for-reddit-d8ddad9522596a6f8fb2e5a5d46ba74f6ed1e234.tar.zst infinity-for-reddit-d8ddad9522596a6f8fb2e5a5d46ba74f6ed1e234.zip |
Fixed voting state inconsistency after orientation change in ViewPostDetailActivity. Fixed title could not be shown properly in CollapsingToolbarLayout in ViewSubredditDetailActivity and ViewUserDetailActivity.
Diffstat (limited to 'app/src/main/java/ml')
4 files changed, 37 insertions, 434 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAdapter.java deleted file mode 100644 index ca0ec181..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAdapter.java +++ /dev/null @@ -1,349 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.graphics.ColorFilter; -import android.net.Uri; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ImageView; -import android.widget.ProgressBar; -import android.widget.TextView; -import android.widget.Toast; - -import com.multilevelview.models.RecyclerViewItem; - -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; - -import androidx.annotation.NonNull; -import androidx.browser.customtabs.CustomTabsIntent; -import androidx.core.content.ContextCompat; -import androidx.paging.PagedListAdapter; -import androidx.recyclerview.widget.DiffUtil; -import androidx.recyclerview.widget.RecyclerView; -import butterknife.BindView; -import butterknife.ButterKnife; -import retrofit2.Retrofit; -import ru.noties.markwon.SpannableConfiguration; -import ru.noties.markwon.view.MarkwonView; - -public class CommentAdapter extends PagedListAdapter<CommentData, RecyclerView.ViewHolder> { - private Context mContext; - private Retrofit mRetrofit; - private Retrofit mOauthRetrofit; - private SharedPreferences mSharedPreferences; - private RecyclerView mRecyclerView; - private String subredditNamePrefixed; - private String article; - private Locale locale; - - private NetworkState networkState; - private RetryLoadingMoreCallback retryLoadingMoreCallback; - - interface RetryLoadingMoreCallback { - void retryLoadingMore(); - } - - CommentAdapter(Context context, Retrofit retrofit, Retrofit oauthRetrofit, - SharedPreferences sharedPreferences, RecyclerView recyclerView, - String subredditNamePrefixed, String article, Locale locale) { - super(DIFF_CALLBACK); - mContext = context; - mRetrofit = retrofit; - mOauthRetrofit = oauthRetrofit; - mSharedPreferences = sharedPreferences; - mRecyclerView = recyclerView; - this.subredditNamePrefixed = subredditNamePrefixed; - this.article = article; - this.locale = locale; - } - - static final DiffUtil.ItemCallback<CommentData> DIFF_CALLBACK = new DiffUtil.ItemCallback<CommentData>() { - @Override - public boolean areItemsTheSame(@NonNull CommentData commentData, @NonNull CommentData t1) { - return commentData.getId().equals(t1.getId()); - } - - @Override - public boolean areContentsTheSame(@NonNull CommentData commentData, @NonNull CommentData t1) { - return commentData.getCommentContent().equals(t1.getCommentContent()); - } - }; - - @NonNull - @Override - public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) { - return new CommentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false)); - } - - @Override - public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) { - final CommentData commentItem = getItem(i); - - String authorPrefixed = "u/" + commentItem.getAuthor(); - ((CommentViewHolder) viewHolder).authorTextView.setText(authorPrefixed); - ((CommentViewHolder) viewHolder).authorTextView.setOnClickListener(view -> { - Intent intent = new Intent(mContext, ViewUserDetailActivity.class); - intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, commentItem.getAuthor()); - mContext.startActivity(intent); - }); - - ((CommentViewHolder) viewHolder).commentTimeTextView.setText(commentItem.getCommentTime()); - SpannableConfiguration spannableConfiguration = SpannableConfiguration.builder(mContext).linkResolver((view, link) -> { - if(link.startsWith("/u/")) { - Intent intent = new Intent(mContext, ViewUserDetailActivity.class); - intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, link.substring(3)); - mContext.startActivity(intent); - } else if(link.startsWith("/r/")) { - Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class); - intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, link.substring(3)); - mContext.startActivity(intent); - } else { - CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); - // add share action to menu list - builder.addDefaultShareMenuItem(); - builder.setToolbarColor(mContext.getResources().getColor(R.color.colorPrimary)); - CustomTabsIntent customTabsIntent = builder.build(); - customTabsIntent.launchUrl(mContext, Uri.parse(link)); - } - }).build(); - - ((CommentViewHolder) viewHolder).commentMarkdownView.setMarkdown(spannableConfiguration, commentItem.getCommentContent()); - ((CommentViewHolder) viewHolder).scoreTextView.setText(Integer.toString(commentItem.getScore())); - - ((CommentViewHolder) viewHolder).verticalBlock.getLayoutParams().width = commentItem.getDepth() * 16; - if(commentItem.hasReply()) { - setExpandButton(((CommentViewHolder) viewHolder).expandButton, commentItem.isExpanded()); - } - - ((CommentViewHolder) viewHolder).expandButton.setOnClickListener(view -> { - if(commentItem.hasChildren() && commentItem.getChildren().size() > 0) { - setExpandButton(((CommentViewHolder) viewHolder).expandButton, commentItem.isExpanded()); - } else { - ((CommentViewHolder) viewHolder).loadMoreCommentsProgressBar.setVisibility(View.VISIBLE); - FetchComment.fetchComment(mRetrofit, subredditNamePrefixed, article, commentItem.getId(), - locale, false, commentItem.getDepth(), new FetchComment.FetchCommentListener() { - @Override - public void onFetchCommentSuccess(List<?> commentData, - String parentId, ArrayList<String> children) { - commentItem.addChildren((List<RecyclerViewItem>) commentData); - ((CommentViewHolder) viewHolder).loadMoreCommentsProgressBar - .setVisibility(View.GONE); - ((CommentViewHolder) viewHolder).expandButton - .setImageResource(R.drawable.ic_expand_less_black_20dp); - - /*ParseComment.parseComment(response, new ArrayList<>(), - locale, false, commentItem.getDepth(), - new ParseComment.ParseCommentListener() { - @Override - public void onParseCommentSuccess(List<?> commentData, - String parentId, String children) { - commentItem.addChildren((List<RecyclerViewItem>) commentData); - ((CommentViewHolder) viewHolder).loadMoreCommentsProgressBar - .setVisibility(View.GONE); - ((CommentViewHolder) viewHolder).expandButton - .setImageResource(R.drawable.ic_expand_less_black_20dp); - } - - @Override - public void onParseCommentFailed() { - ((CommentViewHolder) viewHolder).loadMoreCommentsProgressBar - .setVisibility(View.GONE); - } - });*/ - } - - @Override - public void onFetchCommentFailed() { - ((CommentViewHolder) viewHolder).loadMoreCommentsProgressBar - .setVisibility(View.GONE); - } - }); - } - }); - - switch (commentItem.getVoteType()) { - case 1: - ((CommentViewHolder) viewHolder).upvoteButton - .setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); - break; - case 2: - ((CommentViewHolder) viewHolder).downvoteButton - .setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); - break; - } - - ((CommentViewHolder) viewHolder).upvoteButton.setOnClickListener(view -> { - final boolean isDownvotedBefore = ((CommentViewHolder) viewHolder).downvoteButton.getColorFilter() != null; - final ColorFilter minusButtonColorFilter = ((CommentViewHolder) viewHolder).downvoteButton.getColorFilter(); - ((CommentViewHolder) viewHolder).downvoteButton.clearColorFilter(); - - if (((CommentViewHolder) viewHolder).upvoteButton.getColorFilter() == null) { - ((CommentViewHolder) viewHolder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); - if(isDownvotedBefore) { - ((CommentViewHolder) viewHolder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 2)); - } else { - ((CommentViewHolder) viewHolder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 1)); - } - - VoteThing.voteThing(mOauthRetrofit,mSharedPreferences, new VoteThing.VoteThingListener() { - @Override - public void onVoteThingSuccess(int position1) { - commentItem.setVoteType(1); - if(isDownvotedBefore) { - commentItem.setScore(commentItem.getScore() + 2); - } else { - commentItem.setScore(commentItem.getScore() + 1); - } - } - - @Override - public void onVoteThingFail(int position1) { - Toast.makeText(mContext, "Cannot upvote this comment", Toast.LENGTH_SHORT).show(); - ((CommentViewHolder) viewHolder).upvoteButton.clearColorFilter(); - ((CommentViewHolder) viewHolder).scoreTextView.setText(Integer.toString(commentItem.getScore())); - ((CommentViewHolder) viewHolder).downvoteButton.setColorFilter(minusButtonColorFilter); - } - }, commentItem.getFullName(), RedditUtils.DIR_UPVOTE, ((CommentViewHolder) viewHolder).getAdapterPosition()); - } else { - //Upvoted before - ((CommentViewHolder) viewHolder).upvoteButton.clearColorFilter(); - ((CommentViewHolder) viewHolder).scoreTextView.setText(Integer.toString(commentItem.getScore() - 1)); - - VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { - @Override - public void onVoteThingSuccess(int position1) { - commentItem.setVoteType(0); - commentItem.setScore(commentItem.getScore() - 1); - } - - @Override - public void onVoteThingFail(int position1) { - Toast.makeText(mContext, "Cannot unvote this comment", Toast.LENGTH_SHORT).show(); - ((CommentViewHolder) viewHolder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 1)); - ((CommentViewHolder) viewHolder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); - commentItem.setScore(commentItem.getScore() + 1); - } - }, commentItem.getFullName(), RedditUtils.DIR_UNVOTE, ((CommentViewHolder) viewHolder).getAdapterPosition()); - } - }); - - ((CommentViewHolder) viewHolder).downvoteButton.setOnClickListener(view -> { - final boolean isUpvotedBefore = ((CommentViewHolder) viewHolder).upvoteButton.getColorFilter() != null; - - final ColorFilter upvoteButtonColorFilter = ((CommentViewHolder) viewHolder).upvoteButton.getColorFilter(); - ((CommentViewHolder) viewHolder).upvoteButton.clearColorFilter(); - - if (((CommentViewHolder) viewHolder).downvoteButton.getColorFilter() == null) { - ((CommentViewHolder) viewHolder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); - if (isUpvotedBefore) { - ((CommentViewHolder) viewHolder).scoreTextView.setText(Integer.toString(commentItem.getScore() - 2)); - } else { - ((CommentViewHolder) viewHolder).scoreTextView.setText(Integer.toString(commentItem.getScore() - 1)); - } - - VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { - @Override - public void onVoteThingSuccess(int position12) { - commentItem.setVoteType(-1); - if(isUpvotedBefore) { - commentItem.setScore(commentItem.getScore() - 2); - } else { - commentItem.setScore(commentItem.getScore() - 1); - } - } - - @Override - public void onVoteThingFail(int position12) { - Toast.makeText(mContext, "Cannot downvote this comment", Toast.LENGTH_SHORT).show(); - ((CommentViewHolder) viewHolder).downvoteButton.clearColorFilter(); - ((CommentViewHolder) viewHolder).scoreTextView.setText(Integer.toString(commentItem.getScore())); - ((CommentViewHolder) viewHolder).upvoteButton.setColorFilter(upvoteButtonColorFilter); - } - }, commentItem.getFullName(), RedditUtils.DIR_DOWNVOTE, viewHolder.getAdapterPosition()); - } else { - //Down voted before - ((CommentViewHolder) viewHolder).downvoteButton.clearColorFilter(); - ((CommentViewHolder) viewHolder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 1)); - - VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { - @Override - public void onVoteThingSuccess(int position12) { - commentItem.setVoteType(0); - commentItem.setScore(commentItem.getScore()); - } - - @Override - public void onVoteThingFail(int position12) { - Toast.makeText(mContext, "Cannot unvote this comment", Toast.LENGTH_SHORT).show(); - ((CommentViewHolder) viewHolder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); - ((CommentViewHolder) viewHolder).scoreTextView.setText(Integer.toString(commentItem.getScore())); - commentItem.setScore(commentItem.getScore()); - } - }, commentItem.getFullName(), RedditUtils.DIR_UNVOTE, viewHolder.getAdapterPosition()); - } - }); - } - - @Override - public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { - ((CommentMultiLevelRecyclerViewAdapter.CommentViewHolder) holder).expandButton.setVisibility(View.GONE); - ((CommentMultiLevelRecyclerViewAdapter.CommentViewHolder) holder).loadMoreCommentsProgressBar.setVisibility(View.GONE); - } - - @Override - public int getItemCount() { - if(hasExtraRow()) { - return super.getItemCount() + 1; - } - return super.getItemCount(); - } - - private boolean hasExtraRow() { - return networkState != null && networkState.getStatus() != NetworkState.Status.SUCCESS; - } - - void setNetworkState(NetworkState newNetworkState) { - NetworkState previousState = this.networkState; - boolean previousExtraRow = hasExtraRow(); - this.networkState = newNetworkState; - boolean newExtraRow = hasExtraRow(); - if (previousExtraRow != newExtraRow) { - if (previousExtraRow) { - notifyItemRemoved(super.getItemCount()); - } else { - notifyItemInserted(super.getItemCount()); - } - } else if (newExtraRow && !previousState.equals(newNetworkState)) { - notifyItemChanged(getItemCount() - 1); - } - } - - private void setExpandButton(ImageView expandButton, boolean isExpanded) { - // set the icon based on the current state - expandButton.setVisibility(View.VISIBLE); - expandButton.setImageResource(isExpanded ? R.drawable.ic_expand_less_black_20dp : R.drawable.ic_expand_more_black_20dp); - } - - class CommentViewHolder extends RecyclerView.ViewHolder { - @BindView(R.id.author_text_view_item_post_comment) TextView authorTextView; - @BindView(R.id.comment_time_text_view_item_post_comment) TextView commentTimeTextView; - @BindView(R.id.comment_markdown_view_item_post_comment) MarkwonView commentMarkdownView; - @BindView(R.id.plus_button_item_post_comment) ImageView upvoteButton; - @BindView(R.id.score_text_view_item_post_comment) TextView scoreTextView; - @BindView(R.id.minus_button_item_post_comment) ImageView downvoteButton; - @BindView(R.id.expand_button_item_post_comment) ImageView expandButton; - @BindView(R.id.load_more_comments_progress_bar) ProgressBar loadMoreCommentsProgressBar; - @BindView(R.id.reply_button_item_post_comment) ImageView replyButton; - @BindView(R.id.vertical_block_item_post_comment) View verticalBlock; - - CommentViewHolder(View itemView) { - super(itemView); - ButterKnife.bind(this, itemView); - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 56302115..fb449ac6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -69,7 +69,8 @@ public class ViewPostDetailActivity extends AppCompatActivity { private RequestManager glide; private int orientation; - private String orientationState = "OS"; + private static String ORIENTATION_STATE = "OS"; + private static final String POST_STATE = "PS"; private Post mPost; private int postListPosition = -1; @@ -136,9 +137,13 @@ public class ViewPostDetailActivity extends AppCompatActivity { getSupportActionBar().setDisplayHomeAsUpEnabled(true); - orientation = getResources().getConfiguration().orientation; - - mPost = getIntent().getExtras().getParcelable(EXTRA_POST_DATA); + if(savedInstanceState == null) { + orientation = getResources().getConfiguration().orientation; + mPost = getIntent().getExtras().getParcelable(EXTRA_POST_DATA); + } else { + orientation = savedInstanceState.getInt(ORIENTATION_STATE); + mPost = savedInstanceState.getParcelable(POST_STATE); + } if(getIntent().hasExtra(EXTRA_POST_LIST_POSITION)) { postListPosition = getIntent().getExtras().getInt(EXTRA_POST_LIST_POSITION); @@ -576,7 +581,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { @Subscribe public void onVoteEvent(VoteEventToDetailActivity event) { - if(mPost.getId() == event.postId) { + if(mPost.getId().equals(event.postId)) { mPost.setVoteType(event.voteType); mScoreTextView.setText(Integer.toString(mPost.getScore() + event.voteType)); } @@ -595,13 +600,8 @@ public class ViewPostDetailActivity extends AppCompatActivity { @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); - outState.putInt(orientationState, orientation); - } - - @Override - protected void onRestoreInstanceState(Bundle savedInstanceState) { - super.onRestoreInstanceState(savedInstanceState); - orientation = savedInstanceState.getInt(orientationState); + outState.putInt(ORIENTATION_STATE, orientation); + outState.putParcelable(POST_STATE, mPost); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index 3531dfbd..e5f94cf3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -11,10 +11,15 @@ import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProviders; + import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; import com.bumptech.glide.request.RequestOptions; -import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.chip.Chip; import com.google.android.material.snackbar.Snackbar; @@ -28,11 +33,6 @@ import SubredditDatabase.SubredditRoomDatabase; import SubredditDatabase.SubredditViewModel; import SubscribedSubredditDatabase.SubscribedSubredditDao; import SubscribedSubredditDatabase.SubscribedSubredditRoomDatabase; -import androidx.appcompat.app.AppCompatActivity; -import androidx.appcompat.widget.Toolbar; -import androidx.coordinatorlayout.widget.CoordinatorLayout; -import androidx.fragment.app.Fragment; -import androidx.lifecycle.ViewModelProviders; import butterknife.BindView; import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; @@ -82,9 +82,6 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { ((Infinity) getApplication()).getmNetworkComponent().inject(this); - final Toolbar toolbar = findViewById(R.id.toolbar); - setSupportActionBar(toolbar); - //Get status bar height int statusBarHeight = 0; int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); @@ -92,40 +89,20 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { statusBarHeight = getResources().getDimensionPixelSize(resourceId); } - ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); - params.topMargin = statusBarHeight; - String subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME_KEY); String title = "r/" + subredditName; subredditNameTextView.setText(title); + CollapsingToolbarLayout collapsingToolbarLayout = findViewById(R.id.collapsing_toolbar_layout_view_subreddit_detail_activity); - AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout_view_subreddit_detail_activity); + collapsingToolbarLayout.setTitleEnabled(false); - appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { - int previousVerticalOffset = 0; - int scrollRange = -1; + Toolbar toolbar = findViewById(R.id.toolbar); + toolbar.setTitle(title); + setSupportActionBar(toolbar); - @Override - public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { - if(scrollRange == -1) { - scrollRange = appBarLayout.getTotalScrollRange(); - } else { - if(verticalOffset < previousVerticalOffset) { - //Scroll down - if(scrollRange - Math.abs(verticalOffset) <= toolbar.getHeight()) { - collapsingToolbarLayout.setTitle(title); - } - } else { - //Scroll up - if(scrollRange - Math.abs(verticalOffset) > toolbar.getHeight()) { - collapsingToolbarLayout.setTitle(" ");//carefull there should a space between double quote otherwise it wont work - } - } - previousVerticalOffset = verticalOffset; - } - } - }); + ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); + params.topMargin = statusBarHeight; subscribedSubredditDao = SubscribedSubredditRoomDatabase.getDatabase(this).subscribedSubredditDao(); glide = Glide.with(this); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java index 22ead232..00b9fe4b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -10,10 +10,15 @@ import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProviders; + import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; import com.bumptech.glide.request.RequestOptions; -import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.chip.Chip; import com.google.android.material.snackbar.Snackbar; @@ -27,11 +32,6 @@ import User.UserDao; import User.UserData; import User.UserRoomDatabase; import User.UserViewModel; -import androidx.appcompat.app.AppCompatActivity; -import androidx.appcompat.widget.Toolbar; -import androidx.coordinatorlayout.widget.CoordinatorLayout; -import androidx.fragment.app.Fragment; -import androidx.lifecycle.ViewModelProviders; import butterknife.BindView; import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; @@ -74,15 +74,11 @@ public class ViewUserDetailActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_user_detail); - Toolbar toolbar = findViewById(R.id.toolbar); - setSupportActionBar(toolbar); ButterKnife.bind(this); ((Infinity) getApplication()).getmNetworkComponent().inject(this); - setSupportActionBar(toolbar); - //Get status bar height int statusBarHeight = 0; int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); @@ -90,40 +86,19 @@ public class ViewUserDetailActivity extends AppCompatActivity { statusBarHeight = getResources().getDimensionPixelSize(resourceId); } - ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); - params.topMargin = statusBarHeight; - String userName = getIntent().getExtras().getString(EXTRA_USER_NAME_KEY); String title = "u/" + userName; userNameTextView.setText(title); CollapsingToolbarLayout collapsingToolbarLayout = findViewById(R.id.collapsing_toolbar_layout_view_user_detail_activity); - AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout_view_user_detail_activity); + collapsingToolbarLayout.setTitleEnabled(false); - appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { - int previousVerticalOffset = 0; - int scrollRange = -1; + Toolbar toolbar = findViewById(R.id.toolbar); + toolbar.setTitle(title); + setSupportActionBar(toolbar); - @Override - public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { - if(scrollRange == -1) { - scrollRange = appBarLayout.getTotalScrollRange(); - } else { - if(verticalOffset < previousVerticalOffset) { - //Scroll down - if(scrollRange - Math.abs(verticalOffset) <= toolbar.getHeight()) { - collapsingToolbarLayout.setTitle(title); - } - } else { - //Scroll up - if(scrollRange - Math.abs(verticalOffset) > toolbar.getHeight()) { - collapsingToolbarLayout.setTitle(" ");//carefull there should a space between double quote otherwise it wont work - } - } - previousVerticalOffset = verticalOffset; - } - } - }); + ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); + params.topMargin = statusBarHeight; subscribedUserDao = SubscribedUserRoomDatabase.getDatabase(this).subscribedUserDao(); glide = Glide.with(this); |