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

invalidateRepeatCounts causes duplicate loading of newly added bricks #55

Closed
argallo opened this issue Dec 21, 2016 · 1 comment
Closed

Comments

@argallo
Copy link

argallo commented Dec 21, 2016

lets say we have one brick in a brick section and when we click a button we add 2 to our BrickRepeatCountDataSource and then call invalidate bricks. If we add a print inside the datasource of the brick thats getting repeated we will see this:
Brick 2
Brick 1
Brick 0
Brick 1
Brick 2

if we do this again we will see:
Brick 4
Brick 3
Brick 0
Brick 1
Brick 2
Brick 3
Brick 4

As you can see the newly added bricks are having their datasource called twice. The reason this is an issue is because if we want to make a asynchronous method call that affects how the datasource will load the next time its refreshed, we are stuck making two asynchronous calls because these bricks reload too quickly so the first call doesn't return in time.

Here is the code I used to determine the multiple reloads:

//  OrientationExampleViewController.swift
//  BrickKit-Example
//
//  Created by Anthony Gallo on 11/28/16.
//  Copyright © 2016 Wayfair LLC. All rights reserved.
//

import UIKit
import BrickKit


private let NavBarHeight: CGFloat = 64
private let saveButtonHeight: CGFloat = 50
private let PhotoImageBrick = "PhotoImageBrick"
private let SaveButtonBrick = "SaveButtonBrick"

class OrientationExampleViewController: BrickViewController {
    
    override class var title: String {
        return "Example"
    }
    override class var subTitle: String {
        return "Example"
    }
    
    var titleView: UIView!
    var titleLabel: UILabel!
    var num = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = .brickBackground
        registerBricks()
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "AddFriend"), style: .Plain, target: self, action: #selector(MockTwitterViewController.backAction))

        
        let mainSection = BrickSection(bricks: [setupPhoto(), setupSave()])
        
        self.brickCollectionView.setSection(mainSection)
    }
    
    
    func registerBricks() {
        self.brickCollectionView.registerBrickClass(ImageBrick.self)
        self.brickCollectionView.registerBrickClass(ButtonBrick.self)
    }
    
    private func setupPhoto() -> BrickSection {
        
        //let photoImageBrickModel = ImageBrickModel(image: UIImage(named:"ImageCarouselExample")!, contentMode: .ScaleAspectFill)
        let section = BrickSection("test", bricks: [ImageBrick(PhotoImageBrick, height: .Orientation(landscape: .Fixed(size: UIScreen.mainScreen().fixedCoordinateSpace.bounds.width - (NavBarHeight+saveButtonHeight)) , portrait: .Fixed(size: 50)), dataSource: self)])
        section.repeatCountDataSource = self
        return section
    }
    
    private func setupSave() -> ButtonBrick {
        let saveButtonBrickModel = ButtonBrickCellModel(title: "save", configureButtonBlock: ({ (cell) in
            cell.button.setTitleColor(UIColor.blueColor(), forState: .Normal)
        }))
        return ButtonBrick(SaveButtonBrick, height: .Fixed(size: saveButtonHeight), backgroundColor: UIColor.whiteColor(), dataSource: saveButtonBrickModel, delegate: self)
    }
    
}

extension OrientationExampleViewController: ImageBrickDataSource {
    func imageForImageBrickCell(imageBrickCell: ImageBrickCell) -> UIImage? {
        print("test \(imageBrickCell.index)")
        return UIImage(named:"ImageCarouselExample")!
    }
    func contentModeForImageBrickCell(imageBrickCell: ImageBrickCell) -> UIViewContentMode {
        return .ScaleAspectFill
    }
}

extension OrientationExampleViewController: BrickRepeatCountDataSource {
    func repeatCount(for identifier: String, with collectionIndex: Int, collectionIdentifier: String) -> Int {
        return num
    }
}

extension OrientationExampleViewController: ButtonBrickCellDelegate {
    func didTapOnButtonForButtonBrickCell(cell: ButtonBrickCell){
        if cell.brick.identifier == SaveButtonBrick {
            num+=2
            self.brickCollectionView.invalidateRepeatCounts(true)
        }
    }
}
@rubencagnie
Copy link
Contributor

This could be resolved by
self.brickCollectionView.invalidateRepeatCounts(reloadAllSections: false) in the didTapOnButtonForButtonBrickCell function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants