Skip to content
This repository was archived by the owner on Jan 17, 2019. It is now read-only.

Move layout logic into the collection view factory #331

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion sources/HUBCollectionViewFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
* under the License.
*/

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "HUBHeaderMacros.h"

@protocol HUBComponentLayoutManager;
@protocol HUBComponentRegistry;

@class HUBCollectionView;

Expand All @@ -28,6 +32,15 @@ NS_ASSUME_NONNULL_BEGIN
/// Factory used to create collection views for use in a `HUBViewController`
@interface HUBCollectionViewFactory : NSObject

/**
Designated initializer.

@param componentRegistry The registry to use to lookup component information
@param componentLayoutManager The object that manages layout for components in the view controller
*/
- (instancetype)initWithComponentRegistry:(id<HUBComponentRegistry>)componentRegistry
componentLayoutManager:(id<HUBComponentLayoutManager>)componentLayoutManager HUB_DESIGNATED_INITIALIZER;

/// Create a collection view. It will be setup with a default layout.
- (HUBCollectionView *)createCollectionView;

Expand Down
28 changes: 27 additions & 1 deletion sources/HUBCollectionViewFactory.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,42 @@
*/

#import "HUBCollectionViewFactory.h"

#import "HUBCollectionView.h"
#import "HUBCollectionViewLayout.h"

NS_ASSUME_NONNULL_BEGIN

@interface HUBCollectionViewFactory ()

@property (nonatomic, strong, readonly) id<HUBComponentRegistry> componentRegistry;
@property (nonatomic, strong, readonly) id<HUBComponentLayoutManager> componentLayoutManager;

@end

@implementation HUBCollectionViewFactory

- (instancetype)initWithComponentRegistry:(id<HUBComponentRegistry>)componentRegistry
componentLayoutManager:(id<HUBComponentLayoutManager>)componentLayoutManager
{
NSParameterAssert(componentRegistry != nil);
NSParameterAssert(componentLayoutManager != nil);

self = [super init];
if (self) {
_componentRegistry = componentRegistry;
_componentLayoutManager = componentLayoutManager;
}
return self;
}

- (HUBCollectionView *)createCollectionView
{
HUBCollectionViewLayout *collectionViewLayout = [[HUBCollectionViewLayout alloc] initWithComponentRegistry:self.componentRegistry
componentLayoutManager:self.componentLayoutManager];

HUBCollectionView *collectionView = [[HUBCollectionView alloc] initWithFrame:CGRectZero
collectionViewLayout:[UICollectionViewLayout new]];
collectionViewLayout:collectionViewLayout];

collectionView.accessibilityIdentifier = @"collectionView";
return collectionView;
Expand Down
5 changes: 2 additions & 3 deletions sources/HUBConfigViewControllerFactory.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ - (HUBViewController *)createViewControllerWithConfig:(HUBConfig *)config


HUBViewModelRenderer * const viewModelRenderer = [HUBViewModelRenderer new];
HUBCollectionViewFactory * const collectionViewFactory = [HUBCollectionViewFactory new];
HUBCollectionViewFactory * const collectionViewFactory = [[HUBCollectionViewFactory alloc] initWithComponentRegistry:config.componentRegistry
componentLayoutManager:config.componentLayoutManager];
HUBComponentReusePool * const componentReusePool = [[HUBComponentReusePool alloc] initWithComponentRegistry:config.componentRegistry];

id<HUBActionHandler> const actionHandlerWrapper = [[HUBActionHandlerWrapper alloc] initWithActionHandler:actionHandler
Expand All @@ -74,9 +75,7 @@ - (HUBViewController *)createViewControllerWithConfig:(HUBConfig *)config
viewModelLoader:viewModelLoader
viewModelRenderer:viewModelRenderer
collectionViewFactory:collectionViewFactory
componentRegistry:config.componentRegistry
componentReusePool:componentReusePool
componentLayoutManager:config.componentLayoutManager
actionHandler:actionHandlerWrapper
scrollHandler:scrollHandlerToUse
imageLoader:imageLoader];
Expand Down
6 changes: 0 additions & 6 deletions sources/HUBViewControllerExperimentalImplementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#import "HUBViewController.h"

