Git Product home page Git Product logo

fastadapter-swift's Introduction

FastAdapter-Swift

Features

  • expandable items
  • layoutkit support
  • footer, header items
  • multi sections
  • background measuring
  • modularity
  • event hooks
  • drag and drop
  • collection and table view support

TODO

  • auto collapsing for expandable
  • single expanded
  • multi adapter support
  • click listener
  • selectable, selection, multiselection
  • filter
  • split layoutkit items to different module
  • add extension modules (endless scroll, waterfall layout)

Untested

  • expandable with more then one depth
  • items without using layoutkit
class DeviceItem: ModelLayoutItem<Device> {
    override func getLayout() -> Layout? {
        return DeviceLayout(device: model)
    }
}

class SampleItem: Item {
    
    private var size: CGSize?
    
    private let name: String
    
    private let font: UIFont
    
    init(name: String, font: UIFont) {
        self.name = name
        self.font = font
    }

    override func onBind(cell: inout UICollectionViewCell) {
        if let myCell = cell as? MyCollectionViewCell {
            myCell.nameLabel.text = name
            myCell.nameLabel.font = font
        }
    }
    
    override func getCell() -> AnyClass {
        return MyCollectionViewCell.self
    }
    
    override func onMeasure(width: CGFloat?, height: CGFloat?) -> Bool {
        guard let width = width, let height = heightWithConstrainedWidth(width, font: font) else {
            return false
        }
        size = CGSize(width: width, height: height)
        return true
    }
    
    override func getSize() -> CGSize {
        return size ?? .zero
    }
    
    func heightWithConstrainedWidth(_ width: CGFloat, font: UIFont) -> CGFloat? {
        let constraintRect = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)
        let boundingBox = name.boundingRect(with: constraintRect, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
    
        return ceil(boundingBox.height)
    }
}
...
let fastAdapter = FastAdapter<DeviceItem>()
let modelAdapter = ModelAdapter<Device, DeviceItem>(interceptor: {
    model in
    return DeviceItem(model: device)
})
let itemAdapter = ItemAdapter<SampleItem>()
itemAdapter.add(SampleItem(name: name, font: font))
...
let itemAdapter = ItemAdapter<Item>()
itemAdapter.add(DeviceItem(model: device))
itemAdapter.add(SampleItem(name: name, font: font))
...
fastAdapter.adapter = modelAdapter
...
fastAdapter.with(listView: collectionView)
fastAdapter.with(listView: tableView)
...
modelAdapter.add(Device())

Event hooks

public extension Events {
    public static let close = Event(name: "close")
}

class SampleEventHookItem: Item, Hookable {
    func someFunction() {
        self.event(.close)
    }
}

fastAdapter.eventHooks.add(on: .close) {
    [weak self] item, event in
    self?.dismiss(animated: true, completion: nil)
}

Advanced event hooks

public extension Events {
    public static let customEvent = Event(name: "custom event")
}

class MyCustomEvent: Event {
    private let data: Data
    
    public init(data: Data) {
        self.data = data
        super.init(name: "custom event")
    }
}

class SampleEventHookItem: Item, Hookable {
    func someFunction() {
        self.event(MyCustomEvent(data: data))
    }
}

fastAdapter.eventHooks.add(on: .customEvent) {
    item, event in
    if let customEvent = event as? MyCustomEvent {
        print(customEvent.data)
    }
}

fastadapter-swift's People

Contributors

fabianterhorst avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fastadapter-swift's Issues

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.