-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63fc89a
commit 4d85c26
Showing
13 changed files
with
325 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import Foundation | ||
import RxSwift | ||
import RxCocoa | ||
|
||
class RepositoryViewModel2 { | ||
|
||
// @INPUT | ||
let updateRepository = PublishRelay<Repository>() | ||
let updateUsername = PublishRelay<String?>() | ||
let updateDescription = PublishRelay<String?>() | ||
let updateProfileImage = PublishRelay<URL?>() | ||
|
||
// @OUTPUT | ||
var username = BehaviorRelay<String?>(value: nil) | ||
var profileURL = BehaviorRelay<URL?>(value: nil) | ||
var desc = BehaviorRelay<String?>(value: nil) | ||
var status = BehaviorRelay<String?>(value: nil) | ||
|
||
let id: Int | ||
let disposeBag = DisposeBag() | ||
|
||
deinit { | ||
RepoProvider.release(id: id) | ||
} | ||
|
||
init(repository: Repository) { | ||
self.id = repository.id | ||
|
||
RepoProvider.addAndUpdate(repository) | ||
|
||
let repoObserver = RepoProvider.observable(id: id) | ||
.asObservable() | ||
.share(replay: 1, scope: .whileConnected) | ||
|
||
repoObserver | ||
.map { $0?.user?.username } | ||
.bind(to: username) | ||
.disposed(by: disposeBag) | ||
|
||
repoObserver | ||
.map { $0?.user?.profileURL } | ||
.bind(to: profileURL) | ||
.disposed(by: disposeBag) | ||
|
||
repoObserver | ||
.map { $0?.desc } | ||
.bind(to: desc) | ||
.disposed(by: disposeBag) | ||
|
||
repoObserver | ||
.map { item -> String? in | ||
var statusArray: [String] = [] | ||
if let isForked = item?.isForked, isForked { | ||
statusArray.append("Forked") | ||
} | ||
|
||
if let isPrivate = item?.isPrivate, isPrivate { | ||
statusArray.append("Private") | ||
} | ||
|
||
return statusArray.isEmpty ? nil: statusArray.joined(separator: " · ") | ||
}.bind(to: status) | ||
.disposed(by: disposeBag) | ||
|
||
self.updateRepository.subscribe(onNext: { newRepo in | ||
RepoProvider.update(newRepo) | ||
}).disposed(by: disposeBag) | ||
|
||
updateUsername.withLatestFrom(repoObserver) { ($0, $1) } | ||
.subscribe(onNext: { text, repo in | ||
guard let repo = repo else { return } | ||
repo.user?.username = text ?? "" | ||
RepoProvider.update(repo) | ||
}).disposed(by: disposeBag) | ||
|
||
updateDescription.withLatestFrom(repoObserver) { ($0, $1) } | ||
.subscribe(onNext: { text, repo in | ||
guard let repo = repo else { return } | ||
repo.desc = text | ||
RepoProvider.update(repo) | ||
}).disposed(by: disposeBag) | ||
|
||
updateProfileImage.withLatestFrom(repoObserver) { ($0, $1) } | ||
.subscribe(onNext: { url, repo in | ||
guard let repo = repo else { return } | ||
repo.user?.profileURL = url | ||
RepoProvider.update(repo) | ||
}).disposed(by: disposeBag) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters