Skip to content
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

difference between "MVP" and "Flux" #5

Open
wants to merge 46 commits into
base: mvp
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
e16f4d2
add viewModels
marty-suzuki Jan 23, 2018
82f3e80
add Actions, Dispatcher, Stores
marty-suzuki Jan 23, 2018
c45ad67
fix syntax
marty-suzuki Feb 17, 2018
52705ee
Merge branch 'mvp' into mvvm
marty-suzuki Mar 1, 2019
e6e2ba9
fix conflict
marty-suzuki Mar 2, 2019
ec7f9e2
Merge branch 'mvp' into mvvm
marty-suzuki Mar 2, 2019
1149765
fix project
marty-suzuki Mar 2, 2019
a54c3d6
fix FavoriteModel
marty-suzuki Mar 3, 2019
42af1c9
fix SearchModel
marty-suzuki Mar 3, 2019
0dfe9fe
fix RepositoryModel
marty-suzuki Mar 3, 2019
65ac8f4
fix diff
marty-suzuki Mar 3, 2019
92a8c4d
Merge branch 'mvvm' into flux
marty-suzuki Mar 3, 2019
614e65c
fix conflict
marty-suzuki Mar 4, 2019
0882440
add Flux
marty-suzuki Mar 4, 2019
e2bd594
Merge branch 'mvp' into mvvm
marty-suzuki Mar 4, 2019
8734125
Merge branch 'mvvm' into flux
marty-suzuki Mar 4, 2019
afd129a
fix diff
marty-suzuki Mar 4, 2019
4aaf7e5
remove extra files
marty-suzuki Mar 4, 2019
f437c3c
Merge branch 'mvp' into mvvm
marty-suzuki Feb 12, 2021
e271174
Fix ApiSession
marty-suzuki Feb 12, 2021
82f4104
Fix FavoriteModel
marty-suzuki Feb 12, 2021
fc26e10
Fix RepositoryModel
marty-suzuki Feb 12, 2021
ee8508a
Fix SearchModel
marty-suzuki Feb 12, 2021
4b28c0d
Fix Favorite
marty-suzuki Feb 12, 2021
74b9128
Fix Repository
marty-suzuki Feb 12, 2021
6e17dd2
Fix UserRepository
marty-suzuki Feb 12, 2021
acc7832
Fix Search
marty-suzuki Feb 12, 2021
51087f4
Fix injection
marty-suzuki Feb 12, 2021
4cc53eb
Fix xcodeproj
marty-suzuki Feb 12, 2021
d0dbf62
Merge branch 'mvp' into mvvm
marty-suzuki Feb 12, 2021
ad63067
Fix images
marty-suzuki Feb 12, 2021
0016cc6
Merge branch 'mvp' into mvvm
marty-suzuki Feb 12, 2021
151c60e
Merge branch 'mvvm' into flux
marty-suzuki Feb 13, 2021
c99bf0c
Remove common flux
marty-suzuki Feb 14, 2021
9ee9f50
Add favorite flux
marty-suzuki Feb 14, 2021
1aea403
Add repository flux
marty-suzuki Feb 14, 2021
e6576ef
Add user repository flux
marty-suzuki Feb 14, 2021
ecbd20e
Add search flux
marty-suzuki Feb 14, 2021
4809ec9
Fix injection
marty-suzuki Feb 14, 2021
2eea7d7
Fix xcodeproj
marty-suzuki Feb 14, 2021
422e543
Fix injection
marty-suzuki Feb 14, 2021
43bb448
Merge branch 'mvvm' into flux
marty-suzuki Feb 14, 2021
b061a08
Fix xcodeproject
marty-suzuki Feb 14, 2021
588e4c6
Fix AppDelegate
marty-suzuki Feb 14, 2021
bb4ab4a
Update README
marty-suzuki Feb 14, 2021
46318b8
Fix images
marty-suzuki Feb 14, 2021
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
Binary file added Images/Flux/Views.key
Binary file not shown.
Binary file added Images/MVVM/Views.key
Binary file not shown.
Binary file modified Images/favorite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Images/repository.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Images/search.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Images/structure.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Images/user_repository.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 12 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# iOSDesignPatternSamples (MVP)
# iOSDesignPatternSamples (Flux)

This is Github user search demo app that made with MVP design pattern.
This is Github user search demo app that made with Flux design pattern.

## Application Structure

Expand All @@ -13,43 +13,40 @@ Search Github user and show user result list

![](./Images/search.png)

- [SearchView](./iOSDesignPatternSamples/Sources/UI/Search/SearchViewController.swift)
- [SearchPresenter](./iOSDesignPatternSamples/Sources/UI/Search/SearchViewPresenter.swift)
- [SearchViewPresenter](./iOSDesignPatternSamples/Sources/UI/Search/SearchViewPresenter.swift) <- Adapt SearchPresenter
- [SearchViewDataSource](./iOSDesignPatternSamples/Sources/UI/Search/SearchViewDataSource.swift) <- Adapt UITableViewDataSource and UITableViewDelegate
- [SearchAction](./iOSDesignPatternSamples/Sources/UI/Search/Flux/SearchAction.swift)
- [SearchStore](./iOSDesignPatternSamples/Sources/UI/Search/Flux/SearchStore.swift)

