Skip to content

Commit

Permalink
Add VIPER Arch as example (#4)
Browse files Browse the repository at this point in the history
* feat: Add VIPER Arch as example
* refactor: Update hierarchy folders for VIPER module
  • Loading branch information
specktro committed Oct 18, 2021
1 parent 03ef856 commit f1bad50
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 3 deletions.
56 changes: 56 additions & 0 deletions Arch.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

/* Begin PBXBuildFile section */
5017EC81271B2AA000FD07C0 /* SharedViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5017EC80271B2AA000FD07C0 /* SharedViewController.swift */; };
5017ECA0271E20B700FD07C0 /* MessageInteractor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5017EC9F271E20B700FD07C0 /* MessageInteractor.swift */; };
5017ECA2271E20BF00FD07C0 /* MessagePresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5017ECA1271E20BF00FD07C0 /* MessagePresenter.swift */; };
5017ECA4271E20C900FD07C0 /* ShowMessageProtocols.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5017ECA3271E20C900FD07C0 /* ShowMessageProtocols.swift */; };
5017ECA6271E20CF00FD07C0 /* MessageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5017ECA5271E20CF00FD07C0 /* MessageView.swift */; };
50B428C21C385D9400939043 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50B428C11C385D9400939043 /* AppDelegate.swift */; };
50B428C41C385D9400939043 /* MVCViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50B428C31C385D9400939043 /* MVCViewController.swift */; };
50B428C91C385D9400939043 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 50B428C81C385D9400939043 /* Assets.xcassets */; };
Expand All @@ -32,6 +36,10 @@

/* Begin PBXFileReference section */
5017EC80271B2AA000FD07C0 /* SharedViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SharedViewController.swift; sourceTree = "<group>"; };
5017EC9F271E20B700FD07C0 /* MessageInteractor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MessageInteractor.swift; path = VIPER/interactor/MessageInteractor.swift; sourceTree = "<group>"; };
5017ECA1271E20BF00FD07C0 /* MessagePresenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MessagePresenter.swift; path = VIPER/presenter/MessagePresenter.swift; sourceTree = "<group>"; };
5017ECA3271E20C900FD07C0 /* ShowMessageProtocols.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ShowMessageProtocols.swift; path = VIPER/protocols/ShowMessageProtocols.swift; sourceTree = "<group>"; };
5017ECA5271E20CF00FD07C0 /* MessageView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MessageView.swift; path = VIPER/view/MessageView.swift; sourceTree = "<group>"; };
50B428BE1C385D9400939043 /* Arch.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Arch.app; sourceTree = BUILT_PRODUCTS_DIR; };
50B428C11C385D9400939043 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
50B428C31C385D9400939043 /* MVCViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MVCViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -66,6 +74,49 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
5017EC8F271C857200FD07C0 /* VIPER */ = {
isa = PBXGroup;
children = (
5017EC90271C858300FD07C0 /* protocols */,
5017EC91271C858A00FD07C0 /* view */,
5017EC92271C858E00FD07C0 /* presenter */,
5017EC95271C85A200FD07C0 /* interactor */,
);
name = VIPER;
sourceTree = "<group>";
};
5017EC90271C858300FD07C0 /* protocols */ = {
isa = PBXGroup;
children = (
5017ECA3271E20C900FD07C0 /* ShowMessageProtocols.swift */,
);
name = protocols;
sourceTree = "<group>";
};
5017EC91271C858A00FD07C0 /* view */ = {
isa = PBXGroup;
children = (
5017ECA5271E20CF00FD07C0 /* MessageView.swift */,
);
name = view;
sourceTree = "<group>";
};
5017EC92271C858E00FD07C0 /* presenter */ = {
isa = PBXGroup;
children = (
5017ECA1271E20BF00FD07C0 /* MessagePresenter.swift */,
);
name = presenter;
sourceTree = "<group>";
};
5017EC95271C85A200FD07C0 /* interactor */ = {
isa = PBXGroup;
children = (
5017EC9F271E20B700FD07C0 /* MessageInteractor.swift */,
);
name = interactor;
sourceTree = "<group>";
};
50B428B51C385D9400939043 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -93,6 +144,7 @@
50C690D41C38F53B00806296 /* MVC */,
50C690D31C38F52100806296 /* MVP */,
50C690D61C38F85700806296 /* MVVM */,
5017EC8F271C857200FD07C0 /* VIPER */,
50B428C81C385D9400939043 /* Assets.xcassets */,
50B428CA1C385D9400939043 /* LaunchScreen.storyboard */,
50B428CD1C385D9400939043 /* Info.plist */,
Expand Down Expand Up @@ -244,7 +296,11 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5017ECA6271E20CF00FD07C0 /* MessageView.swift in Sources */,
50B428F21C38660800939043 /* MVPViewController.swift in Sources */,
5017ECA0271E20B700FD07C0 /* MessageInteractor.swift in Sources */,
5017ECA4271E20C900FD07C0 /* ShowMessageProtocols.swift in Sources */,
5017ECA2271E20BF00FD07C0 /* MessagePresenter.swift in Sources */,
50C690D81C39001100806296 /* GreetingViewModel.swift in Sources */,
50C690DA1C3901B700806296 /* MVVMViewController.swift in Sources */,
5017EC81271B2AA000FD07C0 /* SharedViewController.swift in Sources */,
Expand Down
14 changes: 12 additions & 2 deletions Arch/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
let model: Person = Person(firstName: "Sprieto", lastName: "Moreno")

