MOBILE-2681 dashboard: Save user preferences on timeline and overview

main
Pau Ferrer Ocaña 2018-10-17 11:26:33 +02:00
parent f233dbf5b4
commit 6b5c9416db
3 changed files with 105 additions and 28 deletions

View File

@ -17,6 +17,7 @@ import { Searchbar } from 'ionic-angular';
import * as moment from 'moment'; import * as moment from 'moment';
import { CoreEventsProvider } from '@providers/events'; import { CoreEventsProvider } from '@providers/events';
import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreUtilsProvider } from '@providers/utils/utils';
import { CoreSitesProvider } from '@providers/sites';
import { CoreCoursesProvider } from '@core/courses/providers/courses'; import { CoreCoursesProvider } from '@core/courses/providers/courses';
import { CoreCoursesHelperProvider } from '@core/courses/providers/helper'; import { CoreCoursesHelperProvider } from '@core/courses/providers/helper';
import { CoreCourseHelperProvider } from '@core/course/providers/helper'; import { CoreCourseHelperProvider } from '@core/course/providers/helper';
@ -42,6 +43,7 @@ export class AddonBlockMyOverviewComponent extends AddonBlockComponent implement
future: [] future: []
}; };
selectedFilter = 'inprogress'; selectedFilter = 'inprogress';
currentSite: any;
downloadAllCoursesEnabled: boolean; downloadAllCoursesEnabled: boolean;
filteredCourses: any[]; filteredCourses: any[];
prefetchCoursesData = { prefetchCoursesData = {
@ -62,7 +64,8 @@ export class AddonBlockMyOverviewComponent extends AddonBlockComponent implement
constructor(injector: Injector, private coursesProvider: CoreCoursesProvider, constructor(injector: Injector, private coursesProvider: CoreCoursesProvider,
private courseCompletionProvider: AddonCourseCompletionProvider, private eventsProvider: CoreEventsProvider, private courseCompletionProvider: AddonCourseCompletionProvider, private eventsProvider: CoreEventsProvider,
private courseHelper: CoreCourseHelperProvider, private utils: CoreUtilsProvider, private courseHelper: CoreCourseHelperProvider, private utils: CoreUtilsProvider,
private courseOptionsDelegate: CoreCourseOptionsDelegate, private coursesHelper: CoreCoursesHelperProvider) { private courseOptionsDelegate: CoreCourseOptionsDelegate, private coursesHelper: CoreCoursesHelperProvider,
private sitesProvider: CoreSitesProvider) {
super(injector, 'AddonBlockMyOverviewComponent'); super(injector, 'AddonBlockMyOverviewComponent');
} }
@ -85,7 +88,11 @@ export class AddonBlockMyOverviewComponent extends AddonBlockComponent implement
} }
}); });
this.currentSite = this.sitesProvider.getCurrentSite();
this.currentSite.getSiteConfig('AddonBlockMyOverviewFilter', this.selectedFilter).then((value) => {
this.selectedFilter = value;
super.ngOnInit(); super.ngOnInit();
});
} }
/** /**
@ -246,6 +253,7 @@ export class AddonBlockMyOverviewComponent extends AddonBlockComponent implement
* The selected courses have changed. * The selected courses have changed.
*/ */
selectedChanged(): void { selectedChanged(): void {
this.currentSite.setSiteConfig('AddonBlockMyOverviewFilter', this.selectedFilter);
this.filteredCourses = this.courses[this.selectedFilter]; this.filteredCourses = this.courses[this.selectedFilter];
} }

View File

