Skip to content

Commit 0d639f9

Browse files
committed
MOBILE-1392 prefetch: Allow disable prefetch sections
1 parent 10fd2f5 commit 0d639f9

File tree

5 files changed

+63
-28
lines changed

5 files changed

+63
-28
lines changed

www/core/components/course/controllers/sections.js

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,21 @@ angular.module('mm.core.course')
2323
*/
2424
.controller('mmCourseSectionsCtrl', function($mmCourse, $mmUtil, $scope, $stateParams, $translate, $mmCourseHelper, $mmEvents,
2525
$mmSite, $mmCoursePrefetchDelegate, $mmCourses, $q, $ionicHistory, $ionicPlatform, mmCoreCourseAllSectionsId,
26-
mmCoreEventSectionStatusChanged) {
27-
var courseid = $stateParams.courseid;
26+
mmCoreEventSectionStatusChanged, $mmConfig, mmCoreSettingsDownloadSection) {
27+
var courseid = $stateParams.courseid,
28+
downloadSectionsEnabled;
2829

2930
$scope.courseid = courseid;
3031

32+
function checkDownloadSectionsEnabled() {
33+
return $mmConfig.get(mmCoreSettingsDownloadSection, true).then(function(enabled) {
34+
downloadSectionsEnabled = enabled;
35+
}).catch(function() {
36+
// Shouldn't happen.
37+
downloadSectionsEnabled = false;
38+
});
39+
}
40+
3141
function loadSections(refresh) {
3242
// Get full course data. If not refreshing we'll try to get it from cache to speed up the response.
3343
return $mmCourses.getUserCourse(courseid).then(function(course) {
@@ -42,27 +52,28 @@ angular.module('mm.core.course')
4252
id: mmCoreCourseAllSectionsId
4353
}].concat(sections);
4454

45-
// Calculate status of the sections.
46-
return $mmCourseHelper.calculateSectionsStatus(result, courseid, true, refresh).catch(function() {
47-
// Ignore errors (shouldn't happen).
48-
}).then(function(downloadpromises) {
49-
// If we restored any download we'll recalculate the status once all of them have finished.
50-
if (downloadpromises && downloadpromises.length) {
51-
$mmUtil.allPromises(downloadpromises).catch(function() {
52-
if (!$scope.$$destroyed) {
53-
$mmUtil.showErrorModal('mm.course.errordownloadingsection', true);
54-
}
55-
}).finally(function() {
56-
if (!$scope.$$destroyed) {
57-
// Recalculate the status.
58-
$mmCourseHelper.calculateSectionsStatus($scope.sections, courseid, false);
59-
}
60-
});
61-
}
62-
}).finally(function() {
63-
// Show the sections even if some calculation fails (it shouldn't).
64-
$scope.sections = result;
65-
});
55+
$scope.sections = result;
56+
57+
if (downloadSectionsEnabled) {
58+
// Calculate status of the sections.
59+
return $mmCourseHelper.calculateSectionsStatus(result, courseid, true, refresh).catch(function() {
60+
// Ignore errors (shouldn't happen).
61+
}).then(function(downloadpromises) {
62+
// If we restored any download we'll recalculate the status once all of them have finished.
63+
if (downloadpromises && downloadpromises.length) {
64+
$mmUtil.allPromises(downloadpromises).catch(function() {
65+
if (!$scope.$$destroyed) {
66+
$mmUtil.showErrorModal('mm.course.errordownloadingsection', true);
67+
}
68+
}).finally(function() {
69+
if (!$scope.$$destroyed) {
70+
// Recalculate the status.
71+
$mmCourseHelper.calculateSectionsStatus($scope.sections, courseid, false);
72+
}
73+
});
74+
}
75+
});
76+
}
6677
});
6778
});
6879
}).catch(function(error) {
@@ -120,13 +131,16 @@ angular.module('mm.core.course')
120131
});
121132
};
122133

