Skip to content

Commit d3c3c56

Browse files
crazyserverdpalou
authored andcommitted
MOBILE-4660 storagemanager: Prefetch subsections
1 parent a3bb081 commit d3c3c56

File tree

9 files changed

+343
-308
lines changed

9 files changed

+343
-308
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// (C) Copyright 2015 Moodle Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
export const ADDON_MOD_SUBSECTION_COMPONENT = 'mmaModSubsection';
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// (C) Copyright 2015 Moodle Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import { Injectable } from '@angular/core';
16+
import { CoreCourseResourcePrefetchHandlerBase } from '@features/course/classes/resource-prefetch-handler';
17+
import { CoreCourse, CoreCourseAnyModuleData, CoreCourseWSSection } from '@features/course/services/course';
18+
import { makeSingleton } from '@singletons';
19+
import { ADDON_MOD_SUBSECTION_COMPONENT } from '../../constants';
20+
import { CoreCourseHelper, CoreCourseModuleData } from '@features/course/services/course-helper';
21+
import { CoreFileSizeSum } from '@services/plugin-file-delegate';
22+
import { CoreCourseModulePrefetchDelegate } from '@features/course/services/module-prefetch-delegate';
23+
import { CoreSites } from '@services/sites';
24+
25+
/**
26+
* Handler to prefetch subsections.
27+
*/
28+
@Injectable({ providedIn: 'root' })
29+
export class AddonModSubsectionPrefetchHandlerService extends CoreCourseResourcePrefetchHandlerBase {
30+
31+
name = 'AddonModSubsection';
32+
modName = 'subsection';
33+
component = ADDON_MOD_SUBSECTION_COMPONENT;
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
protected async performDownloadOrPrefetch(
39+
siteId: string,
40+
module: CoreCourseModuleData,
41+
courseId: number,
42+
): Promise<void> {
43+
const section = await this.getSection(module, courseId, siteId);
44+
if (!section) {
45+
return;
46+
}
47+
48+
await CoreCourseHelper.prefetchSections([section], courseId);
49+
}
50+
51+
/**
52+
* @inheritdoc
53+
*/
54+
async getDownloadSize(module: CoreCourseAnyModuleData, courseId: number): Promise<CoreFileSizeSum> {
55+
const section = await this.getSection(module, courseId);
56+
if (!section) {
57+
return { size: 0, total: true };
58+
}
59+
60+
return await CoreCourseModulePrefetchDelegate.getDownloadSize(section.modules, courseId);
61+
}
62+
63+
/**
64+
* @inheritdoc
65+
*/
66+
async getDownloadedSize(module: CoreCourseAnyModuleData, courseId: number): Promise<number> {
67+
const section = await this.getSection(module, courseId);
68+
if (!section) {
69+
return 0;
70+
}
71+
72+
return CoreCourseHelper.getModulesDownloadedSize(section.modules, courseId);
73+
74+
}
75+
76+
/**
77+
* Get the section of a module.
78+
*
79+
* @param module Module.
80+
* @param courseId Course ID.
81+
* @param siteId Site ID. If not defined, current site.
82+
* @returns Promise resolved with the section if found.
83+
*/
84+
protected async getSection(
85+
module: CoreCourseAnyModuleData,
86+
courseId: number,
87+
siteId?: string,
88+
): Promise<CoreCourseWSSection | undefined> {
89+
siteId = siteId ?? CoreSites.getCurrentSiteId();
90+
91+
const sections = await CoreCourse.getSections(courseId, false, true, undefined, siteId);
92+
93+
return sections.find((section) =>
94+
section.component === 'mod_subsection' && section.itemid === module.instance);
95+
}
96+
97+
}
98+
export const AddonModSubsectionPrefetchHandler = makeSingleton(AddonModSubsectionPrefetchHandlerService);

src/addons/mod/subsection/subsection.module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import { APP_INITIALIZER, NgModule } from '@angular/core';
1616
import { CoreContentLinksDelegate } from '@features/contentlinks/services/contentlinks-delegate';
1717
import { AddonModSubsectionIndexLinkHandler } from './services/handlers/index-link';
18+
import { AddonModSubsectionPrefetchHandler } from './services/handlers/prefetch';
19+
import { CoreCourseModulePrefetchDelegate } from '@features/course/services/module-prefetch-delegate';
1820

1921
@NgModule({
2022
providers: [
@@ -23,6 +25,7 @@ import { AddonModSubsectionIndexLinkHandler } from './services/handlers/index-li
2325
multi: true,
2426
useValue: () => {
2527
CoreContentLinksDelegate.registerHandler(AddonModSubsectionIndexLinkHandler.instance);
28+
CoreCourseModulePrefetchDelegate.registerHandler(AddonModSubsectionPrefetchHandler.instance);
2629
},
2730
},
2831
],

src/addons/storagemanager/pages/course-storage/course-storage.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ <h1>{{ 'addon.storagemanager.coursedownloads' | translate }}</h1>
8181
<div class="storage-buttons" slot="end" *ngIf="(!section.calculatingSize && section.totalSize > 0) || downloadEnabled">
8282
<div *ngIf="downloadEnabled" slot="end" class="core-button-spinner">
8383
<core-download-refresh *ngIf="!section.isDownloading && section.downloadStatus !== statusDownloaded"
84-
[status]="section.downloadStatus" [enabled]="true" (action)="prefecthSection(section)"
84+
[status]="section.downloadStatus" [enabled]="true" (action)="prefetchSection(section)"
8585
[loading]="section.isDownloading || section.isCalculating" [canTrustDownload]="true"
8686
[statusesTranslatable]="{notdownloaded: 'addon.storagemanager.downloaddatafrom' }"
8787
[statusSubject]="section.name" />

0 commit comments

Comments
 (0)