GithubHelp home page GithubHelp logo

intuit / cardparts Goto Github PK

View Code? Open in Web Editor NEW
2.5K 48.0 224.0 16.35 MB

A reactive, card-based UI framework built on UIKit for iOS developers.

License: Other

Ruby 0.39% Swift 99.61%
swift ios cardparts cards uikit ui

cardparts's Introduction

Mint Logo

Build Status Version License Platform

MintSights by CardParts CardParts in Mint CardParts in Turbo

CardParts - made with ❤️ by Intuit:

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

In ViewController.swift you will be able to change the cards displayed and/or their order by commenting out one of the loadCards(cards: ) functions. If you want to change the content of any of these cards, you can look into each of the CardPartsViewController you pass into the function such as: TestCardController, Thing1CardController, Thing2CardController, etc.

Requirements

  • iOS 10.0+
  • Xcode 10.2+
  • Swift 5.0+
  • CocoaPods 1.6.1+

Installation

CardParts is available through CocoaPods. You can install it with the following command:

$ gem install cocoapods

To add CardParts to your project, simply add the following line to your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'CardParts'
end

Then, run the following command:

$ pod install

Communication and Contribution

  • If you need help, open an issue and tag as help wanted.
  • If you found a bug, open an issue and tag as bug.
  • If you have a feature request, open an issue and tag as feature.
  • If you want to contribute, submit a pull request.
    • In order to submit a pull request, please fork this repo and submit a PR from your forked repo.
    • Have a detailed message as to what your PR fixes/enhances/adds.
    • Each PR must get two approvals from our team before we will merge.

Overview

CardParts is the second generation Card UI framework for the iOS Mint application. This version includes many updates to the original card part framework, including improved MVVM, data binding (via RxSwift), use of stack views and self sizing collection views instead sizing cells, 100% swift and much more. The result is a much simpler, easier to use, more powerful, and easier to maintain framework. This framework is currently used by the iOS Mint application and the iOS Turbo application.

CardPart Example in Mint

Quick Start

See how quickly you can get a card displayed on the screen while adhering to the MVVM design pattern:

import RxCocoa

class MyCardsViewController: CardsViewController {

    let cards: [CardController] = [TestCardController()]

    override func viewDidLoad() {
        super.viewDidLoad()

        loadCards(cards: cards)
    }
}

class TestCardController: CardPartsViewController  {

    var viewModel = TestViewModel()
    var titlePart = CardPartTitleView(type: .titleOnly)
    var textPart = CardPartTextView(type: .normal)

    override func viewDidLoad() {
        super.viewDidLoad()

        viewModel.title.asObservable().bind(to: titlePart.rx.title).disposed(by: bag)
        viewModel.text.asObservable().bind(to: textPart.rx.text).disposed(by: bag)

        setupCardParts([titlePart, textPart])
    }
}

class TestViewModel {

    var title = BehaviorRelay(value: "")
    var text = BehaviorRelay(value: "")

    init() {

        // When these values change, the UI in the TestCardController
        // will automatically update
        title.accept("Hello, world!")
        text.accept("CardParts is awesome!")
    }
}

Note: RxCocoa is required for BehaviorRelay, thus you must import it wherever you may find yourself using it.

Architecture

There are two major parts to the card parts framework. The first is the CardsViewController which will display the cards. It is responsible for displaying cards in the proper order and managing the lifetime of the cards. The second major component is the cards themselves which are typically instances of CardPartsViewController. Each instance of CardPartsViewController displays the content of a single card, using one or more card parts (more details later).

CardsViewController

The CardsViewController uses a collection view where each cell is a single card. The cells will render the frames for the cards, but are designed to have a child ViewController that will display the contents of the card. Thus CardsViewController is essentially a list of child view controllers that are rendered in special cells that draw the card frames.

To use a CardsViewController, you first need to subclass it. Then in the viewDidLoad method call the super class loadCards method passing an array of CardControllers. Each instance of a CardController will be rendered as a single card. The loadCards method does this by getting the view controller for each CardController and adding them as child view controllers to the card cells. Here is an example:

class TestCardsViewController: CardsViewController {

    let cards: [CardController] = [TestCardController(), AnotherTestCardController()]

    override func viewDidLoad() {
        super.viewDidLoad()

        loadCards(cards: cards)
    }
}

Each card must implement the CardController protocol (note that CardPartsViewController discussed later implements this protocol already). The CardController protocol has a single method:

protocol CardController : NSObjectProtocol {

    func viewController() -> UIViewController

}

The viewController() method must return the viewController that will be added as a child controller to the card cell. If the CardController is a UIViewController it can simply return self for this method.

Load specific cards

While normally you may call loadCards(cards:) to load an array of CardControllers, you may want the ability to load reload a specific set of cards. We offer the ability via the loadSpecificCards(cards: [CardController] , indexPaths: [IndexPath]) API. Simply pass in the full array of new cards as well as the indexPaths that you would like reloaded.

Custom Card Margins

By default, the margins of your CardsViewController will match the theme's cardCellMargins property. You can change the margins for all CardsViewControllers in your application by applying a new theme or setting CardParts.theme.cardCellMargins = UIEdgeInsets(...). Alternatively, if you want to change the margins for just one CardsViewController, you can set the cardCellMargins property of that CardsViewController. To change the margin for an individual card see CustomMarginCardTrait. This property will default to use the theme's margins if you do not specify a new value for it. Changing this value should be done in the init of your custom CardsViewController, but must occur after super.init because it is changing a property of the super class. For example:

class MyCardsViewController: CardsViewController {

	init() {
		// set up properties
		super.init(nibName: nil, bundle: nil)
		self.cardCellMargins = UIEdgeInsets(/* custom card margins */)
	}

	...
}

If you use storyboards with CardsViewController subclasses in your storyboard, the cardCellMargins property will take the value of the CardParts.theme.cardCellMargins when the required init(coder:) initializer is called. If you are trying to change the theme for your whole application, you will need to do so in this initializer of the first view controller in your storyboard to be initialized, and changes will take effect in all other view controllers. For example:

required init?(coder: NSCoder) {
	YourCardPartTheme().apply()
	super.init(coder: coder)
}

Card Traits

The Card Parts framework defines a set of traits that can be used to modify the appearance and behavior of cards. These traits are implemented as protocols and protocol extensions. To add a trait to a card simply add the trait protocol to the CardController definition. For example:

class MyCard: UIViewController, CardController, TransparentCardTrait {

}

MyCard will now render itself with a transparent card background and frame. No extra code is needed, just adding the TransparentCardTrait as a protocol is all that is necessary.

Most traits require no extra code. The default protocol extensions implemented by the framework implement all the code required for the trait to modify the card. A few traits do require implementing a function or property. See the documentation for each trait below for more information.

NoTopBottomMarginsCardTrait

By default each card has margin at the top and bottom of the card frame. Adding the NoTopBottomMarginsCardTrait trait will remove that margin allowing the card to render to use the entire space inside the card frame.

TransparentCardTrait

Each card is rendered with a frame that draws the border around the card. Adding TransparentCardTrait will not display that border allowing the card to render without a frame.

EditableCardTrait

If the EditableCardTrait trait is added, the card will be rendered with an edit button in upper right of the card. When user taps in the edit button, the framework will call the cards onEditButtonTap() method. The EditableCardTrait protocol requires the CardController to implement the onEditButtonTap() method.

HiddenCardTrait

The HiddenCardTrait trait requires the CardController to implement an isHidden variable:

    var isHidden: BehaviorRelay<Bool> { get }