@protocol HUBFeatureInfo;
@protocol HUBComponentRegistry;
@protocol HUBComponentLayoutManager;
@protocol HUBActionHandler;
@protocol HUBViewControllerScrollHandler;
@protocol HUBImageLoader;
Expand All @@ -43,9 +41,7 @@ NS_ASSUME_NONNULL_BEGIN
* @param featureInfo Information about the feature that the view controller is for
* @param viewModelLoader The object to use to load view models for the view controller
* @param collectionViewFactory The factory to use to create collection views
* @param componentRegistry The registry to use to lookup component information
* @param componentReusePool The reuse pool to use to manage component wrappers
* @param componentLayoutManager The object that manages layout for components in the view controller
* @param actionHandler The object that will handle actions for this view controller
* @param scrollHandler The object that will handle scrolling for the view controller
* @param imageLoader The loader to use to load images for components
Expand All @@ -54,9 +50,7 @@ NS_ASSUME_NONNULL_BEGIN
featureInfo:(id<HUBFeatureInfo>)featureInfo
viewModelLoader:(id<HUBViewModelLoader>)viewModelLoader
collectionViewFactory:(HUBCollectionViewFactory *)collectionViewFactory
componentRegistry:(id<HUBComponentRegistry>)componentRegistry
componentReusePool:(HUBComponentReusePool *)componentReusePool
componentLayoutManager:(id<HUBComponentLayoutManager>)componentLayoutManager
actionHandler:(id<HUBActionHandler>)actionHandler
scrollHandler:(id<HUBViewControllerScrollHandler>)scrollHandler
imageLoader:(id<HUBImageLoader>)imageLoader NS_DESIGNATED_INITIALIZER;
Expand Down
14 changes: 0 additions & 14 deletions sources/HUBViewControllerExperimentalImplementation.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#import "HUBComponentViewObserver.h"
#import "HUBComponentWrapper.h"
#import "HUBComponentWrapperImageLoader.h"
#import "HUBComponentRegistry.h"
#import "HUBComponentCollectionViewCell.h"
#import "HUBUtilities.h"
#import "HUBCollectionViewFactory.h"
Expand Down Expand Up @@ -62,9 +61,7 @@ @interface HUBViewControllerExperimentalImplementation () <
@property (nonatomic, strong, readonly) id<HUBFeatureInfo> featureInfo;
@property (nonatomic, strong, readonly) id<HUBViewModelLoader> viewModelLoader;
@property (nonatomic, strong, readonly) HUBCollectionViewFactory *collectionViewFactory;
@property (nonatomic, strong, readonly) id<HUBComponentRegistry> componentRegistry;
@property (nonatomic, strong, readonly) HUBComponentReusePool *componentReusePool;
@property (nonatomic, strong, readonly) id<HUBComponentLayoutManager> componentLayoutManager;
@property (nonatomic, strong, readonly) id<HUBActionHandler> actionHandler;
@property (nonatomic, strong, readonly) id<HUBViewControllerScrollHandler> scrollHandler;
@property (nonatomic, strong, nullable, readonly) id<HUBContentReloadPolicy> contentReloadPolicy;
Expand Down Expand Up @@ -101,9 +98,7 @@ - (instancetype)initWithViewURI:(NSURL *)viewURI
featureInfo:(id<HUBFeatureInfo>)featureInfo
viewModelLoader:(id<HUBViewModelLoader>)viewModelLoader
collectionViewFactory:(HUBCollectionViewFactory *)collectionViewFactory
componentRegistry:(id<HUBComponentRegistry>)componentRegistry
componentReusePool:(HUBComponentReusePool *)componentReusePool
componentLayoutManager:(id<HUBComponentLayoutManager>)componentLayoutManager
actionHandler:(id<HUBActionHandler>)actionHandler
scrollHandler:(id<HUBViewControllerScrollHandler>)scrollHandler
imageLoader:(id<HUBImageLoader>)imageLoader
Expand All @@ -112,9 +107,7 @@ - (instancetype)initWithViewURI:(NSURL *)viewURI
NSParameterAssert(featureInfo != nil);
NSParameterAssert(viewModelLoader != nil);
NSParameterAssert(collectionViewFactory != nil);
NSParameterAssert(componentRegistry != nil);
NSParameterAssert(componentReusePool != nil);
NSParameterAssert(componentLayoutManager != nil);
NSParameterAssert(actionHandler != nil);
NSParameterAssert(scrollHandler != nil);
NSParameterAssert(imageLoader != nil);
Expand All @@ -127,9 +120,7 @@ - (instancetype)initWithViewURI:(NSURL *)viewURI
_featureInfo = featureInfo;
_viewModelLoader = viewModelLoader;
_collectionViewFactory = collectionViewFactory;
_componentRegistry = componentRegistry;
_componentReusePool = componentReusePool;
_componentLayoutManager = componentLayoutManager;
_actionHandler = actionHandler;
_scrollHandler = scrollHandler;
_componentWrapperImageLoader = [[HUBComponentWrapperImageLoader alloc] initWithImageLoader:imageLoader];
Expand Down Expand Up @@ -893,11 +884,6 @@ - (HUBOperation *)createReloadCollectionViewOperation
return;
}