// MARK: - MVC Architecture
let view: MVCViewController = MVCViewController()
view.person = model
// let view: MVCViewController = MVCViewController()
// view.person = model

// MARK: - MVP Architecture
// let view: MVPViewController = MVPViewController()
Expand All @@ -35,6 +35,16 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
// let view: MVVMViewController = MVVMViewController()
// view.viewModel = viewModel

// MARK: - VIPER Architecture
let view: MessageViewProtocol = MessageView()
let presenter: MessagePresenterProtocol = MessagePresenter()
let interactor: MessageInteractorProtocol = MessageInteractor()
view.presenter = presenter
presenter.view = view
presenter.interactor = interactor
interactor.presenter = presenter
interactor.person = model

window?.rootViewController = view
window?.makeKeyAndVisible()

Expand Down
2 changes: 1 addition & 1 deletion Arch/SharedViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SharedViewController.swift
// Arch
//
// Created by Miguel Angel Gómez Rivero on 16/10/21.
// Created by specktro on 16/10/21.
// Copyright © 2021 specktro. All rights reserved.
//

Expand Down
26 changes: 26 additions & 0 deletions Arch/VIPER/interactor/MessageInteractor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// MessageInteractor.swift
// Arch
//
// Created by specktro on 17/10/21.
// Copyright © 2021 specktro. All rights reserved.
//

import Foundation

// MARK: MessageInteractor class
final class MessageInteractor: MessageInteractorProtocol {
// MARK: - Properties
weak var presenter: MessagePresenterProtocol?
var person: Person?

// MARK: - MessageInteractorProtocol methods
func prepareMessage() {
guard let person = person else {
preconditionFailure("The person model was not provided")
}

let greeting: String = "Hello" + " " + person.firstName + " " + person.lastName
presenter?.retrieve(message: greeting)
}
}
22 changes: 22 additions & 0 deletions Arch/VIPER/presenter/MessagePresenter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// MessagePresenter.swift
// Arch
//
// Created by specktro on 17/10/21.
// Copyright © 2021 specktro. All rights reserved.
//

import Foundation

final class MessagePresenter: MessagePresenterProtocol {
weak var view: MessageViewProtocol?
var interactor: MessageInteractorProtocol?

func askMessage() {
interactor?.prepareMessage()
}

func retrieve(message: String) {
view?.show(message)
}
}
38 changes: 38 additions & 0 deletions Arch/VIPER/protocols/ShowMessageProtocols.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// ShowMessageProtocols.swift
// Arch
//
// Created by specktro on 17/10/21.
// Copyright © 2021 specktro. All rights reserved.
//

import UIKit

// MARK: MessageViewProtocol
protocol MessageViewProtocol: UIViewController {
var presenter: MessagePresenterProtocol? { get set }

// Output
func show(_ message: String)
}

// MARK: MessagePresenterProtocol protocol
protocol MessagePresenterProtocol: AnyObject {
var view: MessageViewProtocol? { get set }
var interactor: MessageInteractorProtocol? { get set }

// Input
func askMessage()

// Output
func retrieve(message: String)
}

// MARK: MessageInteractorProtocol protocol
protocol MessageInteractorProtocol: AnyObject {
var presenter: MessagePresenterProtocol? { get set }
var person: Person? { get set }

// Input
func prepareMessage()
}
25 changes: 25 additions & 0 deletions Arch/VIPER/view/MessageView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// MessageView.swift
// Arch
//
// Created by specktro on 17/10/21.
// Copyright © 2021 specktro. All rights reserved.
//

import UIKit

// MARK: MessageView class
final class MessageView: SharedViewController, MessageViewProtocol {
// MARK: - Properties
var presenter: MessagePresenterProtocol?

// MARK: - SharedViewController method
override func didTapButton(_ button: UIButton) {
presenter?.askMessage()
}

// MARK: - MessageViewProtocol
func show(_ message: String) {
greetingLabel?.text = message
}
}

0 comments on commit f1bad50

Please sign in to comment.