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

138 lines
5.0 KiB
TypeScript
Raw Normal View History

2018-03-17 17:25:40 +01:00
import { Component, OnInit, OnDestroy } from "@angular/core";
2019-02-12 05:41:21 +01:00
import { HttpErrorResponse } from "@angular/common/http";
2019-02-11 05:03:05 +01:00
import { Subscription, Observable } from "rxjs";
2018-09-09 07:29:23 +02:00
import { Store } from "@ngxs/store";
2019-06-01 20:22:12 +02:00
import { faPlus, faCog, faSearch } from "@fortawesome/free-solid-svg-icons";
2019-08-25 07:43:47 +02:00
import { faCommentAlt, faCalendarAlt } from "@fortawesome/free-regular-svg-icons";
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";
2019-02-11 05:03:05 +01:00
import { 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";
2019-02-12 05:41:21 +01:00
import { NotificationService } from "../../services/notification.service";
2019-03-25 06:09:53 +01:00
import { UserNotificationService, UserNotification } from '../../services/user-notification.service';
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 {
2019-02-11 05:03:05 +01:00
faCommentAlt = faCommentAlt;
2019-06-01 20:22:12 +02:00
faSearch = faSearch;
faPlus = faPlus;
faCog = faCog;
2019-08-25 07:43:47 +02:00
faCalendarAlt = faCalendarAlt;
2019-02-11 05:03:05 +01:00
accounts: AccountWithNotificationWrapper[] = [];
2019-02-11 04:23:10 +01:00
hasAccounts: boolean;
private accounts$: Observable<AccountInfo[]>;
2018-03-16 01:43:53 +01:00
private accountSub: Subscription;
private notificationSub: Subscription;
2018-03-16 03:43:41 +01:00
2018-09-22 06:22:51 +02:00
constructor(
2019-03-25 06:09:53 +01:00
private readonly userNotificationServiceService: UserNotificationService,
2019-02-12 05:41:21 +01:00
private readonly notificationService: NotificationService,
2018-09-22 06:22:51 +02:00
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.accountSub = this.accounts$.subscribe((accounts: AccountInfo[]) => {
2018-09-22 06:22:51 +02:00
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 AccountWithNotificationWrapper();
accWrapper.info = acc;
this.accounts.push(accWrapper);
this.mastodonService.retrieveAccountDetails(acc)
.then((result: Account) => {
accWrapper.avatar = result.avatar;
2019-02-12 05:41:21 +01:00
})
.catch((err: HttpErrorResponse) => {
this.notificationService.notifyHttpError(err);
});
}
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) {
2019-01-31 07:01:48 +01:00
this.accounts = this.accounts.filter(x => x.info.id !== delAcc.info.id);
2018-09-22 06:22:51 +02:00
}
2019-02-11 04:23:10 +01:00
this.hasAccounts = this.accounts.length > 0;
2018-09-22 06:22:51 +02:00
}
});
this.notificationSub = this.userNotificationServiceService.userNotifications.subscribe((notifications: UserNotification[]) => {
notifications.forEach((notification: UserNotification) => {
const acc = this.accounts.find(x => x.info.id === notification.account.id);
if(acc){
acc.hasActivityNotifications = notification.hasNewMentions || notification.hasNewNotifications;
}
});
});
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.accountSub.unsubscribe();
this.notificationSub.unsubscribe();
2018-09-22 06:22:51 +02:00
}
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;
}
2019-08-25 07:43:47 +02:00
openScheduledStatuses(): boolean {
this.navigationService.openPanel(LeftPanelType.ScheduledStatuses);
return false;
}
2018-03-16 01:43:53 +01:00
}
export class AccountWithNotificationWrapper extends AccountWrapper {
// constructor(accountWrapper: AccountWrapper) {
// super();
// this.avatar = accountWrapper.avatar;
// this.info = accountWrapper.info;
// }
hasActivityNotifications: boolean;
}