123-
loadSections().finally(function() {
124-
$scope.sectionsLoaded = true;
134+
checkDownloadSectionsEnabled().then(function() {
135+
loadSections().finally(function() {
136+
$scope.sectionsLoaded = true;
137+
});
125138
});
126139

127140
// Listen for section status changes.
128141
var statusObserver = $mmEvents.on(mmCoreEventSectionStatusChanged, function(data) {
129-
if ($scope.sections && $scope.sections.length && data.siteid === $mmSite.getId() && !$scope.$$destroyed && data.sectionid) {
142+
if (downloadSectionsEnabled && $scope.sections && $scope.sections.length && data.siteid === $mmSite.getId() &&
143+
!$scope.$$destroyed&& data.sectionid) {
130144
// Check if the affected section is being downloaded. If so, we don't update section status
131145
// because it'll already be updated when the download finishes.
132146
if ($mmCoursePrefetchDelegate.isBeingDownloaded($mmCourseHelper.getSectionDownloadId({id: data.sectionid}))) {

www/core/components/settings/controllers/general.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ angular.module('mm.core.settings')
2121
* @ngdoc controller
2222
* @name mmSettingsGeneralCtrl
2323
*/
24-
.controller('mmSettingsGeneralCtrl', function($scope, $mmLang, $ionicHistory, $mmEvents, mmCoreEventLanguageChanged,
25-
mmCoreSettingsReportInBackground, mmCoreConfigConstants) {
24+
.controller('mmSettingsGeneralCtrl', function($scope, $mmLang, $ionicHistory, $mmEvents, $mmConfig, mmCoreEventLanguageChanged,
25+
mmCoreSettingsReportInBackground, mmCoreConfigConstants, mmCoreSettingsDownloadSection) {
2626

2727
$scope.langs = mmCoreConfigConstants.languages;
2828

@@ -38,6 +38,14 @@ angular.module('mm.core.settings')
3838
});
3939
};
4040

41+
$mmConfig.get(mmCoreSettingsDownloadSection, true).then(function(downloadSectionEnabled) {
42+
$scope.downloadSection = downloadSectionEnabled;
43+
});
44+
45+
$scope.downloadSectionChanged = function(downloadSection) {
46+
$mmConfig.set(mmCoreSettingsDownloadSection, downloadSection);
47+
};
48+
4149
if (localStorage && localStorage.getItem && localStorage.setItem) {
4250
$scope.showReport = true;
4351
$scope.reportInBackground = parseInt(localStorage.getItem(mmCoreSettingsReportInBackground), 10) === 1;

www/core/components/settings/lang/en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"displayformat": "Display format",
1818
"devicewebworkers": "Device Web Workers supported",
1919
"enabledebugging": "Enable debugging",
20+
"enabledownloadsection": "Enable download sections",
21+
"enabledownloadsectiondescription": "Disable this option to speed up the loading of course sections.",
2022
"enablesyncwifi": "Allow sync only when on Wi-Fi",
2123
"errordeletesitefiles": "Error deleting site files.",
2224
"errorsyncsite": "Error synchronizing the site data, please check your internet connection and try again.",

www/core/components/settings/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
angular.module('mm.core.settings', [])
1616

17+
.constant('mmCoreSettingsDownloadSection', 'mmCoreSettingsDownloadSection')
1718
.constant('mmCoreSettingsReportInBackground', 'mmCoreReportInBackground')
1819
.constant('mmCoreSettingsSyncOnlyOnWifi', 'mmCoreSyncOnlyOnWifi')
1920

www/core/components/settings/templates/general.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
</div>
88
<select ng-model="selectedLanguage" ng-options="code as name for (code, name) in langs" ng-change="languageChanged(selectedLanguage)" aria-labelledby="mm-settings-languagelabel">
99
</select>
10+
</li>
11+
<li class="item item-toggle item-text-wrap">
12+
<p id="mm-settings-downloadsectionlabel" class="item-heading">{{ 'mm.settings.enabledownloadsection' | translate }}</p>
13+
<p>{{ 'mm.settings.enabledownloadsectiondescription' | translate }}</p>
14+
<label class="toggle" aria-labelledby="mm-settings-downloadsectionlabel">
15+
<input id="mm-settings-downloadsectioncheckbox" type="checkbox" ng-model="downloadSection" ng-change="downloadSectionChanged(downloadSection)">
16+
<div class="track">
17+
<div class="handle"></div>
18+
</div>
19+
</label>
1020
</li>
1121
<li class="item item-toggle item-text-wrap" ng-if="showReport">
1222
<p id="mm-settings-reportlabel" class="item-heading">{{ 'mm.settings.reportinbackground' | translate }}</p>

0 commit comments

Comments
 (0)