Skip to content

Commit

Permalink
Merge pull request #10 from odigeoteam/develop
Browse files Browse the repository at this point in the history
Add Editable protocol
  • Loading branch information
adellibovi authored Nov 2, 2016
2 parents 4875f83 + 2076052 commit 5594284
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ enum MoreAboutItemType {
}
}

class MoreAboutItem: Item, Selectable {
class MoreAboutItem: Item, Selectable, Editable {

var type: MoreAboutItemType
var title: String?

var drawer: CellDrawer.Type = MoreAboutDrawer.self
var onSelection: (Selectable) -> () = { _ in }


var actions: [UITableViewRowAction]?

init(type: MoreAboutItemType) {

self.type = type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ class MoreAboutSection: Section {
self.presenter = presenter
self.manager = manager

let moreAction = UITableViewRowAction(style: .normal, title: "More", handler: { action, indexPath in
print("MoreAction executed")
})
let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete", handler: { action, indexPath in
self.items.remove(at: indexPath.row)
})

let types: [MoreAboutItemType] = [.faq, .contact, .terms, .feedback, .share, .rate]
for type in types {
let moreAboutItem = MoreAboutItem(type: type)
Expand All @@ -36,6 +43,7 @@ class MoreAboutSection: Section {
guard let manager = self.manager else { return }
item.deselect(in: manager, animated: true)
}
moreAboutItem.actions = [deleteAction, moreAction]
items.append(moreAboutItem)
}
}
Expand Down
2 changes: 1 addition & 1 deletion TableViewKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "TableViewKit"
s.version = "0.9.2"
s.version = "0.9.3"
s.summary = "Empowering UITableView with painless multi-type cell support and build-in automatic state transition animations"
s.homepage = "http://github.com/odigeoteam/TableViewKit/"
s.license = "MIT"
Expand Down
4 changes: 4 additions & 0 deletions TableViewKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
2564E61A1D88419B00A9DC3E /* TestRegisterNibCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 2564E6191D88419B00A9DC3E /* TestRegisterNibCell.xib */; };
2564E61C1D88450F00A9DC3E /* TestRegisterHeaderFooterView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 2564E61B1D88450F00A9DC3E /* TestRegisterHeaderFooterView.xib */; };
25837C781D87F819001EF4B8 /* ItemCompatible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 25837C771D87F819001EF4B8 /* ItemCompatible.swift */; };
25B0B7541DC74F6C00591467 /* Editable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 25B0B7531DC74F6C00591467 /* Editable.swift */; };
CC5CF11C1D839E71004DECB3 /* ArrayDiff.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC5CF11B1D839E71004DECB3 /* ArrayDiff.swift */; };
CC97D76D1D741DC4009CDF9D /* Selectable.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC97D76C1D741DC4009CDF9D /* Selectable.swift */; };
CC9D50E11D8496180010FCA3 /* TableViewDelegateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC9D50E01D8496180010FCA3 /* TableViewDelegateTests.swift */; };
Expand Down Expand Up @@ -47,6 +48,7 @@
2564E6191D88419B00A9DC3E /* TestRegisterNibCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = TestRegisterNibCell.xib; sourceTree = "<group>"; };
2564E61B1D88450F00A9DC3E /* TestRegisterHeaderFooterView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = TestRegisterHeaderFooterView.xib; sourceTree = "<group>"; };
25837C771D87F819001EF4B8 /* ItemCompatible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ItemCompatible.swift; path = Protocols/ItemCompatible.swift; sourceTree = "<group>"; };
25B0B7531DC74F6C00591467 /* Editable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Editable.swift; path = Protocols/Editable.swift; sourceTree = "<group>"; };
4BCAD51C1D89A704002F3420 /* Nimble.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Nimble.framework; path = "External/Nimble/build/Debug-iphoneos/Nimble.framework"; sourceTree = "<group>"; };
CC5CF11B1D839E71004DECB3 /* ArrayDiff.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArrayDiff.swift; sourceTree = "<group>"; };
CC97D76C1D741DC4009CDF9D /* Selectable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Selectable.swift; path = Protocols/Selectable.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -111,6 +113,7 @@
CCEF387F1D8D5A7A00F5893F /* HeaderFooterDrawer.swift */,
CCBE458C1D72F79C00414A64 /* Item.swift */,
CC97D76C1D741DC4009CDF9D /* Selectable.swift */,
25B0B7531DC74F6C00591467 /* Editable.swift */,
25837C771D87F819001EF4B8 /* ItemCompatible.swift */,
CCBE458A1D72F79C00414A64 /* CellDrawer.swift */,
);
Expand Down Expand Up @@ -292,6 +295,7 @@
CC97D76D1D741DC4009CDF9D /* Selectable.swift in Sources */,
CCCAC1281D7DA2D00001FC1D /* Height.swift in Sources */,
CC5CF11C1D839E71004DECB3 /* ArrayDiff.swift in Sources */,
25B0B7541DC74F6C00591467 /* Editable.swift in Sources */,
25837C781D87F819001EF4B8 /* ItemCompatible.swift in Sources */,
CCEF38801D8D5A7A00F5893F /* HeaderFooterDrawer.swift in Sources */,
CCEF387E1D8D59C000F5893F /* NibClassType.swift in Sources */,
Expand Down
8 changes: 8 additions & 0 deletions TableViewKit/Protocols/Editable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation

