Skip to content

Commit aa93558

Browse files
committed
Merge pull request #401 from dpalou/MOBILE-1439
MOBILE-1439 prefetch: Check size in all resources and mmFile
2 parents 38c4d2b + a451569 commit aa93558

File tree

7 files changed

+72
-69
lines changed

7 files changed

+72
-69
lines changed

www/addons/mod_book/services/handlers.js

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

2828
var self = {};
2929

@@ -67,11 +67,8 @@ angular.module('mm.addons.mod_book')
6767
action: function(e) {
6868
e.preventDefault();
6969
e.stopPropagation();
70-
$mmaModBook.prefetchContent(module).catch(function() {
71-
if (!$scope.$$destroyed) {
72-
$mmUtil.showErrorModal('mm.core.errordownloading', true);
73-
}
74-
});
70+
var size = $mmaModBookPrefetchHandler.getDownloadSize(module);
71+
$mmCourseHelper.prefetchModule($mmaModBook, module, size, false);
7572
}
7673
};
7774

@@ -82,14 +79,8 @@ angular.module('mm.addons.mod_book')
8279
action: function(e) {
8380
e.preventDefault();
8481
e.stopPropagation();
85-
86-
$mmaModBook.invalidateContent(module.id).finally(function() {
87-
$mmaModBook.prefetchContent(module).catch(function() {
88-
if (!$scope.$$destroyed) {
89-
$mmUtil.showErrorModal('mm.core.errordownloading', true);
90-
}
91-
});
92-
});
82+
var size = $mmaModBookPrefetchHandler.getDownloadSize(module);
83+
$mmCourseHelper.prefetchModule($mmaModBook, module, size, true);
9384
}
9485
};
9586

