From 0ec97cbf4f2148d71e284ca0e51fb33d459f6f9d Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Sun, 10 Mar 2019 19:23:33 -0400 Subject: [PATCH] clear medias after sending status #56 --- .../create-status/create-status.component.ts | 8 +++- src/app/services/media.service.ts | 42 ++++++++++--------- src/index.html | 2 +- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/app/components/create-status/create-status.component.ts b/src/app/components/create-status/create-status.component.ts index 8d39a443..538f6d68 100644 --- a/src/app/components/create-status/create-status.component.ts +++ b/src/app/components/create-status/create-status.component.ts @@ -166,7 +166,7 @@ export class CreateStatusComponent implements OnInit, OnDestroy { .then((status: Status) => { return this.sendStatus(acc, this.status, visibility, this.title, status, mediaAttachments); }) - .then((res: Status) => { + .then((res: Status) => { this.title = ''; this.status = ''; this.onClose.emit(); @@ -194,7 +194,11 @@ export class CreateStatusComponent implements OnInit, OnDestroy { } 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 { return this.mastodonService.postNewStatus(account, s, visibility, title, inReplyToId, []); } diff --git a/src/app/services/media.service.ts b/src/app/services/media.service.ts index f7bdb2c1..b1e3b190 100644 --- a/src/app/services/media.service.ts +++ b/src/app/services/media.service.ts @@ -1,9 +1,7 @@ import { Injectable } from '@angular/core'; -import { HttpClient, HttpHeaders } from '@angular/common/http'; -import { BehaviorSubject, Subject } from 'rxjs'; +import { BehaviorSubject } from 'rxjs'; import { AccountInfo } from '../states/accounts.state'; -import { ApiRoutes } from './models/api.settings'; import { Attachment } from './models/mastodon.interfaces'; import { MastodonService } from './mastodon.service'; import { NotificationService } from './notification.service'; @@ -12,47 +10,45 @@ import { NotificationService } from './notification.service'; @Injectable({ providedIn: 'root' }) -export class MediaService { - private apiRoutes = new ApiRoutes(); - +export class MediaService { mediaSubject: BehaviorSubject = new BehaviorSubject([]); constructor( private readonly notificationService: NotificationService, private readonly mastodonService: MastodonService) { } - uploadMedia(account: AccountInfo, files: File[]){ + uploadMedia(account: AccountInfo, files: File[]) { for (let file of files) { 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 wrapper = new MediaWrapper(uniqueId, file, null, null); + const wrapper = new MediaWrapper(uniqueId, file, null); let medias = this.mediaSubject.value; medias.push(wrapper); this.mediaSubject.next(medias); - this.mastodonService.uploadMediaAttachment(account, file) + this.mastodonService.uploadMediaAttachment(account, file) .then((attachment: Attachment) => { let currentMedias = this.mediaSubject.value; let currentMedia = currentMedias.filter(x => x.id === uniqueId)[0]; - if(currentMedia){ + if (currentMedia) { currentMedia.attachment = attachment; this.mediaSubject.next(currentMedias); } }) - .catch((err)=>{ + .catch((err) => { this.remove(wrapper); this.notificationService.notifyHttpError(err); }); } - update( account: AccountInfo, media: MediaWrapper): any { - if(media.attachment.description === media.description) return; - + update(account: AccountInfo, media: MediaWrapper): any { + if (media.attachment.description === media.description) return; + this.mastodonService.updateMediaAttachment(account, media.attachment.id, media.description) .then((att: Attachment) => { let medias = this.mediaSubject.value; @@ -70,12 +66,20 @@ export class MediaService { let filteredMedias = medias.filter(x => x.id !== media.id); this.mediaSubject.next(filteredMedias); } + + clearMedia() { + this.mediaSubject.next([]); + } + + // migrate } export class MediaWrapper { constructor( public id: string, - public file: File, - public attachment: Attachment, - public description: string) {} + public file: File, + public attachment: Attachment) { } + + public description: string; + public isMigrating: boolean; } diff --git a/src/index.html b/src/index.html index 1d470ce9..4ec1ad1c 100644 --- a/src/index.html +++ b/src/index.html @@ -52,7 +52,7 @@ - +