Skip to content

Commit 5174deb

Browse files
committed
MOBILE-1297 prefetch: Deprecate modules status and simplify handlers
1 parent 2a190cf commit 5174deb

File tree

21 files changed

+878
-909
lines changed

21 files changed

+878
-909
lines changed

www/addons/mod_book/services/book.js

Lines changed: 26 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -21,73 +21,10 @@ angular.module('mm.addons.mod_book')
2121
* @ngdoc service
2222
* @name $mmaModBook
2323
*/
24-
.factory('$mmaModBook', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmCourse, mmaModBookComponent,
25-
mmCoreDownloading, mmCoreDownloaded) {
24+
.factory('$mmaModBook', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, mmaModBookComponent) {
2625
$log = $log.getInstance('$mmaModBook');
2726

28-
var self = {},
29-
downloadPromises = {}; // To handle downloads.
30-
31-
/**
32-
* Downloads or prefetches all the content.
33-
*
34-
* @param {Object} module The module object.
35-
* @param {Boolean} prefetch True if prefetching, false otherwise.
36-
* @return {Promise} Promise resolved when all content is downloaded. Data returned is not reliable.
37-
*/
38-
function downloadOrPrefetch(module, prefetch) {
39-
40-
var siteid = $mmSite.getId();
41-
if (downloadPromises[siteid] && downloadPromises[siteid][module.id]) {
42-
// There's already a download ongoing for this module, return the promise.
43-
return downloadPromises[siteid][module.id];
44-
} else if (!downloadPromises[siteid]) {
45-
downloadPromises[siteid] = {};
46-
}
47-
48-
var revision = $mmCourse.getRevisionFromContents(module.contents),
49-
timemod = $mmCourse.getTimemodifiedFromContents(module.contents),
50-
dwnPromise,
51-
deleted = false;
52-
53-
// Set module as downloading.
54-
dwnPromise = $mmCourse.storeModuleStatus(siteid, module.id, mmCoreDownloading, revision, timemod).then(function() {
55-
var promises = [];
56-
57-
angular.forEach(module.contents, function(content) {
58-
var url = content.fileurl,
59-
filetimemodified = content.timemodified;
60-
if (!self.isFileDownloadable(content)) {
61-
return;
62-
}
63-
64-
if (prefetch) {
65-
promises.push($mmFilepool.addToQueueByUrl(siteid, url, mmaModBookComponent, module.id, filetimemodified));
66-
} else {
67-
promises.push($mmFilepool.downloadUrl(siteid, url, false, mmaModBookComponent, module.id, filetimemodified));
68-
}
69-
});
70-
71-
return $q.all(promises).then(function() {
72-
// Success prefetching, store module as downloaded.
73-
return $mmCourse.storeModuleStatus(siteid, module.id, mmCoreDownloaded, revision, timemod);
74-
}).catch(function() {
75-
// Error downloading, go back to previous status and reject the promise.
76-
return $mmCourse.setModulePreviousStatus(siteid, module.id).then(function() {
77-
return $q.reject();
78-
});
79-
});
80-
}).finally(function() {
81-
// Download finished, delete the promise.
82-
delete downloadPromises[siteid][module.id];
83-
deleted = true;
84-
});
85-
86-
if (!deleted) { // In case promise was finished immediately.
87-
downloadPromises[siteid][module.id] = dwnPromise;
88-
}
89-
return dwnPromise;
90-
}
27+
var self = {};
9128

9229
/**
9330
* Download all the content.
@@ -99,7 +36,8 @@ angular.module('mm.addons.mod_book')
9936
* @return {Promise} Promise resolved when all content is downloaded. Data returned is not reliable.
10037
*/
10138
self.downloadAllContent = function(module) {
102-
return downloadOrPrefetch(module, false);
39+
var files = self.getDownloadableFiles(module);
40+
return $mmFilepool.downloadPackage($mmSite.getId(), files, mmaModBookComponent, module.id);
10341
};
10442

10543
/**
@@ -135,22 +73,6 @@ angular.module('mm.addons.mod_book')
13573
});
13674
};
13775

138-
/**
139-
* Get a download promise. If the promise is not set, return undefined.
140-
*
141-
* @module mm.addons.mod_book
142-
* @ngdoc method
143-
* @name $mmaModBook#getDownloadPromise
144-
* @param {String} siteId Site ID.
145-
* @param {Number} moduleId Module ID.
146-
* @return {Promise} Download promise or undefined.
147-
*/
148-
self.getDownloadPromise = function(siteId, moduleId) {
149-
if (downloadPromises[siteId] && downloadPromises[siteId][moduleId]) {
150-
return downloadPromises[siteId][moduleId];
151-
}
152-
};
153-
15476
/**
15577
* Returns a list of file event names.
15678
*
@@ -174,6 +96,26 @@ angular.module('mm.addons.mod_book')
17496
});
17597
};
17698

99+
/**
100+
* Returns a list of files that can be downloaded.
101+
*
102+
* @module mm.addons.mod_book
103+
* @ngdoc method
104+
* @name $mmaModBook#getDownloadableFiles
105+
* @param {Object} module The module object returned by WS.
106+
* @return {Object[]} List of files.
107+
*/
108+
self.getDownloadableFiles = function(module) {
109+
var files = [];
110+
111+
angular.forEach(module.contents, function(content) {
112+
if (self.isFileDownloadable(content)) {
113+
files.push(content);
114+
}
115+
});
116+
117+
return files;
118+
};
177119

178120
/**
179121
* Get the book toc as an array.
@@ -427,7 +369,8 @@ angular.module('mm.addons.mod_book')
427369
* @return {Promise} Promise resolved when all content is downloaded. Data returned is not reliable.
428370
*/
429371
self.prefetchContent = function(module) {
430-
return downloadOrPrefetch(module, true);
372+
var files = self.getDownloadableFiles(module);
373+
return $mmFilepool.prefetchPackage($mmSite.getId(), files, mmaModBookComponent, module.id);
431374
};
432375

433376
return self;

www/addons/mod_book/services/course_content_handler.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ angular.module('mm.addons.mod_book')
2121
* @ngdoc service
2222
* @name $mmaModBookCourseContentHandler
2323
*/
24-
.factory('$mmaModBookCourseContentHandler', function($mmCourse, $mmaModBook, $mmEvents, $state, $mmSite, $mmUtil,
25-
$mmaModBookPrefetchHandler, mmCoreDownloading, mmCoreNotDownloaded, mmCoreOutdated, mmCoreDownloaded,
26-
mmCoreCourseModuleStatusChanged) {
24+
.factory('$mmaModBookCourseContentHandler', function($mmCourse, $mmaModBook, $mmEvents, $state, $mmSite, $mmUtil, $mmFilepool,
25+
$mmCoursePrefetchDelegate, mmCoreDownloading, mmCoreNotDownloaded, mmCoreOutdated, mmCoreDownloaded,
26+
mmCoreEventPackageStatusChanged, mmaModBookComponent) {
2727

2828
var self = {};
2929

@@ -53,8 +53,8 @@ angular.module('mm.addons.mod_book')
5353
return function($scope) {
5454
var downloadBtn,
5555
refreshBtn,
56-
revision = $mmCourse.getRevisionFromContents(module.contents),
57-
timemodified = $mmCourse.getTimemodifiedFromContents(module.contents);
56+
revision = $mmFilepool.getRevisionFromFileList(module.contents),
57+
timemodified = $mmFilepool.getTimemodifiedFromFileList(module.contents);
5858

5959
downloadBtn = {
6060
hidden: true,
@@ -107,14 +107,14 @@ angular.module('mm.addons.mod_book')
107107
}
108108

109109
// Listen for changes on this module status.
110-
var statusObserver = $mmEvents.on(mmCoreCourseModuleStatusChanged, function(data) {
111-
if (data.siteid === $mmSite.getId() && data.moduleid === module.id) {
110+
var statusObserver = $mmEvents.on(mmCoreEventPackageStatusChanged, function(data) {
111+
if (data.siteid === $mmSite.getId() && data.componentId === module.id && data.component === mmaModBookComponent) {
112112
showStatus(data.status);
113113
}
114114
});
115115

116116
// Get current status to decide which icon should be shown.
117-
$mmaModBookPrefetchHandler.getStatus(module, revision, timemodified).then(showStatus);
117+
$mmCoursePrefetchDelegate.getModuleStatus(module, revision, timemodified).then(showStatus);
118118

119119
$scope.$on('$destroy', function() {
120120
statusObserver && statusObserver.off && statusObserver.off();

www/addons/mod_book/services/prefetch_handler.js

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,23 @@ angular.module('mm.addons.mod_book')
2121
* @ngdoc service
2222
* @name $mmaModBookPrefetchHandler
2323
*/
24-
.factory('$mmaModBookPrefetchHandler', function($mmaModBook, $mmCourse, $mmSite, mmCoreDownloading, mmCoreDownloaded,
25-
mmCoreOutdated) {
24+
.factory('$mmaModBookPrefetchHandler', function($mmaModBook, mmCoreDownloaded, mmCoreOutdated, mmaModBookComponent) {
2625

2726
var self = {};
2827

28+
self.component = mmaModBookComponent;
29+
2930
/**
3031
* Determine the status of a module based on the current status detected.
3132
*
3233
* @module mm.addons.mod_book
3334
* @ngdoc method
3435
* @name $mmaModBookPrefetchHandler#determineStatus
35-
* @param {Object} module Module.
36-
* @param {String} status Current status.
37-
* @param {Boolean} restoreDownloads True if it should restore downloads if needed.
38-
* @return {String} Module status.
36+
* @param {String} status Current status.
37+
* @return {String} Status to show.
3938
*/
40-
self.determineStatus = function(module, status, restoreDownloads) {
41-
if (status == mmCoreDownloading && restoreDownloads) {
42-
var siteid = $mmSite.getId();
43-
// Check if the download is being handled.
44-
if (!$mmaModBook.getDownloadPromise(siteid, module.id)) {
45-
// Not handled, the app was probably restarted or something weird happened.
46-
// Re-start download (files already on queue or already downloaded will be skipped).
47-
$mmaModBook.prefetchContent(module);
48-
}
49-
return status;
50-
} else if (status === mmCoreDownloaded) {
39+
self.determineStatus = function(status) {
40+
if (status === mmCoreDownloaded) {
5141
// Books are always treated as outdated since revision and timemodified aren't reliable.
5242
return mmCoreOutdated;
5343
} else {
@@ -56,23 +46,22 @@ angular.module('mm.addons.mod_book')
5646
};
5747

5848
/**
59-
* Get the module status.
49+
* Get the download size of a module.
6050
*
6151
* @module mm.addons.mod_book
6252
* @ngdoc method
63-
* @name $mmaModBookPrefetchHandler#getStatus
64-
* @param {Object} module Module.
65-
* @param {Number} [revision] Module's revision. If not defined, it will be calculated using module data.
66-
* @param {Number} [timemodified] Module's timemodified. If not defined, it will be calculated using module data.
67-
* @return {Promise} Promise resolved with the status.
53+
* @name $mmaModBookPrefetchHandler#getDownloadSize
54+
* @param {Object} module Module to get the size.
55+
* @return {Number} Size.
6856
*/
69-
self.getStatus = function(module, revision, timemodified) {
70-
revision = revision || $mmCourse.getRevisionFromContents(module.contents);
71-
timemodified = timemodified || $mmCourse.getTimemodifiedFromContents(module.contents);
72-
73-
return $mmCourse.getModuleStatus($mmSite.getId(), module.id, revision, timemodified).then(function(status) {
74-
return self.determineStatus(module, status, true);
57+
self.getDownloadSize = function(module) {
58+
var size = 0;
59+
angular.forEach(module.contents, function(content) {
60+
if ($mmaModBook.isFileDownloadable(content) && content.filesize) {
61+
size = size + content.filesize;
62+
}
7563
});
64+
return size;
7665
};
7766

7867
/**
@@ -87,20 +76,6 @@ angular.module('mm.addons.mod_book')
8776
return $mmaModBook.isPluginEnabled();
8877
};
8978

90-
/**
91-
* Whether or not a file belonging to a mod_book is downloadable.
92-
* The file param must have a 'type' attribute like in core_course_get_contents response.
93-
*
94-
* @module mm.addons.mod_book
95-
* @ngdoc method
96-
* @name $mmaModBookPrefetchHandler#isFileDownloadable
97-
* @param {Object} file File to check.
98-
* @return {Boolean} True if downloadable, false otherwise.
99-
*/
100-
self.isFileDownloadable = function(file) {
101-
return $mmaModBook.isFileDownloadable(file);
102-
};
103-
10479
/**
10580
* Prefetch the module.
10681
*

www/addons/mod_imscp/services/course_content_handler.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ angular.module('mm.addons.mod_imscp')
2121
* @ngdoc service
2222
* @name $mmaModImscpCourseContentHandler
2323
*/
24-
.factory('$mmaModImscpCourseContentHandler', function($mmCourse, $mmaModImscp, $mmEvents, $state, $mmSite, $mmUtil,
25-
$mmaModImscpPrefetchHandler, mmCoreDownloading, mmCoreNotDownloaded, mmCoreOutdated, mmCoreCourseModuleStatusChanged) {
24+
.factory('$mmaModImscpCourseContentHandler', function($mmCourse, $mmaModImscp, $mmEvents, $state, $mmSite, $mmUtil, $mmFilepool,
25+
$mmCoursePrefetchDelegate, mmCoreDownloading, mmCoreNotDownloaded, mmCoreOutdated, mmCoreEventPackageStatusChanged,
26+
mmaModImscpComponent) {
2627

2728
var self = {};
2829

@@ -52,8 +53,8 @@ angular.module('mm.addons.mod_imscp')
5253
return function($scope) {
5354
var downloadBtn,
5455
refreshBtn,
55-
revision = $mmCourse.getRevisionFromContents(module.contents),
56-
timemodified = $mmCourse.getTimemodifiedFromContents(module.contents);
56+
revision = $mmFilepool.getRevisionFromFileList(module.contents),
57+
timemodified = $mmFilepool.getTimemodifiedFromFileList(module.contents);
5758

5859
downloadBtn = {
5960
hidden: true,
@@ -104,14 +105,14 @@ angular.module('mm.addons.mod_imscp')
104105
}
105106

106107
// Listen for changes on this module status.
107-
var statusObserver = $mmEvents.on(mmCoreCourseModuleStatusChanged, function(data) {
108-
if (data.siteid === $mmSite.getId() && data.moduleid === module.id) {
108+
var statusObserver = $mmEvents.on(mmCoreEventPackageStatusChanged, function(data) {
109+
if (data.siteid === $mmSite.getId() && data.componentId === module.id && data.component === mmaModImscpComponent) {
109110
showStatus(data.status);
110111
}
111112
});
112113

113114
// Get current status to decide which icon should be shown.
114-
$mmaModImscpPrefetchHandler.getStatus(module, revision, timemodified).then(showStatus);
115+
$mmCoursePrefetchDelegate.getModuleStatus(module, revision, timemodified).then(showStatus);
115116

116117
$scope.$on('$destroy', function() {
117118
statusObserver && statusObserver.off && statusObserver.off();

0 commit comments

Comments
 (0)