Skip to content

Commit

Permalink
Merge pull request #12 from kmcgill88/check-memory-leaks
Browse files Browse the repository at this point in the history
fix memory leaks
  • Loading branch information
kmcgill88 authored Jul 8, 2017
2 parents 6f769f5 + da1c6ff commit 6219c96
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 37 deletions.
62 changes: 62 additions & 0 deletions Example/Tests/McPickerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,66 @@ class McPickerTests: XCTestCase {
//
XCTAssertEqual(UIColor.purple, mcPicker.picker.backgroundColor)
}

func testDimissViews_callsAnimateViews() {
// Given
//
class TestMcPicker: McPicker {
var direction: McPicker.AnimationDirection?
var calledShow = false
override func animateViews(direction: McPicker.AnimationDirection) {
self.direction = direction
}

override func show(cancelHandler: @escaping () -> Void, doneHandler: @escaping ([Int : String]) -> Void) {
animateViews(direction: .in)
calledShow = true
}
}
let mcPicker = TestMcPicker(data: data)

// When
//
mcPicker.show(cancelHandler: {}) { (_: [Int : String]) in }
XCTAssertFalse(mcPicker.isPopoverMode)
XCTAssertEqual(McPicker.AnimationDirection.in, mcPicker.direction)
mcPicker.dismissViews()

// Then
//
XCTAssertTrue(mcPicker.calledShow)
XCTAssertEqual(McPicker.AnimationDirection.out, mcPicker.direction)
}

func testDimissViews_calls() {
// Given
//
class TestVC: UIViewController {
var presented = false
override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
presented = true
}
}
class TestMcPicker: McPicker {
var calledAnimateViews = false
override func animateViews(direction: McPicker.AnimationDirection) {
calledAnimateViews = true
}
}
let testVC = TestVC()
let mcPicker = TestMcPicker(data: data)

// When
//
mcPicker.showAsPopover(fromViewController: testVC, sourceView: UIView()) { (_: [Int : String]) -> Void in }
XCTAssertTrue(mcPicker.mcPickerPopoverViewController != nil)
XCTAssertTrue(mcPicker.isPopoverMode)

mcPicker.dismissViews()

// Then
//
XCTAssertNil(mcPicker.mcPickerPopoverViewController)
XCTAssertFalse(mcPicker.calledAnimateViews)
}
}
2 changes: 1 addition & 1 deletion McPicker.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'McPicker'
s.version = '0.3.0'
s.version = '0.3.1'
s.summary = 'McPicker is a UIPickerView drop-in solution with animations that is rotation ready.'

s.description = <<-DESC
Expand Down
71 changes: 36 additions & 35 deletions McPicker/Classes/McPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ open class McPicker: UIView {
}
internal var isPopoverMode = false
internal var mcPickerPopoverViewController: McPickerPopoverViewController?
internal enum AnimationDirection {
case `in`, out // swiftlint:disable:this identifier_name
}

fileprivate var doneHandler:(_ selections: [Int:String]) -> Void = {_ in }
fileprivate var cancelHandler:() -> Void = {_ in }
Expand All @@ -94,9 +97,6 @@ open class McPicker: UIView {
return window
}

private enum AnimationDirection {
case `in`, out // swiftlint:disable:this identifier_name
}
private enum Constant {
static let pickerHeight: CGFloat = 216.0
static let toolBarHeight: CGFloat = 44.0
Expand Down Expand Up @@ -243,45 +243,16 @@ open class McPicker: UIView {
self.addSubview(backgroundView)
}

private func setup() {
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(cancel)))

setToolbarItems(items: [fixedSpace, cancelBarButton, flexibleSpace, doneBarButton, fixedSpace])

self.backgroundColor = UIColor.black.withAlphaComponent(Constant.backgroundAlpha)
backgroundView.backgroundColor = UIColor.white

picker.delegate = self
picker.dataSource = self

sizeViews()

// Default selection to first item per component
//
for (index, element) in pickerData.enumerated() {
pickerSelection[index] = element.first
}
}

@objc private func done() {
self.doneHandler(self.pickerSelection)
self.dismissViews()
}

@objc private func cancel() {
self.cancelHandler()
self.dismissViews()
}

private func dismissViews() {
internal func dismissViews() {
if isPopoverMode {
mcPickerPopoverViewController?.dismiss(animated: true, completion: nil)
mcPickerPopoverViewController = nil // Release, as to not create a retain cycle.
} else {
animateViews(direction: .out)
}
}

private func animateViews(direction: AnimationDirection) {
internal func animateViews(direction: AnimationDirection) {
var backgroundFrame = backgroundView.frame

if direction == .in {
Expand Down Expand Up @@ -318,6 +289,36 @@ open class McPicker: UIView {
})
}
}

private func setup() {
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(cancel)))

setToolbarItems(items: [fixedSpace, cancelBarButton, flexibleSpace, doneBarButton, fixedSpace])

self.backgroundColor = UIColor.black.withAlphaComponent(Constant.backgroundAlpha)
backgroundView.backgroundColor = UIColor.white

picker.delegate = self
picker.dataSource = self

sizeViews()

// Default selection to first item per component
//
for (index, element) in pickerData.enumerated() {
pickerSelection[index] = element.first
}
}

@objc private func done() {
self.doneHandler(self.pickerSelection)
self.dismissViews()
}

@objc private func cancel() {
self.cancelHandler()
self.dismissViews()
}
}

extension McPicker : UIPickerViewDataSource {
Expand Down
2 changes: 1 addition & 1 deletion McPicker/Classes/McPickerPopoverViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import UIKit

internal class McPickerPopoverViewController: UIViewController {

var mcPicker: McPicker?
weak var mcPicker: McPicker?

internal convenience init(mcPicker: McPicker) {
self.init(nibName: nil, bundle: nil)
Expand Down

0 comments on commit 6219c96

Please sign in to comment.