In last post we have seen how to bind all classes together. In this post we would see app flows from view controller to interactor and to view controller.To make it more decoupled we would generate some protocols and implement it in classes.
Step:1 list-ViewController
Step:2 list-Presenter
Step:3 list-Interactor
Step:4 list-Presenter
Protocols:
1) ViewController Interface:
Implemented by ViewController
Object owned by Presenter
2) Presenter Interface:
Implemented by Presenter
Object owned by ViewController
3) Interactor Input Interface:
Implemented by interactor
Object owned by presenter
4) Interactor output Interface:
Implemented by presenter
Object owned by interactor
Note:Presenter is implementing two protocols.
This is the end of the post. In next post we will see how VIPER is useful for testing and coverage.
Step:1 list-ViewController
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.global().async {
self.eventHandler?.updateView()
}
}
Step:2 list-Presenter
func updateView() {
listInteractor?.findListData()
}
Step:3 list-Interactor
func findListData() {
//Find data
//output?.foundListData(aList: nil);
let reader = FileReader()
if let data = reader.readJsonFile(name: "company") {
let myArr:NSArray? = ParseData(aObject: data)
if myArr != nil {
output?.foundList(aList: myArr);
}
}
}
Step:4 list-Presenter
func foundList(aList:NSArray?) {
let sortedArray:NSArray? = self.SortCompanyList(aList: aList)
if(sortedArray != nil) {
userInterface?.reloadList(alist: sortedArray)
}
else {
userInterface?.showNoContenctView()
}
}
Protocols:
1) ViewController Interface:
Implemented by ViewController
Object owned by Presenter
protocol listViewInterface {
func showNoContenctView()
func reloadList(alist:NSArray?)
}
*********************************************************
class ViewController: UIViewController,listViewInterface
{
//MARK: listViewInterface methods implementation
func reloadList(alist:NSArray?) {
//reload table view
}
func showNoContenctView() {
//Show alert code
}
}
*********************************************************
class listPresenter: NSObject {
var userInterface:listViewInterface?
}
2) Presenter Interface:
Implemented by Presenter
Object owned by ViewController
protocol listPresenterInterface {
func updateView();
}
*********************************************************
class listPresenter: NSObject,listPresenterInterface {
var userInterface:listViewInterface?
func updateView() {
//Call for data adjustment and interactor calling
}
}
*********************************************************
class ViewController: UIViewController,listViewInterface {
var eventHandler:listPresenterInterface?
override func viewDidLoad() {
super.viewDidLoad()
self.eventHandler?.updateView();
}
//MARK: listViewInterface implementation
func reloadList(alist:NSArray?) {
//reload table view
}
func showNoContenctView(){
//Show alert code
}
}
3) Interactor Input Interface:
Implemented by interactor
Object owned by presenter
protocol listInteractorInput {
func findListData()
}
*********************************************************
class listInteractor: NSObject,listInteractorInput {
func findListData() {
//Find data
}
*********************************************************
class listPresenter: NSObject,listPresenterInterface {
var listInteractor:listInteractorInput?
func updateView() {
//Code for data adjustment and interactor calling
listInteractor?.findListData();
}
}
4) Interactor output Interface:
Implemented by presenter
Object owned by interactor
protocol listInteractorOutput {
func foundList(aList:NSArray?)
}
*********************************************************
class listPresenter: NSObject,listPresenterInterface,listInteractorOutput {
var listInteractor:listInteractorInput?
var userInterface:listViewInterface?
func updateView() {
listInteractor?.findListData();
}
func foundList(aList:NSArray?) {
userInterface?.reloadList(alist: sortedArray);
}
}
*********************************************************
class listInteractor: NSObject,listInteractorInput {
var output:listInteractorOutput?
func findListData() {
//Find data
}
}
Note:Presenter is implementing two protocols.
This is the end of the post. In next post we will see how VIPER is useful for testing and coverage.
No comments:
Post a Comment
Please do not spam.