Skip to content

Commit

Permalink
Fix back button
Browse files Browse the repository at this point in the history
  • Loading branch information
justinswart committed Jul 30, 2019
1 parent c23f4b7 commit b2cb23b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,9 @@ internal final class DeprecatedRewardPledgeViewController: UIViewController {
.observeForUI()
.observeValues { [weak self] in self?.load(items: $0) }

self.viewModel.outputs.dismissViewController
self.viewModel.outputs.popToRootViewController
.observeForControllerAction()
.observeValues { [weak self] in self?.dismiss(animated: true, completion: nil) }
.observeValues { [weak self] in self?.navigationController?.popToRootViewController(animated: true) }

self.viewModel.outputs.expandRewardDescription
.observeForUI()
Expand Down Expand Up @@ -651,6 +651,12 @@ internal final class DeprecatedRewardPledgeViewController: UIViewController {
self.viewModel.inputs.descriptionLabelIsTruncated(self.descriptionLabel.isTruncated())
}

override func willMove(toParent parent: UIViewController?) {
super.willMove(toParent: parent)

self.viewModel.inputs.willMove(toParent: parent)
}

@objc fileprivate func shippingButtonTapped() {
self.viewModel.inputs.shippingButtonTapped()
}
Expand All @@ -675,10 +681,6 @@ internal final class DeprecatedRewardPledgeViewController: UIViewController {
self.viewModel.inputs.expandDescriptionTapped()
}

@IBAction fileprivate func closeButtonTapped() {
self.viewModel.inputs.closeButtonTapped()
}

@objc fileprivate func pledgedTextFieldChanged() {
self.viewModel.inputs.pledgeTextFieldChanged(self.pledgeTextField.text ?? "")
}
Expand Down
9 changes: 1 addition & 8 deletions Kickstarter-iOS/Views/Storyboards/RewardPledge.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -472,13 +472,7 @@
<constraint firstAttribute="trailing" secondItem="a0d-eK-Yei" secondAttribute="trailing" id="dOb-vF-MGO"/>
</constraints>
</view>
<navigationItem key="navigationItem" id="huU-Yh-pg1">
<barButtonItem key="leftBarButtonItem" image="close-icon" id="LJ3-iZ-7wB">
<connections>
<action selector="closeButtonTapped" destination="hRf-bi-Adk" id="xWD-4b-Fn7"/>
</connections>
</barButtonItem>
</navigationItem>
<navigationItem key="navigationItem" id="huU-Yh-pg1"/>
<simulatedNavigationBarMetrics key="simulatedTopBarMetrics" prompted="NO"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<size key="freeformSize" width="400" height="900"/>
Expand Down Expand Up @@ -656,7 +650,6 @@
</scenes>
<resources>
<image name="checkmark-icon" width="11" height="9"/>
<image name="close-icon" width="12" height="12"/>
<image name="drop-down-disclosure-icon" width="8" height="4"/>
</resources>
</document>
31 changes: 15 additions & 16 deletions Library/ViewModels/DeprecatedRewardPledgeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ public protocol DeprecatedRewardPledgeViewModelInputs {
/// Call when the shipping picker has notified us that shipping has changed.
func change(shippingRule: ShippingRule)

/// Call when the close button is tapped.
func closeButtonTapped()

/// Call with the project and reward provided to the view.
func configureWith(project: Project, reward: Reward, applePayCapable: Bool)

Expand Down Expand Up @@ -68,6 +65,9 @@ public protocol DeprecatedRewardPledgeViewModelInputs {

/// Call when the view loads.
func viewDidLoad()

/// Call when the vc will move to parent.
func willMove(toParent parent: UIViewController?)
}

public protocol DeprecatedRewardPledgeViewModelOutputs {
Expand Down Expand Up @@ -98,8 +98,8 @@ public protocol DeprecatedRewardPledgeViewModelOutputs {
/// Emits a boolean that determines if the "different payment method" button is hidden.
var differentPaymentMethodButtonHidden: Signal<Bool, Never> { get }

/// Emits when the controller should be dismissed.
var dismissViewController: Signal<(), Never> { get }
/// Emits when the stack should be popped.
var popToRootViewController: Signal<(), Never> { get }

/// Emits a string to be put into the estimated delivery date label.
var estimatedDeliveryDateLabelText: Signal<String, Never> { get }
Expand Down Expand Up @@ -618,10 +618,9 @@ public final class DeprecatedRewardPledgeViewModel: Type, Inputs, Outputs {
: ($0.title ?? "")
}

self.dismissViewController = Signal.merge(
self.closeButtonTappedProperty.signal,
self.errorAlertTappedShouldDismissProperty.signal.filter(isTrue).ignoreValues()
)
self.popToRootViewController = self.errorAlertTappedShouldDismissProperty.signal
.filter(isTrue)
.ignoreValues()

let projectAndRewardAndPledgeContext = projectAndReward
.map { project, reward -> (Project, Reward, Koala.PledgeContext) in
Expand Down Expand Up @@ -697,7 +696,7 @@ public final class DeprecatedRewardPledgeViewModel: Type, Inputs, Outputs {
}

projectAndRewardAndPledgeContext
.takeWhen(self.closeButtonTappedProperty.signal)
.takeWhen(self.willMoveToParentProperty.signal.filter(isNil))
.observeValues {
AppEnvironment.current.koala.trackClosedReward(project: $0, reward: $1, pledgeContext: $2)
}
Expand Down Expand Up @@ -791,11 +790,6 @@ public final class DeprecatedRewardPledgeViewModel: Type, Inputs, Outputs {
self.changedShippingRuleProperty.value = shippingRule
}

fileprivate let closeButtonTappedProperty = MutableProperty(())
public func closeButtonTapped() {
self.closeButtonTappedProperty.value = ()
}

fileprivate let continueToPaymentsButtonTappedProperty = MutableProperty(())
public func continueToPaymentsButtonTapped() {
self.continueToPaymentsButtonTappedProperty.value = ()
Expand Down Expand Up @@ -884,6 +878,11 @@ public final class DeprecatedRewardPledgeViewModel: Type, Inputs, Outputs {
self.viewDidLoadProperty.value = ()
}

fileprivate let willMoveToParentProperty = MutableProperty<UIViewController?>(nil)
public func willMove(toParent parent: UIViewController?) {
self.willMoveToParentProperty.value = parent
}

public let applePayButtonHidden: Signal<Bool, Never>
public let continueToPaymentsButtonHidden: Signal<Bool, Never>
public var conversionLabelHidden: Signal<Bool, Never> {
Expand All @@ -900,7 +899,7 @@ public final class DeprecatedRewardPledgeViewModel: Type, Inputs, Outputs {
}

public let differentPaymentMethodButtonHidden: Signal<Bool, Never>
public let dismissViewController: Signal<(), Never>
public let popToRootViewController: Signal<(), Never>
public let estimatedDeliveryDateLabelText: Signal<String, Never>
public let estimatedFulfillmentStackViewHidden: Signal<Bool, Never>
public let expandRewardDescription: Signal<(), Never>
Expand Down
60 changes: 31 additions & 29 deletions Library/ViewModels/DeprecatedRewardPledgeViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal final class DeprecatedRewardPledgeViewModelTests: TestCase {
fileprivate let countryLabelText = TestObserver<String, Never>()
fileprivate let descriptionLabelText = TestObserver<String, Never>()
fileprivate let differentPaymentMethodButtonHidden = TestObserver<Bool, Never>()
fileprivate let dismissViewController = TestObserver<(), Never>()
fileprivate let popToRootViewController = TestObserver<(), Never>()
fileprivate let estimatedDeliveryDateLabelText = TestObserver<String, Never>()
private let estimatedFulfillmentStackViewHidden = TestObserver<Bool, Never>()
fileprivate let expandRewardDescription = TestObserver<(), Never>()
Expand Down Expand Up @@ -90,7 +90,7 @@ internal final class DeprecatedRewardPledgeViewModelTests: TestCase {
self.vm.outputs.descriptionLabelText.observe(self.descriptionLabelText.observer)
self.vm.outputs.differentPaymentMethodButtonHidden
.observe(self.differentPaymentMethodButtonHidden.observer)
self.vm.outputs.dismissViewController.observe(self.dismissViewController.observer)
self.vm.outputs.popToRootViewController.observe(self.popToRootViewController.observer)
self.vm.outputs.estimatedDeliveryDateLabelText.observe(self.estimatedDeliveryDateLabelText.observer)
self.vm.outputs.estimatedFulfillmentStackViewHidden
.observe(self.estimatedFulfillmentStackViewHidden.observer)
Expand Down Expand Up @@ -369,16 +369,18 @@ internal final class DeprecatedRewardPledgeViewModelTests: TestCase {
self.differentPaymentMethodButtonHidden.assertValues([true])
}

func testDismissViewController() {
func testTrackClosedRewardOnBack() {
self.vm.inputs.configureWith(project: .template, reward: .template, applePayCapable: false)
self.vm.inputs.viewDidLoad()

self.dismissViewController.assertValueCount(0)
XCTAssertEqual(["Reward Checkout", "Selected Reward"], self.trackingClient.events)

self.vm.inputs.closeButtonTapped()
// moving a non-nil VC does not track
self.vm.inputs.willMove(toParent: UIViewController())

self.dismissViewController.assertValueCount(1)
XCTAssertEqual(["Reward Checkout", "Selected Reward"], self.trackingClient.events)

self.vm.inputs.willMove(toParent: nil)

XCTAssertEqual(["Reward Checkout", "Selected Reward", "Closed Reward"], self.trackingClient.events)
}
Expand Down Expand Up @@ -1349,7 +1351,7 @@ internal final class DeprecatedRewardPledgeViewModelTests: TestCase {

self.vm.inputs.errorAlertTappedOK(shouldDismiss: false)

self.dismissViewController.assertValueCount(0)
self.popToRootViewController.assertValueCount(0)

self.pledgeIsLoading.assertValues([true, false])
self.loadingOverlayIsHidden.assertValues([true, false, true])
Expand Down Expand Up @@ -2236,7 +2238,7 @@ internal final class DeprecatedRewardPledgeViewModelTests: TestCase {

self.vm.inputs.errorAlertTappedOK(shouldDismiss: false)

self.dismissViewController.assertValueCount(0)
self.popToRootViewController.assertValueCount(0)

self.vm.inputs.pledgeTextFieldChanged("100000")

Expand Down Expand Up @@ -2270,7 +2272,7 @@ internal final class DeprecatedRewardPledgeViewModelTests: TestCase {

self.vm.inputs.errorAlertTappedOK(shouldDismiss: false)

self.dismissViewController.assertValueCount(0)
self.popToRootViewController.assertValueCount(0)

XCTAssertEqual(
[
Expand Down Expand Up @@ -2315,7 +2317,7 @@ internal final class DeprecatedRewardPledgeViewModelTests: TestCase {

self.vm.inputs.errorAlertTappedOK(shouldDismiss: false)

self.dismissViewController.assertValueCount(0)
self.popToRootViewController.assertValueCount(0)

self.vm.inputs.pledgeTextFieldChanged("100000")
self.vm.inputs.continueToPaymentsButtonTapped()
Expand All @@ -2330,7 +2332,7 @@ internal final class DeprecatedRewardPledgeViewModelTests: TestCase {

self.vm.inputs.errorAlertTappedOK(shouldDismiss: false)

self.dismissViewController.assertValueCount(0)
self.popToRootViewController.assertValueCount(0)
}
}

Expand Down Expand Up @@ -2457,11 +2459,11 @@ internal final class DeprecatedRewardPledgeViewModelTests: TestCase {
self.shippingIsLoading.assertValues([true, false])
self.showAlertMessage.assertValues([Strings.We_were_unable_to_load_the_shipping_destinations()])
self.showAlertShouldDismiss.assertValues([true])
self.dismissViewController.assertValueCount(0)
self.popToRootViewController.assertValueCount(0)

self.vm.inputs.errorAlertTappedOK(shouldDismiss: true)

self.dismissViewController.assertValueCount(1)
self.popToRootViewController.assertValueCount(1)
}
}

Expand Down Expand Up @@ -2499,11 +2501,11 @@ internal final class DeprecatedRewardPledgeViewModelTests: TestCase {

self.showAlertMessage.assertValues(["Something went wrong yo."])
self.showAlertShouldDismiss.assertValues([true])
self.dismissViewController.assertValueCount(0)
self.popToRootViewController.assertValueCount(0)

self.vm.inputs.errorAlertTappedOK(shouldDismiss: true)

self.dismissViewController.assertValueCount(1)
self.popToRootViewController.assertValueCount(1)

withEnvironment(
apiService: MockService(createPledgeResult: Result(failure: errorEmptyMessage)),
Expand All @@ -2524,11 +2526,11 @@ internal final class DeprecatedRewardPledgeViewModelTests: TestCase {
["Something went wrong yo.", Strings.general_error_something_wrong()]
)
self.showAlertShouldDismiss.assertValues([true, true])
self.dismissViewController.assertValueCount(1)
self.popToRootViewController.assertValueCount(1)

self.vm.inputs.errorAlertTappedOK(shouldDismiss: true)

self.dismissViewController.assertValueCount(2)
self.popToRootViewController.assertValueCount(2)
}
}
}
Expand Down Expand Up @@ -2567,11 +2569,11 @@ internal final class DeprecatedRewardPledgeViewModelTests: TestCase {

self.showAlertMessage.assertValues(["Something went wrong yo."])
self.showAlertShouldDismiss.assertValues([true])
self.dismissViewController.assertValueCount(0)
self.popToRootViewController.assertValueCount(0)

self.vm.inputs.errorAlertTappedOK(shouldDismiss: true)

self.dismissViewController.assertValueCount(1)
self.popToRootViewController.assertValueCount(1)

withEnvironment(
apiService: MockService(changePaymentMethodResult: Result(failure: errorEmptyMessage)),
Expand All @@ -2592,11 +2594,11 @@ internal final class DeprecatedRewardPledgeViewModelTests: TestCase {
["Something went wrong yo.", Strings.general_error_something_wrong()]
)
self.showAlertShouldDismiss.assertValues([true, true])
self.dismissViewController.assertValueCount(1)
self.popToRootViewController.assertValueCount(1)

self.vm.inputs.errorAlertTappedOK(shouldDismiss: true)

self.dismissViewController.assertValueCount(2)
self.popToRootViewController.assertValueCount(2)
}
}
}
Expand Down Expand Up @@ -2635,11 +2637,11 @@ internal final class DeprecatedRewardPledgeViewModelTests: TestCase {

self.showAlertMessage.assertValues(["Something went wrong yo."])
self.showAlertShouldDismiss.assertValues([true])
self.dismissViewController.assertValueCount(0)
self.popToRootViewController.assertValueCount(0)

self.vm.inputs.errorAlertTappedOK(shouldDismiss: true)

self.dismissViewController.assertValueCount(1)
self.popToRootViewController.assertValueCount(1)

withEnvironment(
apiService: MockService(updatePledgeResult: Result(failure: errorEmptyMessage)),
Expand All @@ -2660,11 +2662,11 @@ internal final class DeprecatedRewardPledgeViewModelTests: TestCase {
["Something went wrong yo.", Strings.general_error_something_wrong()]
)
self.showAlertShouldDismiss.assertValues([true, true])
self.dismissViewController.assertValueCount(1)
self.popToRootViewController.assertValueCount(1)

self.vm.inputs.errorAlertTappedOK(shouldDismiss: true)

self.dismissViewController.assertValueCount(2)
self.popToRootViewController.assertValueCount(2)
}
}
}
Expand Down Expand Up @@ -2713,11 +2715,11 @@ internal final class DeprecatedRewardPledgeViewModelTests: TestCase {

self.showAlertMessage.assertValues(["Something went wrong yo."])
self.showAlertShouldDismiss.assertValues([true])
self.dismissViewController.assertValueCount(0)
self.popToRootViewController.assertValueCount(0)

self.vm.inputs.errorAlertTappedOK(shouldDismiss: true)

self.dismissViewController.assertValueCount(1)
self.popToRootViewController.assertValueCount(1)

withEnvironment(
apiService: MockService(createPledgeResult: Result(failure: errorEmptyMessage)),
Expand Down Expand Up @@ -2748,11 +2750,11 @@ internal final class DeprecatedRewardPledgeViewModelTests: TestCase {
["Something went wrong yo.", Strings.general_error_something_wrong()]
)
self.showAlertShouldDismiss.assertValues([true, true])
self.dismissViewController.assertValueCount(1)
self.popToRootViewController.assertValueCount(1)

self.vm.inputs.errorAlertTappedOK(shouldDismiss: true)

self.dismissViewController.assertValueCount(2)
self.popToRootViewController.assertValueCount(2)
}
}
}
Expand Down

0 comments on commit b2cb23b

Please sign in to comment.