Skip to content

Commit 09b232b

Browse files
committed
Merge pull request #298 from dpalou/MOBILE-1297-fix
MOBILE-1297 prefetch: Calculate after each section in download all
2 parents 552d6dc + 7cc482b commit 09b232b

File tree

1 file changed

+64
-33
lines changed

1 file changed

+64
-33
lines changed

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

Lines changed: 64 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,62 @@ angular.module('mm.core.course')
2727

2828
var self = {};
2929

30+
/**
31+
* Calculate the status of a section.
32+
*
33+
* @module mm.core.course
34+
* @ngdoc method
35+
* @name $mmCourseHelper#calculateSectionStatus
36+
* @param {Object[]} section Section to calculate its status. Can't be "All sections".
37+
* @param {Boolean} restoreDownloads True if it should restore downloads. It will try to restore this section downloads.
38+
* @param {Boolean} refresh True if it shouldn't use module status cache (slower).
39+
* @param {Promise[]} [dwnpromises] If section download is restored, a promise will be added to this array. Required
40+
* if restoreDownloads=true.
41+
* @return {Promise} Promise resolved when the state is calculated.
42+
*/
43+
self.calculateSectionStatus = function(section, restoreDownloads, refresh, dwnpromises) {
44+
45+
if (section.id !== mmCoreCourseAllSectionsId) {
46+
// Get the status of this section.
47+
return $mmCoursePrefetchDelegate.getModulesStatus(section.id, section.modules, refresh, restoreDownloads)
48+
.then(function(result) {
49+
50+
// Check if it's being downloaded. We can't trust status 100% because downloaded books are always outdated.
51+
var downloadid = self.getSectionDownloadId(section);
52+
if ($mmCoursePrefetchDelegate.isBeingDownloaded(downloadid)) {
53+
result.status = mmCoreDownloading;
54+
}
55+
56+
// Set this section data.
57+
section.showDownload = result.status === mmCoreNotDownloaded;
58+
section.showRefresh = result.status === mmCoreOutdated;
59+
60+
if (result.status !== mmCoreDownloading) {
61+
section.isDownloading = false;
62+
section.total = 0;
63+
} else if (!restoreDownloads) {
64+
// Set download data.
65+
section.count = 0;
66+
section.total = result[mmCoreOutdated].length + result[mmCoreNotDownloaded].length +
67+
result[mmCoreDownloading].length;
68+
section.isDownloading = true;
69+
} else {
70+
// Restore or re-start the prefetch.
71+
var promise = self.startOrRestorePrefetch(section, result).then(function() {
72+
// Re-calculate the status of this section once finished.
73+
return self.calculateSectionStatus(section);
74+
});
75+
if (dwnpromises) {
76+
dwnpromises.push(promise);
77+
}
78+
}
79+
80+
return result;
81+
});
82+
}
83+
return $q.reject();
84+
};
85+
3086
/**
3187
* Calculate the status of a list of sections, setting attributes to determine the icons/data to be shown.
3288
*
@@ -35,9 +91,7 @@ angular.module('mm.core.course')
3591
* @name $mmCourseHelper#calculateSectionsStatus
3692
* @param {Object[]} sections Sections to calculate their status.
3793
* @param {Boolean} restoreDownloads True if it should restore downloads. It will try to restore section downloads
38-
* and module downloads.
39-
* @param {Boolean} refresh True if it shouldn't use module status cache.
40-
* It's used only if refresh=false.
94+
* @param {Boolean} refresh True if it shouldn't use module status cache (slower).
4195
* @return {Promise} Promise resolved when the states are calculated. Returns an array of download promises
4296
* with the restored downloads (only if restoreDownloads=true).
4397
*/
@@ -53,38 +107,12 @@ angular.module('mm.core.course')
53107
// "All sections" section status is calculated using the status of the rest of sections.
54108
allsectionssection = section;
55109
} else {
56-
var statuspromise = $mmCoursePrefetchDelegate.getModulesStatus(section.id, section.modules,
57-
refresh, restoreDownloads).then(function(result) {
58-
59-
// Check if it's being downloaded. We can't trust status 100% because downloaded books are always outdated.
60-
var downloadid = self.getSectionDownloadId(section);
61-
if ($mmCoursePrefetchDelegate.isBeingDownloaded(downloadid)) {
62-
result.status = mmCoreDownloading;
63-
}
110+
statuspromises.push(self.calculateSectionStatus(section, restoreDownloads, refresh, downloadpromises)
111+
.then(function(result) {
64112

65113
// Calculate "All sections" status.
66114
allsectionsstatus = $mmFilepool.determinePackagesStatus(allsectionsstatus, result.status);
67-
68-
// Set this section data.
69-
section.showDownload = result.status === mmCoreNotDownloaded;
70-
section.showRefresh = result.status === mmCoreOutdated;
71-
72-
if (result.status !== mmCoreDownloading) {
73-
section.isDownloading = false;
74-
section.total = 0;
75-
} else if (restoreDownloads) {
76-
// Restore or re-start the prefetch.
77-
downloadpromises.push(self.startOrRestorePrefetch(section, result));
78-
} else {
79-
// Set download data.
80-
section.count = 0;
81-
section.total = result[mmCoreOutdated].length + result[mmCoreNotDownloaded].length +
82-
result[mmCoreDownloading].length;
83-
section.isDownloading = true;
84-
}
85-
});
86-
87-
statuspromises.push(statuspromise);
115+
}));
88116
}
89117
});
90118

@@ -176,7 +204,10 @@ angular.module('mm.core.course')
176204
section.isDownloading = true;
177205
angular.forEach(sections, function(s) {
178206
if (s.id != mmCoreCourseAllSectionsId) {
179-
promises.push(self.prefetchSection(s, false, sections));
207+
promises.push(self.prefetchSection(s, false, sections).then(function() {
208+
// Calculate only the section that finished.
209+
return self.calculateSectionStatus(s);
210+
}));
180211
}
181212
});
182213

0 commit comments

Comments
 (0)