1
0
mirror of https://github.com/NicolasConstant/sengi synced 2025-02-10 17:00:49 +01:00

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 favoriteStatePerAccountId: { [id: string]: boolean; } = {};
private bootedStatePerAccountId: { [id: string]: boolean; } = {}; private bootedStatePerAccountId: { [id: string]: boolean; } = {};
private bookmarkStatePerAccountId: { [id: string]: boolean; } = {};
private accounts$: Observable<AccountInfo[]>; private accounts$: Observable<AccountInfo[]>;
private accountSub: Subscription; private accountSub: Subscription;
@ -78,10 +79,12 @@ export class ActionBarComponent implements OnInit, OnDestroy {
if (status.reblog) { if (status.reblog) {
this.favoriteStatePerAccountId[account.id] = status.reblog.favourited; this.favoriteStatePerAccountId[account.id] = status.reblog.favourited;
this.bootedStatePerAccountId[account.id] = status.reblog.reblogged; this.bootedStatePerAccountId[account.id] = status.reblog.reblogged;
this.bookmarkStatePerAccountId[account.id] = status.reblog.bookmarked;
this.displayedStatus = status.reblog; this.displayedStatus = status.reblog;
} else { } else {
this.favoriteStatePerAccountId[account.id] = status.favourited; this.favoriteStatePerAccountId[account.id] = status.favourited;
this.bootedStatePerAccountId[account.id] = status.reblogged; this.bootedStatePerAccountId[account.id] = status.reblogged;
this.bookmarkStatePerAccountId[account.id] = status.bookmarked;
this.displayedStatus = status; this.displayedStatus = status;
} }
@ -99,9 +102,11 @@ export class ActionBarComponent implements OnInit, OnDestroy {
if (state && state.statusId === this.displayedStatus.url) { if (state && state.statusId === this.displayedStatus.url) {
this.favoriteStatePerAccountId[state.accountId] = state.isFavorited; this.favoriteStatePerAccountId[state.accountId] = state.isFavorited;
this.bootedStatePerAccountId[state.accountId] = state.isRebloged; this.bootedStatePerAccountId[state.accountId] = state.isRebloged;
this.bookmarkStatePerAccountId[state.accountId] = state.isBookmarked;
this.checkIfFavorited(); this.checkIfFavorited();
this.checkIfBoosted(); this.checkIfBoosted();
this.checkIfBookmarked();
} }
}); });
} }
@ -118,6 +123,7 @@ export class ActionBarComponent implements OnInit, OnDestroy {
memoryStatusState.forEach((state: StatusState) => { memoryStatusState.forEach((state: StatusState) => {
this.favoriteStatePerAccountId[state.accountId] = state.isFavorited; this.favoriteStatePerAccountId[state.accountId] = state.isFavorited;
this.bootedStatePerAccountId[state.accountId] = state.isRebloged; 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.checkIfFavorited();
this.checkIfBoosted(); this.checkIfBoosted();
this.checkIfBookmarked();
} }
showContent(): boolean { showContent(): boolean {
@ -243,10 +250,41 @@ export class ActionBarComponent implements OnInit, OnDestroy {
if (this.bookmarkingIsLoading) return; if (this.bookmarkingIsLoading) return;
this.bookmarkingIsLoading = true; this.bookmarkingIsLoading = true;
setTimeout(() => {
this.isBookmarked = !this.isBookmarked; 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; this.bookmarkingIsLoading = false;
}, 2000); });
// setTimeout(() => {
// this.isBookmarked = !this.isBookmarked;
// this.bookmarkingIsLoading = false;
// }, 2000);
return false; 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) { browseThread(event: OpenThreadEvent) {
this.browseThreadEvent.next(event); this.browseThreadEvent.next(event);
} }

View File

@ -29,7 +29,7 @@ export class StatusesStateService {
this.cachedStatusStates[statusId] = {}; this.cachedStatusStates[statusId] = {};
if (!this.cachedStatusStates[statusId][accountId]) { 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 { } else {
this.cachedStatusStates[statusId][accountId].isFavorited = isFavorited; this.cachedStatusStates[statusId][accountId].isFavorited = isFavorited;
} }
@ -42,20 +42,35 @@ export class StatusesStateService {
this.cachedStatusStates[statusId] = {}; this.cachedStatusStates[statusId] = {};
if (!this.cachedStatusStates[statusId][accountId]) { 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 { } else {
this.cachedStatusStates[statusId][accountId].isRebloged = isRebloged; this.cachedStatusStates[statusId][accountId].isRebloged = isRebloged;
} }
this.stateNotification.next(this.cachedStatusStates[statusId][accountId]); 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 { export class StatusState {
constructor( constructor(
public statusId: string, public statusId: string,
public accountId: string, public accountId: string,
public isFavorited: boolean, public isFavorited: boolean,
public isRebloged: boolean) { public isRebloged: boolean,
public isBookmarked: boolean) {
} }
} }