@ -15,6 +15,7 @@
import { Component, OnInit, Injector } from '@angular/core'; import { Component, OnInit, Injector } from '@angular/core';
import * as moment from 'moment'; import * as moment from 'moment';
import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreUtilsProvider } from '@providers/utils/utils';
import { CoreSitesProvider } from '@providers/sites';
import { CoreCoursesProvider } from '@core/courses/providers/courses'; import { CoreCoursesProvider } from '@core/courses/providers/courses';
import { CoreCoursesHelperProvider } from '@core/courses/providers/helper'; import { CoreCoursesHelperProvider } from '@core/courses/providers/helper';
import { CoreCourseOptionsDelegate } from '@core/course/providers/options-delegate'; import { CoreCourseOptionsDelegate } from '@core/course/providers/options-delegate';
@ -31,6 +32,7 @@ import { AddonBlockTimelineProvider } from '../../providers/timeline';
export class AddonBlockTimelineComponent extends AddonBlockComponent implements OnInit { export class AddonBlockTimelineComponent extends AddonBlockComponent implements OnInit {
sort = 'sortbydates'; sort = 'sortbydates';
filter = 'next30days'; filter = 'next30days';
currentSite: any;
timeline = { timeline = {
events: [], events: [],
loaded: false, loaded: false,
@ -49,7 +51,7 @@ export class AddonBlockTimelineComponent extends AddonBlockComponent implements
constructor(injector: Injector, private coursesProvider: CoreCoursesProvider, private utils: CoreUtilsProvider, constructor(injector: Injector, private coursesProvider: CoreCoursesProvider, private utils: CoreUtilsProvider,
private timelineProvider: AddonBlockTimelineProvider, private courseOptionsDelegate: CoreCourseOptionsDelegate, private timelineProvider: AddonBlockTimelineProvider, private courseOptionsDelegate: CoreCourseOptionsDelegate,
private coursesHelper: CoreCoursesHelperProvider) { private coursesHelper: CoreCoursesHelperProvider, private sitesProvider: CoreSitesProvider) {
super(injector, 'AddonBlockTimelineComponent'); super(injector, 'AddonBlockTimelineComponent');
} }
@ -58,8 +60,15 @@ export class AddonBlockTimelineComponent extends AddonBlockComponent implements
* Component being initialized. * Component being initialized.
*/ */
ngOnInit(): void { ngOnInit(): void {
this.currentSite = this.sitesProvider.getCurrentSite();
this.currentSite.getSiteConfig('AddonBlockTimelineFilter', this.filter).then((value) => {
this.filter = value;
this.switchFilter(); this.switchFilter();
});
this.currentSite.getSiteConfig('AddonBlockTimelineSort', this.sort).then((value) => {
this.sort = value;
super.ngOnInit(); super.ngOnInit();
});
} }
/** /**
@ -167,6 +176,7 @@ export class AddonBlockTimelineComponent extends AddonBlockComponent implements
* Change timeline filter being viewed. * Change timeline filter being viewed.
*/ */
switchFilter(): void { switchFilter(): void {
this.currentSite.setSiteConfig('AddonBlockTimelineFilter', this.filter);
switch (this.filter) { switch (this.filter) {
case 'overdue': case 'overdue':
this.dataFrom = -14; this.dataFrom = -14;
@ -200,6 +210,7 @@ export class AddonBlockTimelineComponent extends AddonBlockComponent implements
* Change timeline sort being viewed. * Change timeline sort being viewed.
*/ */
switchSort(): void { switchSort(): void {
this.currentSite.setSiteConfig('AddonBlockTimelineSort', this.sort);
if (!this.timeline.loaded && this.sort == 'sortbydates') { if (!this.timeline.loaded && this.sort == 'sortbydates') {
this.fetchContent(); this.fetchContent();
} else if (!this.timelineCourses.loaded && this.sort == 'sortbycourses') { } else if (!this.timelineCourses.loaded && this.sort == 'sortbycourses') {

View File

@ -167,7 +167,9 @@ export class CoreSite {
// Variables for the database. // Variables for the database.
protected WS_CACHE_TABLE = 'wscache'; protected WS_CACHE_TABLE = 'wscache';
protected tableSchema = { protected CONFIG_TABLE = 'core_site_config';
protected tableSchemas = [
{
name: this.WS_CACHE_TABLE, name: this.WS_CACHE_TABLE,
columns: [ columns: [
{ {
@ -188,7 +190,23 @@ export class CoreSite {
type: 'INTEGER' type: 'INTEGER'
} }
] ]
}; },
{
name: this.CONFIG_TABLE,
columns: [
{
name: 'name',
type: 'TEXT',
unique: true,
notNull: true
},
{
name: 'value'
}
]
}
];
// Versions of Moodle releases. // Versions of Moodle releases.
protected MOODLE_RELEASES = { protected MOODLE_RELEASES = {
@ -247,7 +265,7 @@ export class CoreSite {
*/ */
initDB(): void { initDB(): void {
this.db = this.dbProvider.getDB('Site-' + this.id); 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]]; return this.MOODLE_RELEASES[releases[position + 1]];
} }
/**
* Deletes a site setting.
*
* @param {string} name The config name.
* @return {Promise<any>} Promise resolved when done.
*/
deleteSiteConfig(name: string): Promise<any> {
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<any>} Resolves upon success along with the config data. Reject on failure.
*/
getSiteConfig(name: string, defaultValue?: any): Promise<any> {
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<any>} Promise resolved when done.
*/
setSiteConfig(name: string, value: number | string): Promise<any> {
return this.db.insertRecord(this.CONFIG_TABLE, { name: name, value: value });
}
} }