Last active
April 10, 2019 09:44
-
-
Save noxt/f0beeda147e9af790167b88ae4b7ad68 to your computer and use it in GitHub Desktop.
Component Connector
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
// Components/Categories/CategoriesConnector.swift | |
final class CategoriesConnector: Connector { | |
private let disposer = Disposer() | |
let repositories: RepositoryProviderProtocol | |
required init(repositories: RepositoryProviderProtocol) { | |
self.repositories = repositories | |
} | |
func connect(to component: CategoriesComponent) { | |
component.props = mapToProps(state: .initial) | |
core.observe(on: .main) { [weak self] (state) in | |
guard let self = self else { | |
return | |
} | |
component.props = self.mapToProps(state: state) | |
}.dispose(on: disposer) | |
} | |
func mapToProps(state: AppFeature.State) -> CategoriesProps { | |
return CategoriesProps( | |
state: mapToPropsState(state: state), | |
loadCategoriesList: CategoriesFeature.Commands.loadCategoriesList(repositories) | |
) | |
} | |
private func mapToPropsState(state: AppFeature.State) -> CategoriesProps.State { | |
let categoriesState = state.categoriesState | |
let transactionState = state.transactionState | |
guard !categoriesState.isLoading else { | |
return .loading | |
} | |
var categories: [CategoriesProps.CategoryInfo] = [] | |
for id in categoriesState.sortOrder { | |
if let category = categoriesState.categories[id] { | |
categories.append(mapCategoryToProps( | |
category: category, | |
isSelected: transactionState.categoryID == category.id | |
)) | |
} | |
} | |
return .idle(categories: categories) | |
} | |
private func mapCategoryToProps(category: Category, isSelected: Bool) -> CategoriesProps.CategoryInfo { | |
let command: PlainCommand? | |
if !isSelected { | |
command = PlainCommand { [unowned self] in | |
TransactionFeature.Commands.selectCategory(self.repositories).execute(with: category) | |
} | |
} else { | |
command = nil | |
} | |
return CategoriesProps.CategoryInfo( | |
title: category.title, | |
icon: category.icon.image, | |
currentBalance: "0 BYN", | |
selectCommand: command | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment