xmartlabs / Eureka

Elegant iOS form builder in Swift
https://eurekacommunity.github.io
MIT License
11.79k stars 1.33k forks source link

[question: ButtonRow] How to prepare view controller before pushing it? #2091

Closed Ludotrico closed 3 years ago

Ludotrico commented 4 years ago

My code currently looks like this:

        <<< ButtonRow() {
            $0.title = "Restrictions"

            $0.presentationMode = .presentModally(controllerProvider: ControllerProvider.callback(builder: {
                let VC = RestrictionsVC()

                VC.event = self.event
                VC.editingEvent = true

                return UINavigationController(rootViewController: VC)

            }), onDismiss: nil)
        }.onCellSelection {  _, row in
                //Display a UIActivityIndicator (spinner) to indicate API call to user
                let cell = row.cell!

                cell.addSubview(spinner)
                spinner.pinEdges(to: cell)
        }

This works fine but I would like to complete an API call when the user selects on the row. As of now, when the user taps on the row, it immediately pushes the view controller. I would like for the activity indicator to appear in the row, and only push the view controller once my API call is completed. How can I do this?

rkohl commented 3 years ago

You need to override func customDidSelect()

Create a row subclass and add UIActivityIndicator to cell.

`open class _RowPresenter: Row<ButtonCellOf>, PresenterRowType where VCType: UIViewController {

open var spinner: UIActivityIndicatorView = UIActivityIndicatorView(style: .medium)

private func addSpinnerToCell() { spinner.hidesWhenStopped = true cell.addSubview(spinner) //do layout stuff }

required public init(tag: String?) { super.init(tag: tag)

addSpinnerToCell()

}

open override func customDidSelect() { super.customDidSelect() if let presentationMode = presentationMode, !isDisabled { if let controller = presentationMode.makeController() { controller.row = self onPresentCallback?(cell.formViewController()!, controller)

    spinner.startAnimating()
    // do API call
    // call line below when ready to present
    presentationMode.present(controller, row: self, presentingController: cell.formViewController()!)
  } else {
    presentationMode.present(nil, row: self, presentingController: cell.formViewController()!)
  }
}

}`

mats-claassen commented 3 years ago

Closing due to lack of activity