Sengi-Windows-MacOS-Linux/src/app/services/statuses-state.service.ts

107 lines
3.6 KiB
TypeScript
Raw Normal View History

2020-02-16 07:39:45 +01:00
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
2020-04-24 06:08:22 +02:00
import { StatusWrapper } from '../models/common.model';
2020-02-16 07:39:45 +01:00
@Injectable({
providedIn: 'root'
})
2020-03-14 01:20:51 +01:00
export class StatusesStateService {
2020-04-24 06:08:22 +02:00
private cachedStatusText: { [statusId: string]: string } = {};
2020-02-16 07:39:45 +01:00
private cachedStatusStates: { [statusId: string]: { [accountId: string]: StatusState } } = {};
public stateNotification = new Subject<StatusState>();
constructor() { }
2020-02-19 05:51:34 +01:00
getStateForStatus(statusId: string): StatusState[] {
if(!this.cachedStatusStates[statusId])
2020-02-16 07:39:45 +01:00
return null;
2020-02-19 05:51:34 +01:00
let results: StatusState[] = [];
Object.entries(this.cachedStatusStates[statusId]).forEach(
([key, value]) => {
results.push(value);
}
);
return results;
2020-02-16 07:39:45 +01:00
}
statusFavoriteStatusChanged(statusId: string, accountId: string, isFavorited: boolean) {
if (!this.cachedStatusStates[statusId])
this.cachedStatusStates[statusId] = {};
if (!this.cachedStatusStates[statusId][accountId]) {
this.cachedStatusStates[statusId][accountId] = new StatusState(statusId, accountId, isFavorited, null, null);
2020-02-16 07:39:45 +01:00
} else {
this.cachedStatusStates[statusId][accountId].isFavorited = isFavorited;
}
this.stateNotification.next(this.cachedStatusStates[statusId][accountId]);
}
statusReblogStatusChanged(statusId: string, accountId: string, isRebloged: boolean) {
if (!this.cachedStatusStates[statusId])
this.cachedStatusStates[statusId] = {};
if (!this.cachedStatusStates[statusId][accountId]) {
this.cachedStatusStates[statusId][accountId] = new StatusState(statusId, accountId, null, isRebloged, null);
2020-02-16 07:39:45 +01:00
} else {
this.cachedStatusStates[statusId][accountId].isRebloged = isRebloged;
}
this.stateNotification.next(this.cachedStatusStates[statusId][accountId]);
}
2020-03-14 01:20:51 +01:00
statusBookmarkStatusChanged(statusId: string, accountId: string, isBookmarked: boolean) {
if (!this.cachedStatusStates[statusId])
this.cachedStatusStates[statusId] = {};
if (!this.cachedStatusStates[statusId][accountId]) {
this.cachedStatusStates[statusId][accountId] = new StatusState(statusId, accountId, null, null, isBookmarked);
2020-03-14 01:20:51 +01:00
} else {
this.cachedStatusStates[statusId][accountId].isBookmarked = isBookmarked;
}
this.stateNotification.next(this.cachedStatusStates[statusId][accountId]);
}
2020-04-24 06:08:22 +02:00
setStatusContent(data: string, replyingToStatus: StatusWrapper){
if(replyingToStatus){
this.cachedStatusText[replyingToStatus.status.uri] = data;
} else {
this.cachedStatusText['none'] = data;
}
}
getStatusContent(replyingToStatus: StatusWrapper): string{
let data: string;
if(replyingToStatus){
data = this.cachedStatusText[replyingToStatus.status.uri];
} else {
data = this.cachedStatusText['none'];
}
if(!data) return '';
return data;
}
resetStatusContent(replyingToStatus: StatusWrapper){
if(replyingToStatus){
this.cachedStatusText[replyingToStatus.status.uri] = '';
} else {
this.cachedStatusText['none'] = '';
}
}
2020-02-16 07:39:45 +01:00
}
2020-02-19 05:51:34 +01:00
export class StatusState {
2020-03-14 01:20:51 +01:00
2020-02-19 05:51:34 +01:00
constructor(
public statusId: string,
public accountId: string,
public isFavorited: boolean,
2020-03-14 01:20:51 +01:00
public isRebloged: boolean,
public isBookmarked: boolean) {
2020-02-16 07:39:45 +01:00
}
}