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 "MVVM" #2

Open
wants to merge 49 commits into
base: mvc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 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
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
c09a019
Merge branch 'mvc' into mvp
marty-suzuki Mar 4, 2019
e2bd594
Merge branch 'mvp' into mvvm
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
422e543
Fix injection
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 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.
38 changes: 32 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
# iOSDesignPatternSamples (MVC)
# iOSDesignPatternSamples (MVVM)

This is Github user search demo app that made with MVC design pattern.
This is Github user search demo app that made with MVVM 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)

- [SearchViewModel](./iOSDesignPatternSamples/Sources/UI/Search/SearchViewModel.swift)
- [SearchViewDataSource](./iOSDesignPatternSamples/Sources/UI/Search/SearchViewDataSource.swift) <- Adapt UITableViewDataSource and UITableViewDelegate

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

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

- [FavoriteViewModel](./iOSDesignPatternSamples/Sources/UI/Favorite/FavoriteViewModel.swift)
- [FavoriteViewDataSource](./iOSDesignPatternSamples/Sources/UI/Favorite/FavoriteViewDataSource.swift) <- Adapt UITableViewDataSource and UITableViewDelegate

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

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

- [UserRepositoryViewModel](./iOSDesignPatternSamples/Sources/UI/UserRepository/UserRepositoryViewModel.swift)
- [UserRepositoryViewDataSource](./iOSDesignPatternSamples/Sources/UI/UserRepository/UserRepositoryViewDataSource.swift) <- Adapt UITableViewDataSource and UITableViewDelegate

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

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

- [RepositoryViewModel](./iOSDesignPatternSamples/Sources/UI/Repository/RepositoryViewModel.swift)

## How to add / remove favorites

