Commit 7524e262 authored by adamta's avatar adamta Committed by Commit Bot

[iOS] Maintain scroll position for Discover feed

Adds enough height to the CS collection view to allow for scroll
position to be maintained in Discover feed. Added height is calculated
from the cached NTP offset.

Bug: 1085419, 1108403
Change-Id: Ia9c2ed7dd7d5d3d8aa3413266404f956ae524cbf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2317811
Commit-Queue: Adam Trudeau-Arcaro <adamta@google.com>
Reviewed-by: default avatarGauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarSergio Collazos <sczs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#796864}
parent 9dbb5937
...@@ -66,7 +66,10 @@ ...@@ -66,7 +66,10 @@
#include "ios/chrome/grit/ios_strings.h" #include "ios/chrome/grit/ios_strings.h"
#import "ios/public/provider/chrome/browser/chrome_browser_provider.h" #import "ios/public/provider/chrome/browser/chrome_browser_provider.h"
#import "ios/public/provider/chrome/browser/discover_feed/discover_feed_provider.h" #import "ios/public/provider/chrome/browser/discover_feed/discover_feed_provider.h"
#include "ui/base/l10n/l10n_util_mac.h" #import "ios/web/public/navigation/navigation_item.h"
#import "ios/web/public/navigation/navigation_manager.h"
#import "ios/web/public/web_state.h"
#import "ui/base/l10n/l10n_util_mac.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support." #error "This file requires ARC support."
...@@ -211,8 +214,20 @@ ...@@ -211,8 +214,20 @@
self.metricsRecorder = [[ContentSuggestionsMetricsRecorder alloc] init]; self.metricsRecorder = [[ContentSuggestionsMetricsRecorder alloc] init];
self.metricsRecorder.delegate = self.contentSuggestionsMediator; self.metricsRecorder.delegate = self.contentSuggestionsMediator;
// Offset to maintain Discover feed scroll position.
CGFloat offset = 0;
if (IsDiscoverFeedEnabled() && contentSuggestionsEnabled) {
web::NavigationManager* navigationManager =
self.webState->GetNavigationManager();
web::NavigationItem* item = navigationManager->GetVisibleItem();
if (item) {
offset = item->GetPageDisplayState().scroll_state().content_offset().y;
}
}
self.suggestionsViewController = [[ContentSuggestionsViewController alloc] self.suggestionsViewController = [[ContentSuggestionsViewController alloc]
initWithStyle:CollectionViewControllerStyleDefault]; initWithStyle:CollectionViewControllerStyleDefault
offset:offset];
[self.suggestionsViewController [self.suggestionsViewController
setDataSource:self.contentSuggestionsMediator]; setDataSource:self.contentSuggestionsMediator];
self.suggestionsViewController.suggestionCommandHandler = self.NTPMediator; self.suggestionsViewController.suggestionCommandHandler = self.NTPMediator;
......
...@@ -13,6 +13,14 @@ ...@@ -13,6 +13,14 @@
// is pinned to the top of the collection. // is pinned to the top of the collection.
@interface ContentSuggestionsLayout : MDCCollectionViewFlowLayout @interface ContentSuggestionsLayout : MDCCollectionViewFlowLayout
// The cached scroll position of the NTP.
@property(nonatomic, assign) CGFloat offset;
// Creates layout with |offset| as additional height. Allows the view's height
// to be increased enough to maintain the scroll position. Only needed if
// Discover feed is enabled.
- (instancetype)initWithOffset:(CGFloat)offset;
@end @end
#endif // IOS_CHROME_BROWSER_UI_CONTENT_SUGGESTIONS_CONTENT_SUGGESTIONS_LAYOUT_H_ #endif // IOS_CHROME_BROWSER_UI_CONTENT_SUGGESTIONS_CONTENT_SUGGESTIONS_LAYOUT_H_
...@@ -16,6 +16,13 @@ ...@@ -16,6 +16,13 @@
@implementation ContentSuggestionsLayout @implementation ContentSuggestionsLayout
- (instancetype)initWithOffset:(CGFloat)offset {
if (self = [super init]) {
_offset = offset;
}
return self;
}
- (CGSize)collectionViewContentSize { - (CGSize)collectionViewContentSize {
CGFloat collectionViewHeight = self.collectionView.bounds.size.height; CGFloat collectionViewHeight = self.collectionView.bounds.size.height;
CGFloat headerHeight = [self firstHeaderHeight]; CGFloat headerHeight = [self firstHeaderHeight];
...@@ -40,6 +47,11 @@ ...@@ -40,6 +47,11 @@
CGSize contentSize = [super collectionViewContentSize]; CGSize contentSize = [super collectionViewContentSize];
if (contentSize.height < minimumHeight) { if (contentSize.height < minimumHeight) {
contentSize.height = minimumHeight; contentSize.height = minimumHeight;
// Increases the minimum height to allow the page to scroll to the cached
// position.
if (self.offset > 0) {
contentSize.height += self.offset;
}
} }
return contentSize; return contentSize;
} }
......
...@@ -34,8 +34,10 @@ extern NSString* const ...@@ -34,8 +34,10 @@ extern NSString* const
: CollectionViewController <ContentSuggestionsCollectionControlling, : CollectionViewController <ContentSuggestionsCollectionControlling,
ContentSuggestionsConsumer> ContentSuggestionsConsumer>
// Inits view controller with |offset| to maintain scroll position if needed.
// Offset is only required if Discover feed is enabled.
- (instancetype)initWithStyle:(CollectionViewControllerStyle)style - (instancetype)initWithStyle:(CollectionViewControllerStyle)style
NS_DESIGNATED_INITIALIZER; offset:(CGFloat)offset NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithLayout:(UICollectionViewLayout*)layout - (instancetype)initWithLayout:(UICollectionViewLayout*)layout
style:(CollectionViewControllerStyle)style style:(CollectionViewControllerStyle)style
......
...@@ -74,6 +74,10 @@ NSString* const kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix = ...@@ -74,6 +74,10 @@ NSString* const kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix =
// The FeedView CollectionView contained by discoverFeedVC. // The FeedView CollectionView contained by discoverFeedVC.
@property(nonatomic, strong) UICollectionView* feedView; @property(nonatomic, strong) UICollectionView* feedView;
// Navigation offset applied to the layout height to maintain the scroll
// position, since the feed height is dynamic.
@property(nonatomic) CGFloat offset;
@end @end
@implementation ContentSuggestionsViewController @implementation ContentSuggestionsViewController
...@@ -90,8 +94,11 @@ NSString* const kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix = ...@@ -90,8 +94,11 @@ NSString* const kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix =
#pragma mark - Lifecycle #pragma mark - Lifecycle
- (instancetype)initWithStyle:(CollectionViewControllerStyle)style { - (instancetype)initWithStyle:(CollectionViewControllerStyle)style
UICollectionViewLayout* layout = [[ContentSuggestionsLayout alloc] init]; offset:(CGFloat)offset {
_offset = offset;
UICollectionViewLayout* layout =
[[ContentSuggestionsLayout alloc] initWithOffset:offset];
self = [super initWithLayout:layout style:style]; self = [super initWithLayout:layout style:style];
if (self) { if (self) {
_collectionUpdater = [[ContentSuggestionsCollectionUpdater alloc] init]; _collectionUpdater = [[ContentSuggestionsCollectionUpdater alloc] init];
...@@ -290,6 +297,13 @@ NSString* const kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix = ...@@ -290,6 +297,13 @@ NSString* const kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix =
// Resize the collection as it might have been rotated while not being // Resize the collection as it might have been rotated while not being
// presented (e.g. rotation on stack view). // presented (e.g. rotation on stack view).
[self updateConstraints]; [self updateConstraints];
// Remove forced height if it was already applied, since the scroll position
// was already maintained.
if (self.offset > 0) {
ContentSuggestionsLayout* layout = static_cast<ContentSuggestionsLayout*>(
self.collectionView.collectionViewLayout);
layout.offset = 0;
}
} }
- (void)viewDidLayoutSubviews { - (void)viewDidLayoutSubviews {
......
...@@ -39,7 +39,8 @@ ...@@ -39,7 +39,8 @@
_dataSource = [[SCContentSuggestionsDataSource alloc] init]; _dataSource = [[SCContentSuggestionsDataSource alloc] init];
_suggestionViewController = [[ContentSuggestionsViewController alloc] _suggestionViewController = [[ContentSuggestionsViewController alloc]
initWithStyle:CollectionViewControllerStyleDefault]; initWithStyle:CollectionViewControllerStyleDefault
offset:0];
[_suggestionViewController setDataSource:_dataSource]; [_suggestionViewController setDataSource:_dataSource];
_suggestionViewController.suggestionCommandHandler = _suggestionViewController.suggestionCommandHandler =
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment