Skip to content

Madara2hor/CollectionManager

Repository files navigation

CollectionManager


📄Description

CollectionManager - a UICollectionView manager that allows you to easily manage sections. Sections can be: vertical, horizontal and consist of one cell.

Library example


🔨Connecting

  1. Add CollectionManager in the pod file.
target 'app_name' do
  use_frameworks!
  pod 'CollectionManager' -> 'last.version'
  1. Next, install the library from the root folder of the project.
pod install
  1. Connect the library in the desired class.
import CollectionManager

HowTo

final class ViewController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView! {
        didSet {
            collectionManager = CollectionViewManager(
                collectionView: collectionView,
                sections: [
                    section
                ], 
                hiddenSections: [],
                config: .default
            )
        }
    }
    
    var collectionManager: Manager!
    var section: SectionManager!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

}

🤌🏼Properties

Manager

Property getter, setter
collectionView ✅, ❌
flowLayout ✅, ❌
config ✅, ❌
visibleSections ✅, ❌
delegate ✅, ✅

SectionManager

Property getter, setter
collectionManager ✅, ❌
sectionConfig ✅, ✅
numberOfItems ✅, ❌
cellSize ✅, ✅
headerHeight ✅, ✅

SectionManager properties also available in TypedSection and ViewControllerSection.

TypedSection

Property getter, setter
skeletonSetup ✅, ✅
loadingState ✅, ❌
rows ✅, ✅
columns ✅, ✅
onLoadingView ✅, ✅
isNeedOnLoadingBottomViewCell ✅, ✅
onLoadingBottomViewCellSize ✅, ✅

ViewControllerSection

Property getter, setter
viewController ✅, ❌
reusableId ✅, ✅
loadingState ✅, ❌

🤙🏼Methods

Manager

ReloadData

func reloadData(
    in section: SectionManager? = nil,
    onlyVisible: Bool = true
)

Example:

// Reload data in only visible sections.
collectionManager.reloadData()
// Reload data in all sections.
collectionManager.reloadData(onlyVisible: false)
// Reload data in the section if it is visible.
collectionManager.reloadData(in: section) 
// Will reload data in the section even if it is hidden.
collectionManager.reloadData(in: section, onlyVisible: false) 

ReloadSection

func reloadSection(_ section: SectionManager)

Example:

// Reload data in the section if it is visible.
collectionManager.reloadSection(section)

SetSection

func setSection(
    _ section: SectionManager,
    hidden: Bool
)

Example:

// Show/hide section You need.
collectionManager.setSection(section, hidden: true)

ScrollToTop

func scrollToTop()

Example:

collectionManager.scrollToTop()

SectionManager

SectionDidLoadData

func sectionDidLoadData(_ section: SectionManager)

Example:

class Section: SectionManager {
    
    var loadingState: SectionLoadingState = .none {
        didSet {
            guard loadingState != oldValue else { return }
            switch loadingState {
            case .full:
                // You can show the skeleton on a cell there.
                break
            case .partial:
                break
            case .none:
                // You can hide the skeleton from the cell there.
                collectionManager?.sectionDidLoadData(self)
            @unknown default:
                fatalError("Unknown section loading state: \(loadingState)")
            }
        }
    }
    
}

extension Section: SectionLoadable {

    func loadData() {
        loadingState = .full
    }

}

❗️When using the method, the delegate will work on the completion of the data loading. Therefore, for better work, it is necessary to extend the class with the SectionLoadable protocol.

SetupCell

func setupCell(
    cellFactory: CellFactory
) -> UICollectionViewCell {
    fatalError("setupCell(at:cellFactory:) must be implemented in a sub-class")
}

Example:

override func setupCell(
    cellFactory: CellFactory
) -> UICollectionViewCell {
    return cellFactory.cell(of: YourCell.self)
}

SetupCell

func setupHeader(
    cellFactory: CellFactory
) -> UICollectionReusableView? {
    return nil
}

Example:

override func setupHeader(
    cellFactory: CellFactory
) -> UICollectionReusableView? {
    return cellFactory.header(of: YourHeaderView.self)
}

ItemDidSelect

func itemDidSelect(index: Int)

Example:

override func itemDidSelect(index: Int) {
    // Do something with Your data.
}

DidScrollToEnd

func didScrollToEnd()

Example:

override func didScrollToEnd() {
    // You can upload data there, for example.
}

SectionManager methods also available in TypedSection and ViewControllerSection.

TypedSection

UpdateData

func update(with cellObjects: [Cell.CardCellObject]?)

Example:

update(with: ArrayOfItems)

LoadData

func loadData()

Example:

func loadData() {
    super.loadData()
    // Request data there.
}

This method also available in a ViewControllerSection.

UploadData

func uploadData()

Example:

func uploadData() {
    super.uploadData()
    // Request additional data there.
}

DataLoaded

func dataLoaded()

Example:

func onDataLoaded() {
    dataLoaded()
}

Автор

Kirill Kapis, [email protected]


License

CollectionManager is available under the MIT license. See the LICENSE file for more info.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published