Skip to content

Commit

Permalink
adding preset controller and tools to make custom ones
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiki270 committed Apr 25, 2018
1 parent 764df1e commit 7f0e948
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 33 deletions.
8 changes: 8 additions & 0 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

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

4 changes: 4 additions & 0 deletions Example/Presentables.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
1541CA39200C349800CE03C3 /* TableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 156D7AA41F422E570075ACE7 /* TableViewCell.swift */; };
156801342091176500FA296A /* ManagerTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 156801332091176500FA296A /* ManagerTableViewController.swift */; };
156D7AFA1F431A150075ACE7 /* TableViewHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 156D7AF91F431A150075ACE7 /* TableViewHeader.swift */; };
158B4B1D206410E300FCE847 /* CollectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 158B4B1C206410E300FCE847 /* CollectionViewController.swift */; };
158B4B1F2064111500FCE847 /* CollectionDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 158B4B1E2064111500FCE847 /* CollectionDataManager.swift */; };
Expand All @@ -33,6 +34,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
156801332091176500FA296A /* ManagerTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ManagerTableViewController.swift; sourceTree = "<group>"; };
156D7AA41F422E570075ACE7 /* TableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewCell.swift; sourceTree = "<group>"; };
156D7AF91F431A150075ACE7 /* TableViewHeader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewHeader.swift; sourceTree = "<group>"; };
158B4B1C206410E300FCE847 /* CollectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -85,6 +87,7 @@
children = (
158B4B1C206410E300FCE847 /* CollectionViewController.swift */,
607FACD71AFB9204008FA782 /* TableViewController.swift */,
156801332091176500FA296A /* ManagerTableViewController.swift */,
15CA94761FD0408F001AA1FE /* ViewController.swift */,
);
name = "View controllers";
Expand Down Expand Up @@ -418,6 +421,7 @@
buildActionMask = 2147483647;
files = (
159C16D5200C3DA0008BE539 /* TableDataManager.swift in Sources */,
156801342091176500FA296A /* ManagerTableViewController.swift in Sources */,
607FACD81AFB9204008FA782 /* TableViewController.swift in Sources */,
158B4B2620641A4800FCE847 /* CollectionViewCell.swift in Sources */,
15CA94771FD0408F001AA1FE /* ViewController.swift in Sources */,
Expand Down
40 changes: 40 additions & 0 deletions Example/Presentables/ManagerTableViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// ManagerTableViewController.swift
// Presentables_Example
//
// Created by Ondrej Rafaj on 25/04/2018.
// Copyright © 2018 CocoaPods. All rights reserved.
//

import Foundation
import Presentables


class ManagerTableViewController: PresentableTableViewController {

override func viewDidLoad() {
super.viewDidLoad()

let section = PresentableSection()

let header = Presentable<TableViewHeader>.create { (header) in
header.titleLabel.text = "It works yet again!"
}
section.header = header

let presentable = Presentable<TableViewCell2>.create({ (cell) in
cell.textLabel?.text = "Custom cell"
}).cellSelected {
print("First cell has been selected")
}
section.presentables.append(presentable)

data.append(section)

presentableManager.selectedCell = { info in
info.tableView.deselectRow(at: info.indexPath, animated: true)
print("Did select cell no. \((info.indexPath.row + 1))")
}
}

}
2 changes: 1 addition & 1 deletion Example/Presentables/TableDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TableDataManager: PresentableTableViewDataManager {

// MARK: Overriding table view delegate (optional)

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}

Expand Down
21 changes: 19 additions & 2 deletions Example/Presentables/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ class ViewController: UIViewController {
make.height.equalTo(44)
}

let tableButton2 = UIButton()
tableButton2.backgroundColor = tableButton2.tintColor
tableButton2.layer.cornerRadius = 6
tableButton2.setTitle("Table view example 2", for: .normal)
tableButton2.setTitleColor(.white, for: .normal)
view.addSubview(tableButton2)
tableButton2.addTarget(self, action: #selector(didTapTable2Button), for: .touchUpInside)
tableButton2.snp.makeConstraints { (make) in
make.top.equalTo(tableButton.snp.bottom).offset(20)
make.left.right.height.equalTo(tableButton)
}

let collectionButton = UIButton()
collectionButton.backgroundColor = collectionButton.tintColor
collectionButton.layer.cornerRadius = 6
Expand All @@ -45,8 +57,8 @@ class ViewController: UIViewController {
view.addSubview(collectionButton)
collectionButton.addTarget(self, action: #selector(didTapCollectionButton), for: .touchUpInside)
collectionButton.snp.makeConstraints { (make) in
make.top.equalTo(tableButton.snp.bottom).offset(20)
make.left.right.height.equalTo(tableButton)
make.top.equalTo(tableButton2.snp.bottom).offset(20)
make.left.right.height.equalTo(tableButton2)
}
}

Expand All @@ -57,6 +69,11 @@ class ViewController: UIViewController {
navigationController?.pushViewController(c, animated: true)
}

@objc func didTapTable2Button() {
let c = ManagerTableViewController()
navigationController?.pushViewController(c, animated: true)
}

@objc func didTapCollectionButton() {
let c = CollectionViewController()
navigationController?.pushViewController(c, animated: true)
Expand Down
2 changes: 1 addition & 1 deletion Presentables.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'Presentables'
s.version = '0.6.2'
s.version = '0.6.3'
s.summary = 'Simple reactive library for managing table views & collection views, written in Swift'

# This description is used to generate tags and improve search results.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// PresentableTableViewController.swift
// Presentables
//
// Created by Ondrej Rafaj on 25/04/2018.
//

import Foundation
import UIKit


open class PresentableTableViewController: UITableViewController {

public let presentableManager = PresentableTableViewDataManager()

public var data: PresentableSections {
get { return presentableManager.data }
set { presentableManager.data = newValue }
}

// MARK: View lifecycle

open override func viewDidLoad() {
super.viewDidLoad()

var manager: PresentableManager = presentableManager
tableView.bind(withPresentableManager: &manager)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ open class PresentableTableViewDataManager: NSObject, PresentableManager, UITabl

// MARK: Actions

public func reloadData() {
open func reloadData() {
tableView?.reloadData()
}

public func reload(section: Int) {
open func reload(section: Int) {
tableView?.reloadSections([section], with: .none)
}

public func reload(indexPath: IndexPath) {
open func reload(indexPath: IndexPath) {
tableView?.reloadRows(at: [indexPath], with: .none)
}

Expand Down Expand Up @@ -68,46 +68,46 @@ open class PresentableTableViewDataManager: NSObject, PresentableManager, UITabl
return cell!
}

// MARK: Private helpers

func tableView(_ tableView: UITableView, presentable: AnyPresentable) -> UIView? {
let identifier: String = presentable.identifier
var view = tableView.dequeueReusableHeaderFooterView(withIdentifier: identifier)
if view == nil {
tableView.register(presentable.storedType, forHeaderFooterViewReuseIdentifier: presentable.identifier)
view = tableView.dequeueReusableHeaderFooterView(withIdentifier: identifier)
guard view != nil else {
return nil
}
}
presentable.runConfigure(with: view)
return view
}

// MARK: Delegate

open func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let presentable: AnyPresentable = data.header(forSection: section) else {
return nil
}
return self.tableView(tableView, presentable: presentable)
}

public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
open func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return data[section].header == nil ? 0 : UITableViewAutomaticDimension
}

open func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
guard let presentable: AnyPresentable = data.footer(forSection: section) else {
return nil
}
return self.tableView(tableView, presentable: presentable)
}

public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
open func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return data[section].footer == nil ? 0 : UITableViewAutomaticDimension
}

// MARK: Private helpers

func tableView(_ tableView: UITableView, presentable: AnyPresentable) -> UIView? {
let identifier: String = presentable.identifier
var view = tableView.dequeueReusableHeaderFooterView(withIdentifier: identifier)
if view == nil {
tableView.register(presentable.storedType, forHeaderFooterViewReuseIdentifier: presentable.identifier)
view = tableView.dequeueReusableHeaderFooterView(withIdentifier: identifier)
guard view != nil else {
return nil
}
}
presentable.runConfigure(with: view)
return view
}

// MARK: Delegate

open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let presentable: AnyPresentable = data.presentable(forIndexPath: indexPath)
presentable.selected?()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// PresentableTableViewManageable.swift
// Presentables
//
// Created by Ondrej Rafaj on 25/04/2018.
//

import Foundation


public protocol PresentableTableViewManageable {
var tableView: UITableView { get }
var presentableManager: PresentableTableViewDataManager { get }
var data: PresentableSections { get set }
}


extension PresentableTableViewManageable {

func bind() {
var manager: PresentableManager = presentableManager
tableView.bind(withPresentableManager: &manager)
}

}
Loading

0 comments on commit 7f0e948

Please sign in to comment.