-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ios] Add example of animated group items inside CollectionView (#63)
* [ios] Add example of animated group items inside CollectionView * add xcpretty to builds * fix function builder errors * address comments
- Loading branch information
Showing
8 changed files
with
191 additions
and
7 deletions.
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
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
68 changes: 68 additions & 0 deletions
68
Example/EpoxyExample/ViewControllers/LayoutGroups/DynamicLayoutGroupsViewController.swift
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,68 @@ | ||
// Created by Tyler Hedrick on 11/11/21. | ||
// Copyright © 2021 Airbnb Inc. All rights reserved. | ||
|
||
import Epoxy | ||
import UIKit | ||
|
||
final class DynamicLayoutGroupsViewController: CollectionViewController { | ||
|
||
// MARK: Lifecycle | ||
|
||
init() { | ||
super.init(layout: UICollectionViewCompositionalLayout.list) | ||
setItems(items, animated: false) | ||
} | ||
|
||
// MARK: Private | ||
|
||
private enum DataID: CaseIterable { | ||
case row1 | ||
case row2 | ||
case row3 | ||
} | ||
|
||
private var openOptions: [AnyHashable: Bool] = [:] | ||
|
||
private var items: [ItemModeling] { | ||
DataID.allCases.map { id in | ||
DynamicRow.itemModel( | ||
dataID: id, | ||
content: .init( | ||
title: "Want to know more?", | ||
subtitle: "Tap below to reveal a set of options you can choose from", | ||
revealOptionsButton: openOptions(id) ? nil : "Reveal options", | ||
options: options(for: id), | ||
footer: "Thank you"), | ||
behaviors: .init(didTapRevealOptions: { [weak self] in | ||
self?.openOptions[id] = true | ||
self?.updateData() | ||
}, didTapOption: { [weak self] option in | ||
print("Selected option \(option)") | ||
self?.openOptions[id] = false | ||
self?.updateData() | ||
})) | ||
} | ||
} | ||
|
||
private func updateData() { | ||
setItems(items, animated: true) | ||
} | ||
|
||
private func openOptions(_ dataID: AnyHashable) -> Bool { | ||
openOptions[dataID] ?? false | ||
} | ||
|
||
private func options(for dataID: AnyHashable) -> [String]? { | ||
if openOptions(dataID) { | ||
return [ | ||
"Option 1", | ||
"Option 2", | ||
"Option 3", | ||
"Option 4", | ||
"Option 5", | ||
] | ||
} | ||
return nil | ||
} | ||
|
||
} |
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
101 changes: 101 additions & 0 deletions
101
Example/EpoxyExample/Views/LayoutGroups/DynamicRow.swift
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,101 @@ | ||
// Created by Tyler Hedrick on 11/11/21. | ||
// Copyright © 2021 Airbnb Inc. All rights reserved. | ||
|
||
import Epoxy | ||
import UIKit | ||
|
||
final class DynamicRow: BaseRow, EpoxyableView { | ||
|
||
// MARK: Lifecycle | ||
|
||
override init() { | ||
super.init() | ||
layout.install(in: self) | ||
layout.constrainToMarginsWithHighPriorityBottom() | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
// MARK: Internal | ||
|
||
// MARK: ContentConfigurableView | ||
|
||
struct Content: Equatable { | ||
let title: String | ||
let subtitle: String | ||
let revealOptionsButton: String? | ||
let options: [String]? | ||
let footer: String | ||
} | ||
|
||
// MARK: BehaviorsConfigurableView | ||
|
||
struct Behaviors { | ||
let didTapRevealOptions: (() -> Void)? | ||
let didTapOption: ((String) -> Void)? | ||
} | ||
|
||
func setContent(_ content: Content, animated: Bool) { | ||
layout.setItems { | ||
Label.groupItem( | ||
dataID: DataID.title, | ||
content: content.title, | ||
style: Label.Style.style(with: .title2)) | ||
// force text to hug tightly to avoid height changes during animation | ||
.contentHuggingPriority(.required, for: .vertical) | ||
|
||
Label.groupItem( | ||
dataID: DataID.subtitle, | ||
content: content.subtitle, | ||
style: Label.Style.style(with: .body)) | ||
// force text to hug tightly to avoid height changes during animation | ||
.contentHuggingPriority(.required, for: .vertical) | ||
|
||
if let revealOptionsText = content.revealOptionsButton { | ||
Button.groupItem( | ||
dataID: DataID.revealOptions, | ||
content: .init(title: revealOptionsText), | ||
behaviors: .init { [weak self] _ in | ||
self?.didTapRevealOptions?() | ||
}, | ||
style: .init()) | ||
} else if let options = content.options { | ||
options.map { option in | ||
Button.groupItem( | ||
dataID: option, | ||
content: .init(title: option), | ||
behaviors: .init { [weak self] _ in | ||
self?.didTapOption?(option) | ||
}, | ||
style: .init()) | ||
} | ||
} | ||
|
||
Label.groupItem( | ||
dataID: DataID.footer, | ||
content: content.footer, | ||
style: .style(with: .footnote)) | ||
} | ||
} | ||
|
||
func setBehaviors(_ behaviors: Behaviors?) { | ||
didTapRevealOptions = behaviors?.didTapRevealOptions | ||
didTapOption = behaviors?.didTapOption | ||
} | ||
|
||
// MARK: Private | ||
|
||
private enum DataID { | ||
case title | ||
case subtitle | ||
case revealOptions | ||
case footer | ||
} | ||
|
||
private let layout = VGroup() | ||
private var didTapRevealOptions: (() -> Void)? = nil | ||
private var didTapOption: ((String) -> Void)? = nil | ||
|
||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
source 'https://rubygems.org' do | ||
gem 'cocoapods', '~> 1.10.0' | ||
gem "rake", "~> 13.0.0" | ||
gem 'rake', "~> 13.0.0" | ||
end |
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 |
---|---|---|
|
@@ -94,4 +94,4 @@ DEPENDENCIES | |
rake (~> 13.0.0)! | ||
|
||
BUNDLED WITH | ||
2.1.4 | ||
2.2.6 |
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