From c43dbde001c7b460a3445cd8568c39a2c69d9f34 Mon Sep 17 00:00:00 2001 From: Dylan Shine Date: Mon, 11 Jun 2018 11:44:22 -0700 Subject: [PATCH] 2.1.1 (#11) * Update Example to use width of tableview to size ImageTableViewCells * Implement lastIndex as computed property on Array * Update Header Comments * Rename supplementaryView to componentView for perform(command: OKTableViewSectionComponentCommand, componentView: OKTableViewSectionComponentView, for view: UIView, in section: Int) * Update README * Rename endIndex back to lastIndex --- .../ContentFeedViewController.swift | 10 +++++++--- .../Factories/PostTableViewSectionFactory.swift | 4 ++-- .../OKTableViewLiaison/ImageTableViewRow.swift | 15 ++++++++------- Example/Podfile.lock | 4 ++-- .../OKTableViewLiaison.podspec.json | 4 ++-- Example/Pods/Manifest.lock | 4 ++-- .../OKTableViewLiaison/Info.plist | 2 +- .../Tests/OKTableViewSection+UnitTests.swift | 16 ++++++++-------- OKTableViewLiaison.podspec | 2 +- ...TableViewSectionComponentDisplayOption.swift | 2 +- .../Enums/OKTableViewSectionComponentView.swift | 2 +- .../Classes/Extensions/Array+SafeAccess.swift | 10 +++++++++- .../Liaison/OKTableViewLiaison+Pagination.swift | 10 +++++----- .../Liaison/OKTableViewLiaison+Row.swift | 2 +- .../Liaison/OKTableViewLiaison+Section.swift | 4 ++-- ...OKTableViewLiaison+UITableViewDelegate.swift | 8 ++++---- .../OKAnyTableViewSectionComponent.swift | 2 +- .../Classes/Sections/OKTableViewSection.swift | 4 ++-- .../Sections/OKTableViewSectionComponent.swift | 2 +- README.md | 17 +++++++++-------- 20 files changed, 69 insertions(+), 55 deletions(-) diff --git a/Example/OKTableViewLiaison/ContentFeedViewController.swift b/Example/OKTableViewLiaison/ContentFeedViewController.swift index 9697749..f4b61b3 100644 --- a/Example/OKTableViewLiaison/ContentFeedViewController.swift +++ b/Example/OKTableViewLiaison/ContentFeedViewController.swift @@ -16,7 +16,7 @@ final class ContentFeedViewController: UIViewController { private var initialSections: [PostTableViewSection] { return Post.initialPosts() - .map(PostTableViewSectionFactory.section(for:)) + .map { PostTableViewSectionFactory.section(for: $0, width: tableView.frame.width) } } override func viewDidLoad() { @@ -27,7 +27,11 @@ final class ContentFeedViewController: UIViewController { liaison.paginationDelegate = self liaison.liaise(tableView: tableView) - liaison.append(sections: initialSections) + } + + override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + liaison.append(sections: initialSections, animated: false) } @objc private func refreshSections() { @@ -50,7 +54,7 @@ extension ContentFeedViewController: OKTableViewLiaisonPaginationDelegate { liaison.scroll(to: indexPath) let sections = Post.paginatedPosts() - .map(PostTableViewSectionFactory.section(for:)) + .map { PostTableViewSectionFactory.section(for: $0, width: tableView.frame.width) } DispatchQueue.main.asyncAfter(deadline: .now() + 2) { self.liaison.append(sections: sections) diff --git a/Example/OKTableViewLiaison/Factories/PostTableViewSectionFactory.swift b/Example/OKTableViewLiaison/Factories/PostTableViewSectionFactory.swift index c5bc4d5..f397c16 100644 --- a/Example/OKTableViewLiaison/Factories/PostTableViewSectionFactory.swift +++ b/Example/OKTableViewLiaison/Factories/PostTableViewSectionFactory.swift @@ -11,9 +11,9 @@ import OKTableViewLiaison enum PostTableViewSectionFactory { - static func section(for post: Post) -> PostTableViewSection { + static func section(for post: Post, width: CGFloat) -> PostTableViewSection { let rows: [OKAnyTableViewRow] = [ - ImageTableViewRow(image: post.content), + ImageTableViewRow(image: post.content, width: width), ActionButtonsTableViewRow(), TextTableViewRowFactory.likesRow(numberOfLikes: post.numberOfLikes), TextTableViewRowFactory.captionRow(user: post.user.username, caption: post.caption), diff --git a/Example/OKTableViewLiaison/ImageTableViewRow.swift b/Example/OKTableViewLiaison/ImageTableViewRow.swift index dc2404b..08afedf 100644 --- a/Example/OKTableViewLiaison/ImageTableViewRow.swift +++ b/Example/OKTableViewLiaison/ImageTableViewRow.swift @@ -9,19 +9,20 @@ import Foundation import OKTableViewLiaison -final class ImageTableViewRow: OKTableViewRow { +final class ImageTableViewRow: OKTableViewRow { - init(image: UIImage) { + init(image: UIImage, width: CGFloat) { - super.init(image, registrationType: ImageTableViewRow.defaultNibRegistrationType) + super.init((image, width), registrationType: ImageTableViewRow.defaultNibRegistrationType) - set(height: .height) { image -> CGFloat in + set(height: .height) { model -> CGFloat in + let (image, width) = model let ratio = image.size.width / image.size.height - return UIScreen.main.bounds.width / ratio + return width / ratio } - set(command: .configuration) { cell, image, indexPath in - cell.contentImageView.image = image + set(command: .configuration) { cell, model, indexPath in + cell.contentImageView.image = model.0 cell.contentImageView.contentMode = .scaleAspectFill } } diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 8de57f7..4ab4dab 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -1,5 +1,5 @@ PODS: - - OKTableViewLiaison (2.1.0) + - OKTableViewLiaison (2.1.1) DEPENDENCIES: - OKTableViewLiaison (from `../`) @@ -9,7 +9,7 @@ EXTERNAL SOURCES: :path: "../" SPEC CHECKSUMS: - OKTableViewLiaison: 48bf6db8ab6bbf205892d7a2f510c4fb239a5286 + OKTableViewLiaison: 451c9b818476b11c97d35a9ab88a3096c4c44018 PODFILE CHECKSUM: 9150dd9310401db4896273bd584c73be0d2714d2 diff --git a/Example/Pods/Local Podspecs/OKTableViewLiaison.podspec.json b/Example/Pods/Local Podspecs/OKTableViewLiaison.podspec.json index b3ce830..ab5568c 100644 --- a/Example/Pods/Local Podspecs/OKTableViewLiaison.podspec.json +++ b/Example/Pods/Local Podspecs/OKTableViewLiaison.podspec.json @@ -1,6 +1,6 @@ { "name": "OKTableViewLiaison", - "version": "2.1.0", + "version": "2.1.1", "license": { "type": "MIT", "file": "LICENSE" @@ -13,7 +13,7 @@ }, "source": { "git": "https://github.com/okcupid/OKTableViewLiaison.git", - "tag": "2.1.0" + "tag": "2.1.1" }, "platforms": { "ios": "9.0" diff --git a/Example/Pods/Manifest.lock b/Example/Pods/Manifest.lock index 8de57f7..4ab4dab 100644 --- a/Example/Pods/Manifest.lock +++ b/Example/Pods/Manifest.lock @@ -1,5 +1,5 @@ PODS: - - OKTableViewLiaison (2.1.0) + - OKTableViewLiaison (2.1.1) DEPENDENCIES: - OKTableViewLiaison (from `../`) @@ -9,7 +9,7 @@ EXTERNAL SOURCES: :path: "../" SPEC CHECKSUMS: - OKTableViewLiaison: 48bf6db8ab6bbf205892d7a2f510c4fb239a5286 + OKTableViewLiaison: 451c9b818476b11c97d35a9ab88a3096c4c44018 PODFILE CHECKSUM: 9150dd9310401db4896273bd584c73be0d2714d2 diff --git a/Example/Pods/Target Support Files/OKTableViewLiaison/Info.plist b/Example/Pods/Target Support Files/OKTableViewLiaison/Info.plist index 7f71fff..92aaf05 100644 --- a/Example/Pods/Target Support Files/OKTableViewLiaison/Info.plist +++ b/Example/Pods/Target Support Files/OKTableViewLiaison/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 2.1.0 + 2.1.1 CFBundleSignature ???? CFBundleVersion diff --git a/Example/Tests/OKTableViewSection+UnitTests.swift b/Example/Tests/OKTableViewSection+UnitTests.swift index c348dd0..5ceee0f 100644 --- a/Example/Tests/OKTableViewSection+UnitTests.swift +++ b/Example/Tests/OKTableViewSection+UnitTests.swift @@ -230,12 +230,12 @@ final class OKTableViewSection_UnitTests: XCTestCase { let view = UITableViewHeaderFooterView() let section = OKTableViewSection(componentDisplayOption: .both(headerComponent: header, footerComponent: footer)) - section.perform(command: .configuration, supplementaryView: .header, for: view, in: 0) - section.perform(command: .configuration, supplementaryView: .footer, for: view, in: 0) - section.perform(command: .didEndDisplaying, supplementaryView: .header, for: view, in: 0) - section.perform(command: .didEndDisplaying, supplementaryView: .footer, for: view, in: 0) - section.perform(command: .willDisplay, supplementaryView: .header, for: view, in: 0) - section.perform(command: .willDisplay, supplementaryView: .footer, for: view, in: 0) + section.perform(command: .configuration, componentView: .header, for: view, in: 0) + section.perform(command: .configuration, componentView: .footer, for: view, in: 0) + section.perform(command: .didEndDisplaying, componentView: .header, for: view, in: 0) + section.perform(command: .didEndDisplaying, componentView: .footer, for: view, in: 0) + section.perform(command: .willDisplay, componentView: .header, for: view, in: 0) + section.perform(command: .willDisplay, componentView: .footer, for: view, in: 0) XCTAssertEqual(headerConfiguration, "Configured!") XCTAssertEqual(footerConfiguration, "Configured!") @@ -263,8 +263,8 @@ final class OKTableViewSection_UnitTests: XCTestCase { let view = UIView() let section = OKTableViewSection(componentDisplayOption: .both(headerComponent: header, footerComponent: footer)) - section.perform(command: .configuration, supplementaryView: .header, for: view, in: 0) - section.perform(command: .configuration, supplementaryView: .footer, for: view, in: 0) + section.perform(command: .configuration, componentView: .header, for: view, in: 0) + section.perform(command: .configuration, componentView: .footer, for: view, in: 0) XCTAssertEqual(headerConfiguration, "") XCTAssertEqual(footerConfiguration, "") diff --git a/OKTableViewLiaison.podspec b/OKTableViewLiaison.podspec index 885f434..2b55268 100644 --- a/OKTableViewLiaison.podspec +++ b/OKTableViewLiaison.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'OKTableViewLiaison' - s.version = '2.1.0' + s.version = '2.1.1' s.license = 'MIT' s.summary = 'Framework to help you better manage UITableViews.' s.description = 'OKTableViewLiaison abstracts and simplifies UITableView construction and management.' diff --git a/OKTableViewLiaison/Classes/Enums/OKTableViewSectionComponentDisplayOption.swift b/OKTableViewLiaison/Classes/Enums/OKTableViewSectionComponentDisplayOption.swift index 8f9e1d3..2a8096c 100644 --- a/OKTableViewLiaison/Classes/Enums/OKTableViewSectionComponentDisplayOption.swift +++ b/OKTableViewLiaison/Classes/Enums/OKTableViewSectionComponentDisplayOption.swift @@ -1,5 +1,5 @@ // -// OKTableViewSectionSupplementaryViewDisplayOption.swift +// OKTableViewSectionComponentDisplayOption.swift // OKTableViewLiaison // // Created by Dylan Shine on 3/21/18. diff --git a/OKTableViewLiaison/Classes/Enums/OKTableViewSectionComponentView.swift b/OKTableViewLiaison/Classes/Enums/OKTableViewSectionComponentView.swift index 7776947..244f082 100644 --- a/OKTableViewLiaison/Classes/Enums/OKTableViewSectionComponentView.swift +++ b/OKTableViewLiaison/Classes/Enums/OKTableViewSectionComponentView.swift @@ -1,5 +1,5 @@ // -// OKTableViewSectionSupplementaryView.swift +// OKTableViewSectionComponentView.swift // OKTableViewLiaison // // Created by Dylan Shine on 3/22/18. diff --git a/OKTableViewLiaison/Classes/Extensions/Array+SafeAccess.swift b/OKTableViewLiaison/Classes/Extensions/Array+SafeAccess.swift index 6f3b62d..6b68e69 100644 --- a/OKTableViewLiaison/Classes/Extensions/Array+SafeAccess.swift +++ b/OKTableViewLiaison/Classes/Extensions/Array+SafeAccess.swift @@ -17,5 +17,13 @@ extension Array { return self[index] } - + + var lastIndex: Index { + guard !isEmpty else { + return 0 + } + + return count - 1 + } + } diff --git a/OKTableViewLiaison/Classes/Liaison/OKTableViewLiaison+Pagination.swift b/OKTableViewLiaison/Classes/Liaison/OKTableViewLiaison+Pagination.swift index c2f3777..23aa491 100644 --- a/OKTableViewLiaison/Classes/Liaison/OKTableViewLiaison+Pagination.swift +++ b/OKTableViewLiaison/Classes/Liaison/OKTableViewLiaison+Pagination.swift @@ -15,8 +15,8 @@ extension OKTableViewLiaison { return nil } - let row = lastSection.rows.count - 1 - let section = sections.count - 1 + let row = lastSection.rows.lastIndex + let section = sections.lastIndex return IndexPath(row: row, section: section) } @@ -46,9 +46,9 @@ extension OKTableViewLiaison { waitingForPaginatedResults = false if animated { - deleteSection(at: sections.count - 1, animation: .none) + deleteSection(at: sections.lastIndex, animation: .none) } else { - sections.remove(at: sections.count - 1) + sections.remove(at: sections.lastIndex) } } @@ -58,7 +58,7 @@ extension OKTableViewLiaison { guard !rows.isEmpty, let lastSection = sections.last else { return } - let firstNewIndexPath = IndexPath(row: lastSection.rows.count, section: sections.count - 1) + let firstNewIndexPath = IndexPath(row: lastSection.rows.count, section: sections.lastIndex) lastSection.append(rows: rows) reloadData() paginationDelegate?.paginationEnded(indexPath: firstNewIndexPath) diff --git a/OKTableViewLiaison/Classes/Liaison/OKTableViewLiaison+Row.swift b/OKTableViewLiaison/Classes/Liaison/OKTableViewLiaison+Row.swift index bcac358..793f694 100644 --- a/OKTableViewLiaison/Classes/Liaison/OKTableViewLiaison+Row.swift +++ b/OKTableViewLiaison/Classes/Liaison/OKTableViewLiaison+Row.swift @@ -20,7 +20,7 @@ public extension OKTableViewLiaison { !rows.isEmpty else { return } var lastRowIndex = _section.rows.count - 1 - + _section.append(rows: rows) let indexPaths = rows.map { row -> IndexPath in diff --git a/OKTableViewLiaison/Classes/Liaison/OKTableViewLiaison+Section.swift b/OKTableViewLiaison/Classes/Liaison/OKTableViewLiaison+Section.swift index c58519b..d1e0389 100644 --- a/OKTableViewLiaison/Classes/Liaison/OKTableViewLiaison+Section.swift +++ b/OKTableViewLiaison/Classes/Liaison/OKTableViewLiaison+Section.swift @@ -19,7 +19,7 @@ public extension OKTableViewLiaison { guard !sections.isEmpty else { return } let lowerBound = self.sections.count - let upperBound = lowerBound + sections.count - 1 + let upperBound = lowerBound + sections.lastIndex let indexSet = IndexSet(integersIn: lowerBound...upperBound) self.sections.append(contentsOf: sections) @@ -112,7 +112,7 @@ public extension OKTableViewLiaison { guard !self.sections.isEmpty else { return } - let sectionsRange = 0...self.sections.count - 1 + let sectionsRange = 0...self.sections.lastIndex let indexSet = IndexSet(integersIn: sectionsRange) self.sections.removeAll() diff --git a/OKTableViewLiaison/Classes/Liaison/OKTableViewLiaison+UITableViewDelegate.swift b/OKTableViewLiaison/Classes/Liaison/OKTableViewLiaison+UITableViewDelegate.swift index 6de23c2..665e884 100644 --- a/OKTableViewLiaison/Classes/Liaison/OKTableViewLiaison+UITableViewDelegate.swift +++ b/OKTableViewLiaison/Classes/Liaison/OKTableViewLiaison+UITableViewDelegate.swift @@ -119,19 +119,19 @@ extension OKTableViewLiaison: UITableViewDelegate { } public func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { - sections.element(at: section)?.perform(command: .willDisplay, supplementaryView: .header, for: view, in: section) + sections.element(at: section)?.perform(command: .willDisplay, componentView: .header, for: view, in: section) } public func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) { - sections.element(at: section)?.perform(command: .willDisplay, supplementaryView: .footer, for: view, in: section) + sections.element(at: section)?.perform(command: .willDisplay, componentView: .footer, for: view, in: section) } public func tableView(_ tableView: UITableView, didEndDisplayingHeaderView view: UIView, forSection section: Int) { - sections.element(at: section)?.perform(command: .didEndDisplaying, supplementaryView: .header, for: view, in: section) + sections.element(at: section)?.perform(command: .didEndDisplaying, componentView: .header, for: view, in: section) } public func tableView(_ tableView: UITableView, didEndDisplayingFooterView view: UIView, forSection section: Int) { - sections.element(at: section)?.perform(command: .didEndDisplaying, supplementaryView: .footer, for: view, in: section) + sections.element(at: section)?.perform(command: .didEndDisplaying, componentView: .footer, for: view, in: section) } } diff --git a/OKTableViewLiaison/Classes/Protocols/OKAnyTableViewSectionComponent.swift b/OKTableViewLiaison/Classes/Protocols/OKAnyTableViewSectionComponent.swift index 83cf7fe..058275b 100644 --- a/OKTableViewLiaison/Classes/Protocols/OKAnyTableViewSectionComponent.swift +++ b/OKTableViewLiaison/Classes/Protocols/OKAnyTableViewSectionComponent.swift @@ -1,5 +1,5 @@ // -// OKAnyTableViewSectionSupplementaryView.swift +// OKAnyTableViewSectionComponent.swift // OKTableViewLiaison // // Created by Dylan Shine on 5/29/18. diff --git a/OKTableViewLiaison/Classes/Sections/OKTableViewSection.swift b/OKTableViewLiaison/Classes/Sections/OKTableViewSection.swift index 8071826..a27932b 100644 --- a/OKTableViewLiaison/Classes/Sections/OKTableViewSection.swift +++ b/OKTableViewLiaison/Classes/Sections/OKTableViewSection.swift @@ -41,8 +41,8 @@ open class OKTableViewSection { } // MARK: - Supplementary Views - public func perform(command: OKTableViewSectionComponentCommand, supplementaryView: OKTableViewSectionComponentView, for view: UIView, in section: Int) { - switch supplementaryView { + public func perform(command: OKTableViewSectionComponentCommand, componentView: OKTableViewSectionComponentView, for view: UIView, in section: Int) { + switch componentView { case .header: componentDisplayOption.header?.perform(command: command, for: view, in: section) case .footer: diff --git a/OKTableViewLiaison/Classes/Sections/OKTableViewSectionComponent.swift b/OKTableViewLiaison/Classes/Sections/OKTableViewSectionComponent.swift index 627f070..9ea599d 100644 --- a/OKTableViewLiaison/Classes/Sections/OKTableViewSectionComponent.swift +++ b/OKTableViewLiaison/Classes/Sections/OKTableViewSectionComponent.swift @@ -1,5 +1,5 @@ // -// OKTableViewSectionSupplementaryView.swift +// OKTableViewSectionComponent.swift // OKTableViewLiaison // // Created by Dylan Shine on 5/29/18. diff --git a/README.md b/README.md index bf08afb..9ab8545 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ liaison.liaise(tableView: tableView) ``` By liaising your tableView with the liaison, the liaison becomes both its `UITableViewDataSource`, `UITableViewDelegate`, and `UITableViewDataSourcePrefetching`. -In the event you would like to remove the tableView from the liaison, simply invoke `liaison.detachTableView()`. +In the event you would like to remove the tableView from the liaison, simply invoke `liaison.detach()`. OKTableViewLiaison populates sections and rows using two main types: @@ -233,7 +233,7 @@ To use a custom pagination spinner, you can pass an instance `OKAnyTableViewRow` ### Tips & Tricks -Because `OKTableViewSection` and `OKTableViewRow` utilize generic types and manage view/cell type registration, instantiating multiple different configurations of sections and rows can get verbose. Creating a subclass or utilizing a factory to create your various `OKTableViewSection`/`OKTableViewRow` may be useful. +Because `OKTableViewSection` and `OKTableViewRow` utilize generic types and manage view/cell type registration, instantiating multiple different configurations of sections and rows can get verbose. Creating a subclass or utilizing a factory to create your various `OKTableViewRow`/`OKTableViewSectionComponent` types may be useful. ```swift final class PostTextTableViewRow: OKTableViewRow { @@ -246,15 +246,16 @@ final class PostTextTableViewRow: OKTableViewRow ```swift static func contentRow(with image: UIImage, width: CGFloat) -> AnyTableViewRow { - let row = OKTableViewRow(image) + let row = OKTableViewRow((image, width)) - row.set(height: .height) { image -> CGFloat in - let ratio = image.size.width / image.size.height - return width / ratio + row.set(height: .height) { model -> CGFloat in + let (image, width) = model + let ratio = image.size.width / image.size.height + return width / ratio } - row.set(command: .configuration) { cell, image, indexPath in - cell.contentImageView.image = image + row.set(command: .configuration) { cell, model, indexPath in + cell.contentImageView.image = model.0 cell.contentImageView.contentMode = .scaleAspectFill }