ViewState

A View State library for Swift, it returns the results for each state.

How to use?

  • In ViewModel calls the states that will return to ViewController
func fetchMovie() -> ViewState<[Movie], AFError?> {
        self.viewState.fetchSource {
            self.service.getApiMovie(
                onSuccess: { (resultArray) in
                    self.movieArray = MovieMapper(resultArray: resultArray).transform()
                    self.viewState.success(data: self.movieArray)
            },
                onError: { (messageError) in
                    self.viewState.error(error: messageError)
            })
        }

      return viewState
}
  • In the ViewController it calls the ViewModel method and places the states of each one.
private func setupFetchMovie() {
     viewModel.fetchMovie()
          .successObserver(onSuccess)
          .loadingObserver(onLoading)
          .errorObserver(onError)
}
private func onSuccess(movie: [Movie]) {
      self.collection.reloadData()
      alert.dismiss(animated: true, completion: nil)
}
    
private func onLoading() {
      let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
      loadingIndicator.hidesWhenStopped = true
      loadingIndicator.style = UIActivityIndicatorView.Style.medium
      loadingIndicator.startAnimating();
        
      alert.view.addSubview(loadingIndicator)
      present(alert, animated: true, completion: nil)
}
    
private func onError(message: AFError?) {
      ErrorView.instance.showError()
}

Installation

Swift Package Manager

import ViewState
dependencies: [
    .package(url: "https://github.com/joaolfp/ViewState.git", .upToNextMajor(from: "1.0.3"))
]

Cocoapods

import ViewStateKit
target '<Your Target Name>' do
  pod 'ViewStateKit'
end
$ pod install

Author

Coded by João Lucas

GitHub