Sengi-Windows-MacOS-Linux/src/app/components/floating-column/manage-account/my-account/my-account.component.ts

115 lines
4.3 KiB
TypeScript
Raw Normal View History

2019-04-08 02:34:33 +02:00
import { Component, OnInit, OnDestroy, Input } from '@angular/core';
import { Observable, Subscription } from 'rxjs';
import { Store, Select } from '@ngxs/store';
import { faCheckSquare } from "@fortawesome/free-regular-svg-icons";
import { NotificationService } from '../../../../services/notification.service';
import { StreamElement, StreamTypeEnum, AddStream, RemoveAllStreams } from '../../../../states/streams.state';
import { AccountWrapper } from '../../../../models/account.models';
import { RemoveAccount } from '../../../../states/accounts.state';
import { NavigationService } from '../../../../services/navigation.service';
2019-05-19 02:44:36 +02:00
import { MastodonService } from '../../../../services/mastodon.service';
@Component({
selector: 'app-my-account',
templateUrl: './my-account.component.html',
styleUrls: ['./my-account.component.scss']
})
2019-04-08 02:34:33 +02:00
export class MyAccountComponent implements OnInit, OnDestroy {
faCheckSquare = faCheckSquare;
2019-04-08 02:34:33 +02:00
availableStreams: StreamWrapper[] = [];
2019-05-19 02:44:36 +02:00
availableLists: StreamWrapper[] = [];
2019-04-08 02:34:33 +02:00
private _account: AccountWrapper;
@Input('account')
set account(acc: AccountWrapper) {
this._account = acc;
this.loadStreams(acc);
}
get account(): AccountWrapper {
return this._account;
}
@Select(state => state.streamsstatemodel.streams) streamElements$: Observable<StreamElement[]>;
private streamChangedSub: Subscription;
constructor(
private readonly store: Store,
private readonly navigationService: NavigationService,
2019-05-19 02:44:36 +02:00
private readonly mastodonService: MastodonService,
private readonly notificationService: NotificationService) { }
ngOnInit() {
2019-04-08 02:34:33 +02:00
this.streamChangedSub = this.streamElements$.subscribe((streams: StreamElement[]) => {
this.loadStreams(this.account);
});
}
ngOnDestroy(): void {
if(this.streamChangedSub) {
this.streamChangedSub.unsubscribe();
}
}
private loadStreams(account: AccountWrapper){
const instance = account.info.instance;
this.availableStreams.length = 0;
2019-05-19 02:44:36 +02:00
this.availableStreams.push(new StreamWrapper(new StreamElement(StreamTypeEnum.global, 'Federated Timeline', account.info.id, null, null, null, instance)));
this.availableStreams.push(new StreamWrapper(new StreamElement(StreamTypeEnum.local, 'Local Timeline', account.info.id, null, null, null, instance)));
this.availableStreams.push(new StreamWrapper(new StreamElement(StreamTypeEnum.personnal, 'Home', account.info.id, null, null, null, instance)));
2019-04-08 02:34:33 +02:00
const loadedStreams = <StreamElement[]>this.store.snapshot().streamsstatemodel.streams;
this.availableStreams.forEach(s => {
if(loadedStreams.find(x => x.id === s.id)){
s.isAdded = true;
} else {
s.isAdded = false;
}
});
2019-05-19 02:44:36 +02:00
this.availableLists.length = 0;
this.mastodonService.getLists(account.info)
.then((streams: StreamElement[]) => {
this.availableLists.length = 0;
for (let stream of streams) {
let wrappedStream = new StreamWrapper(stream);
if(loadedStreams.find(x => x.id == stream.id)){
wrappedStream.isAdded = true;
} else {
wrappedStream.isAdded = false;
}
this.availableLists.push(wrappedStream);
}
})
.catch(err => {
});
}
2019-04-08 02:34:33 +02:00
addStream(stream: StreamWrapper): boolean {
if (stream && !stream.isAdded) {
this.store.dispatch([new AddStream(stream)]).toPromise()
.then(() => {
2019-04-08 02:34:33 +02:00
stream.isAdded = true;
});
}
return false;
}
removeAccount(): boolean {
const accountId = this.account.info.id;
this.store.dispatch([new RemoveAllStreams(accountId), new RemoveAccount(accountId)]);
this.navigationService.closePanel();
return false;
}
}
2019-04-08 02:34:33 +02:00
class StreamWrapper extends StreamElement {
constructor(stream: StreamElement) {
2019-05-19 02:44:36 +02:00
super(stream.type, stream.name, stream.accountId, stream.tag, stream.list, stream.listId, stream.instance);
2019-04-08 02:34:33 +02:00
}
isAdded: boolean;
}