LeadKit/TIUIElements/Sources/Views/DefaultHomogeneousItemsColl.../DefaultCollectionViewModel....

33 lines
939 B
Swift

import TISwiftUtils
import Foundation
open class DefaultCollectionViewModel<ItemViewModel> {
public typealias SelectItemHandlerPayload = (itemViewModel: ItemViewModel, indexPath: IndexPath)
public var items: [ItemViewModel]
public var selectItemHandler: ParameterClosure<SelectItemHandlerPayload>?
public init(items: [ItemViewModel],
selectItemHandler: ParameterClosure<SelectItemHandlerPayload>? = nil) {
self.items = items
self.selectItemHandler = selectItemHandler
}
open func numberOfSections() -> Int {
1
}
open func numberOfItems(inSection section: Int) -> Int {
items.count
}
open func itemViewModel(at indexPath: IndexPath) -> ItemViewModel {
items[indexPath.row]
}
open func didSelectItem(at indexPath: IndexPath) {
selectItemHandler?(SelectItemHandlerPayload(itemViewModel(at: indexPath), indexPath))
}
}