if (![self.collectionView.collectionViewLayout isKindOfClass:[HUBCollectionViewLayout class]]) {
self.collectionView.collectionViewLayout = [[HUBCollectionViewLayout alloc] initWithComponentRegistry:self.componentRegistry
componentLayoutManager:self.componentLayoutManager];
}

[self saveStatesForVisibleComponents];
[self configureHeaderComponent];
[self configureOverlayComponents];
Expand Down
10 changes: 4 additions & 6 deletions sources/HUBViewControllerFactoryImplementation.m
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ - (HUBViewController *)createStandardViewControllerForViewURI:(NSURL *)viewURI

HUBViewModelRenderer * const viewModelRenderer = [HUBViewModelRenderer new];
id<HUBImageLoader> const imageLoader = [self.imageLoaderFactory createImageLoader];
HUBCollectionViewFactory * const collectionViewFactory = [HUBCollectionViewFactory new];
HUBCollectionViewFactory * const collectionViewFactory = [[HUBCollectionViewFactory alloc] initWithComponentRegistry:self.componentRegistry
componentLayoutManager:self.componentLayoutManager];
HUBComponentReusePool * const componentReusePool = [[HUBComponentReusePool alloc] initWithComponentRegistry:self.componentRegistry];

id<HUBActionHandler> const actionHandler = featureRegistration.actionHandler ?: self.defaultActionHandler;
Expand All @@ -182,9 +183,7 @@ - (HUBViewController *)createStandardViewControllerForViewURI:(NSURL *)viewURI
viewModelLoader:viewModelLoader
viewModelRenderer:viewModelRenderer
collectionViewFactory:collectionViewFactory
componentRegistry:self.componentRegistry
componentReusePool:componentReusePool
componentLayoutManager:self.componentLayoutManager
actionHandler:actionHandlerWrapper
scrollHandler:scrollHandlerToUse
imageLoader:imageLoader];
Expand All @@ -200,7 +199,8 @@ - (HUBViewController *)createExperimentalViewControllerForViewURI:(NSURL *)viewU
featureRegistration:featureRegistration];

id<HUBImageLoader> const imageLoader = [self.imageLoaderFactory createImageLoader];
HUBCollectionViewFactory * const collectionViewFactory = [HUBCollectionViewFactory new];
HUBCollectionViewFactory * const collectionViewFactory = [[HUBCollectionViewFactory alloc] initWithComponentRegistry:self.componentRegistry
componentLayoutManager:self.componentLayoutManager];
HUBComponentReusePool * const componentReusePool = [[HUBComponentReusePool alloc] initWithComponentRegistry:self.componentRegistry];

id<HUBActionHandler> const actionHandler = featureRegistration.actionHandler ?: self.defaultActionHandler;
Expand All @@ -215,9 +215,7 @@ - (HUBViewController *)createExperimentalViewControllerForViewURI:(NSURL *)viewU
featureInfo:featureInfo
viewModelLoader:viewModelLoader
collectionViewFactory:collectionViewFactory
componentRegistry:self.componentRegistry
componentReusePool:componentReusePool
componentLayoutManager:self.componentLayoutManager
actionHandler:actionHandlerWrapper
scrollHandler:scrollHandlerToUse
imageLoader:imageLoader];
Expand Down
6 changes: 0 additions & 6 deletions sources/HUBViewControllerImplementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#import "HUBViewController.h"

