Skip to content

Commit

Permalink
Merge pull request #12925 from brave/pr12908_bravenews-ui-fixes_1.38.x
Browse files Browse the repository at this point in the history
[Android] Brave news - ui fixes
  • Loading branch information
kjozwiak authored Apr 16, 2022
2 parents 96eca16 + 027fbf1 commit f422ef0
Showing 1 changed file with 127 additions and 57 deletions.
184 changes: 127 additions & 57 deletions android/java/org/chromium/chrome/browser/ntp/BraveNewTabPageLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ public class BraveNewTabPageLayout
"https://brave.com/privacy/browser/#brave-today";
private static final int ITEMS_PER_PAGE = 18;
private static final int MINIMUM_VISIBLE_HEIGHT_THRESHOLD = 50;
private static final int NEWS_SCROLL_TO_TOP_NEW = -1;
private static final int NEWS_SCROLL_TO_TOP_RELOAD = -2;

private View mBraveStatsViewFallBackLayout;

Expand Down Expand Up @@ -318,7 +320,7 @@ public void onPageScrollStateChanged(int state) {}
mComesFromNewTab = BraveActivity.getBraveActivity().isComesFromNewTab();
mIsFeedLoaded = BraveActivity.getBraveActivity().isLoadedFeed();
mExistingNewsFeedObject = BraveActivity.getBraveActivity().getNewsItemsFeedCards();
mPrevScrollPosition = 1;
mPrevScrollPosition = NEWS_SCROLL_TO_TOP_NEW;

