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 "MVC" and "Flux" #3

Open
wants to merge 70 commits into
base: mvc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
f298266
add DataSources and Presenters
marty-suzuki Jan 23, 2018
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
44bcb71
sort project
marty-suzuki Feb 28, 2019
adeec19
Merge branch 'mvc' into mvp
marty-suzuki Feb 28, 2019
1678d8f
fix conflict
marty-suzuki Feb 28, 2019
9bde5a1
fix Main.storyboard
marty-suzuki Mar 1, 2019
41ac4a8
Merge branch 'mvc' into mvp
marty-suzuki Mar 1, 2019
04b397c
fix conflict
marty-suzuki Mar 1, 2019
af026a7
Merge branch 'mvc' into mvp
marty-suzuki Mar 1, 2019
dabba37
fix initializer
marty-suzuki Mar 1, 2019
52705ee
Merge branch 'mvp' into mvvm
marty-suzuki Mar 1, 2019
e6e2ba9
fix conflict
marty-suzuki Mar 2, 2019
9bb5b8e
Merge branch 'mvc' into mvp
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
c09a019
Merge branch 'mvc' into mvp
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
40358f6
Merge branch 'mvc' into mvp
marty-suzuki Feb 11, 2021
a099ef1
Fix UserRepositoryViewPresenter
marty-suzuki Feb 11, 2021
fd6ab91
Fix RepositoryViewPresenter
marty-suzuki Feb 11, 2021
b9bcd11
Fix FavoriteViewPresenter
marty-suzuki Feb 11, 2021
ddade5a
Fix SearchViewPresenter
marty-suzuki Feb 11, 2021
f519cc1
Fix injection
marty-suzuki Feb 11, 2021
3437684
Merge branch 'mvc' into mvp
marty-suzuki Feb 11, 2021
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
4f3d146
Fix FavoriteViewPresenter
marty-suzuki Feb 12, 2021
ac28868
Merge branch 'mvc' into mvp
marty-suzuki Feb 12, 2021
745d7ed
Fix images
marty-suzuki Feb 12, 2021
efee15f
Merge branch 'mvc' into mvp
marty-suzuki Feb 12, 2021
c40298b
Fix images
marty-suzuki Feb 12, 2021
d0dbf62
Merge branch 'mvp' into mvvm
marty-suzuki Feb 12, 2021
ad63067
Fix images
marty-suzuki Feb 12, 2021
ef603e2
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 modified Images/MVC/Views.key
Binary file not shown.
Binary file added Images/MVP/Views.key
Binary file not shown.
Binary file added Images/MVVM/Views.key
Binary file not shown.
Binary file added 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 added 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 added 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 added 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.
45 changes: 38 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
# iOSDesignPatternSamples (MVC)
# iOSDesignPatternSamples (Flux)

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

## Application Structure

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

## ViewControllers

- [SearchViewController](./iOSDesignPatternSamples/Sources/UI/Search/SearchViewController.swift) -> Search Github user and show user result list
- [FavoriteViewController](./iOSDesignPatternSamples/Sources/UI/Favorite/FavoriteViewController.swift) -> Show local on memory favorite repositories
- [UserRepositoryViewController](./iOSDesignPatternSamples/Sources/UI/UserRepository/UserRepositoryViewController.swift) -> Show Github user's repositories
- [RepositoryViewController](./iOSDesignPatternSamples/Sources/UI/Repository/RepositoryViewController.swift) -> Show a repository and add / remove local on memory favorites
### [SearchViewController](./iOSDesignPatternSamples/Sources/UI/Search/SearchViewController.swift)
Search Github user and show user result list

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

- [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)

- [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)

- [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)

- [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
92 changes: 92 additions & 0 deletions iOSDesignPatternSamples.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

76 changes: 64 additions & 12 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,24 +23,76 @@ 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(
searchModel: SearchModel(
sendRequest: ApiSession.shared.send,
asyncAfter: { DispatchQueue.global().asyncAfter(deadline: $0, execute: $1) },
mainAsync: { work in DispatchQueue.main.async { work() } }
),
makeFavoriteModel: { [favoriteModel] in favoriteModel },
makeRepositoryModel: {
RepositoryModel(
user: $0,
action: SearchAction(
notificationCenter: .default,
dispatcher: searchDispatcher,
searchModel: SearchModel(
sendRequest: ApiSession.shared.send
)
),
store: SearchStore(
dispatcher: searchDispatcher
),
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
)
},
makeRepositoryStore: { repository in
RepositoryStore(
repository: repository,
dispatcher: repositoryDispatcher
)
}
)
nc.setViewControllers([searchVC], animated: false)

case let (1, nc as UINavigationController):
let favoriteVC = FavoriteViewController(favoriteModel: favoriteModel)
let favoriteDispatcher = FavoriteDispatcher()
let repositoryDispatcher = RepositoryDispatcher()
let favoriteVC = FavoriteViewController(
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)

default:
Expand All @@ -51,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