clear medias after sending status #56

This commit is contained in:
Nicolas Constant 2019-03-10 19:23:33 -04:00
parent 1c7dd94452
commit 0ec97cbf4f
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
3 changed files with 30 additions and 22 deletions

View File

@ -166,7 +166,7 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
.then((status: Status) => { .then((status: Status) => {
return this.sendStatus(acc, this.status, visibility, this.title, status, mediaAttachments); return this.sendStatus(acc, this.status, visibility, this.title, status, mediaAttachments);
}) })
.then((res: Status) => { .then((res: Status) => {
this.title = ''; this.title = '';
this.status = ''; this.status = '';
this.onClose.emit(); this.onClose.emit();
@ -194,7 +194,11 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
} }
if(i === 0){ if(i === 0){
return this.mastodonService.postNewStatus(account, s, visibility, title, inReplyToId, attachments.map(x => x.id)); return this.mastodonService.postNewStatus(account, s, visibility, title, inReplyToId, attachments.map(x => x.id))
.then((status:Status) => {
this.mediaService.clearMedia();
return status;
});
} else { } else {
return this.mastodonService.postNewStatus(account, s, visibility, title, inReplyToId, []); return this.mastodonService.postNewStatus(account, s, visibility, title, inReplyToId, []);
} }

View File

@ -1,9 +1,7 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http'; import { BehaviorSubject } from 'rxjs';
import { BehaviorSubject, Subject } from 'rxjs';
import { AccountInfo } from '../states/accounts.state'; import { AccountInfo } from '../states/accounts.state';
import { ApiRoutes } from './models/api.settings';
import { Attachment } from './models/mastodon.interfaces'; import { Attachment } from './models/mastodon.interfaces';
import { MastodonService } from './mastodon.service'; import { MastodonService } from './mastodon.service';
import { NotificationService } from './notification.service'; import { NotificationService } from './notification.service';
@ -12,47 +10,45 @@ import { NotificationService } from './notification.service';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class MediaService { export class MediaService {
private apiRoutes = new ApiRoutes();
mediaSubject: BehaviorSubject<MediaWrapper[]> = new BehaviorSubject<MediaWrapper[]>([]); mediaSubject: BehaviorSubject<MediaWrapper[]> = new BehaviorSubject<MediaWrapper[]>([]);
constructor( constructor(
private readonly notificationService: NotificationService, private readonly notificationService: NotificationService,
private readonly mastodonService: MastodonService) { } private readonly mastodonService: MastodonService) { }
uploadMedia(account: AccountInfo, files: File[]){ uploadMedia(account: AccountInfo, files: File[]) {
for (let file of files) { for (let file of files) {
this.postMedia(account, file); this.postMedia(account, file);
} }
} }
private postMedia(account: AccountInfo, file: File){ private postMedia(account: AccountInfo, file: File) {
const uniqueId = `${file.name}${file.size}${Math.random()}`; const uniqueId = `${file.name}${file.size}${Math.random()}`;
const wrapper = new MediaWrapper(uniqueId, file, null, null); const wrapper = new MediaWrapper(uniqueId, file, null);
let medias = this.mediaSubject.value; let medias = this.mediaSubject.value;
medias.push(wrapper); medias.push(wrapper);
this.mediaSubject.next(medias); this.mediaSubject.next(medias);
this.mastodonService.uploadMediaAttachment(account, file) this.mastodonService.uploadMediaAttachment(account, file)
.then((attachment: Attachment) => { .then((attachment: Attachment) => {
let currentMedias = this.mediaSubject.value; let currentMedias = this.mediaSubject.value;
let currentMedia = currentMedias.filter(x => x.id === uniqueId)[0]; let currentMedia = currentMedias.filter(x => x.id === uniqueId)[0];
if(currentMedia){ if (currentMedia) {
currentMedia.attachment = attachment; currentMedia.attachment = attachment;
this.mediaSubject.next(currentMedias); this.mediaSubject.next(currentMedias);
} }
}) })
.catch((err)=>{ .catch((err) => {
this.remove(wrapper); this.remove(wrapper);
this.notificationService.notifyHttpError(err); this.notificationService.notifyHttpError(err);
}); });
} }
update( account: AccountInfo, media: MediaWrapper): any { update(account: AccountInfo, media: MediaWrapper): any {
if(media.attachment.description === media.description) return; if (media.attachment.description === media.description) return;
this.mastodonService.updateMediaAttachment(account, media.attachment.id, media.description) this.mastodonService.updateMediaAttachment(account, media.attachment.id, media.description)
.then((att: Attachment) => { .then((att: Attachment) => {
let medias = this.mediaSubject.value; let medias = this.mediaSubject.value;
@ -70,12 +66,20 @@ export class MediaService {
let filteredMedias = medias.filter(x => x.id !== media.id); let filteredMedias = medias.filter(x => x.id !== media.id);
this.mediaSubject.next(filteredMedias); this.mediaSubject.next(filteredMedias);
} }
clearMedia() {
this.mediaSubject.next([]);
}
// migrate
} }
export class MediaWrapper { export class MediaWrapper {
constructor( constructor(
public id: string, public id: string,
public file: File, public file: File,
public attachment: Attachment, public attachment: Attachment) { }
public description: string) {}
public description: string;
public isMigrating: boolean;
} }

View File

@ -52,7 +52,7 @@
</style> </style>
</head> </head>
<body> <body ondragstart="return false;" ondrop="return false;">
<app-root> <app-root>
<div class="lds-ripple"> <div class="lds-ripple">
<div></div> <div></div>