Expand Down
28 changes: 28 additions & 0 deletions iOSDesignPatternSamples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@
0706D2760E1B3A990D0C277A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64602919324DEDBC0429D452 /* AppDelegate.swift */; };
072EB0FFF0B72F37C8CF669A /* SearchModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9ED48619F4A0CDE4B2D46702 /* SearchModel.swift */; };
1791BB0E1AEB38868026578F /* RepositoryViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D51BC0E506CEA4C9ACAA8DB /* RepositoryViewController.swift */; };
1B6B63FE22A5A86F6028991E /* UserRepositoryViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85BA8AC18BFBE203EBECBDF0 /* UserRepositoryViewModel.swift */; };
450252B4BA07F8D498E608EF /* RepositoryViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42AAD61E2A4041414E741BC4 /* RepositoryViewModel.swift */; };
4EF255BBD109181D8AD5058F /* SearchViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 208BE35457B09256BF71DAD1 /* SearchViewController.swift */; };
5A6EC25DC03393E8F8E064FF /* ApiSession.extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34175ABC0E7BB9870D592CEF /* ApiSession.extension.swift */; };
651202151F5333A9ABE09B22 /* SearchViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B8FB5FFB17FCEE46B7E72E4 /* SearchViewModel.swift */; };
68266EFC53379F0728F6B00B /* FavoriteViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = E0D61178C6EC742BD2F981F1 /* FavoriteViewController.xib */; };
6D22F97989A935ECA42DB3CA /* SearchViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 5EDBADE530FE153A9651F109 /* SearchViewController.xib */; };
6EC13FBC2AE1E8DBEBE88804 /* FavoriteViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6AFD90EB20BB491847814C5E /* FavoriteViewController.swift */; };
71E4B210FC5945A53739149D /* LoadingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E2B00ED49EA7B8CD6C7C4F5 /* LoadingView.swift */; };
72E107687058F53E4B2EF247 /* FavoriteViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A35DE51E635D96C6FB469DF /* FavoriteViewModel.swift */; };
7D1CB8434AAE6D1FC50B9D2E /* SafariServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA63604B63E10BD6DC6520D0 /* SafariServices.framework */; };
7F3AC0844FF343E754FD4431 /* SearchViewDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E02ECB42B97D30045AF6AB2 /* SearchViewDataSource.swift */; };
811C3B9B712E3B67E5AD73FD /* UIKeyboardInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 711633F4B85B3F76D0C88E41 /* UIKeyboardInfo.swift */; };
82992D62FE8ED0043356C237 /* FavoriteViewDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5DE1FF29F81CED271FD929C2 /* FavoriteViewDataSource.swift */; };
9B515DE20E1424AC3D1F08CF /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AE92440962235D7ABB12EAFA /* Main.storyboard */; };
B7B8D28336669C7BF3C29BEC /* UserRepositoryViewDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 969B79174D1421B0FCFE7154 /* UserRepositoryViewDataSource.swift */; };
C3F5B4AE9DAFA2095EBCB56A /* NSObjectProtocol.extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 186C7AADB8679B060E7A2C1B /* NSObjectProtocol.extension.swift */; };
C69B1DDE761E0663FDE1E947 /* UserRepositoryViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6AE9204A886B2448F36B9293 /* UserRepositoryViewController.swift */; };
D0CFC875B535D97424BB8589 /* GithubKit in Frameworks */ = {isa = PBXBuildFile; productRef = C2FA27FA77B01E3C42D84622 /* GithubKit */; };
Expand All @@ -35,13 +42,20 @@
208BE35457B09256BF71DAD1 /* SearchViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchViewController.swift; sourceTree = "<group>"; };
260D4E07190EC496827E1037 /* iOSDesignPatternSamples.app */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = wrapper.application; path = iOSDesignPatternSamples.app; sourceTree = BUILT_PRODUCTS_DIR; };
34175ABC0E7BB9870D592CEF /* ApiSession.extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApiSession.extension.swift; sourceTree = "<group>"; };
3A35DE51E635D96C6FB469DF /* FavoriteViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoriteViewModel.swift; sourceTree = "<group>"; };
3B8FB5FFB17FCEE46B7E72E4 /* SearchViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchViewModel.swift; sourceTree = "<group>"; };
3D51BC0E506CEA4C9ACAA8DB /* RepositoryViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RepositoryViewController.swift; sourceTree = "<group>"; };
42AAD61E2A4041414E741BC4 /* RepositoryViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RepositoryViewModel.swift; sourceTree = "<group>"; };
482D2D42402C917E5C0069BC /* FavoriteModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoriteModel.swift; sourceTree = "<group>"; };
5DE1FF29F81CED271FD929C2 /* FavoriteViewDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoriteViewDataSource.swift; sourceTree = "<group>"; };
5EDBADE530FE153A9651F109 /* SearchViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = SearchViewController.xib; sourceTree = "<group>"; };
64602919324DEDBC0429D452 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
6AE9204A886B2448F36B9293 /* UserRepositoryViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserRepositoryViewController.swift; sourceTree = "<group>"; };
6AFD90EB20BB491847814C5E /* FavoriteViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoriteViewController.swift; sourceTree = "<group>"; };
711633F4B85B3F76D0C88E41 /* UIKeyboardInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIKeyboardInfo.swift; sourceTree = "<group>"; };
7E02ECB42B97D30045AF6AB2 /* SearchViewDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchViewDataSource.swift; sourceTree = "<group>"; };
85BA8AC18BFBE203EBECBDF0 /* UserRepositoryViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserRepositoryViewModel.swift; sourceTree = "<group>"; };
969B79174D1421B0FCFE7154 /* UserRepositoryViewDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserRepositoryViewDataSource.swift; sourceTree = "<group>"; };
9ABD5244E170566F15BBA15E /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
9B5B08A007452F84452B3F0D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
9E2B00ED49EA7B8CD6C7C4F5 /* LoadingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadingView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -79,6 +93,8 @@
children = (
6AE9204A886B2448F36B9293 /* UserRepositoryViewController.swift */,
1F905F35ABF1B1579FEE2B5A /* UserRepositoryViewController.xib */,
969B79174D1421B0FCFE7154 /* UserRepositoryViewDataSource.swift */,
85BA8AC18BFBE203EBECBDF0 /* UserRepositoryViewModel.swift */,
);
path = UserRepository;
sourceTree = "<group>";
Expand All @@ -88,6 +104,8 @@
children = (
208BE35457B09256BF71DAD1 /* SearchViewController.swift */,
5EDBADE530FE153A9651F109 /* SearchViewController.xib */,
7E02ECB42B97D30045AF6AB2 /* SearchViewDataSource.swift */,
3B8FB5FFB17FCEE46B7E72E4 /* SearchViewModel.swift */,
);
path = Search;
sourceTree = "<group>";
Expand All @@ -112,6 +130,7 @@
isa = PBXGroup;
children = (
3D51BC0E506CEA4C9ACAA8DB /* RepositoryViewController.swift */,
42AAD61E2A4041414E741BC4 /* RepositoryViewModel.swift */,
);
path = Repository;
sourceTree = "<group>";
Expand Down Expand Up @@ -152,6 +171,8 @@
children = (
6AFD90EB20BB491847814C5E /* FavoriteViewController.swift */,
E0D61178C6EC742BD2F981F1 /* FavoriteViewController.xib */,
5DE1FF29F81CED271FD929C2 /* FavoriteViewDataSource.swift */,
3A35DE51E635D96C6FB469DF /* FavoriteViewModel.swift */,
);
path = Favorite;
sourceTree = "<group>";
Expand Down Expand Up @@ -283,14 +304,21 @@
0706D2760E1B3A990D0C277A /* AppDelegate.swift in Sources */,
D84737DB64A88B1888B8465D /* FavoriteModel.swift in Sources */,
6EC13FBC2AE1E8DBEBE88804 /* FavoriteViewController.swift in Sources */,
82992D62FE8ED0043356C237 /* FavoriteViewDataSource.swift in Sources */,
72E107687058F53E4B2EF247 /* FavoriteViewModel.swift in Sources */,
71E4B210FC5945A53739149D /* LoadingView.swift in Sources */,
C3F5B4AE9DAFA2095EBCB56A /* NSObjectProtocol.extension.swift in Sources */,
ED8C1E52A3D6C0097A18601E /* RepositoryModel.swift in Sources */,
1791BB0E1AEB38868026578F /* RepositoryViewController.swift in Sources */,
450252B4BA07F8D498E608EF /* RepositoryViewModel.swift in Sources */,
072EB0FFF0B72F37C8CF669A /* SearchModel.swift in Sources */,
4EF255BBD109181D8AD5058F /* SearchViewController.swift in Sources */,
7F3AC0844FF343E754FD4431 /* SearchViewDataSource.swift in Sources */,
651202151F5333A9ABE09B22 /* SearchViewModel.swift in Sources */,
811C3B9B712E3B67E5AD73FD /* UIKeyboardInfo.swift in Sources */,
C69B1DDE761E0663FDE1E947 /* UserRepositoryViewController.swift in Sources */,
B7B8D28336669C7BF3C29BEC /* UserRepositoryViewDataSource.swift in Sources */,
1B6B63FE22A5A86F6028991E /* UserRepositoryViewModel.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
39 changes: 27 additions & 12 deletions iOSDesignPatternSamples/Sources/Common/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
// Copyright © 2017年 marty-suzuki. All rights reserved.
//

import UIKit
import Combine
import GithubKit
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

let favoriteModel = FavoriteModel()
private let favoriteModel = FavoriteModel()

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

Expand All @@ -23,23 +24,38 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
switch value {
case let (0, nc as UINavigationController):
let searchVC = SearchViewController(
searchModel: SearchModel(
sendRequest: ApiSession.shared.send,
asyncAfter: { DispatchQueue.global().asyncAfter(deadline: $0, execute: $1) },
mainAsync: { work in DispatchQueue.main.async { work() } }
viewModel: SearchViewModel(
searchModel: SearchModel(
sendRequest: ApiSession.shared.send
),
notificationCenter: .default
),
makeFavoriteModel: { [favoriteModel] in favoriteModel },
makeRepositoryModel: {
RepositoryModel(
makeUserRepositoryViewModel: { [favoriteModel] in
UserRepositoryViewModel(
user: $0,
sendRequest: ApiSession.shared.send
favoriteModel: favoriteModel,
repositoryModel: RepositoryModel(
user: $0,
sendRequest: ApiSession.shared.send
)
)
},
makeRepositoryViewModel: { [favoriteModel] in
RepositoryViewModel(
repository: $0,
favoritesModel: favoriteModel
)
}
)
nc.setViewControllers([searchVC], animated: false)

case let (1, nc as UINavigationController):
let favoriteVC = FavoriteViewController(favoriteModel: favoriteModel)
let favoriteVC = FavoriteViewController(
viewModel: FavoriteViewModel(favoriteModel: favoriteModel),
makeRepositoryViewModel: { [favoriteModel] in
RepositoryViewModel(repository: $0, favoritesModel: favoriteModel)
}
)
nc.setViewControllers([favoriteVC], animated: false)

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