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

[NT-330] Set initial amount from Backing #863

Merged
merged 6 commits into from Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 15 additions & 1 deletion Library/ViewModels/PledgeAmountViewModel.swift
Expand Up @@ -67,8 +67,16 @@ public final class PledgeAmountViewModel: PledgeAmountViewModelType,
.skipNil()
.map(rounded)

let initialValue = Signal.combineLatest(
project
.map { $0.personalization.backing }
.map(amountFromBacking),
minValue
)
.map { backedAmount, minValue in backedAmount ?? minValue }

let stepperValue = Signal.merge(
minValue,
initialValue,
textFieldInputValue,
self.stepperValueProperty.signal
)
Expand Down Expand Up @@ -207,3 +215,9 @@ public final class PledgeAmountViewModel: PledgeAmountViewModelType,
private func rounded(_ value: Double) -> Double {
return round(value * 100) / 100
}

private func amountFromBacking(_ backing: Backing?) -> Double? {
guard let amount = backing?.amount else { return nil }

return amount - Double(backing?.shippingAmount ?? 0)
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 it's usually better when doing arithmetic using money types to use Decimal:

let shippingAmount: Double = backing?.shippingAmount ?? 0
let pledgeAmount = Decimal(amount) - Decimal(shippingAmount)

return (pledgeAmount as NSDecimalNumber).doubleValue

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm yeah good point - although we seem to have been a little inconsistent with this πŸ€¦β€β™‚

https://github.com/kickstarter/ios-oss/blob/master/Library/ViewModels/BackingViewModel.swift#L176

But I'll change that here!

}
23 changes: 23 additions & 0 deletions Library/ViewModels/PledgeAmountViewModelTests.swift
Expand Up @@ -43,6 +43,29 @@ internal final class PledgeAmountViewModelTests: TestCase {
self.vm.outputs.textFieldValue.observe(self.textFieldValue.observer)
}

func testAmountCurrencyAndStepper_FromBacking() {
let project = Project.template
|> Project.lens.personalization.isBacking .~ true
|> Project.lens.personalization.backing .~ (
.template
|> Backing.lens.reward .~ Reward.postcards
|> Backing.lens.rewardId .~ Reward.postcards.id
|> Backing.lens.shippingAmount .~ 10
|> Backing.lens.amount .~ 700
)

self.vm.inputs.configureWith(project: project, reward: Reward.postcards)

self.amountIsValid.assertValues([true])
self.amountValue.assertValues([690])
self.currency.assertValues(["$"])
self.stepperMinValue.assertValue(PledgeAmountStepperConstants.min)
self.stepperMaxValue.assertValue(PledgeAmountStepperConstants.max)
self.stepperStepValue.assertValue(6)
self.stepperValue.assertValues([6, 690])
self.textFieldValue.assertValues(["690"])
}

func testAmountCurrencyAndStepper_NoReward() {
self.vm.inputs.configureWith(project: .template, reward: Reward.noReward)

Expand Down