The framework will then observe the isHidden variable so that whenever its value is changed the card will be hidden or shown based upon the new value. This allows the CardController to control its visibility by simply modifying the value of its isHidden variable.

ShadowCardTrait

The ShadowCardTrait protocol requires CardController to implement shadowColor(), shadowRadius(), shadowOpacity() and shadowOffset() methods.

    func shadowColor() -> CGColor {
        return UIColor.lightGray.cgColor
    }

    func shadowRadius() -> CGFloat {
        return 10.0
    }

    // The value can be from 0.0 to 1.0.
    // 0.0 => lighter shadow
    // 1.0 => darker shadow
    func shadowOpacity() -> Float {
        return 1.0
    }

    func shadowOffset() -> CGSize {
    	return CGSize(width: 0, height: 5)
    }

shadowColor: lightGray, shadowRadius: 5.0, shadowOpacity: 0.5
Shadow radius 5.0

shadowColor: lightGray, shadowRadius: 10.0, shadowOpacity: 0.5
Shadow radius 10.0

RoundedCardTrait

Use this protocol to define the roundness for the card by implementing the method cornerRadius().

    func cornerRadius() -> CGFloat {
        return 10.0
    }

cornerRadius: 10.0
Shadow radius 5.0

GradientCardTrait

Use this protocol to add a gradient background for the card. The gradients will be added vertically from top to bottom. Optionally you can apply an angle to the gradient. Angles are defined in degrees, any negative or positive degree value is valid.

    func gradientColors() -> [UIColor] {
        return [UIColor.lavender, UIColor.aqua]
    }

    func gradientAngle() -> Float {
        return 45.0
    }

Shadow radius 10.0

BorderCardTrait

Use this protocol to add border color and border width for the card, implement borderWidth(), and borderColor() methods.

    func borderWidth() -> CGFloat {
        return 2.0
    }

    func borderColor() -> CGColor {
        return UIColor.darkGray.cgColor
    }

border

CustomMarginCardTrait

Use this protocol to specifiy a custom margin for the card, implement customMargin() method. Value returned will be used for left and right margins thus centering the card in the superview.

    func customMargin() -> CGFloat {
        return 42.0
    }

CardPartsViewController

CardPartsViewController implements the CardController protocol and builds its card UI by displaying one or more card part views using an MVVM pattern that includes automatic data binding. Each CardPartsViewController displays a list of CardPartView as its subviews. Each CardPartView renders as a row in the card. The CardParts framework implements several different types of CardPartView that display basic views, such as title, text, image, button, separator, etc. All CardPartView implemented by the framework are already styled to correctly match the applied s UI guidelines.

In addition to the card parts, a CardPartsViewController also uses a view model to expose data properties that are bound to the card parts. The view model should contain all the business logic for the card, thus keeping the role of the CardPartsViewController to just creating its view parts and setting up bindings from the view model to the card parts. A simple implementation of a CardPartsViewController based card might look like the following:

class TestCardController: CardPartsViewController  {

    var viewModel = TestViewModel()
    var titlePart = CardPartTitleView(type: .titleOnly)
    var textPart = CardPartTextView(type: .normal)

    override func viewDidLoad() {
        super.viewDidLoad()

        viewModel.title.asObservable().bind(to: titlePart.rx.title).disposed(by: bag)
        viewModel.text.asObservable().bind(to: textPart.rx.text).disposed(by: bag)

        setupCardParts([titlePart, textPart])
    }
}

class TestViewModel {

    var title = BehaviorRelay(value: "")
    var text = BehaviorRelay(value: "")

    init() {

        // When these values change, the UI in the TestCardController
        // will automatically update
        title.accept("Hello, world!")
        text.accept("CardParts is awesome!")
    }
}

The above example creates a card that displays two card parts, a title card part and a text part. The bind calls setup automatic data binding between view model properties and the card part view properties so that whenever the view model properties change, the card part views will automatically update with the correct data.

The call to setupCardParts adds the card part views to the card. It takes an array of CardPartView that specifies which card parts to display, and in what order to display them.

CardPartsFullScreenViewController

This will make the card a full screen view controller. So if you do not want to build with an array of Cards, instead you can make a singular card full-screen.

class TestCardController: CardPartsFullScreenViewController  {
    ...
}

CardParts

The framework includes several predefined card parts that are ready to use. It is also possible to create custom card parts. The following sections list all the predefined card parts and their reactive properties that can be bound to view models.

CardPartTextView

CardPartTextView displays a single text string. The string can wrap to multiple lines. The initializer for CardPartTextView takes a type parameter which can be set to: normal, title, or detail. The type is used to set the default font and textColor for the text.

CardPartTextView exposes the following reactive properties that can be bound to view model properties:

var text: String?
var attributedText: NSAttributedString?
var font: UIFont!
var textColor: UIColor!
var textAlignment: NSTextAlignment
var lineSpacing: CGFloat
var lineHeightMultiple: CGFloat
var alpha: CGFloat
var backgroundColor: UIColor?
var isHidden: Bool
var isUserInteractionEnabled: Bool
var tintColor: UIColor?

CardPartAttributedTextView

CardPartAttributedTextView is comparable to CardPartTextView, but it is built upon UITextView rather than UILabel. This allows for CardPartImageViews to be nested within the CardPartAttrbutedTextView and for text to be wrapped around these nested images. In addition, CardPartAttributedTextView allows for links to be set and opened. CartPartAttributedTextView exposes the following reactive properties that can be bound to view model properties:

var text: String?
var attributedText: NSAttributedString?
var font: UIFont!
var textColor: UIColor!
var textAlignment: NSTextAlignment
var lineSpacing: CGFloat
var lineHeightMultiple: CGFloat
var isEditable: Bool
var dataDetectorTypes: UIDataDetectorTypes
var exclusionPath: [UIBezierPath]?
var linkTextAttributes: [NSAttributedString.Key : Any]
var textViewImage: CardPartImageView?
var isUserInteractionEnabled: Bool
var tintColor: UIColor?

CardPartImageView

CardPartImageView displays a single image. CardPartImageView exposes the following reactive properties that can be bound to view model properties:

var image: UIImage?
var imageName: String?
var alpha: CGFloat
var backgroundColor: UIColor?
var isHidden: Bool
var isUserInteractionEnabled: Bool
var tintColor: UIColor?

CardPartButtonView

CardPartButtonView displays a single button.

CardPartButtonView exposes the following reactive properties that can be bound to view model properties:

var buttonTitle: String?
var isSelected: Bool?
var isHighlighted: Bool?
var contentHorizontalAlignment: UIControlContentHorizontalAlignment
var alpha: CGFloat
var backgroundColor: UIColor?
var isHidden: Bool
var isUserInteractionEnabled: Bool
var tintColor: UIColor?

CardPartTitleView

CardPartTitleView displays a view with a title, and an optional options menu. The initializer requires a type parameter which can be set to either titleOnly or titleWithMenu. If the type is set to titleWithMenu the card part will display a menu icon, that when tapped will display a menu containing the options specified in the menuOptions array. The menuOptionObserver property can be set to a block that will be called when the user selects an item from the menu.

As an example for a title with menu buttons:

let titlePart = CardPartTitleView(type: .titleWithMenu)
titlePart.menuTitle = "Hide this offer"
titlePart.menuOptions = ["Hide"]
titlePart.menuOptionObserver  = {[weak self] (title, index) in
    // Logic to determine which menu option was clicked
    // and how to respond
    if index == 0 {
        self?.hideOfferClicked()
    }
}

CardPartButtonView exposes the following reactive properties that can be bound to view model properties:

