From 6b5c9416db0cc76899bbc11633dc64f53f3611ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Wed, 17 Oct 2018 11:26:33 +0200 Subject: [PATCH] MOBILE-2681 dashboard: Save user preferences on timeline and overview --- .../block/myoverview/component/myoverview.ts | 12 +- .../timeline/components/timeline/timeline.ts | 17 ++- src/classes/site.ts | 104 ++++++++++++++---- 3 files changed, 105 insertions(+), 28 deletions(-) diff --git a/src/addon/block/myoverview/component/myoverview.ts b/src/addon/block/myoverview/component/myoverview.ts index 21176abce..c6876b019 100644 --- a/src/addon/block/myoverview/component/myoverview.ts +++ b/src/addon/block/myoverview/component/myoverview.ts @@ -17,6 +17,7 @@ import { Searchbar } from 'ionic-angular'; import * as moment from 'moment'; import { CoreEventsProvider } from '@providers/events'; import { CoreUtilsProvider } from '@providers/utils/utils'; +import { CoreSitesProvider } from '@providers/sites'; import { CoreCoursesProvider } from '@core/courses/providers/courses'; import { CoreCoursesHelperProvider } from '@core/courses/providers/helper'; import { CoreCourseHelperProvider } from '@core/course/providers/helper'; @@ -42,6 +43,7 @@ export class AddonBlockMyOverviewComponent extends AddonBlockComponent implement future: [] }; selectedFilter = 'inprogress'; + currentSite: any; downloadAllCoursesEnabled: boolean; filteredCourses: any[]; prefetchCoursesData = { @@ -62,7 +64,8 @@ export class AddonBlockMyOverviewComponent extends AddonBlockComponent implement constructor(injector: Injector, private coursesProvider: CoreCoursesProvider, private courseCompletionProvider: AddonCourseCompletionProvider, private eventsProvider: CoreEventsProvider, private courseHelper: CoreCourseHelperProvider, private utils: CoreUtilsProvider, - private courseOptionsDelegate: CoreCourseOptionsDelegate, private coursesHelper: CoreCoursesHelperProvider) { + private courseOptionsDelegate: CoreCourseOptionsDelegate, private coursesHelper: CoreCoursesHelperProvider, + private sitesProvider: CoreSitesProvider) { super(injector, 'AddonBlockMyOverviewComponent'); } @@ -85,7 +88,11 @@ export class AddonBlockMyOverviewComponent extends AddonBlockComponent implement } }); - super.ngOnInit(); + this.currentSite = this.sitesProvider.getCurrentSite(); + this.currentSite.getSiteConfig('AddonBlockMyOverviewFilter', this.selectedFilter).then((value) => { + this.selectedFilter = value; + super.ngOnInit(); + }); } /** @@ -246,6 +253,7 @@ export class AddonBlockMyOverviewComponent extends AddonBlockComponent implement * The selected courses have changed. */ selectedChanged(): void { + this.currentSite.setSiteConfig('AddonBlockMyOverviewFilter', this.selectedFilter); this.filteredCourses = this.courses[this.selectedFilter]; } diff --git a/src/addon/block/timeline/components/timeline/timeline.ts b/src/addon/block/timeline/components/timeline/timeline.ts index 180aec2ef..a09eba441 100644 --- a/src/addon/block/timeline/components/timeline/timeline.ts +++ b/src/addon/block/timeline/components/timeline/timeline.ts @@ -15,6 +15,7 @@ import { Component, OnInit, Injector } from '@angular/core'; import * as moment from 'moment'; import { CoreUtilsProvider } from '@providers/utils/utils'; +import { CoreSitesProvider } from '@providers/sites'; import { CoreCoursesProvider } from '@core/courses/providers/courses'; import { CoreCoursesHelperProvider } from '@core/courses/providers/helper'; import { CoreCourseOptionsDelegate } from '@core/course/providers/options-delegate'; @@ -31,6 +32,7 @@ import { AddonBlockTimelineProvider } from '../../providers/timeline'; export class AddonBlockTimelineComponent extends AddonBlockComponent implements OnInit { sort = 'sortbydates'; filter = 'next30days'; + currentSite: any; timeline = { events: [], loaded: false, @@ -49,7 +51,7 @@ export class AddonBlockTimelineComponent extends AddonBlockComponent implements constructor(injector: Injector, private coursesProvider: CoreCoursesProvider, private utils: CoreUtilsProvider, private timelineProvider: AddonBlockTimelineProvider, private courseOptionsDelegate: CoreCourseOptionsDelegate, - private coursesHelper: CoreCoursesHelperProvider) { + private coursesHelper: CoreCoursesHelperProvider, private sitesProvider: CoreSitesProvider) { super(injector, 'AddonBlockTimelineComponent'); } @@ -58,8 +60,15 @@ export class AddonBlockTimelineComponent extends AddonBlockComponent implements * Component being initialized. */ ngOnInit(): void { - this.switchFilter(); - super.ngOnInit(); + this.currentSite = this.sitesProvider.getCurrentSite(); + this.currentSite.getSiteConfig('AddonBlockTimelineFilter', this.filter).then((value) => { + this.filter = value; + this.switchFilter(); + }); + this.currentSite.getSiteConfig('AddonBlockTimelineSort', this.sort).then((value) => { + this.sort = value; + super.ngOnInit(); + }); } /** @@ -167,6 +176,7 @@ export class AddonBlockTimelineComponent extends AddonBlockComponent implements * Change timeline filter being viewed. */ switchFilter(): void { + this.currentSite.setSiteConfig('AddonBlockTimelineFilter', this.filter); switch (this.filter) { case 'overdue': this.dataFrom = -14; @@ -200,6 +210,7 @@ export class AddonBlockTimelineComponent extends AddonBlockComponent implements * Change timeline sort being viewed. */ switchSort(): void { + this.currentSite.setSiteConfig('AddonBlockTimelineSort', this.sort); if (!this.timeline.loaded && this.sort == 'sortbydates') { this.fetchContent(); } else if (!this.timelineCourses.loaded && this.sort == 'sortbycourses') { diff --git a/src/classes/site.ts b/src/classes/site.ts index 503a6b279..24c2adc2c 100644 --- a/src/classes/site.ts +++ b/src/classes/site.ts @@ -167,28 +167,46 @@ export class CoreSite { // Variables for the database. protected WS_CACHE_TABLE = 'wscache'; - protected tableSchema = { - name: this.WS_CACHE_TABLE, - columns: [ - { - name: 'id', - type: 'TEXT', - primaryKey: true - }, - { - name: 'data', - type: 'TEXT' - }, - { - name: 'key', - type: 'TEXT' - }, - { - name: 'expirationTime', - type: 'INTEGER' - } - ] - }; + protected CONFIG_TABLE = 'core_site_config'; + protected tableSchemas = [ + { + name: this.WS_CACHE_TABLE, + columns: [ + { + name: 'id', + type: 'TEXT', + primaryKey: true + }, + { + name: 'data', + type: 'TEXT' + }, + { + name: 'key', + type: 'TEXT' + }, + { + name: 'expirationTime', + type: 'INTEGER' + } + ] + }, + { + name: this.CONFIG_TABLE, + columns: [ + { + name: 'name', + type: 'TEXT', + unique: true, + notNull: true + }, + { + name: 'value' + } + ] + } + + ]; // Versions of Moodle releases. protected MOODLE_RELEASES = { @@ -247,7 +265,7 @@ export class CoreSite { */ initDB(): void { this.db = this.dbProvider.getDB('Site-' + this.id); - this.db.createTableFromSchema(this.tableSchema); + this.db.createTablesFromSchema(this.tableSchemas); } /** @@ -1543,4 +1561,44 @@ export class CoreSite { return this.MOODLE_RELEASES[releases[position + 1]]; } + + /** + * Deletes a site setting. + * + * @param {string} name The config name. + * @return {Promise} Promise resolved when done. + */ + deleteSiteConfig(name: string): Promise { + return this.db.deleteRecords(this.CONFIG_TABLE, { name: name }); + } + + /** + * Get a site setting. + * + * @param {string} name The config name. + * @param {any} [defaultValue] Default value to use if the entry is not found. + * @return {Promise} Resolves upon success along with the config data. Reject on failure. + */ + getSiteConfig(name: string, defaultValue?: any): Promise { + return this.db.getRecord(this.CONFIG_TABLE, { name: name }).then((entry) => { + return entry.value; + }).catch((error) => { + if (typeof defaultValue != 'undefined') { + return defaultValue; + } + + return Promise.reject(error); + }); + } + + /** + * Set a site setting. + * + * @param {string} name The config name. + * @param {number|string} value The config value. Can only store number or strings. + * @return {Promise} Promise resolved when done. + */ + setSiteConfig(name: string, value: number | string): Promise { + return this.db.insertRecord(this.CONFIG_TABLE, { name: name, value: value }); + } }