www/addons/mod_folder/services/handlers.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ angular.module('mm.addons.mod_folder')
2121
* @ngdoc service
2222
* @name $mmaModFolderHandlers
2323
*/
24-
.factory('$mmaModFolderHandlers', function($mmCourse, $mmaModFolder, $mmEvents, $state, $mmSite, $mmUtil, $mmFilepool,
24+
.factory('$mmaModFolderHandlers', function($mmCourse, $mmaModFolder, $mmEvents, $state, $mmSite, $mmCourseHelper, $mmFilepool,
2525
$mmCoursePrefetchDelegate, mmCoreDownloading, mmCoreNotDownloaded, mmCoreOutdated, mmCoreEventPackageStatusChanged,
26-
mmaModFolderComponent, $mmContentLinksHelper, $q) {
26+
mmaModFolderComponent, $mmContentLinksHelper, $q, $mmaModFolderPrefetchHandler) {
2727
var self = {};
2828

2929
/**
@@ -70,11 +70,10 @@ angular.module('mm.addons.mod_folder')
7070
function prefetchFolder(e) {
7171
e.preventDefault();
7272
e.stopPropagation();
73-
$mmaModFolder.prefetchContent(module).catch(function() {
74-
if (!$scope.$$destroyed) {
75-
$mmUtil.showErrorModal('mm.core.errordownloading', true);
76-
}
77-
});
73+
74+
// Check size and show confirmation if needed.
75+
var size = $mmaModFolderPrefetchHandler.getDownloadSize(module);
76+
$mmCourseHelper.prefetchModule($mmaModFolder, module, size, false);
7877
}
7978

8079
downloadBtn = {

www/addons/mod_imscp/services/handlers.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ angular.module('mm.addons.mod_imscp')
2121
* @ngdoc service
2222
* @name $mmaModImscpHandlers
2323
*/
24-
.factory('$mmaModImscpHandlers', function($mmCourse, $mmaModImscp, $mmEvents, $state, $mmSite, $mmUtil, $mmFilepool,
24+
.factory('$mmaModImscpHandlers', function($mmCourse, $mmaModImscp, $mmEvents, $state, $mmSite, $mmCourseHelper, $mmFilepool,
2525
$mmCoursePrefetchDelegate, mmCoreDownloading, mmCoreNotDownloaded, mmCoreOutdated, mmCoreEventPackageStatusChanged,
26-
mmaModImscpComponent, $mmContentLinksHelper, $q) {
26+
mmaModImscpComponent, $mmContentLinksHelper, $q, $mmaModImscpPrefetchHandler) {
2727
var self = {};
2828

2929
/**
@@ -73,11 +73,8 @@ angular.module('mm.addons.mod_imscp')
7373
action: function(e) {
7474
e.preventDefault();
7575
e.stopPropagation();
76-
$mmaModImscp.prefetchContent(module).catch(function() {
77-
if (!$scope.$$destroyed) {
78-
$mmUtil.showErrorModal('mm.core.errordownloading', true);
79-
}
80-
});
76+
var size = $mmaModImscpPrefetchHandler.getDownloadSize(module);
77+
$mmCourseHelper.prefetchModule($mmaModImscp, module, size, false);
8178
}
8279
};
8380

@@ -88,13 +85,8 @@ angular.module('mm.addons.mod_imscp')
8885
action: function(e) {
8986
e.preventDefault();
9087
e.stopPropagation();
91-
$mmaModImscp.invalidateContent(module.id).then(function() {
92-
$mmaModImscp.prefetchContent(module).catch(function() {
93-
if (!$scope.$$destroyed) {
94-
$mmUtil.showErrorModal('mm.core.errordownloading', true);
95-
}
96-
});
97-
});
88+
var size = $mmaModImscpPrefetchHandler.getDownloadSize(module);
89+
$mmCourseHelper.prefetchModule($mmaModImscp, module, size, true);
9890
}
9991
};
10092

www/addons/mod_page/services/handlers.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ angular.module('mm.addons.mod_page')
2121
* @ngdoc service
2222
* @name $mmaModPageHandlers
2323
*/
24-
.factory('$mmaModPageHandlers', function($mmCourse, $mmaModPage, $mmEvents, $state, $mmSite, $mmUtil, $mmFilepool,
24+
.factory('$mmaModPageHandlers', function($mmCourse, $mmaModPage, $mmEvents, $state, $mmSite, $mmCourseHelper, $mmFilepool,
2525
$mmCoursePrefetchDelegate, mmCoreDownloading, mmCoreNotDownloaded, mmCoreOutdated, mmCoreEventPackageStatusChanged,
26-
mmaModPageComponent, $mmContentLinksHelper, $q) {
26+
mmaModPageComponent, $mmContentLinksHelper, $q, $mmaModPagePrefetchHandler) {
2727
var self = {};
2828

2929
/**
@@ -67,11 +67,8 @@ angular.module('mm.addons.mod_page')
6767
action: function(e) {
6868
e.preventDefault();
6969
e.stopPropagation();
70-
$mmaModPage.prefetchContent(module).catch(function() {
71-
if (!$scope.$$destroyed) {
72-
$mmUtil.showErrorModal('mm.core.errordownloading', true);
73-
}
74-
});
70+
var size = $mmaModPagePrefetchHandler.getDownloadSize(module);
71+
$mmCourseHelper.prefetchModule($mmaModPage, module, size, false);
7572
}
7673
};
7774

@@ -82,13 +79,8 @@ angular.module('mm.addons.mod_page')
8279
action: function(e) {
8380
e.preventDefault();
8481
e.stopPropagation();
85-
$mmaModPage.invalidateContent(module.id).finally(function() {
86-
$mmaModPage.prefetchContent(module).catch(function() {
87-
if (!$scope.$$destroyed) {
88-
$mmUtil.showErrorModal('mm.core.errordownloading', true);
89-
}
90-
});
91-
});
82+
var size = $mmaModPagePrefetchHandler.getDownloadSize(module);
83+
$mmCourseHelper.prefetchModule($mmaModPage, module, size, true);
9284
}
9385
};
9486

www/addons/mod_resource/services/handlers.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ angular.module('mm.addons.mod_resource')
2121
* @ngdoc service
2222
* @name $mmaModResourceHandlers
2323
*/
24-
.factory('$mmaModResourceHandlers', function($mmCourse, $mmaModResource, $mmEvents, $state, $mmSite, $mmUtil,
24+
.factory('$mmaModResourceHandlers', function($mmCourse, $mmaModResource, $mmEvents, $state, $mmSite, $mmCourseHelper,
2525
$mmCoursePrefetchDelegate, $mmFilepool, $mmFS, mmCoreDownloading, mmCoreNotDownloaded, mmCoreOutdated,
26-
mmCoreEventPackageStatusChanged, mmaModResourceComponent, $q, $mmContentLinksHelper) {
26+
mmCoreEventPackageStatusChanged, mmaModResourceComponent, $q, $mmContentLinksHelper, $mmaModResourcePrefetchHandler) {
2727
var self = {};
2828

2929
/**
@@ -67,11 +67,8 @@ angular.module('mm.addons.mod_resource')
6767
action: function(e) {
6868
e.preventDefault();
6969
e.stopPropagation();
70-
$mmaModResource.prefetchContent(module).catch(function() {
71-
if (!$scope.$$destroyed) {
72-
$mmUtil.showErrorModal('mm.core.errordownloading', true);
73-
}
74-
});
70+
var size = $mmaModResourcePrefetchHandler.getDownloadSize(module);
71+
$mmCourseHelper.prefetchModule($mmaModResource, module, size, false);
7572
}
7673
};
7774

@@ -82,13 +79,8 @@ angular.module('mm.addons.mod_resource')
8279
action: function(e) {
8380
e.preventDefault();
8481
e.stopPropagation();
85-
$mmaModResource.invalidateContent(module.id).finally(function() {
86-
$mmaModResource.prefetchContent(module).catch(function() {
87-
if (!$scope.$$destroyed) {
88-
$mmUtil.showErrorModal('mm.core.errordownloading', true);
89-
}
90-
});
91-
});
82+
var size = $mmaModResourcePrefetchHandler.getDownloadSize(module);
83+
$mmCourseHelper.prefetchModule($mmaModResource, module, size, true);
9284
}
9385
};
9486

www/core/components/course/services/helper.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,36 @@ angular.module('mm.core.course')
299299
}
300300
};
301301

302+
/**
303+
* Helper function to prefetch a module, showing a confirmation modal if the size is big
304+
* and invalidating contents if refreshing.
305+
*
306+
* @module mm.core.course
307+
* @ngdoc method
308+
* @name $mmCourseHelper#prefetchModule
309+
* @param {Object} service Service implementing 'invalidateContent' and 'prefetchContent'.
310+
* @param {Object} module Module to download.
311+
* @param {Number} size Size of the module.
312+
* @param {Boolean} refresh True if refreshing, false otherwise.
313+
* @return {Promise} Promise resolved when downloaded.
314+
*/
315+
self.prefetchModule = function(service, module, size, refresh) {
316+
// Show confirmation if needed.
317+
return $mmUtil.confirmDownloadSize(size).then(function() {
318+
// Invalidate content if refreshing and download the data.
319+
var promise = refresh ? service.invalidateContent(module.id) : $q.when();
320+
return promise.catch(function() {
321+
// Ignore errors.
322+
}).then(function() {
323+
return service.prefetchContent(module).catch(function() {
324+
if (!$scope.$$destroyed) {
325+
$mmUtil.showErrorModal('mm.core.errordownloading', true);
326+
}
327+
});
328+
});
329+
});
330+
};
331+
302332
/**
303333
* Prefetch or restore the prefetch of a certain section if it needs to be prefetched.
304334
* If the section is "All sections" it will be ignored.

www/core/directives/file.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ angular.module('mm.core')
2828
* @param {Object} file Required. Object with the following attributes:
2929
* 'filename': Name of the file.
3030
* 'fileurl' or 'url': File URL.
31+
* 'filesize': Optional. Size of the file.
3132
* @param {String} [component] Component the file belongs to.
3233
* @param {Number} [componentId] Component ID.
3334
* @param {Boolean} [timemodified] If set, the value will be used to check if the file is outdated.
@@ -94,6 +95,7 @@ angular.module('mm.core')
9495
link: function(scope, element, attrs) {
9596
var fileurl = scope.file.fileurl || scope.file.url,
9697
filename = scope.file.filename,
98+
filesize = scope.file.filesize,
9799
timemodified = attrs.timemodified || 0,
98100
siteid = $mmSite.getId(),
99101
component = attrs.component,
@@ -116,6 +118,7 @@ angular.module('mm.core')
116118
scope.download = function(e, openAfterDownload) {
117119
e.preventDefault();
118120
e.stopPropagation();
121+
var promise;
119122

120123
if (scope.isDownloading) {
121124
return;
@@ -134,13 +137,17 @@ angular.module('mm.core')
134137
});
135138
});
136139
} else {
137-
// File doesn't need to be opened, add it to queue.
138-
$mmFilepool.invalidateFileByUrl(siteid, fileurl).finally(function() {
139-
scope.isDownloading = true;
140-
$mmFilepool.addToQueueByUrl(siteid, fileurl, component, componentid, timemodified);
140+
// File doesn't need to be opened (it's a prefetch). Show confirm modal if file size is defined and it's big.
141+
promise = filesize ? $mmUtil.confirmDownloadSize(filesize) : $q.when();
142+
promise.then(function() {
143+
// User confirmed, add the file to queue.
144+
$mmFilepool.invalidateFileByUrl(siteid, fileurl).finally(function() {
145+
scope.isDownloading = true;
146+
$mmFilepool.addToQueueByUrl(siteid, fileurl, component, componentid, timemodified);
147+
});
141148
});
142149
}
143-
}
150+
};
144151

145152
scope.$on('$destroy', function() {
146153
if (observer && observer.off) {

0 commit comments

Comments
 (0)