var title: String?
var titleFont: UIFont
var titleColor: UIColor
var menuTitle: String?
var menuOptions: [String]?
var menuButtonImageName: String
var alpha: CGFloat
var backgroundColor: UIColor?
var isHidden: Bool
var isUserInteractionEnabled: Bool
var tintColor: UIColor?

CardPartTitleDescriptionView

CardPartTitleDescriptionView allows you to have a left and right title and description label, however, you are able to also choose the alignment of the right title/description labels. See below:

let rightAligned = CardPartTitleDescriptionView(titlePosition: .top, secondaryPosition: .right) // This will be right aligned
let centerAligned = CardPartTitleDescriptionView(titlePosition: .top, secondaryPosition: .center(amount: 0)) // This will be center aligned with an offset of 0.  You may increase that amount param to shift right your desired amount

CardPartPillLabel

CardPartPillLabel provides you the rounded corners, text aligned being at the center along with vertical and horizontal padding capability.

var verticalPadding:CGFloat
var horizontalPadding:CGFloat

pillLabel

See the example app for a working example.

CardPartIconLabel

CardPartIconLabel provides the capability to add images in eithet directions supporting left , right and center text alignments along with icon binding capability.

    let iconLabel = CardPartIconLabel()
    iconLabel.verticalPadding = 10
    iconLabel.horizontalPadding = 10
    iconLabel.backgroundColor = UIColor.blue
    iconLabel.font = UIFont.systemFont(ofSize: 12)
    iconLabel.textColor = UIColor.black
    iconLabel.numberOfLines = 0
    iconLabel.iconPadding = 5
    iconLabel.icon = UIImage(named: "cardIcon")

cardPartIconLabel

CardPartSeparatorView

CardPartSeparatorView displays a separator line. There are no reactive properties define for CardPartSeparatorView.

CardPartVerticalSeparatorView

As the name describes, it shows a vertical separator view opposed to a horizontal one

CardPartStackView

CardPartStackView displays a UIStackView that can contain other card parts, and even other CardPartStackViews. Using CardPartStackView allows for creating custom layouts of card parts. By nesting CardPartStackViews you can create almost any layout.

To add a card part to the stack view call its addArrangedSubview method, specifying the card part's view property as the view to be added to the stack view. For example:

horizStackPart.addArrangedSubview(imagePart)

Also,provides an option to round the corners of the stackview

let roundedStackView = CardPartStackView()
roundedStackView.cornerRadius = 10.0
roundedStackView.pinBackground(roundedStackView.backgroundView, to: roundedStackView)

roundedStackView

There are no reactive properties defined for CardPartStackView. However you can use the default UIStackView properties (distribution, alignment, spacing, and axis) to configure the stack view.

CardPartTableView

CardPartTableView displays a table view as a card part such that all items in the table view are displayed in the card part (i.e. the table view does not scroll). CardPartTableView leverages Bond's reactive data source support allowing a MutableObservableArray to be bound to the table view.

To setup the data source binding the view model class should expose MutableObservableArray property that contains the table view's data. For example:

var listData = MutableObservableArray(["item 1", "item 2", "item 3", "item 4"])

Then in the view controller the data source binding can be setup as follows:

viewModel.listData.bind(to: tableViewPart.tableView) { listData, indexPath, tableView in

    guard let cell = tableView.dequeueReusableCell(withIdentifier: tableViewPart.kDefaultCellId, for: indexPath) as? CardPartTableViewCell else { return UITableViewCell() }

    cell.leftTitleLabel.text = listData[indexPath.row]

    return cell
}

The last parameter to the bind call is block that will be called when the tableview's cellForRowAt data source method is called. The first parameter to the block is the MutableObservableArray being bound to.

CardPartTableView registers a default cell class (CardPartTableViewCell) that can be used with no additional work. CardPartTableViewCell contains 4 labels, a left justified title, left justified description, right justified title, and a right justified description. Each label can be optionally used, if no text is specified in a label the cell's layout code will correctly layout the remaining labels.

It is also possible to register your own custom cells by calling the register method on tableViewPart.tableView.

You also have access to two delegate methods being called by the tableView as follows:

@objc public protocol CardPartTableViewDelegate {
	func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
	@objc optional func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
}

CardPartTableViewCell

CardPartTableViewCell is the default cell registered for CardPartTableView. The cell contains the following properties:

var leftTitleLabel: UILabel
var leftDescriptionLabel: UILabel
var rightTitleLabel: UILabel
var rightDescriptionLabel: UILabel
var rightTopButton: UIButton
var shouldCenterRightLabel = false
var leftTitleFont: UIFont
var leftDescriptionFont: UIFont
var rightTitleFont: UIFont
var rightDescriptionFont: UIFont
var leftTitleColor: UIColor
var leftDescriptionColor: UIColor
var rightTitleColor: UIColor
var rightDescriptionColor: UIColor

CardPartTableViewCardPartsCell

This will give you the ability to create custom tableView cells out of CardParts. The following code allows you to create a cell:

class MyCustomTableViewCell: CardPartTableViewCardPartsCell {

    let bag = DisposeBag()

    let attrHeader1 = CardPartTextView(type: .normal)
    let attrHeader2 = CardPartTextView(type: .normal)
    let attrHeader3 = CardPartTextView(type: .normal)

    override public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        selectionStyle = .none

        setupCardParts([attrHeader1, attrHeader2, attrHeader3])
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setData(_ data: MyCustomStruct) {
        // Do something in here
    }
}

If you do create a custom cell, you must register it to the CardPartTableView:

tableViewCardPart.tableView.register(MyCustomTableViewCell.self, forCellReuseIdentifier: "MyCustomTableViewCell")

And then as normal, you would bind to your viewModel's data:

viewModel.listData.bind(to: tableViewPart.tableView) { tableView, indexPath, data in

    guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyCustomTableViewCell", for: indexPath) as? MyCustomTableViewCell else { return UITableViewCell() }

    cell.setData(data)

    return cell
}

CardPartCollectionView

CardPartCollectionView underlying engine is RxDataSource. You can look at their documentation for a deeper look but here is an overall approach to how it works:

Start by initializing a CardPartCollectionView with a custom UICollectionViewFlowLayout:

lazy var collectionViewCardPart = CardPartCollectionView(collectionViewLayout: collectionViewLayout)
var collectionViewLayout: UICollectionViewFlowLayout = {
    let layout = UICollectionViewFlowLayout()
    layout.minimumInteritemSpacing = 12
    layout.minimumLineSpacing = 12
    layout.scrollDirection = .horizontal
    layout.itemSize = CGSize(width: 96, height: 128)
    return layout
}()

Now say you have a custom struct you want to pass into your CollectionViewCell:

struct MyStruct {
    var title: String
    var description: String
}

You will need to create a new struct to conform to SectionModelType:

struct SectionOfCustomStruct {
    var header: String
    var items: [Item]
}

extension SectionOfCustomStruct: SectionModelType {

    typealias Item = MyStruct

    init(original: SectionOfCustomStruct, items: [Item]) {
        self = original
        self.items = items
    }
}

Next, create a data source that you will bind to you data: Note: You can create a custom CardPartCollectionViewCell as well - see below.

let dataSource = RxCollectionViewSectionedReloadDataSource<SectionOfCustomStruct>(configureCell: {[weak self] (_, collectionView, indexPath, data) -> UICollectionViewCell in

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)

    return cell
})

Finally, bind your viewModel data to the collectionView and its newly created data source:

viewModel.data.asObservable().bind(to: collectionViewCardPart.collectionView.rx.items(dataSource: dataSource)).disposed(by: bag)

Note: viewModel.data will be a reactive array of SectionOfCustomStruct:

