Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/optimize add sections #190

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 13 additions & 7 deletions Source/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -513,11 +513,13 @@ extension Form : RangeReplaceableCollectionType {
kvoWrapper.sections.replaceObjectsInRange(NSMakeRange(subRange.startIndex, subRange.endIndex - subRange.startIndex), withObjectsFromArray: newElements.map { $0 })
kvoWrapper._allSections.insertContentsOf(newElements, at: indexForInsertionAtIndex(subRange.startIndex))

for section in newElements{
section.wasAddedToForm(self)
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in
for section in newElements{
section.wasAddedToForm(self)
}
}
}
public func removeAll(keepCapacity keepCapacity: Bool = false) {
// not doing anything with capacity
for section in kvoWrapper._allSections{
Expand Down Expand Up @@ -1442,9 +1444,13 @@ extension BaseRow {
assert(section.form?.rowsByTag[t] == nil, "Duplicate tag \(t)")
self.section?.form?.rowsByTag[t] = self
}
addToRowObservers()
evaluateHidden()
evaluateDisabled()
if self.section?.form != nil {
self.addToRowObservers()
dispatch_async(dispatch_get_main_queue()) { [weak self] () -> Void in
self?.evaluateHidden()
self?.evaluateDisabled()
}
}
}

private final func addToHiddenRowObservers() {
Expand Down
44 changes: 24 additions & 20 deletions Tests/FormValuesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,18 @@ class FormValuesTests: BaseEurekaTests {
let textRowInvisible = TextRow("InvisibleRowTag")
textRowInvisible.hidden = true
form +++ Section() <<< textRowInvisible

XCTAssertEqual(form.values(includeHidden: true).count, 2)
XCTAssertTrue(form.values(includeHidden: true)["RowTag"] is String)

XCTAssertEqual(form.allRows.count, 3)
XCTAssertEqual(form.rows.count, 2)
XCTAssertEqual(form.values().count, 1)
XCTAssertEqual(form.values(includeHidden: true).count, 2)

dispatch_async(dispatch_get_main_queue()) { () -> Void in
XCTAssertEqual(form.values(includeHidden: true).count, 2)
XCTAssertTrue(form.values(includeHidden: true)["RowTag"] is String)

XCTAssertEqual(form.allRows.count, 3)
XCTAssertEqual(form.rows.count, 2)
XCTAssertEqual(form.values().count, 1)
XCTAssertEqual(form.values(includeHidden: true).count, 2)
}
}
func testIncludeHiddenFormValues() {
let form = Form()

Expand Down Expand Up @@ -100,16 +102,18 @@ class FormValuesTests: BaseEurekaTests {
textRowInvisible.hidden = true
textRowInvisible.value = "Bye!"
form +++ Section() <<< textRowInvisible

XCTAssertEqual(form.values(includeHidden: true).count, 2)
XCTAssertTrue(form.values(includeHidden: true)["RowTag"] is String)
XCTAssertTrue(form.values(includeHidden: true)["InvisibleRowTag"] is String)

XCTAssertEqual(form.allRows.count, 3)
XCTAssertEqual(form.rows.count, 2)
XCTAssertEqual(form.values().count, 1)
XCTAssertEqual(form.values(includeHidden: true).count, 2)

dispatch_async(dispatch_get_main_queue()) { () -> Void in
XCTAssertEqual(form.values(includeHidden: true).count, 2)
XCTAssertTrue(form.values(includeHidden: true)["RowTag"] is String)
XCTAssertTrue(form.values(includeHidden: true)["InvisibleRowTag"] is String)

XCTAssertEqual(form.allRows.count, 3)
XCTAssertEqual(form.rows.count, 2)
XCTAssertEqual(form.values().count, 1)
XCTAssertEqual(form.values(includeHidden: true).count, 2)
}
}
}
31 changes: 16 additions & 15 deletions Tests/HelperMethodTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,21 @@ class HelperMethodTests: BaseEurekaTests {
let intRow = IntRow("int"){ $0.disabled = true }

formVC.form +++ checkRow <<< switchRow <<< segmentedRow <<< intRow

checkRow.updateCell()
XCTAssertTrue(checkRow.cell.selectionStyle == .None)

switchRow.updateCell()
XCTAssertNotNil(switchRow.cell.switchControl)
XCTAssertFalse(switchRow.cell.switchControl!.enabled)

segmentedRow.updateCell()
XCTAssertFalse(segmentedRow.cell.segmentedControl.enabled)

intRow.updateCell()
XCTAssertFalse(intRow.cell.cellCanBecomeFirstResponder())


dispatch_async(dispatch_get_main_queue()) { () -> Void in
checkRow.updateCell()
XCTAssertTrue(checkRow.cell.selectionStyle == .None)

switchRow.updateCell()
XCTAssertNotNil(switchRow.cell.switchControl)
XCTAssertFalse(switchRow.cell.switchControl!.enabled)

segmentedRow.updateCell()
XCTAssertFalse(segmentedRow.cell.segmentedControl.enabled)

intRow.updateCell()
XCTAssertFalse(intRow.cell.cellCanBecomeFirstResponder())
}
}
}
70 changes: 39 additions & 31 deletions Tests/HiddenRowsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,29 +262,32 @@ class HiddenRowsTests: BaseEurekaTests {
s2
sec2 (2 rows)
*/

XCTAssertEqual(form.count, 5)
XCTAssertEqual(form[0].count, 2)
XCTAssertEqual(form[1].count, 0)
XCTAssertEqual(form[2].count, 2)
XCTAssertEqual(form[3].count, 0)
XCTAssertEqual(form[4].count, 2)

form[0][0].baseValue = "hello, good morning!"

XCTAssertEqual(form.count, 3)
XCTAssertEqual(form[0].count, 2)
XCTAssertEqual(form[1].count, 2)
XCTAssertEqual(form[2].count, 2)

form[0][0].baseValue = "whatever"

XCTAssertEqual(form.count, 5)
XCTAssertEqual(form[1].tag, "s1_ths")
XCTAssertEqual(form[3].tag, "s2_ths")
XCTAssertEqual(form[4].tag, "s3_hrt")

form[0][0].baseValue = "hello, good morning!"

dispatch_async(dispatch_get_main_queue()) { [weak self] () -> Void in
XCTAssertEqual(self?.form.count, 3)
XCTAssertEqual(self?.form[0].count, 2)
XCTAssertEqual(self?.form[1].count, 2)
XCTAssertEqual(self?.form[2].count, 2)

self?.form[0][0].baseValue = "whatever"

dispatch_async(dispatch_get_main_queue()) { () -> Void in
XCTAssertEqual(self?.form.count, 5)
XCTAssertEqual(self?.form[1].tag, "s1_ths")
XCTAssertEqual(self?.form[3].tag, "s2_ths")
XCTAssertEqual(self?.form[4].tag, "s3_hrt")
}
}
}
func testInsertionIndex(){
let r1 = CheckRow("check1_tii_hrt"){ $0.hidden = "$NameRow_s1 contains 'morning'" }
let r2 = CheckRow("check2_tii_hrt"){ $0.hidden = "$NameRow_s1 contains 'morning'" }
Expand All @@ -309,10 +312,12 @@ class HiddenRowsTests: BaseEurekaTests {
// insert another row
form[1].insert(r4, atIndex: 1)

XCTAssertEqual(form[1].count, 2) // all inserted rows should be hidden
XCTAssertEqual(form[1][0].tag, "int1_hrt")
XCTAssertEqual(form[1][1].tag, "txt1_hrt")

dispatch_async(dispatch_get_main_queue()) { () -> Void in
XCTAssertEqual(self.form[1].count, 2) // all inserted rows should be hidden
XCTAssertEqual(self.form[1][0].tag, "int1_hrt")
XCTAssertEqual(self.form[1][1].tag, "txt1_hrt")
}

form[0][0].baseValue = "whatever"

// we inserted r4 at index 1 but there were two rows hidden before it as well so it shall be at index 3
Expand All @@ -331,16 +336,19 @@ class HiddenRowsTests: BaseEurekaTests {
//inserting 2 rows at the end, deleting 1
form[2].replaceRange(Range(start: 1, end: 2), with: [r2, r4])

XCTAssertEqual(form[1].count, 0)
XCTAssertEqual(form[2].count, 1)
XCTAssertEqual(form[2][0].tag, "txt2_hrt")

form[0][0].baseValue = "whatever"

XCTAssertEqual(form[2].count, 3)
XCTAssertEqual(form[2][0].tag, "txt2_hrt")
XCTAssertEqual(form[2][1].tag, "check2_tii_hrt")
XCTAssertEqual(form[2][2].tag, "check4_tii_hrt")

dispatch_async(dispatch_get_main_queue()) { () -> Void in
XCTAssertEqual(self.form[1].count, 0)
XCTAssertEqual(self.form[2].count, 1)
XCTAssertEqual(self.form[2][0].tag, "txt2_hrt")

self.form[0][0].baseValue = "whatever"

dispatch_async(dispatch_get_main_queue()) { () -> Void in
XCTAssertEqual(self.form[2].count, 3)
XCTAssertEqual(self.form[2][0].tag, "txt2_hrt")
XCTAssertEqual(self.form[2][1].tag, "check2_tii_hrt")
XCTAssertEqual(self.form[2][2].tag, "check4_tii_hrt")
}
}
}
}