/// A type that represent an item that can be edited
public protocol Editable: Item {

/// The associated actions
var actions: [UITableViewRowAction]? { get set }
}
8 changes: 0 additions & 8 deletions TableViewKit/Protocols/Stateful.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//
// Stateful.swift
// TableViewKit
//
// Created by Alfredo Delli Bovi on 23/09/2016.
// Copyright © 2016 odigeo. All rights reserved.
//

import Foundation

/// A section that supports states.
Expand Down
10 changes: 9 additions & 1 deletion TableViewKit/TableViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ extension TableViewManager: UITableViewDataSource {
return title(for: {$0.footer}, inSection: section)
}


/// Implementation of UITableViewDataSource
public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
// Intentionally blank. Required to use UITableViewRowActions
}
}

extension TableViewManager: UITableViewDelegate {
Expand Down Expand Up @@ -211,4 +214,9 @@ extension TableViewManager: UITableViewDelegate {
return view(for: {$0.footer}, inSection: section)
}

/// Implementation of UITableViewDelegate
public func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
guard let item = item(at: indexPath) as? Editable else { return nil }
return item.actions
}
}
22 changes: 22 additions & 0 deletions TableViewKitTests/TableViewDelegateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class SelectableItem: Selectable, Item {
}
}

class EditableItem: SelectableItem, Editable {
public var actions: [UITableViewRowAction]?
}


class TableViewDelegateTests: XCTestCase {

Expand Down Expand Up @@ -206,4 +210,22 @@ class TableViewDelegateTests: XCTestCase {
expect(check).to(equal(2))
}

func testEditableRows() {
let section = tableViewManager.sections.first!
let deleteAction = UITableViewRowAction(style: .normal, title: "Delete", handler: { action, indexPath in
print("DeleteAction")
})
let editableItem = EditableItem { _ in }
editableItem.actions = [deleteAction]
section.items.append(editableItem)

let sectionIndex = section.index(in: tableViewManager)!
let rows = tableViewManager.tableView(tableViewManager.tableView, numberOfRowsInSection: sectionIndex)
XCTAssert(rows == 2)

let indexPath = editableItem.indexPath(in: tableViewManager)!
let actions = tableViewManager.tableView(tableViewManager.tableView, editActionsForRowAt: indexPath)
XCTAssertNotNil(actions)
XCTAssert(actions!.count == 1)
}
}

0 comments on commit 5594284

Please sign in to comment.