typealias ReactiveSection = BehaviorRelay<[SectionOfCustomStruct]>
var data = ReactiveSection(value: [])

CardPartCollectionViewCardPartsCell

Just how CardPartTableViewCell has the ability to create tableView cells out of CardParts - so do CollectionViews. Below is an example of how you may create a custom CardPartCollectionViewCardPartsCell:

class MyCustomCollectionViewCell: CardPartCollectionViewCardPartsCell {
    let bag = DisposeBag()

    let mainSV = CardPartStackView()
    let titleCP = CardPartTextView(type: .title)
    let descriptionCP = CardPartTextView(type: .normal)

    override init(frame: CGRect) {

        super.init(frame: frame)

        mainSV.axis = .vertical
        mainSV.alignment = .center
        mainSV.spacing = 10

        mainSV.addArrangedSubview(titleCP)
        mainSV.addArrangedSubview(descriptionCP)

        setupCardParts([mainSV])
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setData(_ data: MyStruct) {

        titleCP.text = data.title
        descriptionCP.text = data.description
    }
}

To use this, you must register it to the CollectionView during viewDidLoad as follows:

collectionViewCardPart.collectionView.register(MyCustomCollectionViewCell.self, forCellWithReuseIdentifier: "MyCustomCollectionViewCell")

Then, inside your data source, simply dequeue this cell:

let dataSource = RxCollectionViewSectionedReloadDataSource<SectionOfSuggestedAccounts>(configureCell: {[weak self] (_, collectionView, indexPath, data) -> UICollectionViewCell in

    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCustomCollectionViewCell", for: indexPath) as? MyCustomCollectionViewCell else { return UICollectionViewCell() }

    cell.setData(data)

    return cell
})

CardPartBarView

CardPartBarView present a horizontal bar graph that can be filled to a certain percentage of your choice. Both the color of the fill and the percent is reactive

let barView = CardPartBarView()
viewModel.percent.asObservable().bind(to: barView.rx.percent).disposed(by:bag)
viewModel.barColor.asObservable().bind(to: barView.rx.barColor).disposed(by: bag)

CardPartPagedView

This CardPart allows you to create a horizontal paged carousel with page controls. Simply feed it with your desired height and an array of CardPartStackView:

let cardPartPages = CardPartPagedView(withPages: initialPages, andHeight: desiredHeight)
cardPartPages.delegate = self

This CardPart also has a delegate:

func didMoveToPage(page: Int)

Which will fire whenever the user swipes to another page

You also have the abililty to automatically move to a specific page by calling the following function on CardPartPagedView

func moveToPage(_ page: Int)

CardPartSliderView

You can set min and max value as well as bind to the current set amount:

let slider = CardPartSliderView()
slider.minimumValue = sliderViewModel.min
slider.maximumValue = sliderViewModel.max
slider.value = sliderViewModel.defaultAmount
slider.rx.value.asObservable().bind(to: sliderViewModel.amount).disposed(by: bag)

CardPartMultiSliderView

You can set min and max value as well as tint color and outer track color:

let slider = CardPartMultiSliderView()
slider.minimumValue = sliderViewModel.min
slider.maximumValue = sliderViewModel.max
slider.orientation = .horizontal
slider.value = [10, 40]
slider.trackWidth = 8
slider.tintColor = .purple
slider.outerTrackColor = .gray

CardPartSpacerView

Allows you to add a space between card parts in case you need a space larger than the default margin. Initialize it with a specific height:

CardPartSpacerView(height: 30)

CardPartTextField

CardPartTextField can take a parameter of type CardPartTextFieldFormat which determines formatting for the UITextField. You may also set properties such as keyboardType, placeholder, font, text, etc.

let amount = CardPartTextField(format: .phone)
amount.keyboardType = .numberPad
amount.placeholder = textViewModel.placeholder
amount.font = dataFont
amount.textColor = UIColor.colorFromHex(0x3a3f47)
amount.text = textViewModel.text.value
amount.rx.text.orEmpty.bind(to: textViewModel.text).disposed(by: bag)

The different formats are as follows:

public enum CardPartTextFieldFormat {
    case none
    case currency(maxLength: Int)
    case zipcode
    case phone
    case ssn
}

CardPartOrientedView

CardPartOrientedView allows you to create an oriented list view of card part elements. This is similar to the CardPartStackView except that this view can orient elements to the top or bottom of the view. This is advantageous when you are using horizontal stack views and need elements to be oriented differently (top arranged or bottom arranged) relative to the other views in the horizontal stack view. To see a good example of this element please take a look at the example application.

The supported orientations are as follows:

public enum Orientation {
    case top
    case bottom
}

To create an oriented view you can use the following code:

let orientedView = CardPartOrientedView(cardParts: [<elements to list vertically>], orientation: .top)

Add the above orientedView to any list of card parts or an existing stack view to orient your elements to the top or bottom of the enclosing view.

CardPartCenteredView

CardPartCenteredView is a CardPart that fits a centered card part proportionally on the phone screen while allowing a left and right side card part to scale appropriately. To create a centered card part please use the following example:

class TestCardController : CardPartsViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let rightTextCardPart = CardPartTextView(type: .normal)
        rightTextCardPart.text = "Right text in a label"

        let centeredSeparator = CardPartVerticalSeparator()

        let leftTextCardPart = CardPartTextView(type: .normal)
        leftTextCardPart.text = "Left text in a label"

        let centeredCardPart = CardPartCenteredView(leftView: leftTextCardPart, centeredView: centeredSeparator, rightView: rightTextCardPart)

        setupCardParts([centeredCardPart])
    }
}

A CardPartCenteredView can take in any card part that conforms to CardPartView as the left, center, and right components. To see a graphical example of the centered card part please look at the example application packaged with this cocoapod.

CardPartConfettiView

Provides the capability to add confetti with various types ( diamonds, star, mixed ) and colors, along with different level of intensity

    let confettiView = CardPartConfettiView()
    confettiView.type  = .diamond
    confettiView.shape = CAEmitterLayerEmitterShape.line
    confettiView.startConfetti()

Confetti

CardPartProgressBarView

Provides the capability to configure different colors and custom marker , it's position to indicate the progress based on the value provided.

    let progressBarView = CardPartProgressBarView(barValues: barValues, barColors: barColors, marker: nil, markerLabelTitle: "", currentValue: Double(720), showShowBarValues: false)
    progressBarView.barCornerRadius = 4.0

ProgressBarView

CardPartMapView

Provides the capability to display a MapView and reactively configure location, map type, and coordinate span (zoom). You also have direct access to the MKMapView instance so that you can add annotations, hook into it's MKMapViewDelegate, or whatever else you'd normally do with Maps.

By default the card part will be rendered at a height of 300 points but you can set a custom height just be resetting the CardPartMapView.intrensicHeight property.

Here's a small example of how to reactively set the location from a changing address field (See the Example project for a working example):

    let initialLocation = CLLocation(latitude: 37.430489, longitude: -122.096260)
    let cardPartMapView = CardPartMapView(type: .standard, location: initialLocation, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))

    cardPartTextField.rx.text
            .flatMap { self.viewModel.getLocation(from: $0) }
            .bind(to: cardPartMapView.rx.location)
            .disposed(by: bag)

MapView

CardPartRadioButton

Provides the capability to add radio buttons with configurable inner/outer circle line width , colors along with tap etc..

    let radioButton = CardPartRadioButton()
    radioButton.outerCircleColor = UIColor.orange
    radioButton.outerCircleLineWidth = 2.0

    radioButton2.rx.tap.subscribe(onNext: {
        print("Radio Button Tapped")
    }).disposed(by: bag)

