-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pamela
committed
Apr 22, 2024
1 parent
4853c41
commit 7053cc5
Showing
13 changed files
with
236 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...l/src/app/components/resource-download-multiple/resource-download-multiple.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
<button mat-stroked-button color="primary" class="downloads-button" (click)="downloadAllResources()" | ||
*ngIf="downloadStatus === 'ready'" | ||
[attr.data-style-variant]="styleVariant"> | ||
<mat-icon>download</mat-icon> | ||
{{ 'Download All' | translate }} | ||
{{ totalSizeMB }} MB | ||
</button> | ||
|
||
<div class="download-progress" *ngIf="downloadStatus === 'pending'"> | ||
<mat-progress-spinner | ||
[class]="'spinner' + styleVariant" | ||
color="primary" | ||
[mode]="downloadProgress > 0 ? 'determinate' : 'indeterminate'" | ||
[value]="downloadProgress" | ||
[diameter]="size" | ||
[style.width]="sizePx" | ||
[attr.data-style-variant]="styleVariant" | ||
> | ||
</mat-progress-spinner> | ||
<button | ||
mat-button | ||
class="download-cancel" | ||
(click)="cancelDownload()" | ||
[disabled]="downloadProgress === 100" | ||
[style.width]="sizePx" | ||
[style.min-width]="sizePx" | ||
[style.height]="sizePx" | ||
[attr.data-style-variant]="styleVariant" | ||
> | ||
<mat-icon>close</mat-icon> | ||
</button> | ||
</div> | ||
<button | ||
mat-icon-button | ||
*ngIf="downloadStatus === 'complete'" | ||
[attr.data-style-variant]="styleVariant" | ||
[style.visibility]="hideOnComplete ? 'hidden' : 'visible'" | ||
> | ||
<mat-icon>check</mat-icon> | ||
</button> | ||
|
||
|
5 changes: 5 additions & 0 deletions
5
...l/src/app/components/resource-download-multiple/resource-download-multiple.component.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.downloads-button { | ||
display: flex; | ||
align-items: center; | ||
margin-left: auto; | ||
} |
21 changes: 21 additions & 0 deletions
21
...rc/app/components/resource-download-multiple/resource-download-multiple.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { ResourceDownloadMultipleComponent } from './resource-download-multiple.component'; | ||
|
||
describe('ResourceDownloadMultipleComponent', () => { | ||
let component: ResourceDownloadMultipleComponent; | ||
let fixture: ComponentFixture<ResourceDownloadMultipleComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [ResourceDownloadMultipleComponent], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(ResourceDownloadMultipleComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
119 changes: 119 additions & 0 deletions
119
...ool/src/app/components/resource-download-multiple/resource-download-multiple.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { Component, | ||
EventEmitter, | ||
Input, | ||
OnDestroy, | ||
Output, } from '@angular/core'; | ||
import { RxAttachment} from 'rxdb'; | ||
import { IResourceFile } from '../../schemas'; | ||
import { Subject, Subscription, takeUntil } from 'rxjs'; | ||
import { FileService } from '@picsa/shared/services/core/file.service'; | ||
|
||
|
||
type IDownloadStatus = 'ready' | 'pending' | 'complete' | 'error'; | ||
|
||
@Component({ | ||
selector: 'picsa-resource-download-multiple', | ||
templateUrl: './resource-download-multiple.component.html', | ||
styleUrl: './resource-download-multiple.component.scss', | ||
}) | ||
|
||
export class ResourceDownloadMultipleComponent implements OnDestroy { | ||
private _dbDoc: IResourceFile[] =[]; | ||
public attachment?: RxAttachment<IResourceFile>; | ||
|
||
downloadStatus: IDownloadStatus = 'ready'; | ||
downloadProgress = 0; | ||
totalSize = 0; | ||
totalSizeMB = 0; | ||
|
||
private componentDestroyed$ = new Subject(); | ||
private downloadSubscription?: Subscription; | ||
|
||
@Output() downloadCompleted = new EventEmitter<void>(); | ||
|
||
@Input() styleVariant: 'primary' | 'white' = 'primary'; | ||
|
||
@Input() size = 48; | ||
|
||
@Input() hideOnComplete = false; | ||
|
||
@Input() set dbDoc(dbDoc: IResourceFile[]) { | ||
this._dbDoc = dbDoc; | ||
} | ||
|
||
|
||
constructor( | ||
private fileService: FileService, | ||
) {} | ||
|
||
public get sizePx() { | ||
return `${this.size}px`; | ||
} | ||
|
||
public get resource() { | ||
return this._dbDoc | ||
; | ||
} | ||
|
||
ngOnInit(): void { | ||
this.calculateTotalSize(); | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.componentDestroyed$.next(true); | ||
this.componentDestroyed$.complete(); | ||
} | ||
|
||
public calculateTotalSize(): void { | ||
if (!this._dbDoc || this._dbDoc.length === 0) { | ||
console.log('No resources available'); | ||
return; | ||
} | ||
|
||
this.totalSize = this._dbDoc.reduce((acc, doc) => acc + doc.size_kb, 0); | ||
const totalSizeMB = this.totalSize / 1024; | ||
this.totalSizeMB = +totalSizeMB.toFixed(2); | ||
} | ||
|
||
public downloadAllResources(): void { | ||
this.downloadStatus = 'pending'; | ||
this.downloadProgress = 0; | ||
|
||
const downloadQueue = [...this.resource]; | ||
const downloadNext = () => { | ||
if (downloadQueue.length === 0) { | ||
this.downloadStatus = 'complete'; | ||
this.downloadCompleted.emit(); | ||
return; | ||
} | ||
|
||
const resource = downloadQueue.shift(); | ||
if (resource) { | ||
this.fileService.downloadFile(resource.url, 'blob').subscribe({ | ||
next: ({ progress }) => { | ||
this.downloadProgress = progress; | ||
}, | ||
error: (error) => { | ||
this.downloadStatus = 'error'; | ||
console.error(error); | ||
}, | ||
complete: () => { | ||
downloadNext(); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
downloadNext(); | ||
} | ||
|
||
public cancelDownload(): void { | ||
if (this.downloadSubscription) { | ||
this.downloadSubscription.unsubscribe(); | ||
this.downloadSubscription = undefined; | ||
} | ||
this.downloadStatus = 'ready'; | ||
this.downloadProgress = 0; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters