Skip to content

Commit

Permalink
2.1.1 (#11)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dylanshine authored Jun 11, 2018
1 parent e9f5fbf commit c43dbde
Show file tree
Hide file tree
Showing 20 changed files with 69 additions and 55 deletions.
10 changes: 7 additions & 3 deletions Example/OKTableViewLiaison/ContentFeedViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
15 changes: 8 additions & 7 deletions Example/OKTableViewLiaison/ImageTableViewRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@
import Foundation
import OKTableViewLiaison

final class ImageTableViewRow: OKTableViewRow<ImageTableViewCell, UIImage> {
final class ImageTableViewRow: OKTableViewRow<ImageTableViewCell, (UIImage, CGFloat)> {

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
}
}
Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- OKTableViewLiaison (2.1.0)
- OKTableViewLiaison (2.1.1)

DEPENDENCIES:
- OKTableViewLiaison (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
OKTableViewLiaison: 48bf6db8ab6bbf205892d7a2f510c4fb239a5286
OKTableViewLiaison: 451c9b818476b11c97d35a9ab88a3096c4c44018

PODFILE CHECKSUM: 9150dd9310401db4896273bd584c73be0d2714d2

Expand Down
4 changes: 2 additions & 2 deletions Example/Pods/Local Podspecs/OKTableViewLiaison.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions Example/Tests/OKTableViewSection+UnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand Down Expand Up @@ -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, "")
Expand Down
2 changes: 1 addition & 1 deletion OKTableViewLiaison.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// OKTableViewSectionSupplementaryViewDisplayOption.swift
// OKTableViewSectionComponentDisplayOption.swift
// OKTableViewLiaison
//
// Created by Dylan Shine on 3/21/18.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// OKTableViewSectionSupplementaryView.swift
// OKTableViewSectionComponentView.swift
// OKTableViewLiaison
//
// Created by Dylan Shine on 3/22/18.
Expand Down
10 changes: 9 additions & 1 deletion OKTableViewLiaison/Classes/Extensions/Array+SafeAccess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,13 @@ extension Array {

return self[index]
}


var lastIndex: Index {
guard !isEmpty else {
return 0
}

return count - 1
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// OKAnyTableViewSectionSupplementaryView.swift
// OKAnyTableViewSectionComponent.swift
// OKTableViewLiaison
//
// Created by Dylan Shine on 5/29/18.
Expand Down
4 changes: 2 additions & 2 deletions OKTableViewLiaison/Classes/Sections/OKTableViewSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// OKTableViewSectionSupplementaryView.swift
// OKTableViewSectionComponent.swift
// OKTableViewLiaison
//
// Created by Dylan Shine on 5/29/18.
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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<PostTextTableViewCell, String> {
Expand All @@ -246,15 +246,16 @@ final class PostTextTableViewRow: OKTableViewRow<PostTextTableViewCell, String>

```swift
static func contentRow(with image: UIImage, width: CGFloat) -> AnyTableViewRow {
let row = OKTableViewRow<PostImageContentTableViewCell, UIImage>(image)
let row = OKTableViewRow<PostImageContentTableViewCell, (UIImage, CGFloat)>((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
}

Expand Down

0 comments on commit c43dbde

Please sign in to comment.