RadioButton

CardPartSwitchView

Provides the capability to add a switch with configurable colors.

    let switchComponent = CardPartSwitchView()
    switchComponent.onTintColor = .blue

RadioButton

CardPartHistogramView

Provides the caoability to generate the bar graph based on the data ranges with customizable bars , lines, colors etc..

    let dataEntries = self.generateRandomDataEntries()
    barHistogram.width = 8
    barHistogram.spacing = 8
    barHistogram.histogramLines = HistogramLine.lines(bottom: true, middle: false, top: false)
    self.barHistogram.updateDataEntries(dataEntries: dataEntries, animated: true)

Histogram

CardPartsBottomSheetViewController

CardPartsBottomSheetViewController provides the capability to show a highly-customizable modal bottom sheet. At its simplest, all you need to do is set the contentVC property to a view controller that you create to control the content of the bottom sheet:

    let bottomSheetViewController = CardPartsBottomSheetViewController()
    bottomSheetViewController.contentVC = MyViewController()
    bottomSheetViewController.presentBottomSheet()

bottom sheet

CardPartsBottomSheetViewController also supports being used as a sticky view at the bottom of the screen, and can be presented on any view (default is keyWindow). For example, the following code creates a sticky view that still permits scrolling behind it and can only be dismissed programmatically.

    let bottomSheetViewController = CardPartsBottomSheetViewController()
    bottomSheetViewController.contentVC = MyStickyViewController()
    bottomSheetViewController.configureForStickyMode()
    bottomSheetViewController.addShadow()
    bottomSheetViewController.presentBottomSheet(on: self.view)

sticky bottom sheet

There are also over two dozen other properties that you can set to further customize the bottom sheet for your needs. You can configure the colors, height, gesture recognizers, handle appearance, animation times, and callback functions with the following properties.

  • var contentVC: UIViewController?: View controller for the content of the bottom sheet. Should set this parameter before presenting bottom sheet.
  • var contentHeight: CGFloat?: Manually set a content height. If not set, height will try to be inferred from contentVC.
  • var bottomSheetBackgroundColor: UIColor: Background color of bottom sheet. Default is white.
  • var bottomSheetCornerRadius: CGFloat: Corner radius of bottom sheet. Default is 16.
  • var handleVC: CardPartsBottomSheetHandleViewController: Pill-shaped handle at the top of the bottom sheet. Can configure handleVC.handleHeight, handleVC.handleWidth, and handleVC.handleColor.
  • var handlePosition: BottomSheetHandlePosition: Positioning of handle relative to bottom sheet. Options are .above(bottomPadding), .inside(topPadding), .none. Default is above with padding 8.
  • var overlayColor: UIColor: Color of the background overlay. Default is black.
  • var shouldIncludeOverlay: Bool: Whether or not to include a background overlay. Default is true.
  • var overlayMaxAlpha: CGFloat: Maximum alpha value of background overlay. Will fade to 0 proportionally with height as bottom sheet is dragged down. Default is 0.5.
  • var dragHeightRatioToDismiss: CGFloat: Ratio of how how far down user must have dragged bottom sheet before releasing it in order to trigger a dismissal. Default is 0.4.
  • var dragVelocityToDismiss: CGFloat: Velocity that must be exceeded in order to dismiss bottom sheet if height ratio is greater than dragHeightRatioToDismiss. Default is 250.
  • var pullUpResistance: CGFloat: Amount that the bottom sheet resists being dragged up. Default 5 means that for every 5 pixels the user drags up, the bottom sheet goes up 1 pixel.
  • var appearAnimationDuration: TimeInterval: Animation time for bottom sheet to appear. Default is 0.5.
  • var dismissAnimationDuration: TimeInterval: Animation time for bottom sheet to dismiss. Default is 0.5.
  • var snapBackAnimationDuration: TimeInterval: Animation time for bottom sheet to snap back to its height. Default is 0.25.
  • var animationOptions: UIView.AnimationOptions: Animation options for bottom sheet animations. Default is UIView.AnimationOptions.curveEaseIn.
  • var changeHeightAnimationDuration: TimeInterval: Animation time for bottom sheet to adjust to a new height when height is changed. Default is 0.25.
  • var shouldListenToOverlayTap: Bool: Whether or not to dismiss if a user taps in the overlay. Default is true.
  • var shouldListenToHandleDrag: Bool: Whether or not to respond to dragging on the handle. Default is true.
  • var shouldListenToContentDrag: Bool: Whether or not to respond to dragging in the content. Default is true.
  • var shouldListenToContainerDrag: Bool: Whether or not to respond to dragging in the container. Default is true.
  • var shouldRequireVerticalDrag: Bool: Whether or not to require a drag to start in the vertical direction. Default is true.
  • var adjustsForSafeAreaBottomInset: Bool: Boolean value for whether or not bottom sheet should automatically add to its height to account for bottom safe area inset. Default is true.
  • var didShow: (() -> Void)?: Callback function to be called when bottom sheet is done preseting.
  • var didDismiss: ((_ dismissalType: BottomSheetDismissalType) -> Void)?: Callback function to be called when bottom sheet is done dismissing itself. Parameter dismissalType: information about how the bottom sheet was dismissed - .tapInOverlay, .swipeDown, .programmatic(info).
  • var didChangeHeight: ((_ newHeight: CGFloat) -> Void)?: Callback function to be called when bottom sheet height changes from dragging or a call to updateHeight.
  • var preferredGestureRecognizers: [UIGestureRecognizer]?: Gesture recognizers that should block the vertical dragging of bottom sheet. Will automatically find and use all gesture recognizers if nil, otherwise will use recognizers in the array. Default is empty array.
  • var shouldListenToKeyboardNotifications: Bool: If there is a text field in the bottom sheet we may want to automatically have the bottom sheet adjust for the keyboard. Default is false.
  • var isModalAccessibilityElement: Bool: Whether or not to treat the bottom sheet as a modal accessibility element, which will block interaction with views underneath. Default is true. It is not recommended that you override this unless you are using the bottom sheet in sticky mode or otherwise without the overlay.
  • var allowsAccessibilityGestureToDismiss: Bool: Whether or not users can use the accessibility escape gesture to dismiss the bottom sheet. Default is true. It is not recommended that you override this unless you are using the bottom sheet in sticky mode or otherwise disabling dismissal or providing another way for VoiceOver users to dismiss.

If you change the contentVC or contentHeight properties, the bottom sheet will automatically update its height. You can also call updateHeight() to trigger an update of the height (this is mainly for if the content of the contentVC has changed and you want the bottom sheet to update to match the new content size).

Because it is uncommon to have access to the bottom sheet view controller from the contentVC,we define a CardPartsBottomSheetDelegate with default implementations for updating to a new contentVC or contentHeight, updating the height, or dismissing the bottom sheet programmatically. In order to use this delegate and its default function implementations, simply have your class conform to CardPartsBottomSheetDelegate and define a var bottomSheetViewController: CardPartsBottomSheetViewController. Then, set that class to be a delegate for your content view controller and you can interface with the bottom sheet through the delegate.

CardPartVideoView

Provides the capability to embed AVPlayer inside a cardpart view.

guard let videoUrl = URL(string: "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4")  else  { return }
let cardPartVideoView = CardPartVideoView(videoUrl: videoUrl)

If you need to access the underlying AVPlayerViewController to further customize it or set its delegate, you can do so through the CardPartVideoView's viewController property. For example:

guard let controller = cardPartVideoView.viewController as? AVPlayerViewController else { return }
controller.delegate = self
controller.entersFullScreenWhenPlaybackBegins = true

