Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Source/UIKit/UIPageControl.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// UIPageControl.swift
// Rex
//
// Created by Torsten Curdt on 7/24/16.
// Copyright (c) 2015 Torsten Curdt. All rights reserved.
//

import ReactiveCocoa
import UIKit

extension UIPageControl {

/// Wraps a page control's `numberOfPages` value in a bindable property.
public var rex_numberOfPages: MutableProperty<Int> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why label and not pageControl

return associatedProperty(self, key: &numberOfPagesKey, initial: { $0.numberOfPages }, setter: { $0.numberOfPages = $1 })
}

/// Wraps a page control's `currentPage` value in a bindable property.
public var rex_currentPage: MutableProperty<Int> {
return associatedProperty(self, key: &currentPageKey, initial: { $0.currentPage }, setter: { $0.currentPage = $1 })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use UIControl's rex_value for this purpose, that way we can keep this property up-to-date since we will process each UIControlEventValueChanged event triggered from any user interaction.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I am not objecting I am not sure I can follow the argument. What do you mean by "keep this property up-to-date"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will keep currentPage and rex_currentPage in sync. Right now we can control UIPageControl's currentPage but if the user interacts with the page control changing to a certain page manually, triggering a UIControlEventValueChanged event, our bindable property will not be updated with the new index.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tcurdt can you please change it to:

public var rex_currentPage: MutableProperty<Int> {
    return UIControl.rex_value(self, getter: { $0.currentPage }, setter: { $0.currentPage = $1 })
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

}
}

private var numberOfPagesKey: UInt8 = 0
private var currentPageKey: UInt8 = 0
47 changes: 47 additions & 0 deletions Tests/UIKit/UIPageControlTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// UIPageControl.swift
// Rex
//
// Created by Torsten Curdt on 7/24/16.
// Copyright (c) 2015 Torsten Curdt. All rights reserved.
//

import XCTest
import ReactiveCocoa
import Result

class UIPageControlTests: XCTestCase {

weak var _pageControl: UIPageControl?

override func tearDown() {
XCTAssert(_pageControl == nil, "Retain cycle detected in UIPageControl properties")
super.tearDown()
}

func testNumberOfPages() {
let pageControl = UIPageControl(frame: CGRectZero)
_pageControl = pageControl

let (pipeSignal, observer) = Signal<Int, NoError>.pipe()
pageControl.rex_numberOfPages <~ SignalProducer(signal: pipeSignal)

observer.sendNext(1)
XCTAssertTrue(pageControl.numberOfPages == 1)
observer.sendNext(2)
XCTAssertTrue(pageControl.numberOfPages == 2)
}

func testNumberOfPages() {
let pageControl = UIPageControl(frame: CGRectZero)
_pageControl = pageControl

let (pipeSignal, observer) = Signal<Int, NoError>.pipe()
pageControl.rex_currentPage <~ SignalProducer(signal: pipeSignal)

observer.sendNext(1)
XCTAssertTrue(pageControl.currentPage == 1)
observer.sendNext(2)
XCTAssertTrue(pageControl.currentPage == 2)
}
}