Skip to content

zzmasoud/ZZComposableInput

Repository files navigation

ZZComposableInput

This multiplatform library helps to get the user's selected items from different sections using high composability.
I'm using it in my personal app CLOC: Tasks & Time Tracker to create/edit tasks, here is a quick preview:

Documentation

ItemType

public protocol ItemType: Hashable {
    var title: String { get }
}

The core model that is used widely in the project.
For now, it is using a typealias AnyItem which is equal to ItemType. It is added for further development.

ItemsLoader

public protocol ItemsLoader {
    associatedtype Item: AnyItem
    
    typealias FetchItemsResult = Result<[Item], Error>
    typealias FetchItemsCompletion = (FetchItemsResult) -> Void

    func loadItems(for section: Int, completion: @escaping FetchItemsCompletion) -> CancellableFetch
}

Abstraction of loading data for each section.

ItemsContainer

public protocol ItemsContainer: AnyObject {
    associatedtype Item: AnyItem
    
    var delegate: ItemsContainerDelegate? { get set }
    var selectionType: ItemsContainerSelectionType { get }
    var items: [Item] { get }
    var selectedItems: [Item]? { get }
    var allowAdding: Bool { get }
    func select(at index: Int)
    func deselect(at index: Int)
    func add(item: Item)
    func removeSelection()
}

Abstraction of controlling items in a container.
It is provided with DefaultItemsContainer which do the things needed, such as:

  • Selection
  • Deselection
  • Prevent selection more than maximum limit
  • Delegation

SectionedViewProtocol

public protocol SectionedViewProtocol: AnyObject {
    associatedtype View
    var view: View { get }
    var selectedSectionIndex: Int { set get }
    var numberOfSections: Int { get }
    var onSectionChange: (() -> Void)? { set get }
    func reload(withTitles: [String])
}

Abstraction of a view with sections, e.g. UISegmentedControl.

ResourceListViewProtocol

public protocol ResourceListViewProtocol: AnyObject {
    associatedtype View
    associatedtype CellController: SelectableCellController
    
    var view: View { get }
    var onSelection: ((Int) -> Void) { get set }
    var onDeselection: ((Int) -> Void) { get set }
    func reloadData(with: [CellController])
    func allowMultipleSelection(_ isOn: Bool)
    func allowAddNew(_ isOn: Bool)
    func deselect(at: Int)
}

Abstraction of a view with list of items e.g. UITableView, UICollectionView.

Getting Started

  • You can head to iOS Snapshot Tests and learn how to use it in your iOS project. There are a lot of mocking which helps you conform your custom views and models to the protocols of this library.
  • There is a public class called ZZComposableInput which you can get started.

Contribution

Please feel free to open an issue to:

  • Repoert a bug
  • Request a feature
  • Ask for help

And also you can help improving this project by opening a PR.

License

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

About

This library helps creating composable inputs and connect them all together.

Topics

Resources

Stars

Watchers

Forks

Languages