wirering bookmarks to service

This commit is contained in:
Nicolas Constant 2020-03-13 20:20:51 -04:00
parent 3b8448369d
commit a9350ff31b
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
2 changed files with 71 additions and 8 deletions

View File

@ -56,6 +56,7 @@ export class ActionBarComponent implements OnInit, OnDestroy {
private favoriteStatePerAccountId: { [id: string]: boolean; } = {};
private bootedStatePerAccountId: { [id: string]: boolean; } = {};
private bookmarkStatePerAccountId: { [id: string]: boolean; } = {};
private accounts$: Observable<AccountInfo[]>;
private accountSub: Subscription;
@ -78,10 +79,12 @@ export class ActionBarComponent implements OnInit, OnDestroy {
if (status.reblog) {
this.favoriteStatePerAccountId[account.id] = status.reblog.favourited;
this.bootedStatePerAccountId[account.id] = status.reblog.reblogged;
this.bookmarkStatePerAccountId[account.id] = status.reblog.bookmarked;
this.displayedStatus = status.reblog;
} else {
this.favoriteStatePerAccountId[account.id] = status.favourited;
this.bootedStatePerAccountId[account.id] = status.reblogged;
this.bookmarkStatePerAccountId[account.id] = status.bookmarked;
this.displayedStatus = status;
}
@ -99,9 +102,11 @@ export class ActionBarComponent implements OnInit, OnDestroy {
if (state && state.statusId === this.displayedStatus.url) {
this.favoriteStatePerAccountId[state.accountId] = state.isFavorited;
this.bootedStatePerAccountId[state.accountId] = state.isRebloged;
this.bookmarkStatePerAccountId[state.accountId] = state.isBookmarked;
this.checkIfFavorited();
this.checkIfBoosted();
this.checkIfBookmarked();
}
});
}
@ -118,6 +123,7 @@ export class ActionBarComponent implements OnInit, OnDestroy {
memoryStatusState.forEach((state: StatusState) => {
this.favoriteStatePerAccountId[state.accountId] = state.isFavorited;
this.bootedStatePerAccountId[state.accountId] = state.isRebloged;
this.bookmarkStatePerAccountId[state.accountId] = state.isBookmarked;
});
}
@ -145,6 +151,7 @@ export class ActionBarComponent implements OnInit, OnDestroy {
this.checkIfFavorited();
this.checkIfBoosted();
this.checkIfBookmarked();
}
showContent(): boolean {
@ -243,10 +250,41 @@ export class ActionBarComponent implements OnInit, OnDestroy {
if (this.bookmarkingIsLoading) return;
this.bookmarkingIsLoading = true;
setTimeout(() => {
this.isBookmarked = !this.isBookmarked;
this.bookmarkingIsLoading = false;
}, 2000);
const account = this.toolsService.getSelectedAccounts()[0];
const usableStatus = this.toolsService.getStatusUsableByAccount(account, this.statusWrapper);
usableStatus
.then((status: Status) => {
if (this.isBookmarked && status.bookmarked) {
return this.mastodonService.unbookmark(account, status);
} else if (!this.isBookmarked && !status.bookmarked) {
return this.mastodonService.bookmark(account, status);
} else {
return Promise.resolve(status);
}
})
.then((bookmarkedStatus: Status) => {
let bookmarked = bookmarkedStatus.bookmarked; //FIXME: when pixelfed will return the good status
if (bookmarked === null) {
bookmarked = !this.bookmarkStatePerAccountId[account.id];
}
this.bookmarkStatePerAccountId[account.id] = bookmarked;
this.checkIfBookmarked();
})
.catch((err: HttpErrorResponse) => {
this.notificationService.notifyHttpError(err, account);
})
.then(() => {
this.statusStateService.statusBookmarkStatusChanged(this.displayedStatus.url, account.id, this.bookmarkStatePerAccountId[account.id]);
this.bookmarkingIsLoading = false;
});
// setTimeout(() => {
// this.isBookmarked = !this.isBookmarked;
// this.bookmarkingIsLoading = false;
// }, 2000);
return false;
}
@ -270,6 +308,16 @@ export class ActionBarComponent implements OnInit, OnDestroy {
}
}
private checkIfBookmarked() {
const selectedAccount = <AccountInfo>this.selectedAccounts[0];
if (selectedAccount) {
this.isBookmarked = this.bookmarkStatePerAccountId[selectedAccount.id];
} else {
this.isBookmarked = false;
}
}
browseThread(event: OpenThreadEvent) {
this.browseThreadEvent.next(event);
}

View File

@ -5,7 +5,7 @@ import { Subject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class StatusesStateService {
export class StatusesStateService {
private cachedStatusStates: { [statusId: string]: { [accountId: string]: StatusState } } = {};
public stateNotification = new Subject<StatusState>();
@ -29,7 +29,7 @@ export class StatusesStateService {
this.cachedStatusStates[statusId] = {};
if (!this.cachedStatusStates[statusId][accountId]) {
this.cachedStatusStates[statusId][accountId] = new StatusState(statusId, accountId, isFavorited, false);
this.cachedStatusStates[statusId][accountId] = new StatusState(statusId, accountId, isFavorited, false, false);
} else {
this.cachedStatusStates[statusId][accountId].isFavorited = isFavorited;
}
@ -42,20 +42,35 @@ export class StatusesStateService {
this.cachedStatusStates[statusId] = {};
if (!this.cachedStatusStates[statusId][accountId]) {
this.cachedStatusStates[statusId][accountId] = new StatusState(statusId, accountId, false, isRebloged);
this.cachedStatusStates[statusId][accountId] = new StatusState(statusId, accountId, false, isRebloged, false);
} else {
this.cachedStatusStates[statusId][accountId].isRebloged = isRebloged;
}
this.stateNotification.next(this.cachedStatusStates[statusId][accountId]);
}
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, false, false, isBookmarked);
} else {
this.cachedStatusStates[statusId][accountId].isBookmarked = isBookmarked;
}
this.stateNotification.next(this.cachedStatusStates[statusId][accountId]);
}
}
export class StatusState {
constructor(
public statusId: string,
public accountId: string,
public isFavorited: boolean,
public isRebloged: boolean) {
public isRebloged: boolean,
public isBookmarked: boolean) {
}
}