@protocol HUBFeatureInfo;
@protocol HUBComponentRegistry;
@protocol HUBComponentLayoutManager;
@protocol HUBActionHandler;
@protocol HUBViewControllerScrollHandler;
@protocol HUBImageLoader;
Expand All @@ -45,9 +43,7 @@ NS_ASSUME_NONNULL_BEGIN
* @param viewModelLoader The object to use to load view models for the view controller
* @param viewModelRenderer The object used to render the view model
* @param collectionViewFactory The factory to use to create collection views
* @param componentRegistry The registry to use to lookup component information
* @param componentReusePool The reuse pool to use to manage component wrappers
* @param componentLayoutManager The object that manages layout for components in the view controller
* @param actionHandler The object that will handle actions for this view controller
* @param scrollHandler The object that will handle scrolling for the view controller
* @param imageLoader The loader to use to load images for components
Expand All @@ -57,9 +53,7 @@ NS_ASSUME_NONNULL_BEGIN
viewModelLoader:(id<HUBViewModelLoader>)viewModelLoader
viewModelRenderer:(HUBViewModelRenderer *)viewModelRenderer
collectionViewFactory:(HUBCollectionViewFactory *)collectionViewFactory
componentRegistry:(id<HUBComponentRegistry>)componentRegistry
componentReusePool:(HUBComponentReusePool *)componentReusePool
componentLayoutManager:(id<HUBComponentLayoutManager>)componentLayoutManager
actionHandler:(id<HUBActionHandler>)actionHandler
scrollHandler:(id<HUBViewControllerScrollHandler>)scrollHandler
imageLoader:(id<HUBImageLoader>)imageLoader NS_DESIGNATED_INITIALIZER;
Expand Down
14 changes: 0 additions & 14 deletions sources/HUBViewControllerImplementation.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#import "HUBComponentViewObserver.h"
#import "HUBComponentWrapper.h"
#import "HUBComponentWrapperImageLoader.h"
#import "HUBComponentRegistry.h"
#import "HUBComponentCollectionViewCell.h"
#import "HUBUtilities.h"
#import "HUBCollectionViewFactory.h"
Expand Down Expand Up @@ -62,9 +61,7 @@ @interface HUBViewControllerImplementation () <
@property (nonatomic, strong, readonly) id<HUBFeatureInfo> featureInfo;
@property (nonatomic, strong, readonly) id<HUBViewModelLoader> viewModelLoader;
@property (nonatomic, strong, readonly) HUBCollectionViewFactory *collectionViewFactory;
@property (nonatomic, strong, readonly) id<HUBComponentRegistry> componentRegistry;
@property (nonatomic, strong, readonly) HUBComponentReusePool *componentReusePool;
@property (nonatomic, strong, readonly) id<HUBComponentLayoutManager> componentLayoutManager;
@property (nonatomic, strong, readonly) id<HUBActionHandler> actionHandler;
@property (nonatomic, strong, readonly) id<HUBViewControllerScrollHandler> scrollHandler;
@property (nonatomic, strong, nullable, readonly) id<HUBContentReloadPolicy> contentReloadPolicy;
Expand Down Expand Up @@ -103,9 +100,7 @@ - (instancetype)initWithViewURI:(NSURL *)viewURI
viewModelLoader:(id<HUBViewModelLoader>)viewModelLoader
viewModelRenderer:(HUBViewModelRenderer *)viewModelRenderer
collectionViewFactory:(HUBCollectionViewFactory *)collectionViewFactory
componentRegistry:(id<HUBComponentRegistry>)componentRegistry
componentReusePool:(HUBComponentReusePool *)componentReusePool
componentLayoutManager:(id<HUBComponentLayoutManager>)componentLayoutManager
actionHandler:(id<HUBActionHandler>)actionHandler
scrollHandler:(id<HUBViewControllerScrollHandler>)scrollHandler
imageLoader:(id<HUBImageLoader>)imageLoader
Expand All @@ -115,9 +110,7 @@ - (instancetype)initWithViewURI:(NSURL *)viewURI
NSParameterAssert(viewModelLoader != nil);
NSParameterAssert(viewModelRenderer != nil);
NSParameterAssert(collectionViewFactory != nil);
NSParameterAssert(componentRegistry != nil);
NSParameterAssert(componentReusePool != nil);
NSParameterAssert(componentLayoutManager != nil);
NSParameterAssert(actionHandler != nil);
NSParameterAssert(scrollHandler != nil);
NSParameterAssert(imageLoader != nil);
Expand All @@ -131,9 +124,7 @@ - (instancetype)initWithViewURI:(NSURL *)viewURI
_viewModelLoader = viewModelLoader;
_viewModelRenderer = viewModelRenderer;
_collectionViewFactory = collectionViewFactory;
_componentRegistry = componentRegistry;
_componentReusePool = componentReusePool;
_componentLayoutManager = componentLayoutManager;
_actionHandler = actionHandler;
_scrollHandler = scrollHandler;
_componentWrapperImageLoader = [[HUBComponentWrapperImageLoader alloc] initWithImageLoader:imageLoader];
Expand Down Expand Up @@ -855,11 +846,6 @@ - (HUBOperation *)createReloadCollectionViewOperation
return;
}

if (![self.collectionView.collectionViewLayout isKindOfClass:[HUBCollectionViewLayout class]]) {
self.collectionView.collectionViewLayout = [[HUBCollectionViewLayout alloc] initWithComponentRegistry:self.componentRegistry
componentLayoutManager:self.componentLayoutManager];
}

[self saveStatesForVisibleComponents];
[self configureHeaderComponent];
[self configureOverlayComponents];
Expand Down
Loading