CardPartVideoView

Card States

CardPartsViewController can optionally support the notion of card states, where a card can be in 3 different states: loading, empty, and hasData. For each state you can specify a unique set of card parts to display. Then when the CardPartsViewController state property is changed, the framework will automatically switch the card parts to display the card parts for that state. Typically you would bind the state property to a state property in your view model so that when the view model changes state the card parts are changed. A simple example:

public enum CardState {
    case none
    case loading
    case empty
    case hasData
    case custom(String)
}

class TestCardController : CardPartsViewController  {

    var viewModel = TestViewModel()
    var titlePart = CardPartTitleView(type: .titleOnly)
    var textPart = CardPartTextView(type: .normal)
    var loadingText = CardPartTextView(type: .normal)
    var emptyText = CardPartTextView(type: .normal)
    var customText = CardPartTextView(type: .normal)

    override func viewDidLoad() {
        super.viewDidLoad()

        viewModel.title.asObservable().bind(to: titlePart.rx.title).disposed(by: bag)
        viewModel.text.asObservable().bind(to: textPart.rx.text).disposed(by: bag)

        loadingText.text = "Loading..."
        emptyText.text = "No data found."
        customText.text = "I am some custom state"

        viewModel.state.asObservable().bind(to: self.rx.state).disposed(by: bag)

        setupCardParts([titlePart, textPart], forState: .hasData)
        setupCardParts([titlePart, loadingText], forState: .loading)
        setupCardParts([titlePart, emptyText], forState: .empty)
        setupCardParts([titlePart, customText], forState: .custom("myCustomState"))
    }
}

Note: There is a custom(String) state which allows you to use more than our predefined set of states:

.custom("myCustomState")

Data Binding

Data binding is implemented using the RxSwift library (https://github.com/ReactiveX/RxSwift). View models should expose their data as bindable properties using the Variable class. In the example above the view model might look like this:

class TestViewModel {

    var title = BehaviorRelay(value: "Testing")
    var text = BehaviorRelay(value: "Card Part Text")
}

Later when the view model's data has changed it can update its property by setting the value attribute of the property:

title.accept(“Hello”)

The view controller can bind the view model property to a view:

viewModel.title.asObservable().bind(to: titlePart.rx.title).disposed(by: bag)

Now, whenever the view model's title property value is changed it will automatically update the titlePart's title.

RxSwift use the concept of "Disposable" and "DisposeBag" to remove bindings. Each call to bind returns a Disposable that can be added to a DisposeBag. CardPartsViewController defines an instance of DisposeBag called "bag" that you can use to automatically remove all your bindings when your CardPartsViewController is deallocated. See the RxSwift documentation for more information on disposables and DisposeBags.

Themes

Out of the box we support 2 themes: Mint and Turbo. These are the 2 Intuit app's that are currently built on top of CardParts. As you can find in the file CardPartsTheme.swift we have a protocol called CardPartsTheme. You may create a class that conforms to CardPartsTheme and set all properties in order to theme CardParts however you may like. Below is an example of some of the themeable properties:

// CardPartTextView
var smallTextFont: UIFont { get set }
var smallTextColor: UIColor { get set }
var normalTextFont: UIFont { get set }
var normalTextColor: UIColor { get set }
var titleTextFont: UIFont { get set }
var titleTextColor: UIColor { get set }
var headerTextFont: UIFont { get set }
var headerTextColor: UIColor { get set }
var detailTextFont: UIFont { get set }
var detailTextColor: UIColor { get set }

// CardPartTitleView
var titleFont: UIFont { get set }
var titleColor: UIColor { get set }

// CardPartButtonView
var buttonTitleFont: UIFont { get set }
var buttonTitleColor: UIColor { get set }
var buttonCornerRadius: CGFloat { get set }

Applying a theme

Generate a class as follows:

public class YourCardPartTheme: CardPartsTheme {
    ...
}

And then in your AppDelegete call YourCardPartTheme().apply() it apply your theme. If you use storyboards with CardsViewControllers in your storyboard, the required init(coder:) initializer gets called prior to AppDelegate. In this case, you will need to apply the theme in this initializer of the first view controller in your storyboard to be initialized, and changes will take effect in all other view controllers. For example:

required init?(coder: NSCoder) {
	YourCardPartTheme().apply()
	super.init(coder: coder)
}

Clickable Cards

You have the ability to add a tap action for each state of any given card. If a part of the card is clicked, the given action will be fired:

self.cardTapped(forState: .empty) {
    print("Card was tapped in .empty state!")
}

self.cardTapped(forState: .hasData) {
    print("Card was tapped in .hasData state!")
}

// The default state for setupCardParts([]) is .none
self.cardTapped {
    print("Card was tapped in .none state")
}

Note: It is always a good idea to weakify self in a closure:

{[weak self] in

}

Listeners

CardParts also supports a listener that allows you to listen to visibility changes in the cards that you have created. In your CardPartsViewController you may implement the CardVisibilityDelegate to gain insight into the visibility of your card within the CardsViewController you have created. This optional delegate can be implemented as follows:

public class YourCardPartsViewController: CardPartsViewController, CardVisibilityDelegate {
    ...

    /**
    Notifies your card parts view controller of the ratio that the card is visible in its container
    and the ratio of its container that the card takes up.
    */
     func cardVisibility(cardVisibilityRatio: CGFloat, containerCoverageRatio: CGFloat) {
        // Any logic you would like to perform based on these ratios
    }
}

Delegates

Any view controller which is a subclass of CardPartsViewController supports gesture delegate for long press on the view. Just need to conform your controller to CardPartsLongPressGestureRecognizerDelegate protocol.

When the view is long pressed didLongPress(_:) will be called where you can custom handle the gesture. Example: Zoom in and Zoom out on gesture state begin/ended.

    func didLongPress(_ gesture: UILongPressGestureRecognizer) -> Void

You can set the minimumPressDuration for your press to register as gesture began. The value is in seconds. default is set to 1 second.

    var minimumPressDuration: CFTimeInterval { get } // In seconds

Example:

extension MYOwnCardPartController: CardPartsLongPressGestureRecognizerDelegate {
	func didLongPress(_ gesture: UILongPressGestureRecognizer) {
		guard let v = gesture.view else { return }

		switch gesture.state {
		case .began:
			// Zoom in
		case .ended, .cancelled:
			// Zoom out
		default: break
		}
	}
	// Gesture starts registering after pressing for more than 0.5 seconds.
	var minimumPressDuration: CFTimeInterval { return 0.5 }
}

Apps That Love CardParts

Publications

License

CardParts is available under the Apache 2.0 license. See the LICENSE file for more info.

cardparts's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cardparts's Issues

Using custom card (with embedded view + autolayout) freezes unless CardCellMargins left/right not set to 0

First off, I want to say that this library is fantastic!

On to the problem:
I am using a stack of custom cards, one of which simply has a chart view embedded inside (taken from the iOS Charts library). However, using this card will cause the app to freeze unless I set the left and right margins to nonzero in the CardsViewController, e.g. with the following application:


let theme = CardPartsMintTheme()
        theme.cardCellMargins = UIEdgeInsets(top: 100, left: 15, bottom: 100, right: 15)
        theme.cardsViewContentInsetTop = 15
        theme.cardsLineSpacing = 15
        theme.apply()

However, doing so means that the cards cannot hug the edge of the screen. As mentioned previously, setting the insets to default (0 for left and right) causes the app to freeze.

See snippet for my custom card below, which has a chart embedded inside:

public class PAChartCardPartView : UIView, CardPartView {
var chart:CombinedChartView?

public init() {
        super.init(frame: CGRect.zero)
        self.chart = CombinedChartView(frame: CGRect.zero)
        self.chart?.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(self.chart!)
        self.chart?.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true
        self.chart?.bottomAnchor.constraint(equalTo: (self.view.bottomAnchor), constant: 0).isActive = true
        self.chart?.leadingAnchor.constraint(equalTo: (self.view.leadingAnchor), constant: 0).isActive = true
        self.chart?.trailingAnchor.constraint(equalTo: (self.view.trailingAnchor), constant: 0).isActive = true
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

Is it possible to use this card without having to use nonzero left and right insets? Any help or insight would be greatly appreciated. Thanks!

Why cards left and right margin missing?

In your demo,I run it finding that the card has left and right margin, just like this:

But the same code I copied to my project,the left and right margin was missing, just like this:

How should I code to has margin?

Carthage

Please add an option to use carthage as a package manager.

Card sequence change

Would it be possible to implement card reordering? For example, on user long press, you can move the card on different position.

Help Wanted: Having Issue Building Examples

Hi I was wondering if you can point me in the right direction. I downloaded the project and in the /examples directory ran 'pod install' it seemed to install the necessary pods, however, when I try to build the project I get a number of errors including on the MainViewController it says it cannot find CardParts to import.

Should I run the pod install at a different level and not in the examples directory. Any help for the fastest path to being able to check out the examples is greatly appreciated. This looks like a really cool library and I am looking forward to using it.

Xcode Version 10.1 (10B61)

Stack

   CocoaPods : 1.5.3
        Ruby : ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]
    RubyGems : 2.5.2.3
        Host : Mac OS X 10.14.2 (18C54)
       Xcode : 10.1 (10B61)
         Git : git version 2.17.2 (Apple Git-113)
Ruby lib dir : /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib
Repositories : master - https://github.com/CocoaPods/Specs.git @ 8325532a439a86921f03327ab078b5e1acea6790

Installation Source

Executable Path: /usr/local/bin/pod

Plugins

cocoapods-clean       : 0.0.1
cocoapods-deintegrate : 1.0.2
cocoapods-plugins     : 1.0.0
cocoapods-search      : 1.0.0
cocoapods-stats       : 1.0.0
cocoapods-trunk       : 1.3.0
cocoapods-try         : 1.1.0

Ambiguous reference when binding to the title of a button

It looks like that RxSwift has a function called "title()". When trying to bind to the title of a button through RxSwift there is an ambiguous reference build error in Xcode.

The fix should be easy we should just be able to rename the binder variable that is exposed in CardPartButtonView.swift so that it does not conflict with the RxSwift function

iPad

Does this lib have iPad support on the roadmap ? (like different collection layout)

Example project currently looks busted

Here's what I see when I launch the example project:

simulator screen shot - iphone se - 2018-05-19 at 09 57 30

At least, I think the other examples that are commented out in the App Delegate should be reenabled. This looks cool, I look forward to diving into it a bit more soon. Cheers!

Add card without reloading all cards

Hi all!
First, thanks for the great lib.
I want to dynamically add and remove cards. Is there a way to add a card without triggering a reload to the whole collectionView?
Using loadCards calls setCardsController which firstly removes all cards from the array.. These cards might have work in progress that I don't want to be reloaded..

Thanks!

CardPartCollectionView binding datasource throws error 'Generic parameter 'Self' could not be inferred'

I am running into an issue binding my CardPartCollectionView datasource.

First I set up my model

struct CollectionViewModel {
    var title: String?
    var image: Storefront.Image?
    var description: String?
    var handle: String?
    var products = Variable([Storefront.Product]())
}

struct SectionOfCollection {
    var header: String
    var items: [Item]
}

extension SectionOfCollection: SectionModelType {
    init(original: SectionOfCollection, items: [Item]) {
        self = original
        self.items = items
    }
    
    typealias Item = Storefront.Product
}

Then I set up my CardPartsFullScreenViewController with an instance of the model, the collectionView CardPart, the layout for the collectionView, then I setup the collectionView with all these components.

class CollectionCardController: CardPartsFullScreenViewController {
    
    let model = CollectionViewModel()
    lazy var collectionViewCardPart = CardPartCollectionView(collectionViewLayout: layout)
    
    lazy var layout: UICollectionViewFlowLayout = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 10
        layout.minimumLineSpacing = 10
        layout.scrollDirection = .vertical
        layout.itemSize = CGSize(width: width, height: height)
        return layout
    }()
    lazy var width: CGFloat = {
        return self.view.frame.width / 2 - 20 - 20
    }()
    lazy var height = self.width + 88
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupCollectionView()
        collectionViewCardPart.collectionView.register(ProductCollectionViewCell.self, forCellWithReuseIdentifier: "ProductCell")
    }
    
    func setupCollectionView() {
        
        let dataSource = RxCollectionViewSectionedReloadDataSource<SectionOfCollection>(configureCell: {[weak self] (_, collectionView, indexPath, product) -> UICollectionViewCell in
            
            print(self?.model.title as Any)
            
            guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductCell", for: indexPath) as? ProductCollectionViewCell else {
                return UICollectionViewCell()
            }
            
            cell.set(product)
            
            return cell
        })
        
        let section = [ SectionOfCollection(header: "Products", items: model.products.value) ]
        
        // This is where the error appears: "Generic parameter 'Self' could not be inferred"
        model.products.asObservable().bind(to: collectionViewCardPart.collectionView.rx.items(dataSource: dataSource)).disposed(by: bag)
    }
}