### [FavoriteViewController](./iOSDesignPatternSamples/Sources/UI/Favorite/FavoriteViewController.swift)
Show local on memory favorite repositories

![](./Images/favorite.png)

- [FavoriteView](./iOSDesignPatternSamples/Sources/UI/Favorite/FavoriteViewController.swift)
- [FavoritePresenter](./iOSDesignPatternSamples/Sources/UI/Favorite/FavoriteViewPresenter.swift)
- [FavoriteViewPresenter](./iOSDesignPatternSamples/Sources/UI/Favorite/FavoriteViewPresenter.swift) <- Adapt FavoritePresenter
- [FavoriteViewDataSource](./iOSDesignPatternSamples/Sources/UI/Favorite/FavoriteViewDataSource.swift) <- Adapt UITableViewDataSource and UITableViewDelegate
- [FavoriteAction](./iOSDesignPatternSamples/Sources/UI/Favorite/Flux/FavoriteAction.swift)
- [FavoriteStore](./iOSDesignPatternSamples/Sources/UI/Favorite/Flux/FavoriteStore.swift)

### [UserRepositoryViewController](./iOSDesignPatternSamples/Sources/UI/UserRepository/UserRepositoryViewController.swift)
Show Github user's repositories

![](./Images/user_repository.png)

- [UserRepositoryView](./iOSDesignPatternSamples/Sources/UI/UserRepository/UserRepositoryViewController.swift)
- [UserRepositoryPresenter](./iOSDesignPatternSamples/Sources/UI/UserRepository/UserRepositoryViewPresenter.swift)
- [UserRepositoryViewPresenter](./iOSDesignPatternSamples/Sources/UI/UserRepository/UserRepositoryViewPresenter.swift) <- Adapt UserRepositoryPresenter
- [UserRepositoryViewDataSource](./iOSDesignPatternSamples/Sources/UI/UserRepository/UserRepositoryViewDataSource.swift) <- Adapt UITableViewDataSource and UITableViewDelegate
- [UserRepositoryAction](./iOSDesignPatternSamples/Sources/UI/UserRepository/Flux/UserRepositoryAction.swift)
- [UserRepositoryStore](./iOSDesignPatternSamples/Sources/UI/UserRepository/Flux/UserRepositoryStore.swift)

### [RepositoryViewController](./iOSDesignPatternSamples/Sources/UI/Repository/RepositoryViewController.swift)
Show a repository and add / remove local on memory favorites

![](./Images/repository.png)

- [RepositoryView](./iOSDesignPatternSamples/Sources/UI/Repository/RepositoryViewController.swift)
- [RepositoryPresenter](./iOSDesignPatternSamples/Sources/UI/Repository/RepositoryViewPresenter.swift)
- [RepositoryViewPresenter](./iOSDesignPatternSamples/Sources/UI/Repository/RepositoryViewPresenter.swift) <- Adapt RepositoryPresenter
- [RepositoryAction](./iOSDesignPatternSamples/Sources/UI/Repository/Flux/RepositoryAction.swift)
- [RepositoryStore](./iOSDesignPatternSamples/Sources/UI/Repository/Flux/RepositoryStore.swift)


## How to add / remove favorites

You can add / remove favorite repositories in RepositoryViewController, but an Array of favorite repository is hold by FavoriteViewController.
You can add / remove favorite repositories in RepositoryViewController. Array of favorite repository is hold by FavoriteModel that injected to each actions, therefore you can use its reference everywhere!

## Run

Expand Down
96 changes: 80 additions & 16 deletions iOSDesignPatternSamples.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

80 changes: 56 additions & 24 deletions iOSDesignPatternSamples/Sources/Common/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
// Copyright © 2017年 marty-suzuki. All rights reserved.
//

