Sengi-Windows-MacOS-Linux/src/app/components/left-side-bar/left-side-bar.component.ts

94 lines
3.2 KiB
TypeScript
Raw Normal View History

2018-03-17 17:25:40 +01:00
import { Component, OnInit, OnDestroy } from "@angular/core";
2018-09-09 07:29:23 +02:00
import { Subscription, BehaviorSubject, Observable } from "rxjs";
import { Store } from "@ngxs/store";
2018-03-17 17:25:40 +01:00
2018-09-09 07:29:23 +02:00
import { Account } from "../../services/models/mastodon.interfaces";
2018-03-16 04:48:30 +01:00
import { AccountWrapper } from "../../models/account.models";
import { AccountsStateModel, AccountInfo, SelectAccount } from "../../states/accounts.state";
2018-09-22 06:22:51 +02:00
import { NavigationService, LeftPanelType } from "../../services/navigation.service";
import { MastodonService } from "../../services/mastodon.service";
2018-09-09 07:29:23 +02:00
2018-03-16 01:43:53 +01:00
@Component({
2018-09-22 06:22:51 +02:00
selector: "app-left-side-bar",
templateUrl: "./left-side-bar.component.html",
styleUrls: ["./left-side-bar.component.scss"]
2018-03-16 01:43:53 +01:00
})
2018-03-17 17:25:40 +01:00
export class LeftSideBarComponent implements OnInit, OnDestroy {
2018-09-22 06:22:51 +02:00
accounts: AccountWrapper[] = [];
private accounts$: Observable<AccountInfo[]>;
2018-03-16 01:43:53 +01:00
// private loadedAccounts: { [index: string]: AccountInfo } = {};
2018-09-22 06:22:51 +02:00
private sub: Subscription;
2018-03-16 03:43:41 +01:00
2018-09-22 06:22:51 +02:00
constructor(
private readonly navigationService: NavigationService,
private readonly mastodonService: MastodonService,
private readonly store: Store) {
2018-03-17 17:25:40 +01:00
2018-09-22 06:22:51 +02:00
this.accounts$ = this.store.select(state => state.registeredaccounts.accounts);
}
2018-03-17 17:25:40 +01:00
2018-09-22 06:22:51 +02:00
private currentLoading: number;
ngOnInit() {
this.accounts$.subscribe((accounts: AccountInfo[]) => {
if (accounts) {
2019-01-31 07:01:48 +01:00
//Update and Add
2018-09-22 06:22:51 +02:00
for (let acc of accounts) {
const previousAcc = this.accounts.find(x => x.info.id === acc.id)
if (previousAcc) {
previousAcc.info.isSelected = acc.isSelected;
} else {
const accWrapper = new AccountWrapper();
accWrapper.info = acc;
this.accounts.push(accWrapper);
this.mastodonService.retrieveAccountDetails(acc)
.then((result: Account) => {
accWrapper.avatar = result.avatar;
});
}
2019-01-31 07:01:48 +01:00
}
2019-01-31 07:01:48 +01:00
//Delete
const deletedAccounts = this.accounts.filter(x => accounts.findIndex(y => y.id === x.info.id) === -1);
for(let delAcc of deletedAccounts){
this.accounts = this.accounts.filter(x => x.info.id !== delAcc.info.id);
2018-09-22 06:22:51 +02:00
}
}
});
}
2018-03-16 03:43:41 +01:00
2018-09-22 06:22:51 +02:00
ngOnDestroy(): void {
this.sub.unsubscribe();
}
2018-09-22 06:22:51 +02:00
onToogleAccountNotify(acc: AccountWrapper) {
this.store.dispatch([new SelectAccount(acc.info)]);
2018-09-22 06:22:51 +02:00
}
2018-03-16 03:43:41 +01:00
2018-09-22 06:22:51 +02:00
onOpenMenuNotify(acc: AccountWrapper) {
this.navigationService.openColumnEditor(acc);
}
2018-09-10 03:55:16 +02:00
2018-09-22 06:22:51 +02:00
createNewStatus(): boolean {
this.navigationService.openPanel(LeftPanelType.CreateNewStatus);
return false;
}
openSearch(): boolean {
this.navigationService.openPanel(LeftPanelType.Search);
return false;
}
2018-09-22 06:22:51 +02:00
addNewAccount(): boolean {
this.navigationService.openPanel(LeftPanelType.AddNewAccount);
return false;
}
openSettings(): boolean {
this.navigationService.openPanel(LeftPanelType.Settings);
return false;
}
2018-03-16 01:43:53 +01:00
}