Created
July 24, 2019 21:43
-
-
Save badrinathvm/c9c3d4b45adb1ca279a36d807bacb955 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CreditCardListViewController.swift | |
// BUPNative | |
// | |
// Created by Venkatnarayansetty, Badarinath on 7/24/19. | |
// | |
import Foundation | |
import UIKit | |
public class CreditCardViewController: UIViewController { | |
fileprivate let reuseIdentifier = "CreditCardCell" | |
fileprivate var dataSource:CollectionViewDataSource<CreditCard>? | |
lazy var collectionView:UICollectionView = { | |
let layout = UICollectionViewFlowLayout() | |
layout.itemSize = UICollectionViewFlowLayout.automaticSize | |
layout.estimatedItemSize = CGSize(width: 343, height: 100) | |
var collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) | |
collectionView.translatesAutoresizingMaskIntoConstraints = false | |
collectionView.register(CreditCardCell.self, forCellWithReuseIdentifier: reuseIdentifier) | |
collectionView.delegate = self | |
return collectionView | |
}() | |
public override func viewDidLoad() { | |
super.viewDidLoad() | |
self.view.backgroundColor = UIColor.white | |
setupCollectionView() | |
} | |
func setupCollectionView() { | |
self.view.addSubview(self.collectionView) | |
self.collectionView.backgroundColor = UIColor.white | |
self.collectionView.layout { | |
$0.top == self.view.safeTopAnchor | |
$0.leading == self.view.safeleadingAnchor + 16 | |
$0.trailing == self.view.safeTrailingAnchor - 16 | |
$0.bottom == self.view.safeBottomAnchor | |
} | |
// let collectionViewLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout | |
// collectionViewLayout.itemSize = UICollectionViewFlowLayout.automaticSize | |
// collectionViewLayout.estimatedItemSize = CGSize(width: 343, height: 100) | |
self.dataSource = CollectionViewDataSource.make(for: [CreditCard(name: "Blue Cash Preferred® Card from American Express"),CreditCard(name: "Blue Cash Everyday® Card from American Express") ], reuseIdentifier: self.reuseIdentifier) | |
self.collectionView.dataSource = self.dataSource | |
self.collectionView.reloadData() | |
} | |
} | |
struct CreditCard { | |
var name:String | |
} | |
extension CollectionViewDataSource where Model == CreditCard { | |
static func make(for cards:[Model], reuseIdentifier: String ) -> CollectionViewDataSource { | |
let dataSource = CollectionViewDataSource(models: cards, reuseIdentifier: reuseIdentifier) { (creditCard, cell) in | |
let creditCardCell = cell as! CreditCardCell | |
creditCardCell.creditCardName.text = creditCard.name | |
} | |
return dataSource | |
} | |
} | |
extension AmericanExpressCreditCardViewController: UICollectionViewDelegateFlowLayout { | |
// public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | |
// let referenceHeight: CGFloat = 200 | |
// return CGSize(width: self.collectionView.bounds.width, height: referenceHeight) | |
// } | |
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { | |
return UIEdgeInsets(top: 24, left: 16, bottom: 0, right: 16) | |
} | |
} | |
class CreditCardCell: UICollectionViewCell { | |
override init(frame: CGRect) { | |
super.init(frame: .zero) | |
setupCell() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
let creditCardName: CardPartTextView = { | |
let cardName = CardPartTextView(type: .title) | |
cardName.translatesAutoresizingMaskIntoConstraints = false | |
cardName.font = UIFont.mintGenericProFontBold(.transactionDollar) | |
cardName.textAlignment = NSTextAlignment.center | |
cardName.textColor = UIColor.MintBlackColor | |
return cardName | |
}() | |
let specialOfferView: SpecialOfferView = { | |
let offerView = SpecialOfferView() | |
offerView.layer.cornerRadius = 4.0 | |
offerView.translatesAutoresizingMaskIntoConstraints = false | |
offerView.backgroundColor = UIColor.specialOfferGreenBackground | |
return offerView | |
}() | |
fileprivate func configureCornerAspectsForCell() { | |
self.layer.cornerRadius = 4.0 | |
self.layer.borderColor = UIColor.MintLightGrayColor.cgColor | |
self.layer.borderWidth = 2.0 | |
} | |
func setupCell() { | |
configureCornerAspectsForCell() | |
[creditCardName, specialOfferView].forEach { | |
self.addSubview($0) | |
} | |
creditCardName.layout { | |
$0.top == self.topAnchor + 24 | |
$0.leading == self.leadingAnchor + 24 | |
$0.trailing == self.trailingAnchor - 24 | |
} | |
specialOfferView.layout { | |
$0.top == self.creditCardName.bottomAnchor + 16 | |
$0.centerX == self.centerXAnchor | |
} | |
NSLayoutConstraint.activate([ | |
specialOfferView.heightAnchor.constraint(equalToConstant: 28), | |
]) | |
} | |
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes { | |
setNeedsLayout() | |
layoutIfNeeded() | |
let size = contentView.systemLayoutSizeFitting(layoutAttributes.size) | |
var newFrame = layoutAttributes.frame | |
newFrame.size.height = ceil(size.height) | |
layoutAttributes.frame = newFrame | |
return layoutAttributes | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment