All About MVVM Base Protocols
Introduction
MVVM is a highly preferred architecture. I also often prefer MVVM architecture in my projects. Recently, I realized that when starting a new project, I constantly copy from my previous projects and I even make just copy-paste many parts of it. I got bored of this and looked for a better way and came across to produce a SDK. Moreover, I can make it available to anyone who likes it.
I aimed to produce an SDK that will enable easy use of the MVVM architecture. This article was created to explain how to use this SDK.
For Whom
For anyone like me who makes a copy-paste from the beginning in every new project and prefers the MVVM architecture. I hope I will make it easier especially for beginners to learn MVVM architecture while using this SDK.
Explanation
Due to the MVVM architecture, it is shaped on 3 different classes or structs, naturally these are also available in the SDK:
• Model
• View
• ViewModel
And in addition to these it contains 2 extra extensions in SDK:
• NSObjectExtension (a NSObject extension that returns the names of classes)
• ViewController+TapGesture (ViewController extension that closes the keyboard when tapping empty space in any ViewController)
File Structure
Extensions
NSObjectExtension
Manually entering the storyboard names as string literals is one method, I use to avoid this as it will cause simple errors later on. It is especially useful in cases where Storyboard and classes share the same name as UIViewController or UITableViewCell.
In short, it returns the name of any class or struct that conforms NSObject class as a string.
ViewController+TapGesture
In the application you prepared, the user opened the keyboard and typed. Then what? Even if empty areas are touched, the keyboard does not close, I use this simple extension for the solution.
The hideKeyboardWhenTappedAround()
method must be called before the keyboard is requested to close. It makes sense to call it in viewDidLoad function to ensure this.
Example usage:
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
}
Referance: https://stackoverflow.com/a/27079103/14814820
Protocols
BaseModelProtocol
This is the only protocol without any rule. Since there is no feature that I can say should be common in model structs, this place was left blank. I didn’t remove it though for possible rules.
BaseViewProtocol
In MVVM architecture, a view must interact with the corresponding viewModel. Since this will apply to every page, it would be correct to add it to BaseViewProtocol.
Also, since UIs can be kept in different storyboards. (I’m in favor of keeping them all separate anyway.) To avoid confusion here; I made it mandatory for each view class to keep its own storyboard name.
instantiate func
This extension can only be used in classes conforming to UIViewController. It is done through to the phrase “where Self: UIViewController”.
I think the rest is clear enough. First the storyBoard and then the viewController are created. And the reference is returned.
BaseViewModelProtocol
In MVVM architecture, a viewModel must interact with the view and the model. Then I thought that associating these other elements with the viewModel will appear in any case, so I should add this situation with BaseViewModelProtocol.
startPage func
I’ve always preferred that page switching be managed in the viewModel. It is used to switch to another page via the startPage extension.
To explain the codes, first the view’s instantiate(custom method in BaseViewProtocol, explained above) method is called. This starts the view. The instance of this created view is assigned to the delegate of the ViewModel. The viewModel property of the corresponding viewController is assigned the reference of the viewModel we are working on.
In short, through the startPage method, the values of the properties in the Base Protocols are assigned. <ViewModel>.delegate and <ViewController>.viewModel
Example usage:
It is only necessary to push the viewController returned when the startPage method of the relevant page is called, to the navigationController.
How did I use it?
Before any page is created, I prefer to create a file where I append “Contract” to the end of the page name and collect all the protocols here. I would be able to see every feature of that page as titles here. (method names and some variables)
I created new protocols for each MVVM element. For example, DetailPageViewModelProtocol must conform to BaseViewModelProtol. I also specified that the viewType should be DetailPageViewControllerProtocol just below, and the ModelType should be the DetailPageModelProtocol. I have expressed this information by adding it after the where statement.
By placing these, variables such as delegate and viewModel in BaseProtocols must be created from classes conforming to the protocols I have given here. Also, being here makes it much more organized.
In each protocol I add the names of the methods that should be extra in these classes.
Example: DetailPageViewModel Class
After preparing the page-specific protocols, it is time to write the classes that will conform to these protocols.
As it is known, when a protocol is conformed, you can add the methods required by the protocol to that class via to xcode. After typealias is determined, the type of the model and the delegate are also determined, which are the variables of the ViewModel. Xcode helps us add them later. So, with the second click, it’s all ready.
Next, it is necessary to fill in the remaining methods for that class. This process continues in the same way for the other elements.
Example project link: https://github.com/TeyUs/Sample_CoreData_MVVM
Conclusion
It will be enough to add BaseProtocols at the beginning of each project, instead of rewriting the clear rules.
I’ve been starting to copy-paste in all the small project I made, I won’t do this anymore.
And it makes me very happy to know that anyone who likes it can use it.
How to Use the SDK
For this, it is necessary to add the package to your project.
File > Add Packages… >( Paste the link in the search bar above:https://github.com/TeyUs/MVVMBaseProtocols.git ) > Add Packages
If you want to use it like me, you can use it as I explained throughout this article after writing “import MVVMBaseProtocols” at the beginning of these file.