I have been able to get my data from the server and map all the ui components to the cards that I'm using as cells, but the datasource hasn't been set correctly due to this error and nothing ever appears!

I did find something on this error based in RXDataSources and I was able to get the error to go away and compile with this solution but it did not bind the datasource so data still does not show.

This one also has some logical explanations but also does not fix the issue either.

Any help is appreciated!

Scroll on CardPartStackView

Is it possible to have a CardPartStackView that scrolls? Or is that only possible for CardPartTableView's and CardPartCollectionView's? What happens if we have to stack data of different types? For example images interspersed with text.

Override CardParts to add custom Views

I would like to add a Charting View to the CardParts. Kinda like you have with Intuit's Charts.
Is there a way to do that?

In general is there a way to adding a custom view

Thanks

Inset calculation bug in CardsViewController

Hi,

If you run the demo on iOS 11 there is an inset on the collection view at the bottom of the size of the tabbar but not on iOS 10. I've looked at the code and in CardsViewController.viewDidLoad() constraint for the collection view are set not relatively to the safe area and with a tabbar height constant. So this cause the difference between iOS 11 and 10. This also cause a small difference at the top where on iOS 10 content is drawn below the status bar and not on iOS 11.

Example binding /scroll bug

Hi,

I'm wondering why the reactive binding is paused when one scroll in the example.
To easily reproduce it just over scroll at the bottom and all the reactive bindings are paused.
It also blinks I thing when you let it go. I didn't tried this on real device, so it may be a simulator only issue.

Help Wanted: Spacing on CardPartPagedView

I am trying to get the paged cards (CardPartPagedView) to fill the sides of the screen without a white space. On the other cards I can use the margins and set them to 0 and that seems to work, however, I cannot figure out how to do it in the CardPartPagedView.

Any pointers greatly appreciated.

space

More examples

We should have a more in-depth example app that shows all of the different style CardParts and some of the available traits.

cardCellMargins.top don't works

I set cardCellMargins top to want add space, but it don't works

        let theme = CardPartsMintTheme()
        theme.cardCellMargins = UIEdgeInsets(top: 200.0, left: 150.0, bottom: 100.0, right: 15.0)
        theme.apply()

