Creating MVP Template with Base View Controller
Introduction
When iOS developers working with design patterns like MVP or MVVM…etc, you need to create multiple files and interact with these files with each other, and you need to repeat these steps every time you create a new screen or new view controller.
so, creating a template for your design pattern is very useful, but at most time you have BaseViewController for your project to handle some actions with easy way, today we create our MVP template with BaseViewController to handle Boilerplate code
Creating StatusApi
most time we need to handle status from a server like handling error, show-hide loading, and Failure, let’s create a protocol to handle it:
protocol StatusApi : class {
func onError(_ message : String )
func onFailure (_ message : String )
func showLoading ()
func hideLoading ()
}
Creating BaseViewController
We create BaseViewController to reduce Duplicate Code, we assume that we create a design in our app without a storyboard. just coding, and also we create BaseVC with a generic class of view
import UIKit
class BaseVC <T : UIView > : UIViewController { override func loadView() {
let t = T()
t.backgroundColor = .white
self.view = t
} var mainView : T {
if let view = self.view as? T {
return view
}else {
let view = T()
self.view = view
return view
}
}
}
now we need to implement the protocol on the base viewContnroller:
import UIKit
class BaseVC <T : UIView > : UIViewController , StatusApi { func onError(_ message : String ){
// show error message
}
func onFailure (_ message : String ){
// show Failure message
}
func showLoading () {
// show loading
}
func hideLoading () {
// hide loading
} override func loadView() {
let t = T()
t.backgroundColor = .white
self.view = t
} var mainView : T {
if let view = self.view as? T {
return view
}else {
let view = T()
self.view = view
return view
}
}
}
We prepared Boilerplate code, so now we start to create our MVP Template
we can copy the Xcode template and edit on it.
Go to: Finder → Applications → Xcode → right-click and select “Show Package Content” → Contents → Developer → Library → Xcode → Templates → File Templates → Source
You can see the implemented templates in Xcode,
copy folder “Swift File.xctemplate” and paste in on desktop
Explore Template Folder
open folder and you can see these files
in TemplateInfo.plist write options for our template
Copy ___FILEBASENAME___.swift 3 time and rename files with :
1- ___FILEBASENAME___VC.swift
2- ___FILEBASENAME___View.swift
3- ___FILEBASENAME___Presenter.swift
4- ___FILEBASENAME___Interactor.swif
Now we writing code in every file
In file ___FILEBASENAME___VC.swift :
In File ___FILEBASENAME___View.swift :
In File ___FILEBASENAME___Presenter.swift
In File ___FILEBASENAME___Interactor.swif
Now our MVP Template is now ready to use, so we need move folder to the same folder we copied from but Don’t forget to change the file name from
“Swift File.xctemplate” to “MVP Template.xctemplate”
goto: Finder → Applications → Xcode → right-click and select “Show Package Content” → Contents → Developer → Library → Xcode → Templates → File Templates → Source
Create a project to test our MVP Template and create BaseVC and StatusApi, Right-click on the folder and select New File
You can see your template, maybe icon not appear in the first time and you need to restart your computer
when you click on the Next button, text field appears to write the name
I write “Home” and click on Next button, you can see the location of files
Click Create and XCode created your files depends on your Template
Thanks for Reading, Tap the 👏 button if you found this article useful!
Sources:
Template: https://github.com/Yusef-Naser/MVP-Template
Project: https://github.com/Yusef-Naser/TestMVPTemplate