-
Notifications
You must be signed in to change notification settings - Fork 93
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
d302996
commit fefa8a7
Showing
26 changed files
with
798 additions
and
612 deletions.
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Large diffs are not rendered by default.
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
29 changes: 29 additions & 0 deletions
29
iOSDesignPatternSamples/Sources/Common/Flux/Repository/Dispatcher.Repository.swift
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,29 @@ | ||
// | ||
// Dispatcher.Repository.swift | ||
// iOSDesignPatternSamples | ||
// | ||
// Created by marty-suzuki on 2017/09/12. | ||
// Copyright © 2017年 marty-suzuki. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import FluxCapacitor | ||
import GithubKit | ||
|
||
extension Dispatcher { | ||
enum Repository: DispatchValue { | ||
typealias RelatedStoreType = RepositoryStore | ||
typealias RelatedActionType = RepositoryAction | ||
|
||
case isRepositoryFetching(Bool) | ||
case addRepositories([GithubKit.Repository]) | ||
case removeAllRepositories | ||
case selectedRepository(GithubKit.Repository?) | ||
case lastPageInfo(PageInfo?) | ||
case repositoryTotalCount(Int) | ||
|
||
case addFavorite(GithubKit.Repository) | ||
case removeFavorite(GithubKit.Repository) | ||
case removeAllFavorites | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
iOSDesignPatternSamples/Sources/Common/Flux/Repository/RepositoryAction.swift
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,77 @@ | ||
// | ||
// RepositoryAction.swift | ||
// iOSDesignPatternSamples | ||
// | ||
// Created by marty-suzuki on 2017/09/12. | ||
// Copyright © 2017年 marty-suzuki. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import FluxCapacitor | ||
import GithubKit | ||
import RxSwift | ||
|
||
final class RepositoryAction: Actionable { | ||
typealias DispatchValueType = Dispatcher.Repository | ||
|
||
private let session: ApiSession | ||
private var disposeBag = DisposeBag() | ||
|
||
init(session: ApiSession = .shared) { | ||
self.session = session | ||
} | ||
|
||
func fetchRepositories(withUserId id: String, after: String?) { | ||
invoke(.isRepositoryFetching(true)) | ||
let request = UserNodeRequest(id: id, after: after) | ||
session.rx.send(request) | ||
.subscribe(onNext: { [weak self] in | ||
self?.invoke(.lastPageInfo($0.pageInfo)) | ||
self?.invoke(.addRepositories($0.nodes)) | ||
self?.invoke(.repositoryTotalCount($0.totalCount)) | ||
}, onDisposed: { [weak self] in | ||
self?.invoke(.isRepositoryFetching(false)) | ||
}) | ||
.disposed(by: disposeBag) | ||
} | ||
|
||
func selectRepository(_ repository: Repository) { | ||
invoke(.selectedRepository(repository)) | ||
} | ||
|
||
func clearSelectedRepository() { | ||
invoke(.selectedRepository(nil)) | ||
} | ||
|
||
func addFavorite(_ repository: Repository) { | ||
invoke(.addFavorite(repository)) | ||
} | ||
|
||
func removeFavorite(_ repository: Repository) { | ||
invoke(.removeFavorite(repository)) | ||
} | ||
|
||
func pageInfo(_ pageInfo: PageInfo) { | ||
invoke(.lastPageInfo(pageInfo)) | ||
} | ||
|
||
func clearPageInfo() { | ||
invoke(.lastPageInfo(nil)) | ||
} | ||
|
||
func addRepositories(_ repositories: [Repository]) { | ||
invoke(.addRepositories(repositories)) | ||
} | ||
|
||
func removeAllRepositories() { | ||
invoke(.removeAllRepositories) | ||
} | ||
|
||
func repositoryTotalCount(_ count: Int) { | ||
invoke(.repositoryTotalCount(count)) | ||
} | ||
|
||
func isRepositoriesFetching(_ isFetching: Bool) { | ||
invoke(.isRepositoryFetching(isFetching)) | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
iOSDesignPatternSamples/Sources/Common/Flux/Repository/RepositoryStore.swift
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,103 @@ | ||
// | ||
// RepositoryStore.swift | ||
// iOSDesignPatternSamples | ||
// | ||
// Created by marty-suzuki on 2017/09/12. | ||
// Copyright © 2017年 marty-suzuki. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import FluxCapacitor | ||
import GithubKit | ||
import RxSwift | ||
import RxCocoa | ||
|
||
final class RepositoryStore: Storable { | ||
typealias DispatchValueType = Dispatcher.Repository | ||
|
||
let isRepositoryFetching: Observable<Bool> | ||
fileprivate let _isRepositoryFetching = BehaviorRelay<Bool>(value: false) | ||
|
||
let favorites: Observable<[Repository]> | ||
fileprivate let _favorites = BehaviorRelay<[Repository]>(value: []) | ||
|
||
let repositories: Observable<[Repository]> | ||
fileprivate let _repositories = BehaviorRelay<[Repository]>(value: []) | ||
|
||
let selectedRepository: Observable<Repository?> | ||
fileprivate let _selectedRepository = BehaviorRelay<Repository?>(value: nil) | ||
|
||
let lastPageInfo: Observable<PageInfo?> | ||
fileprivate let _lastPageInfo = BehaviorRelay<PageInfo?>(value: nil) | ||
|
||
let repositoryTotalCount: Observable<Int> | ||
fileprivate let _repositoryTotalCount = BehaviorRelay<Int>(value: 0) | ||
|
||
init(dispatcher: Dispatcher) { | ||
self.isRepositoryFetching = _isRepositoryFetching.asObservable() | ||
self.favorites = _favorites.asObservable() | ||
self.repositories = _repositories.asObservable() | ||
self.selectedRepository = _selectedRepository.asObservable() | ||
self.lastPageInfo = _lastPageInfo.asObservable() | ||
self.repositoryTotalCount = _repositoryTotalCount.asObservable() | ||
|
||
register { [weak self] in | ||
guard let me = self else { return } | ||
switch $0 { | ||
case .isRepositoryFetching(let value): | ||
me._isRepositoryFetching.accept(value) | ||
case .addRepositories(let value): | ||
me._repositories.accept(me._repositories.value + value) | ||
case .removeAllRepositories: | ||
me._repositories.accept([]) | ||
case .selectedRepository(let value): | ||
me._selectedRepository.accept(value) | ||
case .lastPageInfo(let value): | ||
me._lastPageInfo.accept(value) | ||
case .repositoryTotalCount(let value): | ||
me._repositoryTotalCount.accept(value) | ||
|
||
case .addFavorite(let value): | ||
if me._favorites.value.index(where: { $0.url == value.url }) == nil { | ||
me._favorites.accept(me._favorites.value + [value]) | ||
} | ||
case .removeFavorite(let value): | ||
if let index = self?._favorites.value.index(where: { $0.url == value.url }) { | ||
var favorites = me._favorites.value | ||
favorites.remove(at: index) | ||
me._favorites.accept(favorites) | ||
} | ||
case .removeAllFavorites: | ||
me._favorites.accept([]) | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension RepositoryStore: ValueCompatible {} | ||
|
||
extension Value where Base == RepositoryStore { | ||
var isRepositoryFetching: Bool { | ||
return base._isRepositoryFetching.value | ||
} | ||
|
||
var favorites: [Repository] { | ||
return base._favorites.value | ||
} | ||
|
||
var repositories: [Repository] { | ||
return base._repositories.value | ||
} | ||
|
||
var selectedRepository: Repository? { | ||
return base._selectedRepository.value | ||
} | ||
|
||
var lastPageInfo: PageInfo? { | ||
return base._lastPageInfo.value | ||
} | ||
|
||
var repositoryTotalCount: Int { | ||
return base._repositoryTotalCount.value | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
iOSDesignPatternSamples/Sources/Common/Flux/User/Dispatcher.User.swift
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,27 @@ | ||
// | ||
// Dispatcher.User.swift | ||
// iOSDesignPatternSamples | ||
// | ||
// Created by marty-suzuki on 2017/09/12. | ||
// Copyright © 2017年 marty-suzuki. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import FluxCapacitor | ||
import GithubKit | ||
|
||
extension Dispatcher { | ||
enum User: DispatchValue { | ||
typealias RelatedStoreType = UserStore | ||
typealias RelatedActionType = UserAction | ||
|
||
case isUserFetching(Bool) | ||
case addUsers([GithubKit.User]) | ||
case userTotalCount(Int) | ||
case removeAllUsers | ||
case selectedUser(GithubKit.User?) | ||
case lastPageInfo(PageInfo?) | ||
case lastSearchQuery(String) | ||
case fetchError(Error) | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
iOSDesignPatternSamples/Sources/Common/Flux/User/UserAction.swift
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,74 @@ | ||
// | ||
// UserAction.swift | ||
// iOSDesignPatternSamples | ||
// | ||
// Created by marty-suzuki on 2017/09/12. | ||
// Copyright © 2017年 marty-suzuki. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import FluxCapacitor | ||
import GithubKit | ||
import RxSwift | ||
|
||
final class UserAction: Actionable { | ||
typealias DispatchValueType = Dispatcher.User | ||
|
||
private let session: ApiSession | ||
private var disposeBag = DisposeBag() | ||
|
||
init(session: ApiSession = .shared) { | ||
self.session = session | ||
} | ||
|
||
func fetchUsers(withQuery query: String, after: String?) { | ||
invoke(.lastSearchQuery(query)) | ||
if query.isEmpty { return } | ||
disposeBag = DisposeBag() | ||
invoke(.isUserFetching(true)) | ||
let request = SearchUserRequest(query: query, after: after) | ||
session.rx.send(request) | ||
.subscribe(onNext: { [weak self] in | ||
self?.invoke(.addUsers($0.nodes)) | ||
self?.invoke(.lastPageInfo($0.pageInfo)) | ||
self?.invoke(.userTotalCount($0.totalCount)) | ||
}, onError: { [weak self] in | ||
self?.invoke(.fetchError($0)) | ||
}, onDisposed: { [weak self] in | ||
self?.invoke(.isUserFetching(false)) | ||
}) | ||
.disposed(by: disposeBag) | ||
} | ||
|
||
func selectUser(_ user: User) { | ||
invoke(.selectedUser(user)) | ||
} | ||
|
||
func clearSelectedUser() { | ||
invoke(.selectedUser(nil)) | ||
} | ||
|
||
func addUsers(_ users: [User]) { | ||
invoke(.addUsers(users)) | ||
} | ||
|
||
func removeAllUsers() { | ||
invoke(.removeAllUsers) | ||
} | ||
|
||
func pageInfo(_ pageInfo: PageInfo) { | ||
invoke(.lastPageInfo(pageInfo)) | ||
} | ||
|
||
func clearPageInfo() { | ||
invoke(.lastPageInfo(nil)) | ||
} | ||
|
||
func userTotalCount(_ count: Int) { | ||
invoke(.userTotalCount(count)) | ||
} | ||
|
||
func isUserFetching(_ isFetching: Bool) { | ||
invoke(.isUserFetching(isFetching)) | ||
} | ||
} |
Oops, something went wrong.