Skip to content

difference between "MVC" and "MVP" #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: mvc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix injection
  • Loading branch information
marty-suzuki committed Feb 11, 2021
commit f519cc1382f4d571cc645145b86097f42f82d577
38 changes: 35 additions & 3 deletions iOSDesignPatternSamples/Sources/Common/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,51 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

let favoritePresenter = FavoriteViewPresenter()
let favoriteModel = FavoriteModel()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

if let viewControllers = (window?.rootViewController as? UITabBarController)?.viewControllers {
for value in viewControllers.enumerated() {
switch value {
case let (0, nc as UINavigationController):
let searchVC = SearchViewController(searchPresenter: SearchViewPresenter(), favoritePresenter: favoritePresenter)
let searchVC = SearchViewController(
searchPresenter: SearchViewPresenter(
model: SearchModel(
sendRequest: ApiSession.shared.send,
asyncAfter: { DispatchQueue.global().asyncAfter(deadline: $0, execute: $1) }
),
mainAsync: { work in DispatchQueue.main.async { work() } },
notificationCenter: .default
),
makeRepositoryPresenter: { [favoriteModel] in
RepositoryViewPresenter(
repository: $0,
favoriteModel: favoriteModel
)
},
makeUserRepositoryPresenter: {
UserRepositoryViewPresenter(
model: RepositoryModel(
user: $0,
sendRequest: ApiSession.shared.send
),
mainAsync: { work in DispatchQueue.main.async { work() } }
)
}
)
nc.setViewControllers([searchVC], animated: false)

case let (1, nc as UINavigationController):
let favoriteVC = FavoriteViewController(presenter: favoritePresenter)
let favoriteVC = FavoriteViewController(
presenter: FavoriteViewPresenter(model: favoriteModel),
makeRepositoryPresenter: { [favoriteModel] in
RepositoryViewPresenter(
repository: $0,
favoriteModel: favoriteModel
)
}
)
nc.setViewControllers([favoriteVC], animated: false)

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ final class FavoriteViewController: UIViewController, FavoriteView {
let presenter: FavoritePresenter
let dataSource: FavoriteViewDataSource

init(presenter: FavoritePresenter) {
private let makeRepositoryPresenter: (Repository) -> RepositoryPresenter

init(
presenter: FavoritePresenter,
makeRepositoryPresenter: @escaping (Repository) -> RepositoryPresenter
) {
self.presenter = presenter
self.dataSource = FavoriteViewDataSource(presenter: presenter)
self.makeRepositoryPresenter = makeRepositoryPresenter
super.init(nibName: FavoriteViewController.className, bundle: nil)
}

Expand All @@ -40,7 +46,7 @@ final class FavoriteViewController: UIViewController, FavoriteView {
}

func showRepository(with repository: Repository) {
let repositoryPresenter = RepositoryViewPresenter(repository: repository, favoritePresenter: presenter)
let repositoryPresenter = makeRepositoryPresenter(repository)
let vc = RepositoryViewController(presenter: repositoryPresenter)
navigationController?.pushViewController(vc, animated: true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,21 @@ final class SearchViewController: UIViewController, SearchView {
let searchBar = UISearchBar(frame: .zero)
let loadingView = LoadingView()

let favoritePresenter: FavoritePresenter
let searchPresenter: SearchPresenter
let dataSource: SearchViewDataSource

init(searchPresenter: SearchPresenter, favoritePresenter: FavoritePresenter) {
private let makeRepositoryPresenter: (Repository) -> RepositoryPresenter
private let makeUserRepositoryPresenter: (User) -> UserRepositoryPresenter

init(
searchPresenter: SearchPresenter,
makeRepositoryPresenter: @escaping (Repository) -> RepositoryPresenter,
makeUserRepositoryPresenter: @escaping (User) -> UserRepositoryPresenter
) {
self.searchPresenter = searchPresenter
self.favoritePresenter = favoritePresenter
self.dataSource = SearchViewDataSource(presenter: searchPresenter)
self.makeRepositoryPresenter = makeRepositoryPresenter
self.makeUserRepositoryPresenter = makeUserRepositoryPresenter
super.init(nibName: SearchViewController.className, bundle: nil)
}

Expand Down Expand Up @@ -95,8 +102,11 @@ final class SearchViewController: UIViewController, SearchView {
}

func showUserRepository(with user: User) {
let presenter = UserRepositoryViewPresenter(user: user)
let vc = UserRepositoryViewController(userRepositoryPresenter: presenter, favoritePresenter: favoritePresenter)
let presenter = makeUserRepositoryPresenter(user)
let vc = UserRepositoryViewController(
userRepositoryPresenter: presenter,
makeRepositoryPresenter: makeRepositoryPresenter
)
navigationController?.pushViewController(vc, animated: true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// Copyright © 2017年 marty-suzuki. All rights reserved.
//

import UIKit
import GithubKit
import UIKit

protocol UserRepositoryView: class {
func reloadData()
Expand All @@ -23,15 +23,18 @@ final class UserRepositoryViewController: UIViewController, UserRepositoryView {

let loadingView = LoadingView()

let favoritePresenter: FavoritePresenter
let userRepositoryPresenter: UserRepositoryPresenter
let dataSource: UserRepositoryViewDataSource

private let makeRepositoryPresenter: (Repository) -> RepositoryPresenter

init(userRepositoryPresenter: UserRepositoryPresenter, favoritePresenter: FavoritePresenter) {
self.favoritePresenter = favoritePresenter
init(
userRepositoryPresenter: UserRepositoryPresenter,
makeRepositoryPresenter: @escaping (Repository) -> RepositoryPresenter
) {
self.userRepositoryPresenter = userRepositoryPresenter
self.dataSource = UserRepositoryViewDataSource(presenter: userRepositoryPresenter)

self.makeRepositoryPresenter = makeRepositoryPresenter
super.init(nibName: UserRepositoryViewController.className, bundle: nil)
hidesBottomBarWhenPushed = true
}
Expand All @@ -52,7 +55,7 @@ final class UserRepositoryViewController: UIViewController, UserRepositoryView {
}

func showRepository(with repository: Repository) {
let repositoryPresenter = RepositoryViewPresenter(repository: repository, favoritePresenter: favoritePresenter)
let repositoryPresenter = makeRepositoryPresenter(repository)
let vc = RepositoryViewController(presenter: repositoryPresenter)
navigationController?.pushViewController(vc, animated: true)
}
Expand Down