Skip to content

arthursobrosa/swift-architecture

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Architecture

This repo is a suggestion of an architecture to be used in a UIKit MVVM project.


Rules

Architecture image

- ViewModel does bussiness logics only and is connected to ViewController via a binding Box;

- ViewController is injected with a ViewModel and controls any delegate methods from the View;

- View is used exclusively to create visual stuff;

- View shouldn't have any references to a ViewModel;

- View contains a delegate to connect itself to the ViewController;

(Please check out the example project at the top of the page)


Box

Box example image

- A Box is a class that receives a generic type variable as its value. When the value is changed, the box will trigger a closure with this value as the argument;

- This class will be used to connect properties from a ViewModel to a ViewController;


ViewCode protocol

ViewCode protocol example image

- This protocol will be used when setting up the layout of a view;

- For example, we can create an extension to a view and subscribe this extension to the ViewCodeProtocol. By doing so, the View will automatically contain a setupUI() method;


Delegate

Delegate example image

- Delegates will be used to connect a ViewController to a View;

- Since a View cannot contain any business logics, it will delegate any non-visual tasks to a ViewController;


MVVM example

ViewModel

ViewModel example image

- The ViewModel will be responsible for any business logics, as seen in the method example above (changeText());

- The boxes are created within a ViewModel and are used to keep track of a property's state (just like a @Published property in SwiftUI);


ViewController

ViewController example 1 image

ViewController example 2 image

- ViewController has an instance of ViewModel (let viewModel: ViewModel) and sets its value inside the init;

- ViewController also has an instance of View (lazy var myView: View) and sets its delegate inside its closure initializer (myView.delegate = self);

- (Make sure the ViewController conforms to the correct delegate protocol);

- Inside loadView() method, the ViewController's view is set equal to myView. That automatically makes myView fit perfectly inside the ViewController (no need to set any constraints);

- Inside viewDidLoad() method, we create the connection between ViewController and ViewModel by providing an implementation to the closure present inside the ViewModel's box property;

- Inside viewWillAppear() method, we change an information inside myView;


View

View example 1 image

View example 2 image

- View has an instance of the ViewController delegate and that's how they are connected to each other;

- View contains two subviews (properties): a label (myLabel) and a button (myButton);

- setupUI() method is called within View's initializer to set subviews layout;

- View contains a method that serves as the target for when myButton is tapped (@objc onTapMyButton());

- When the button is tapped, the View calls its delegate and some logics will be performed by the ViewController. In that case, myLabel's text will be updated;

About

Suggestion for a concise MVVM architecture in UIKit

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages