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

Feature/add controller lce states #6

Merged
merged 18 commits into from
Nov 7, 2017
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Updated product variants selection
  • Loading branch information
AntonoffEvgeniy committed Nov 6, 2017
commit 5dda9e945940a45d9fc36186c3baa0f92b563131
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@ class ProductDetailsViewController: UIViewController, ImagesCarouselViewControll
var selectedOptions = [SelectedOption]()
var detailImagesController: ImagesCarouselViewController?
var showingImageIndex = 0
var selectedVariant: ProductVariant?

// MARK: - life cycle
override func viewDidLoad() {
super.viewDidLoad()

setupData()
setupViews()
populateViews()
loadRemoteData()
}

// MARK: - setup
func setupData() {
selectedVariant = product?.variants?.first
if selectedOptions.count == 0 {
setupSelectedOptions()
}
Expand Down Expand Up @@ -80,15 +81,21 @@ class ProductDetailsViewController: UIViewController, ImagesCarouselViewControll
descriptionLabel.text = product.productDescription
}

private func updateOptionsViews() {
populatePrice()
populateAddToCartButton()
populateOptionsView()
}

private func populatePrice() {
// priceLabel.text = "\(product?.variantBySelectedOptions?.price ?? String()) \(product?.currency ?? String())"
// priceLabel.isHidden = product?.variantBySelectedOptions == nil
priceLabel.text = "\(selectedVariant?.price ?? String()) \(product?.currency ?? String())"
priceLabel.isHidden = selectedVariant?.available == nil
}

private func populateAddToCartButton() {
// let enabled = product?.variantBySelectedOptions != nil
// addToCartButton.backgroundColor = enabled ? UIColor.blue : UIColor.lightGray
// addToCartButton.isEnabled = enabled
let enabled = selectedVariant != nil
addToCartButton.backgroundColor = enabled ? UIColor.blue : UIColor.lightGray
addToCartButton.isEnabled = enabled
}

private func populateOptionsView() {
Expand Down Expand Up @@ -139,6 +146,24 @@ class ProductDetailsViewController: UIViewController, ImagesCarouselViewControll
if let index = selectedOptionsNames.index(of: name) {
selectedOptions[index].value = value
}
loadRemoteData()
if let variants = product?.variants {
findVariant(variants: variants)
}
}

private func findVariant(variants: [ProductVariant]) {
let selectedOptionsNames = selectedOptions.map({ $0.name })
let selectedOptionsNValues = selectedOptions.map({ $0.value })

for variant in variants {
let variantNames = variant.selectedOptions?.map({ $0.name }) ?? [String()]
let variantValues = variant.selectedOptions?.map({ $0.value }) ?? [String()]

if selectedOptionsNames == variantNames && selectedOptionsNValues == variantValues {
selectedVariant = variant
updateOptionsViews()
break
}
}
}
}