Skip to content

Commit

Permalink
Finished codestyle and demo updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Zonov committed Dec 20, 2017
1 parent 0887874 commit fd57ab1
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import UIKit

class DatePickerCell: UITableViewCell {

// MARK: Public Properties
weak var datePicker: UIDatePicker!

// MARK: Lifecycle
init() {
let datePicker = UIDatePicker(frame: .zero)
self.datePicker = datePicker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import UIKit

class PickerCell: UITableViewCell {

// MARK: Public Properties
weak var picker: UIPickerView!

// MARK: Lifecycle
init() {
let picker = UIPickerView(frame: .zero)
self.picker = picker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@ public typealias PickerSetupClosure = ((UIPickerView) -> ())

public struct ExpandedCellInfo: Equatable {

// MARK: Private Data Structures
public enum CellType {
case datePicker(DatePickerSetupClosure?)
case picker(PickerSetupClosure)
case custom(CellClosure)
}

// MARK: Public Properties
public var indexPath: IndexPath
public var cellType: CellType


// MARK: Lifecycle
public init(for indexPath: IndexPath, cellType: CellType) {
self.indexPath = indexPath
self.cellType = cellType
}

// MARK: Public
public static func ==(lhs: ExpandedCellInfo, rhs: ExpandedCellInfo) -> Bool {
return lhs.indexPath == rhs.indexPath
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ public typealias TableInfoProvider = UITableViewDelegate & UITableViewDataSource

public class ExpandableTable: NSObject {

// MARK: Private Properties
private var proxy: ExpandableProxy?
private var expandedCell: ExpandedCellInfo?
private weak var tableView: UITableView!
private weak var infoProvider: TableInfoProvider!

// MARK: Lifecycle
public init(with tableView: UITableView, infoProvider: TableInfoProvider) {
self.tableView = tableView
self.infoProvider = infoProvider
Expand All @@ -26,6 +28,7 @@ public class ExpandableTable: NSObject {
tableView.delegate = proxy
}

// MARK: Public
public func expandCell(_ cell: ExpandedCellInfo) {
var resultCell = cell
resultCell.indexPath.incrementRow()
Expand All @@ -47,6 +50,7 @@ public class ExpandableTable: NSObject {
}
}

// MARK: - UITableViewDataSource
extension ExpandableTable: UITableViewDataSource {

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
Expand Down Expand Up @@ -80,6 +84,7 @@ extension ExpandableTable: UITableViewDataSource {

}

// MARK: - UITableViewDelegate
extension ExpandableTable: UITableViewDelegate {

public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
Expand Down
12 changes: 8 additions & 4 deletions ExpandableCell.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
544BBBECEC7CBA6DC12582BA /* Pods_ExpandableCell.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9CAEC0521AA1804F5AD4A219 /* Pods_ExpandableCell.framework */; };
5759872B1FA39FE200239B5E /* TableViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5759872A1FA39FE200239B5E /* TableViewModel.swift */; };
579732361F971A43006DB071 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 579732351F971A43006DB071 /* AppDelegate.swift */; };
579732381F971A43006DB071 /* DemoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 579732371F971A43006DB071 /* DemoViewController.swift */; };
579732381F971A43006DB071 /* ComplexDemoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 579732371F971A43006DB071 /* ComplexDemoViewController.swift */; };
5797323B1F971A43006DB071 /* Demo.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 579732391F971A43006DB071 /* Demo.storyboard */; };
5797323D1F971A43006DB071 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5797323C1F971A43006DB071 /* Assets.xcassets */; };
579732401F971A43006DB071 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5797323E1F971A43006DB071 /* LaunchScreen.storyboard */; };
Expand All @@ -19,13 +19,14 @@
57FF08091FB2D9BB008C0958 /* CenteredLabelCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57FF08081FB2D9BB008C0958 /* CenteredLabelCell.swift */; };
57FF080D1FB2DB73008C0958 /* CellFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57FF080C1FB2DB73008C0958 /* CellFactory.swift */; };
FB3A0DBB1FE82D2A00D5C0A6 /* PickerItemsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB3A0DBA1FE82D2A00D5C0A6 /* PickerItemsController.swift */; };
FBDF2A041FE8FCC600889B28 /* BasicDemoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBDF2A031FE8FCC600889B28 /* BasicDemoViewController.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
5759872A1FA39FE200239B5E /* TableViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewModel.swift; sourceTree = "<group>"; };
579732321F971A43006DB071 /* ExpandableCell.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ExpandableCell.app; sourceTree = BUILT_PRODUCTS_DIR; };
579732351F971A43006DB071 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
579732371F971A43006DB071 /* DemoViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoViewController.swift; sourceTree = "<group>"; };
579732371F971A43006DB071 /* ComplexDemoViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComplexDemoViewController.swift; sourceTree = "<group>"; };
5797323A1F971A43006DB071 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Demo.storyboard; sourceTree = "<group>"; };
5797323C1F971A43006DB071 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
5797323F1F971A43006DB071 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
Expand All @@ -38,6 +39,7 @@
8A984765D95495A0BB3DFE38 /* Pods-ExpandableCell.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ExpandableCell.release.xcconfig"; path = "Pods/Target Support Files/Pods-ExpandableCell/Pods-ExpandableCell.release.xcconfig"; sourceTree = "<group>"; };
9CAEC0521AA1804F5AD4A219 /* Pods_ExpandableCell.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ExpandableCell.framework; sourceTree = BUILT_PRODUCTS_DIR; };
FB3A0DBA1FE82D2A00D5C0A6 /* PickerItemsController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PickerItemsController.swift; sourceTree = "<group>"; };
FBDF2A031FE8FCC600889B28 /* BasicDemoViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BasicDemoViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -112,7 +114,8 @@
57FF08011FB2CEB8008C0958 /* View Controller */ = {
isa = PBXGroup;
children = (
579732371F971A43006DB071 /* DemoViewController.swift */,
FBDF2A031FE8FCC600889B28 /* BasicDemoViewController.swift */,
579732371F971A43006DB071 /* ComplexDemoViewController.swift */,
FB3A0DBA1FE82D2A00D5C0A6 /* PickerItemsController.swift */,
57FF080C1FB2DB73008C0958 /* CellFactory.swift */,
);
Expand Down Expand Up @@ -271,10 +274,11 @@
files = (
5759872B1FA39FE200239B5E /* TableViewModel.swift in Sources */,
57FF08091FB2D9BB008C0958 /* CenteredLabelCell.swift in Sources */,
579732381F971A43006DB071 /* DemoViewController.swift in Sources */,
579732381F971A43006DB071 /* ComplexDemoViewController.swift in Sources */,
57FF07FF1FB2CBC2008C0958 /* TableViewModelFactory.swift in Sources */,
FB3A0DBB1FE82D2A00D5C0A6 /* PickerItemsController.swift in Sources */,
57FF080D1FB2DB73008C0958 /* CellFactory.swift in Sources */,
FBDF2A041FE8FCC600889B28 /* BasicDemoViewController.swift in Sources */,
579732361F971A43006DB071 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
71 changes: 71 additions & 0 deletions ExpandableCell/View Controller/BasicDemoViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// BasicDemoViewController.swift
// ExpandableCell
//
// Created by Andrey Zonov on 19/12/2017.
// Copyright © 2017 Andrey Zonov. All rights reserved.
//

import Foundation
import AZExpandable

class BasicDemoViewController: UIViewController {

// MARK: IBOutlet's
@IBOutlet private weak var tableView: UITableView!

// MARK: Private Properties
private var expandableTable: ExpandableTable!
private var expandedCell: ExpandedCellInfo?

// MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()

CellFactory.registerCells(for: tableView)
expandableTable = ExpandableTable(with: tableView, infoProvider: self)
}

private func toggleItem(at indexPath: IndexPath) {
guard expandedCell?.indexPath != indexPath else {
expandableTable.unexpandCell()
expandedCell = nil
return
}

// Construct your cell here
let cellType: ExpandedCellInfo.CellType = .custom { [weak self] indexPath -> (UITableViewCell) in
let centeredCell = self?.tableView.dequeueReusableCell(withIdentifier: "CenteredLabelCell",
for: indexPath) as! CenteredLabelCell
centeredCell.configure(with: "Hint Inside your custom cell")
centeredCell.backgroundColor = .lightGray
return centeredCell
}
let cell = ExpandedCellInfo(for: indexPath, cellType: cellType)
expandedCell = cell
expandableTable.expandCell(cell)
}
}

// MARK: - UITableViewDataSource
extension BasicDemoViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}

