Skip to content

Commit

Permalink
Merge pull request #783 from hyperoslo/optimize/component-layout-method
Browse files Browse the repository at this point in the history
Add needsLayout to setup() & layout() methods on Component
  • Loading branch information
zenangst authored Dec 20, 2017
2 parents 4f3361e + 3f1781c commit 04b5e61
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Sources/Shared/Classes/SpotsControllerManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ public class SpotsControllerManager {
}

let tempComponent = Component(model: model, configuration: controller.configuration)
tempComponent.setup(with: setupSize)
tempComponent.setup(with: setupSize, needsLayout: false)
tempComponent.model.size = CGSize(
width: controller.view.frame.width,
height: ceil(tempComponent.view.frame.height))
Expand Down
13 changes: 9 additions & 4 deletions Sources/iOS/Classes/Component.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public class Component: NSObject, ComponentHorizontallyScrollable {
/// Setup up the component with a given size, this is usually the parent size when used in a controller context.
///
/// - Parameter size: A `CGSize` that is used to set the frame of the user interface.
public func setup(with size: CGSize) {
public func setup(with size: CGSize, needsLayout: Bool = true) {
view.frame.size = size

setupFooter(with: configuration)
Expand All @@ -181,7 +181,7 @@ public class Component: NSObject, ComponentHorizontallyScrollable {
setupCollectionView(collectionView, with: size)
}

layout(with: size)
layout(with: size, needsLayout: needsLayout)
configurePageControl()
Component.configure?(self)

Expand All @@ -194,15 +194,21 @@ public class Component: NSObject, ComponentHorizontallyScrollable {
/// Configure the view frame with a given size.
///
/// - Parameter size: A `CGSize` used to set a new size to the user interface.
public func layout(with size: CGSize) {
public func layout(with size: CGSize, needsLayout: Bool = true) {
if let tableView = self.tableView {
layoutTableView(tableView, with: size)
} else if let collectionView = self.collectionView {
layoutCollectionView(collectionView, with: size)
}

layoutHeaderFooterViews(size)

guard needsLayout else {
return
}

view.setNeedsLayout()

// Only call `layoutIfNeeded` if the `Component` is not a part of a `SpotsController`.
if let spotsScrollView = (focusDelegate as? SpotsController)?.scrollView {
let isVisibleOnScreen = view.frame.intersects(.init(origin: spotsScrollView.contentOffset,
Expand Down Expand Up @@ -357,4 +363,3 @@ public class Component: NSObject, ComponentHorizontallyScrollable {
view.superview?.layoutIfNeeded()
}
}

13 changes: 10 additions & 3 deletions Sources/macOS/Classes/Component.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ import Cocoa
/// Setup up the component with a given size, this is usually the parent size when used in a controller context.
///
/// - Parameter size: A `CGSize` that is used to set the frame of the user interface.
public func setup(with size: CGSize) {
public func setup(with size: CGSize, needsLayout: Bool = false) {
scrollView.frame.size = size

setupHeader(with: configuration)
Expand All @@ -221,7 +221,7 @@ import Cocoa
setupCollectionView(collectionView, with: size)
}

layout(with: size, animated: false)
layout(with: size, needsLayout: needsLayout, animated: false)
Component.configure?(self)
}

Expand All @@ -230,7 +230,7 @@ import Cocoa
/// - Parameter size: A `CGSize` used to set a new size to the user interface.
/// - Parameter animated: Determines if the `Component` should perform animation when
/// applying its new size.
public func layout(with size: CGSize, animated: Bool = true) {
public func layout(with size: CGSize, needsLayout: Bool = false, animated: Bool = true) {
userInterface?.layoutIfNeeded()

if let tableView = self.tableView {
Expand All @@ -254,6 +254,13 @@ import Cocoa
view.superview?.layoutSubviews()
}
}

guard needsLayout else {
return
}

view.needsLayout = true
view.layoutIfNeeded()
}

/// Setup a collection view with a specific size.
Expand Down

0 comments on commit 04b5e61

Please sign in to comment.