simulator_screen_shot_-iphone_7-_2018-08-28_at_11_03_22

How to easily remove the card ?

When I write the delete event in CardPartsViewController, how can I easily remove the specified card ( actually CollectionViewCell ) in CardsViewController ?

Support for infinite-scrolling lists in a form of a card

Hi, thanks for the interesting framework.

I'm interested, whether it can be used to display an infinite scrollable list in a form of a card. The idea is that the two top- and bottommost parts of the card are rounded, while the shadow is displayed underneath the whole card.

The challenge is that the card can be very large (i.e. transaction history, contact list, etc), so the whole card cannot be loaded into memory, and has to be composed out of multiple reusable components.

Here is the conceptual example of the target result, the bottom card is "infinite".
screen shot 2018-05-29 at 14 32 18

Many apps are using a similar pattern, for example, Duolingo and Snapchat:
screen shot 2018-05-29 at 14 33 19

Is it possible to achieve a similar UI to the examples above with the help of the CardParts framework? Or was it meant to be used only for a fixed-length cards?

Thank you in advance!

Trouble running the example project

I'm having trouble building the example project.

I cloned the repo, installed the pod from within the Example directory, switched the signing, opened from the workspace....all good. When I go to build, however, I get this issues:

'_lock' is inaccessible due to 'private' protection level
in RxSwift/extension DisposeBag

I tried to update the project to 4.2, but that caused additional issues. And I also tried switching the build language version to swift 4.0 (rather than 3.0). No joy.

Any thoughts on what I'm doing wrong?

Thanks! Keith

Add Jazzy Documentation

As we are growing, documentation will be a lot to handle - we should use Jazzy as a way to have clean and concise documentation.

Remove TTTAttributedLabel

In an attempt to remove as many dependencies as possible, we would like to move away from TTTAttributedLabel.

Layout issue: "contentView of collectionViewCell has translatesAutoresizingMaskIntoConstraints false and is missing constraints to the cell"

I see this in the console using the code provided (all from the example project):

[Assert] contentView of collectionViewCell has translatesAutoresizingMaskIntoConstraints false and is missing constraints to the cell, which will cause substandard performance in cell autosizing. Please leave the contentView's translatesAutoresizingMaskIntoConstraints true or else provide constraints between the contentView and the cell. <CardParts.CardCell: 0x10d6a2aa0; baseClass = UICollectionViewCell; frame = (162.5 0; 50 50); layer = <CALayer: 0x107192f00>>

Code:

class CardPartTitleViewCardController: CardPartsViewController {

let cardPartTitleView = CardPartTitleView(type: .titleOnly)
let cardPartTitleWithMenu = CardPartTitleView(type: .titleWithMenu)

override func viewDidLoad() {
    super.viewDidLoad()
    
    cardPartTitleView.title = "I am a standard .titleOnly CardPartTitleView"
    cardPartTitleWithMenu.title = "I am a .titleWithMenu CardPartTitleView"
    
    setupCardParts([cardPartTitleView, cardPartTitleWithMenu])
}
}

class UserAreaViewController: CardsViewController {


var cards: [CardController] = [CardPartTitleViewCardController()]

 
override func viewDidLoad() {
    super.viewDidLoad()
   
    loadCards(cards: cards)

   }
}

At first I thought this is a wrong implementation of our team, but then I took 100% code from the example (provided) and still see this warning.

Example loads empty UI

The example project toggles between 2 empty cards. I doubt this is the desired behavior.

simulator screen shot - iphone x - 2018-05-20 at 11 40 05
simulator screen shot - iphone x - 2018-05-20 at 11 40 07

Clickable Cards

https://github.com/intuit/CardParts#clickable-cards

Could you elaborate on how to use this. where do you add this new code for clickable for the various states

import Foundation
import CardParts

class CardPartTextViewCardController: CardPartsViewController {
    
    let cardPartTextView = CardPartTextView(type: .normal)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        cardPartTextView.text = "This is a CardPartTextView"
        
        setupCardParts([cardPartTextView])
    }
    
}

Example gives 13 errors in Swift 4 in Xcode 10+

When I open the example project and build it, I get 13 errors. I am using Swift 4.2.1 in XCode 10.1. All I did was load up the example project and "pod install" with the terminal. It installed 6 pods, then I rebuilt the project. Build fails with 13 errors.

Ability to add custom CardStates

We should allow users to be able to make custom CardStates so they can setup their CardParts for as many states as they would need.

how can i update from async

I want to decide what card depending on what result i get from an async result but when i attempt to change nothing happing here is my current async code

GMSPlacesClient().lookUpPhotos(forPlaceID: place!.placeID) { (dataList, error) in
if error != nil {
print(error!)
return
}
if let result = dataList?.results {
if result.count > 0 {
GMSPlacesClient().loadPlacePhoto((dataList?.results[0])!, callback: { (image, error) in
if error != nil {
print(error!)
return
}
DispatchQueue.main.async {

                        let card = imgCard()
                        card.cardPartImage.image = image

// self.cardViewController.loadCards(cards: [card,cardBottom])
self.cardViewController.reload(cards: [card,cardBottom])

                        constrain(self.cardViewController.view) { (tv) in
                            tv.height == self.view.frame.size.height
                            tv.leftMargin == tv.superview!.leftMargin + 6
                            tv.rightMargin == tv.superview!.rightMargin - 6
                        }
                        
                        constrain(self.cardViewController.view) { (tv) in
                            tv.height == self.cardViewController.collectionView.contentSize.height
                            tv.bottom == tv.superview!.bottom
                        }
                    }
                })
            } else {
                DispatchQueue.main.async {
                    let card = NoImgCard()
                    self.cardViewController.reload(cards: [card,cardBottom])

// self.cardViewController.loadCards(cards: )

                    constrain(self.cardViewController.view) { (tv) in
                        tv.height == self.view.frame.size.height
                        tv.leftMargin == tv.superview!.leftMargin + 6
                        tv.rightMargin == tv.superview!.rightMargin - 6
                    }
                    constrain(self.cardViewController.view) { (tv) in
                        tv.height == self.cardViewController.collectionView.contentSize.height
                        tv.bottom == tv.superview!.bottom
                    
                    }
                }
            }
        } else {
        }
    }

Card Visibility Delegate

We should be able to know if a given Card is visible on the screen as well as what percent it is visible.

Add an method to modify distribution

The current distribution mode is set to equalSpacing, which is nice for some cards. However, it's preventing me from placing a cardPart at a certain position. Is it possible to add an function like modifyDistribution to switch distribution to .fill, so that cardParts will be placed according to margins?

XCode 10 Example not compiling

I have downloaded zip file then in Example folder i have fired pod install.

Still getting following error

/Users/prashant/Downloads/CardParts-master/Example/Pods/Pods/Target Support Files/Pods-CardParts_Example/Pods-CardParts_Example.debug.xcconfig: unable to open file (in target "CardParts_Example" in project "CardParts") (in target 'CardParts_Example')

and

/Users/prashant/Downloads/CardParts-master/Example/Pods/Pods/Target Support Files/Pods-CardParts_Tests/Pods-CardParts_Tests.debug.xcconfig: unable to open file (in target "CardParts_Tests" in project "CardParts") (in target 'CardParts_Tests')

Please suggest

iOS 9 support

Hi,

Is there a reason not to support iOS 9 ? I've tried to enable it and I had no issue (except theme fonts but one could make the base theme use system fonts). In the code at some place you check for iOS 10 availability so I guessed it was supported before ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.