func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CenteredLabelCell", for: indexPath) as! CenteredLabelCell
cell.configure(with: "Tap to reveal hint for \(indexPath.row) row")
return cell
}
}

// MARK: - UITableViewDelegate
extension BasicDemoViewController: UITableViewDelegate {

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
toggleItem(at: indexPath)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// DemoViewController.swift
// ComplexDemoViewController.swift
// ExpandableCell
//
// Created by Andrey Zonov on 18/10/2017.
Expand All @@ -9,12 +9,12 @@
import UIKit
import AZExpandable

class DemoViewController: UIViewController {
class ComplexDemoViewController: UIViewController {

// MARK: IBOutlet's
@IBOutlet private weak var tableView: UITableView!

// MARK: Private properties
// MARK: Private Properties
private var expandableTable: ExpandableTable!
private var expandedCell: ExpandedCellInfo?
private var tableViewModel = TableViewModelFactory.staticExpandableTable
Expand All @@ -30,7 +30,6 @@ class DemoViewController: UIViewController {
pickerController = PickerItemsController { title in
if let indexPath = self.expandedCell?.indexPath {
var viewModel = self.tableViewModel.cell(for: indexPath)
guard viewModel.expandingType == .picker else { return }
viewModel.title = title
self.tableViewModel.replace(cell: viewModel, at: indexPath)
self.tableView.reloadRows(at: [indexPath], with: .automatic)
Expand All @@ -50,8 +49,9 @@ class DemoViewController: UIViewController {
case .date:
cellType = .datePicker { datePicker in
datePicker.minimumDate = Date()
//TODO: realize
// datePicker.addTarget(self, action: #selector(didChangeValue), for: .valueChanged)
datePicker.addTarget(self.pickerController,
action: #selector(PickerItemsController.datePickerDidChangeValue),
for: .valueChanged)
}

case .picker:
Expand All @@ -76,7 +76,16 @@ class DemoViewController: UIViewController {
}

// MARK: - UITableViewDataSource
extension DemoViewController: UITableViewDataSource {
extension ComplexDemoViewController: UITableViewDataSource {

func numberOfSections(in tableView: UITableView) -> Int {
return tableViewModel.sections.count
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let section = tableViewModel.sections[section]
return section.title
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableViewModel.sections[section].cells.count
Expand All @@ -97,7 +106,7 @@ extension DemoViewController: UITableViewDataSource {
}

// MARK: - UITableViewDelegate
extension DemoViewController: UITableViewDelegate {
extension ComplexDemoViewController: UITableViewDelegate {

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
toggleItem(at: indexPath)
Expand Down
5 changes: 5 additions & 0 deletions ExpandableCell/View Controller/PickerItemsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ class PickerItemsController: NSObject, UIPickerViewDelegate, UIPickerViewDataSou
updateClosure(title)
}
}

@objc func datePickerDidChangeValue(_ datePicker: UIDatePicker) {
let title = DateFormatter.localizedString(from: datePicker.date, dateStyle: .medium, timeStyle: .medium)
updateClosure(title)
}
}
2 changes: 1 addition & 1 deletion ExpandableCell/ViewModels/TableViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct TableViewModel {
var title: String
var expandingType: ExpandingType
}

let title: String?
var cells: [Cell]
}

Expand Down
21 changes: 16 additions & 5 deletions ExpandableCell/ViewModels/TableViewModelFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@ import Foundation
struct TableViewModelFactory {

static var staticExpandableTable: TableViewModel {
let helpCell = TableViewModel.Section.Cell(title: "Help", expandingType: .custom)
let dateCell = TableViewModel.Section.Cell(title: "Date", expandingType: .date)
let pickerCell = TableViewModel.Section.Cell(title: "Picker", expandingType: .picker)

let section = TableViewModel.Section(cells: [helpCell, dateCell, pickerCell])
let helpCell1 = TableViewModel.Section.Cell(title: "Tap to reveal help1", expandingType: .custom)
let helpCell2 = TableViewModel.Section.Cell(title: "Tap to reveal help2", expandingType: .custom)
let helpCell3 = TableViewModel.Section.Cell(title: "Tap to reveal help3", expandingType: .custom)

return TableViewModel(sections: [section])
let dateCell1 = TableViewModel.Section.Cell(title: "Date 1", expandingType: .date)
let dateCell2 = TableViewModel.Section.Cell(title: "Date 2", expandingType: .date)
let dateCell3 = TableViewModel.Section.Cell(title: "Date 3", expandingType: .date)

let pickerCell1 = TableViewModel.Section.Cell(title: "Picker 1", expandingType: .picker)
let pickerCell2 = TableViewModel.Section.Cell(title: "Picker 2", expandingType: .picker)
let pickerCell3 = TableViewModel.Section.Cell(title: "Picker 3", expandingType: .picker)

let sectionHelp = TableViewModel.Section(title: "Help cells section", cells: [helpCell1, helpCell2, helpCell3])
let sectionDate = TableViewModel.Section(title: "Date cells section", cells: [dateCell1, dateCell2, dateCell3])
let sectionPicker = TableViewModel.Section(title: "Picker cells section", cells: [pickerCell1, pickerCell2, pickerCell3])

return TableViewModel(sections: [sectionHelp, sectionDate, sectionPicker])
}
}
Loading

0 comments on commit fd57ab1

Please sign in to comment.