-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add LayoutManager #66
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import MondrianLayout | ||
import StorybookKit | ||
import UIKit | ||
|
||
var _book_layoutManager: BookView { | ||
BookNavigationLink(title: "LayoutManager") { | ||
|
||
let manager = LayoutManager() | ||
var counter = 0 | ||
|
||
BookPreview { | ||
ExampleView(width: 200, height: 200) { view in | ||
|
||
let box1 = UIView.mock(backgroundColor: .neon(.cyan)) | ||
let box2 = UIView.mock(backgroundColor: .neon(.cyan)) | ||
let box3 = UIView.mock(backgroundColor: .neon(.cyan)) | ||
|
||
manager.setup(on: view) { | ||
|
||
if counter % 2 == 0 { | ||
|
||
VStackBlock { | ||
box1 | ||
.viewBlock | ||
.size(.smallSquare) | ||
|
||
box2 | ||
.viewBlock | ||
.size(.smallSquare) | ||
|
||
box3 | ||
.viewBlock | ||
.size(.smallSquare) | ||
|
||
StackingSpacer(minLength: 0) | ||
} | ||
} else { | ||
|
||
HStackBlock { | ||
box1 | ||
.viewBlock | ||
.size(.largeSquare) | ||
|
||
box2 | ||
.viewBlock | ||
.size(.largeSquare) | ||
|
||
box3 | ||
.viewBlock | ||
.size(.largeSquare) | ||
|
||
StackingSpacer(minLength: 0) | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
} | ||
.addButton( | ||
"Update", | ||
handler: { view in | ||
counter += 1 | ||
manager.reloadLayout() | ||
} | ||
) | ||
.addButton( | ||
"Update Animated", | ||
handler: { view in | ||
counter += 1 | ||
UIViewPropertyAnimator(duration: 1.2, dampingRatio: 0.9) { | ||
manager.reloadLayout() | ||
view.layoutIfNeeded() | ||
} | ||
.startAnimation() | ||
} | ||
) | ||
.title("LayoutManager") | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
|
||
import UIKit | ||
|
||
/** | ||
An object that manages layout. | ||
It supports updating the layout. | ||
In the case of defining distinct layouts under some conditions, this helps. | ||
|
||
```swift | ||
/// Defines a instance | ||
/// It needs to be retained somewhere such as inside view controller or view. | ||
let manager = LayoutManager() | ||
|
||
/// Sets up the layout in the escaping closure | ||
manager.setup(on: view) { | ||
VStackBlock { | ||
... | ||
} | ||
} | ||
|
||
/// Calls when you need to get a new layout. | ||
/// It calls the closure set in `setup` to build subviews again. | ||
manager.reloadLayout() | ||
``` | ||
|
||
*/ | ||
public final class LayoutManager { | ||
|
||
private var _layoutBuilder: (() -> [EntrypointBuilder.Either])? | ||
private var currentContext: LayoutBuilderContext? | ||
private weak var targetView: UIView? | ||
|
||
public init() { | ||
} | ||
|
||
/** | ||
Setting up the layout of subviews on the view. | ||
the layout closure is called each calling `reloadLayout` and after `setup`. | ||
|
||
Please avoid creating view and layout-guide instances in the closure. Performance decreases by creating a new instance and destroying the previous one. | ||
*/ | ||
public func setup( | ||
on view: UIView, | ||
@EntrypointBuilder layout: @escaping () -> [EntrypointBuilder.Either] | ||
) { | ||
targetView = view | ||
_layoutBuilder = layout | ||
reloadLayout() | ||
} | ||
|
||
/** | ||
Re lays out the subviews with deactivating the current layout. | ||
*/ | ||
public func reloadLayout() { | ||
guard let _layoutBuilder = _layoutBuilder else { | ||
return | ||
} | ||
|
||
guard let targetView = targetView else { | ||
return | ||
} | ||
|
||
let previousContext = currentContext | ||
|
||
previousContext?.deactivate() | ||
let newContext = targetView.mondrian.buildSubviews(_layoutBuilder) | ||
currentContext = newContext | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How to use