if (mIsNewsOn && mIsShowNewsOn && mIsFeedLoaded && mExistingNewsFeedObject != null) {
mNewsItemsFeedCard = mExistingNewsFeedObject;
Expand All @@ -335,7 +337,7 @@ public void onPageScrollStateChanged(int state) {}
BraveActivity.getBraveActivity().removeSettingsBar();
if (tab != null) {
SharedPreferencesManager.getInstance().writeInt(
Integer.toString(tab.getId()), mPrevScrollPosition);
"position_" + tab.getId(), mPrevScrollPosition);
}
}
}
Expand Down Expand Up @@ -759,6 +761,24 @@ public void onConfigurationChanged(Configuration newConfig) {
checkForNonDisruptiveBanner(ntpImage);
super.onConfigurationChanged(newConfig);
showNTPImage(ntpImage);

if (BraveActivity.getBraveActivity() != null
&& BraveActivity.getBraveActivity().getActivityTab() != null) {
Tab tab = BraveActivity.getBraveActivity().getActivityTab();
int prevScrollPosition = (tab != null)
? SharedPreferencesManager.getInstance().readInt(
"position_" + tab.getId(), 0)
: 0;
if (ConfigurationUtils.isLandscape(mActivity)
&& ConfigurationUtils.isTablet(mActivity)
&& prevScrollPosition == NEWS_SCROLL_TO_TOP_RELOAD) {
keepPosition(NEWS_SCROLL_TO_TOP_RELOAD);
} else if (!ConfigurationUtils.isLandscape(mActivity) && prevScrollPosition >= 1) {
keepPosition(prevScrollPosition + 1);
} else {
keepPosition(prevScrollPosition);
}
}
} else {
super.onConfigurationChanged(newConfig);
}
Expand Down Expand Up @@ -821,6 +841,8 @@ public void run() {
mBraveNewsController.getFeed((feed) -> {
if (feed == null) {
processFeed();
mContainer.setVisibility(View.VISIBLE);
mRecyclerView.setVisibility(View.VISIBLE);
return;
}
try {
Expand Down Expand Up @@ -892,6 +914,9 @@ public void run() {
} // end page loop
processFeed();

mContainer.setVisibility(View.VISIBLE);
mRecyclerView.setVisibility(View.VISIBLE);

BraveActivity.getBraveActivity().setNewsItemsFeedCards(mNewsItemsFeedCard);
BraveActivity.getBraveActivity().setLoadedFeed(true);
} catch (Exception e) {
Expand Down Expand Up @@ -933,6 +958,48 @@ private void refreshFeed() {
}
}

private void keepPosition(int prevScrollPosition) {
processFeed();
int scrollPosition = prevScrollPosition;

if (scrollPosition == NEWS_SCROLL_TO_TOP_RELOAD) {
if (ConfigurationUtils.isLandscape(mActivity)) {
new Handler().postDelayed(() -> {
if (mRecyclerView != null) {
mRecyclerView.scrollToPosition(0);
}
}, 100);
} else {
mParentScrollView.post(() -> {
mContainer.setVisibility(View.VISIBLE);
mRecyclerView.setVisibility(View.VISIBLE);
mParentScrollView.fullScroll(ScrollView.FOCUS_UP);
});
}
} else {
new Handler().postDelayed(() -> {
if (mRecyclerView != null) {
mContainer.setVisibility(View.VISIBLE);
mRecyclerView.setVisibility(View.VISIBLE);

final int scrollPositionFinal = scrollPosition;
if (mParentScrollView != null) {
if (scrollPosition > 0) {
mRecyclerView.scrollToPosition(scrollPosition);
} else if (scrollPosition != NEWS_SCROLL_TO_TOP_RELOAD) {
mParentScrollView.scrollTo(0, 0);
mImageCreditLayout.setAlpha(1.0f);
mImageCreditLayout.requestLayout();
}
}
if (mRecyclerView.getLayoutManager().findViewByPosition(0) != null) {
correctPosition(false);
}
}
}, 100);
}
}

@SuppressLint("ClickableViewAccessibility")
private void initNews() {
mSettingsBarIsClickable = false;
Expand Down Expand Up @@ -1023,12 +1090,13 @@ private void initNews() {
BraveActivity.getBraveActivity().getNewsItemsFeedCards();
Tab tab = BraveActivity.getBraveActivity().getActivityTab();
int prevScrollPosition = (tab != null)
? SharedPreferencesManager.getInstance().readInt(Integer.toString(tab.getId()))
? SharedPreferencesManager.getInstance().readInt("position_" + tab.getId(), 0)
: 0;

mmViewedNewsCardsCount = (tab != null) ? SharedPreferencesManager.getInstance().readInt(
"mViewedNewsCardsCount_" + tab.getId())
: 0;

if (prevScrollPosition == 0) {
isFeedLoaded = false;
existingNewsFeedObject = null;
Expand All @@ -1039,7 +1107,7 @@ private void initNews() {
getFeed();
if (tab != null) {
SharedPreferencesManager.getInstance().writeInt(
Integer.toString(tab.getId()), 1);
"position_" + tab.getId(), NEWS_SCROLL_TO_TOP_NEW);
}
// Brave News interaction started
if (mBraveNewsController != null) {
Expand All @@ -1049,30 +1117,10 @@ private void initNews() {
if (mActivity == null) {
mActivity = BraveActivity.getBraveActivity();
}
processFeed();

if (mRecyclerView != null) {
mRecyclerView.post(new Runnable() {
@Override
public void run() {
int scrollPosition = prevScrollPosition + 1;
if (prevScrollPosition <= 1) {
scrollPosition = 2;
}
final int scrollPositionFinal = scrollPosition;
if (mParentScrollView != null) {
mRecyclerView.scrollToPosition(scrollPositionFinal);
if (scrollPositionFinal == 2) {
mParentScrollView.scrollTo(0, 0);
mImageCreditLayout.setAlpha(1.0f);
mImageCreditLayout.requestLayout();
}
}
if (mRecyclerView.getLayoutManager().findViewByPosition(0) != null) {
correctPosition(false);
}
}
});
if (!ConfigurationUtils.isLandscape(mActivity) && prevScrollPosition > 1) {
keepPosition(prevScrollPosition - 1);
} else {
keepPosition(prevScrollPosition);
}
}
} else {
Expand Down Expand Up @@ -1106,6 +1154,11 @@ public void onScrollChanged() {
}

int scrollY = mParentScrollView.getScrollY();
RecyclerView.LayoutManager manager = mRecyclerView.getLayoutManager();
if (manager instanceof LinearLayoutManager) {
LinearLayoutManager linearLayoutManager =
(LinearLayoutManager) manager;
}
isScrolled = false;
float value = (float) scrollY / mParentScrollView.getMaxScrollAmount();
if (value >= 1) {
Expand Down Expand Up @@ -1218,6 +1271,13 @@ public void onClick(View v) {
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
SharedPreferencesManager.getInstance().writeInt(
"position_"
+ BraveActivity
.getBraveActivity()
.getActivityTab()
.getId(),
NEWS_SCROLL_TO_TOP_RELOAD);
correctPosition(false);
mParentScrollView.fullScroll(
ScrollView.FOCUS_UP);
Expand Down Expand Up @@ -1265,20 +1325,16 @@ public void onClick(View v) {
}
isScrolled = false;
refreshFeed();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (!ConfigurationUtils.isTablet(mActivity)
&& ConfigurationUtils.isLandscape(
mActivity)) {
mParentScrollView.smoothScrollTo(0, 0);
} else {
mParentScrollView.smoothScrollTo(0,
mRecyclerView.getHeight()
- mParentScrollView
.getMaxScrollAmount()
+ dpToPx(getContext(), 90));
}
new Handler().postDelayed(() -> {
if (!ConfigurationUtils.isTablet(mActivity)
&& ConfigurationUtils.isLandscape(mActivity)) {
mParentScrollView.smoothScrollTo(0, 0);
} else {
mParentScrollView.smoothScrollTo(0,
mRecyclerView.getHeight()
- mParentScrollView
.getMaxScrollAmount()
+ dpToPx(getContext(), 90));
}
}, 100);

Expand Down Expand Up @@ -1317,13 +1373,6 @@ public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newStat
long timeDiff = mEndCardViewTime - mStartCardViewTime;
// if viewed for more than 100 ms send the event
if (timeDiff > BraveNewsUtils.BRAVE_NEWS_VIEWD_CARD_TIME) {
if (firstCompletelyVisibleItemPosition > 0) {
SharedPreferencesManager.getInstance().writeInt(
Integer.toString(BraveActivity.getBraveActivity()
.getActivityTab()
.getId()),
firstCompletelyVisibleItemPosition);
}
if (mVisibleCard != null) {
if (!mVisibleCard.isViewStatSent()) {
// send viewed cards events
Expand Down Expand Up @@ -1366,7 +1415,30 @@ public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newStat
linearLayoutManager.findFirstVisibleItemPosition();
int lastVisibleItemPosition =
linearLayoutManager.findLastVisibleItemPosition();

int scrollY = mParentScrollView.getScrollY();

if (BraveActivity.getBraveActivity() != null
&& BraveActivity.getBraveActivity().getActivityTab() != null) {
if (firstCompletelyVisibleItemPosition >= 0) {
int position = 0;
if (firstCompletelyVisibleItemPosition == 0
&& ((ConfigurationUtils.isLandscape(mActivity)
&& scrollY > 600)
|| (!ConfigurationUtils.isLandscape(mActivity)
&& scrollY > 900))) {
position = NEWS_SCROLL_TO_TOP_RELOAD;
} else {
position = firstCompletelyVisibleItemPosition == 0
? NEWS_SCROLL_TO_TOP_NEW
: firstCompletelyVisibleItemPosition;
}
SharedPreferencesManager.getInstance().writeInt("position_"
+ BraveActivity.getBraveActivity()
.getActivityTab()
.getId(),
position);
}
}
mFeedHash = SharedPreferencesManager.getInstance().readString(
BravePreferenceKeys.BRAVE_NEWS_FEED_HASH, "");
//@TODO alex optimize feed availability check
Expand Down Expand Up @@ -1501,9 +1573,6 @@ private void processFeed() {
mOptinLayout.setVisibility(View.GONE);
}

mContainer.setVisibility(View.VISIBLE);
mRecyclerView.setVisibility(View.VISIBLE);

if (mNewsItemsFeedCard != null && mNewsItemsFeedCard.size() > 0) {
mAdapterFeedCard.notifyItemRangeInserted(0, mNewsItemsFeedCard.size());
}
Expand Down Expand Up @@ -1744,10 +1813,11 @@ public void bgWallpaperRetrieved(Bitmap bgWallpaper) {
.getSpec())) {
mPrevScrollPosition =
SharedPreferencesManager.getInstance().readInt(
Integer.toString(
BraveActivity.getBraveActivity()
.getActivityTab()
.getId()));
"position_"
+ BraveActivity.getBraveActivity()
.getActivityTab()
.getId(),
NEWS_SCROLL_TO_TOP_NEW);
if (compositorView.getChildAt(2).getId()
== R.id.news_settings_bar) {
mSettingsBar = (LinearLayout) compositorView.getChildAt(2);
Expand Down

0 comments on commit f422ef0

Please sign in to comment.