import UIKit
import Combine
import GithubKit
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
Expand All @@ -22,42 +23,74 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
for value in viewControllers.enumerated() {
switch value {
case let (0, nc as UINavigationController):
let repositoryDispatcher = RepositoryDispatcher()
let searchDispatcher = SearchDispatcher()
let userRepositoryDispatcher = UserRepositoryDispatcher()
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() } }
),
mainAsync: { work in DispatchQueue.main.async { work() } },
notificationCenter: .default
action: SearchAction(
notificationCenter: .default,
dispatcher: searchDispatcher,
searchModel: SearchModel(
sendRequest: ApiSession.shared.send
)
),
store: SearchStore(
dispatcher: searchDispatcher
),
makeRepositoryPresenter: { [favoriteModel] in
RepositoryViewPresenter(
repository: $0,
makeUserRepositoryAction: { user in
UserRepositoryAction(
dispatcher: userRepositoryDispatcher,
repositoryModel: RepositoryModel(
user: user,
sendRequest: ApiSession.shared.send
)
)
},
makeUserRepositoryStore: { user in
UserRepositoryStore(
user: user,
dispatcher: userRepositoryDispatcher
)
},
makeRepositoryAction: { [favoriteModel] repository in
RepositoryAction(
repository: repository,
dispatcher: repositoryDispatcher,
favoriteModel: favoriteModel
)
},
makeUserRepositoryPresenter: {
UserRepositoryViewPresenter(
model: RepositoryModel(
user: $0,
sendRequest: ApiSession.shared.send
),
mainAsync: { work in DispatchQueue.main.async { work() } }
makeRepositoryStore: { repository in
RepositoryStore(
repository: repository,
dispatcher: repositoryDispatcher
)
}
)
nc.setViewControllers([searchVC], animated: false)

case let (1, nc as UINavigationController):
let favoriteDispatcher = FavoriteDispatcher()
let repositoryDispatcher = RepositoryDispatcher()
let favoriteVC = FavoriteViewController(
presenter: FavoriteViewPresenter(model: favoriteModel),
makeRepositoryPresenter: { [favoriteModel] in
RepositoryViewPresenter(
repository: $0,
action: FavoriteAction(
dispatcher: favoriteDispatcher,
favoriteModel: favoriteModel
),
store: FavoriteStore(
dispatcher: favoriteDispatcher
),
makeRepositoryAction: { [favoriteModel] repository in
RepositoryAction(
repository: repository,
dispatcher: repositoryDispatcher,
favoriteModel: favoriteModel
)
},
makeRepositoryStore: { repository in
RepositoryStore(
repository: repository,
dispatcher: repositoryDispatcher
)
}
)
nc.setViewControllers([favoriteVC], animated: false)
Expand All @@ -71,4 +104,3 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
return true
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ extension ApiSession {
}()
}

typealias SendRequest<T: Request> = (T, @escaping (Result<T.ResponseType, Swift.Error>) -> ()) -> AnyCancellable
typealias SendRequest<T: Request> = (T) -> AnyPublisher<T.ResponseType, Swift.Error>
78 changes: 53 additions & 25 deletions iOSDesignPatternSamples/Sources/Common/FavoriteModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,71 @@
// Copyright © 2017年 marty-suzuki. All rights reserved.
//

import Combine
import GithubKit

protocol FavoriteModelDelegate: AnyObject {
func favoriteDidChange()
}

extension FavoriteModelDelegate {
func favoriteDidChange() {}
}

protocol FavoriteModelType: AnyObject {
var favorites: [Repository] { get }
var delegate: FavoriteModelDelegate? { get set }
var favoritePublisher: Published<[Repository]>.Publisher { get }
func addFavorite(_ repository: Repository)
func removeFavorite(_ repository: Repository)
func contains(_ repository: Repository) -> AnyPublisher<Bool, Never>
}

final class FavoriteModel: FavoriteModelType {
private(set) var favorites: [Repository] = [] {
didSet {
delegate?.favoriteDidChange()
}
@Published
private(set) var favorites: [Repository] = []
var favoritePublisher: Published<[Repository]>.Publisher {
$favorites
}

weak var delegate: FavoriteModelDelegate?


private let _addFavorite = PassthroughSubject<Repository, Never>()
private let _removeFavorite = PassthroughSubject<Repository, Never>()
private var cancellables = Set<AnyCancellable>()

init() {
let favorites1 = _addFavorite
.flatMap { [weak self] repository -> AnyPublisher<[Repository], Never> in
guard let me = self else {
return Empty().eraseToAnyPublisher()
}
var favorites = me.favorites
if favorites.firstIndex(where: { $0.url == repository.url }) != nil {
return Empty().eraseToAnyPublisher()
}
favorites.append(repository)
return Just(favorites).eraseToAnyPublisher()
}

let favorites2 = _removeFavorite
.flatMap { [weak self] repository -> AnyPublisher<[Repository], Never> in
guard let me = self else {
return Empty().eraseToAnyPublisher()
}
var favorites = me.favorites
guard let index = favorites.firstIndex(where: { $0.url == repository.url }) else {
return Empty().eraseToAnyPublisher()
}
favorites.remove(at: index)
return Just(favorites).eraseToAnyPublisher()
}

favorites1.merge(with: favorites2)
.assign(to: \.favorites, on: self)
.store(in: &cancellables)
}

func addFavorite(_ repository: Repository) {
if favorites.firstIndex(where: { $0.url == repository.url }) != nil {
return
}
favorites.append(repository)
_addFavorite.send(repository)
}

func removeFavorite(_ repository: Repository) {
guard let index = favorites.firstIndex(where: { $0.url == repository.url }) else {
return
}
favorites.remove(at: index)
_removeFavorite.send(repository)
}

func contains(_ repository: Repository) -> AnyPublisher<Bool, Never> {
$favorites
.map { $0.contains { $0.url == repository.url } }
.eraseToAnyPublisher()
}
}
Loading