Last active
February 8, 2020 16:40
-
-
Save andrewarosario/07c2f319e1b5d03b40fc04aa744df2ab to your computer and use it in GitHub Desktop.
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
@Injectable() | |
export class SettingsState { | |
// definimos os estados iniciais nos BehaviorSubject | |
// ninguém fora da SettingsState tem acesso a eles | |
// A alteração no estado deve ser tratada por métodos especializados da classe | |
// (por exemplo: addCashflowCategory, updateCashflowCategory, etc) | |
// criamos um BehaviorSubject para cada entidade do estado | |
// mais abaixo criaremos getters e setters para que os mesmos sejam acessados pelos Facades | |
private _updating$ = new BehaviorSubject<boolean>(false); | |
private _cashflowCategories$ = new BehaviorSubject<CashflowCategory[]>(null); | |
// getter que expõe o observable para a camada de abstração | |
get isUpdating$() { | |
return this._updating$.asObservable(); | |
} | |
// setter que atribuímos um valor e ele será repassado para todos os seus subscribers | |
// exemplo: this.isUpdating = true | |
set isUpdating(isUpdating: boolean) { | |
this._updating$.next(isUpdating); | |
} | |
// getter que expõe o observable para a camada de abstração | |
get cashflowCategories$() { | |
return this._cashflowCategories$.asObservable(); | |
} | |
// este getter irá retornar o último valor emitido pelo BehaviorSubject | |
get cashflowCategories() { | |
return this._cashflowCategories$.getValue(); | |
} | |
// setter que atribuímos um valor e ele será repassado para todos os seus subscribers | |
// exemplo: this.cashflowCategories = [] | |
set cashflowCategories(categories: CashflowCategory[]) { | |
this._cashflowCategories$.next(categories); | |
} | |
addCashflowCategory(category: CashflowCategory) { | |
const currentValue = this.cashflowCategories; | |
this.cashflowCategories = [ ...currentValue, category ]; | |
} | |
updateCashflowCategory(updatedCategory: CashflowCategory) { | |
const categories = this.cashflowCategories; | |
const indexOfUpdated = categories.findIndex(category => category.id === updatedCategory.id); | |
categories[indexOfUpdated] = updatedCategory; | |
this.cashflowCategories = [ ...categories ]; | |
} | |
updateCashflowCategoryId(categoryToReplace: CashflowCategory, addedCategoryWithId: CashflowCategory) { | |
const categories = this.cashflowCategories; | |
const updatedCategoryIndex = categories.findIndex(category => category === categoryToReplace); | |
categories[updatedCategoryIndex] = addedCategoryWithId; | |
this.cashflowCategories = [ ...categories ]; | |
} | |
removeCashflowCategory(categoryRemove: CashflowCategory) { | |
const currentValue = this.cashflowCategories$.getValue(); | |
this.cashflowCategories = currentValue.filter(category => category !== categoryRemove); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment