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-1297] Add bonus support #1218

Merged
merged 13 commits into from
Jun 12, 2020
6 changes: 4 additions & 2 deletions Kickstarter-iOS/DataSources/ManagePledgeDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ internal final class ManagePledgeDataSource: ValueCellDataSource {
internal func load(project: Project, rewards: [Reward]) {
self.clearValues()

let values = rewards.map { reward in (project, reward) }
let values = rewards.map { reward -> RewardCardViewData in
(project, reward, .manage)
}

self.set(
values: values,
Expand All @@ -23,7 +25,7 @@ internal final class ManagePledgeDataSource: ValueCellDataSource {

internal override func configureCell(tableCell cell: UITableViewCell, withValue value: Any) {
switch (cell, value) {
case let (cell as RewardTableViewCell, value as (Project, Reward)):
case let (cell as RewardTableViewCell, value as RewardCardViewData):
cell.configureWith(value: value)
default:
assertionFailure("Unrecognized combo: \(cell), \(value).")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Prelude
import UIKit

final class RewardsCollectionViewDataSource: ValueCellDataSource {
func load(_ values: [(Project, Either<Reward, Backing>)]) {
func load(_ values: [RewardCardViewData]) {
self.set(
values: values,
cellClass: RewardCell.self,
Expand All @@ -15,7 +15,7 @@ final class RewardsCollectionViewDataSource: ValueCellDataSource {

override func configureCell(collectionCell cell: UICollectionViewCell, withValue value: Any) {
switch (cell, value) {
case let (cell as RewardCell, value as (Project, Either<Reward, Backing>)):
case let (cell as RewardCell, value as RewardCardViewData):
cell.configureWith(value: value)
default:
assertionFailure("Unrecognized (cell, value) combo.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@testable import Kickstarter_Framework
@testable import KsApi
import Library
import Prelude
import XCTest

Expand All @@ -12,7 +13,7 @@ final class RewardsCollectionViewDataSourceTests: XCTestCase {

func testLoadRewards() {
let project = Project.cosmicSurgery
let rewardsData = project.rewards.map { (project, Either<Reward, Backing>.left($0)) }
let rewardsData = project.rewards.map { reward -> RewardCardViewData in (project, reward, .pledge) }

self.dataSource.load(rewardsData)

Expand Down
2 changes: 1 addition & 1 deletion Kickstarter-iOS/Views/Cells/RewardCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ final class RewardCell: UICollectionViewCell, ValueCell {
|> scrollViewStyle
}

internal func configureWith(value: (project: Project, reward: Either<Reward, Backing>)) {
internal func configureWith(value: RewardCardViewData) {
self.rewardCardContainerView.configure(with: value)
}

Expand Down
4 changes: 2 additions & 2 deletions Kickstarter-iOS/Views/Cells/RewardTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ final class RewardTableViewCell: UITableViewCell, ValueCell {
|> containerViewStyle
}

internal func configureWith(value: (project: Project, reward: Reward)) {
self.rewardCardView.configure(with: (value.project, .left(value.reward)))
internal func configureWith(value: RewardCardViewData) {
self.rewardCardView.configure(with: value)

self.contentView.setNeedsLayout()
self.contentView.layoutIfNeeded()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,5 +228,5 @@ private let totalLabelStyle: LabelStyle = { label in
|> \.textColor .~ UIColor.black
|> \.font .~ UIFont.ksr_subhead().bolded
|> \.adjustsFontForContentSizeCategory .~ true
|> \.text %~ { _ in Strings.Total_amount() }
|> \.text %~ { _ in Strings.Total() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ final class ManagePledgeViewController: UIViewController, MessageBannerViewContr
}()

private lazy var pledgeDetailsSectionViews = {
[pledgeDetailsSectionLabel, self.rewardReceivedViewController.view, self.pledgeDisclaimerView]
[self.pledgeDetailsSectionLabel, self.rewardReceivedViewController.view, self.pledgeDisclaimerView]
}()

private lazy var pledgeDisclaimerView: PledgeDisclaimerView = {
Expand Down Expand Up @@ -187,6 +187,7 @@ final class ManagePledgeViewController: UIViewController, MessageBannerViewContr
override func bindViewModel() {
super.bindViewModel()

self.pledgeDetailsSectionLabel.rac.text = self.viewModel.outputs.pledgeDetailsSectionLabelText
self.pledgeDisclaimerView.rac.hidden = self.viewModel.outputs.pledgeDisclaimerViewHidden
self.rewardReceivedViewController.view.rac.hidden =
self.viewModel.outputs.rewardReceivedViewControllerViewIsHidden
Expand Down Expand Up @@ -223,10 +224,10 @@ final class ManagePledgeViewController: UIViewController, MessageBannerViewContr
self?.pledgeSummaryViewController.configureWith(data)
}

self.viewModel.outputs.configureRewardReceivedWithProject
self.viewModel.outputs.configureRewardReceivedWithData
.observeForControllerAction()
.observeValues { [weak self] project in
self?.rewardReceivedViewController.configureWith(project: project)
.observeValues { [weak self] data in
self?.rewardReceivedViewController.configureWith(data: data)
}

self.viewModel.outputs.loadProjectAndRewardsIntoDataSource
Expand All @@ -235,6 +236,7 @@ final class ManagePledgeViewController: UIViewController, MessageBannerViewContr
self?.dataSource.load(project: project, rewards: rewards)
self?.configureHeaderView()
self?.tableView.reloadData()
self?.tableView.layoutIfNeeded()
Copy link
Contributor

Choose a reason for hiding this comment

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

πŸ˜…

}

self.viewModel.outputs.loadPullToRefreshHeaderView
Expand Down Expand Up @@ -586,7 +588,6 @@ extension ManagePledgeViewController: ManagePledgePaymentMethodViewDelegate {
private let pledgeDetailsSectionLabelStyle: LabelStyle = { label in
label
|> checkoutTitleLabelStyle
|> \.text %~ { _ in Strings.Selected_reward() } // FIXME: context-specific
}

extension ManagePledgeViewController: MessageDialogViewControllerDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ final class ManagePledgeViewControllerTests: TestCase {
let reward = Reward.template
|> Reward.lens.shipping.enabled .~ true
let backedProject = Project.cosmicSurgery
|> Project.lens.personalization.backing .~ (
.template
|> Backing.lens.reward .~ reward
|> Backing.lens.rewardId .~ reward.id
)
|> \.rewards .~ [reward]

let envelope = ManagePledgeViewBackingEnvelope.template
Expand Down Expand Up @@ -83,6 +88,11 @@ final class ManagePledgeViewControllerTests: TestCase {
|> Reward.lens.shipping.enabled .~ true
let backedProject = Project.cosmicSurgery
|> Project.lens.creator.id .~ 1
|> Project.lens.personalization.backing .~ (
.template
|> Backing.lens.reward .~ reward
|> Backing.lens.rewardId .~ reward.id
)
|> \.rewards .~ [reward]

let envelope = ManagePledgeViewBackingEnvelope.template
Expand Down Expand Up @@ -134,6 +144,11 @@ final class ManagePledgeViewControllerTests: TestCase {
let reward = Reward.noReward

let backedProject = Project.cosmicSurgery
|> Project.lens.personalization.backing .~ (
.template
|> Backing.lens.reward .~ reward
|> Backing.lens.rewardId .~ reward.id
)
|> \.rewards .~ [reward]

let envelope = ManagePledgeViewBackingEnvelope.template
Expand Down Expand Up @@ -190,6 +205,11 @@ final class ManagePledgeViewControllerTests: TestCase {
|> Reward.lens.shipping.enabled .~ true

let backedProject = Project.cosmicSurgery
|> Project.lens.personalization.backing .~ (
.template
|> Backing.lens.reward .~ reward
|> Backing.lens.rewardId .~ reward.id
)
|> \.rewards .~ [reward]

let envelope = ManagePledgeViewBackingEnvelope.template
Expand Down Expand Up @@ -244,6 +264,11 @@ final class ManagePledgeViewControllerTests: TestCase {
|> Reward.lens.shipping.enabled .~ true

let backedProject = Project.cosmicSurgery
|> Project.lens.personalization.backing .~ (
.template
|> Backing.lens.reward .~ reward
|> Backing.lens.rewardId .~ reward.id
)
|> \.rewards .~ [reward]

let envelope = ManagePledgeViewBackingEnvelope.template
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,45 @@ import Library
import Prelude
import UIKit

final class ManageViewPledgeRewardReceivedViewController: ToggleViewController {
final class ManageViewPledgeRewardReceivedViewController: UIViewController {
// MARK: - Properties

private let viewModel: ManageViewPledgeRewardReceivedViewModelType
= ManageViewPledgeRewardReceivedViewModel()

private lazy var rootStackView: UIStackView = { UIStackView(frame: .zero) }()
private lazy var titleLabel: UILabel = { UILabel(frame: .zero) }()
private lazy var toggleViewController: ToggleViewController = {
ToggleViewController(nibName: nil, bundle: nil)
}()

// MARK: - Lifecycle

override func viewDidLoad() {
super.viewDidLoad()

self.toggle.addTarget(
self.toggleViewController.toggle.addTarget(
self,
action: #selector(ManageViewPledgeRewardReceivedViewController.toggleValueDidChange(_:)),
for: .valueChanged
)

self.configureViews()

self.viewModel.inputs.viewDidLoad()
}

// MARK: - Views

private func configureViews() {
_ = (self.rootStackView, self.view)
|> ksr_addSubviewToParent()
|> ksr_constrainViewToEdgesInParent()

_ = ([self.titleLabel, self.toggleViewController.view], self.rootStackView)
|> ksr_addArrangedSubviewsToStackView()
}

// MARK: - Actions

@objc private func toggleValueDidChange(_ toggle: UISwitch) {
Expand All @@ -31,20 +50,31 @@ final class ManageViewPledgeRewardReceivedViewController: ToggleViewController {

// MARK: - Configuration

public func configureWith(project: Project) {
self.viewModel.inputs.configureWith(project)
public func configureWith(data: ManageViewPledgeRewardReceivedViewData) {
self.viewModel.inputs.configureWith(data)
}

// MARK: - Styles

override func bindStyles() {
super.bindStyles()

_ = self.titleLabel
_ = self.view
|> \.layer.borderColor .~ UIColor.ksr_grey_500.cgColor

_ = self.rootStackView
|> \.isLayoutMarginsRelativeArrangement .~ true
|> \.axis .~ .vertical
|> \.spacing .~ Styles.grid(1)
|> \.insetsLayoutMarginsFromSafeArea .~ false

_ = self.toggleViewController.titleLabel
|> checkoutTitleLabelStyle
|> \.font .~ UIFont.ksr_subhead()
|> \.textColor .~ .ksr_text_black
|> \.text %~ { _ in Strings.Reward_received() }

_ = self.toggle
_ = self.toggleViewController.toggle
|> checkoutSwitchControlStyle
|> \.accessibilityLabel %~ { _ in Strings.Reward_received() }
}
Expand All @@ -54,6 +84,28 @@ final class ManageViewPledgeRewardReceivedViewController: ToggleViewController {
override func bindViewModel() {
super.bindViewModel()

self.toggle.rac.on = self.viewModel.outputs.rewardReceived
self.titleLabel.rac.attributedText = self.viewModel.outputs.estimatedDeliveryDateLabelAttributedText
self.toggleViewController.toggle.rac.on = self.viewModel.outputs.rewardReceived
self.toggleViewController.view.rac.hidden = self.viewModel.outputs.rewardReceivedHidden

self.viewModel.outputs.marginWidth
.observeForUI()
.observeValues { [weak self] borderWidth in
self?.view.layer.borderWidth = borderWidth
}

self.viewModel.outputs.layoutMargins
.observeForUI()
.observeValues { [weak self] layoutMargins in
self?.rootStackView.layoutMargins = layoutMargins
}

self.viewModel.outputs.cornerRadius
.observeForUI()
.observeValues { [weak self] radius in
guard let self = self else { return }
_ = self.view
|> roundedStyle(cornerRadius: radius)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,22 @@ final class ManageViewPledgeRewardReceivedViewControllerTests: TestCase {
}

func testView_Toggle_Off() {
let data = ManageViewPledgeRewardReceivedViewData(
project: .template,
backerCompleted: false,
estimatedDeliveryOn: 1_475_361_315,
backingState: .collected
)

let devices = [Device.phone4_7inch, Device.phone5_8inch, Device.pad]
combos(Language.allLanguages, devices).forEach { language, device in
withEnvironment(language: language) {
let controller = ManageViewPledgeRewardReceivedViewController.instantiate()
controller.configureWith(project: .template)
controller.configureWith(data: data)

let (parent, _) = traitControllers(device: device, orientation: .portrait, child: controller)

parent.view.frame.size.height = 60
parent.view.frame.size.height = 80

FBSnapshotVerifyView(
parent.view, identifier: "lang_\(language)_device_\(device)"
Expand All @@ -38,18 +45,22 @@ final class ManageViewPledgeRewardReceivedViewControllerTests: TestCase {
}

func testView_Toggle_On() {
let data = ManageViewPledgeRewardReceivedViewData(
project: .template,
backerCompleted: false,
estimatedDeliveryOn: 1_475_361_315,
backingState: .collected
)

let devices = [Device.phone4_7inch, Device.phone5_8inch, Device.pad]
combos(Language.allLanguages, devices).forEach { language, device in
withEnvironment(language: language) {
let project = Project.template
|> Project.lens.personalization .. Project.Personalization.lens.backing .~ .template

let controller = ManageViewPledgeRewardReceivedViewController.instantiate()
controller.configureWith(project: project)
controller.configureWith(data: data)

let (parent, _) = traitControllers(device: device, orientation: .portrait, child: controller)

parent.view.frame.size.height = 60
parent.view.frame.size.height = 80

FBSnapshotVerifyView(
parent.view, identifier: "lang_\(language)_device_\(device)"
Expand Down
Loading