Skip to content

Instantly share code, notes, and snippets.

@noxt
Last active April 10, 2019 09:44
Show Gist options
  • Save noxt/f0beeda147e9af790167b88ae4b7ad68 to your computer and use it in GitHub Desktop.
Save noxt/f0beeda147e9af790167b88ae4b7ad68 to your computer and use it in GitHub Desktop.
Component Connector
// 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