MOBILE-3833 settings: Update list manager

main
Noel De Martin 2022-01-12 14:45:47 +01:00
parent a02bb045db
commit b15a9efaba
4 changed files with 162 additions and 113 deletions

View File

@ -0,0 +1,45 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { Params } from '@angular/router';
import { CoreRoutedItemsManagerSource } from '@classes/items-management/routed-items-manager-source';
import { CoreSettingsDelegate, CoreSettingsHandlerToDisplay } from '../services/settings-delegate';
/**
* Provides a collection of site settings.
*/
export class CoreSettingsHandlersSource extends CoreRoutedItemsManagerSource<CoreSettingsHandlerToDisplay> {
/**
* @inheritdoc
*/
protected async loadPageItems(): Promise<{ items: CoreSettingsHandlerToDisplay[] }> {
return { items: CoreSettingsDelegate.getHandlers() };
}
/**
* @inheritdoc
*/
getItemPath(handler: CoreSettingsHandlerToDisplay): string {
return handler.page;
}
/**
* @inheritdoc
*/
getItemQueryParams(handler: CoreSettingsHandlerToDisplay): Params {
return handler.params || {};
}
}

View File

@ -0,0 +1,90 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { CoreConstants } from '@/core/constants';
import { Params } from '@angular/router';
import { CoreRoutedItemsManagerSource } from '@classes/items-management/routed-items-manager-source';
import { SHAREDFILES_PAGE_NAME } from '@features/sharedfiles/sharedfiles.module';
import { CoreApp } from '@services/app';
/**
* Provides a collection of setting sections.
*/
export class CoreSettingsSectionsSource extends CoreRoutedItemsManagerSource<CoreSettingsSection> {
/**
* @inheritdoc
*/
protected async loadPageItems(): Promise<{ items: CoreSettingsSection[] }> {
const sections: CoreSettingsSection[] = [
{
name: 'core.settings.general',
path: 'general',
icon: 'fas-wrench',
},
{
name: 'core.settings.spaceusage',
path: 'spaceusage',
icon: 'fas-tasks',
},
{
name: 'core.settings.synchronization',
path: 'sync',
icon: CoreConstants.ICON_SYNC,
},
];
if (CoreApp.isIOS()) {
sections.push({
name: 'core.sharedfiles.sharedfiles',
path: SHAREDFILES_PAGE_NAME + '/list/root',
icon: 'fas-folder',
params: { manage: true },
});
}
sections.push({
name: 'core.settings.about',
path: 'about',
icon: 'fas-id-card',
});
return { items: sections };
}
/**
* @inheritdoc
*/
getItemPath(section: CoreSettingsSection): string {
return section.path;
}
/**
* @inheritdoc
*/
getItemQueryParams(section: CoreSettingsSection): Params {
return section.params || {};
}
}
/**
* Settings section.
*/
export type CoreSettingsSection = {
name: string;
path: string;
icon: string;
params?: Params;
};

View File

@ -13,13 +13,11 @@
// limitations under the License.
import { AfterViewInit, Component, OnDestroy, ViewChild } from '@angular/core';
import { Params } from '@angular/router';
import { CorePageItemsListManager } from '@classes/page-items-list-manager';
import { CoreSplitViewComponent } from '@components/split-view/split-view';
import { CoreConstants } from '@/core/constants';
import { SHAREDFILES_PAGE_NAME } from '@features/sharedfiles/sharedfiles.module';
import { CoreApp } from '@services/app';
import { CoreListItemsManager } from '@classes/items-management/list-items-manager';
import { CoreSettingsSection, CoreSettingsSectionsSource } from '@features/settings/classes/settings-sections-source';
import { CoreRoutedItemsManagerSourcesTracker } from '@classes/items-management/routed-items-manager-sources-tracker';
@Component({
selector: 'page-core-settings-index',
@ -27,16 +25,22 @@ import { CoreApp } from '@services/app';
})
export class CoreSettingsIndexPage implements AfterViewInit, OnDestroy {
sections: CoreSettingsSectionsManager = new CoreSettingsSectionsManager(CoreSettingsIndexPage);
sections: CoreListItemsManager<CoreSettingsSection>;
@ViewChild(CoreSplitViewComponent) splitView!: CoreSplitViewComponent;
constructor() {
const source = CoreRoutedItemsManagerSourcesTracker.getOrCreateSource(CoreSettingsSectionsSource, []);
this.sections = new CoreListItemsManager(source, CoreSettingsIndexPage);
}
/**
* @inheritdoc
*/
ngAfterViewInit(): void {
this.sections.setItems(this.getSections());
this.sections.start(this.splitView);
async ngAfterViewInit(): Promise<void> {
await this.sections.load();
await this.sections.start(this.splitView);
}
/**
@ -46,77 +50,4 @@ export class CoreSettingsIndexPage implements AfterViewInit, OnDestroy {
this.sections.destroy();
}
/**
* Get the sections.
*
* @returns Sections.
*/
protected getSections(): CoreSettingsSection[] {
const sections: CoreSettingsSection[] = [
{
name: 'core.settings.general',
path: 'general',
icon: 'fas-wrench',
},
{
name: 'core.settings.spaceusage',
path: 'spaceusage',
icon: 'fas-tasks',
},
{
name: 'core.settings.synchronization',
path: 'sync',
icon: CoreConstants.ICON_SYNC,
},
];
if (CoreApp.isIOS()) {
sections.push({
name: 'core.sharedfiles.sharedfiles',
path: SHAREDFILES_PAGE_NAME + '/list/root',
icon: 'fas-folder',
params: { manage: true },
});
}
sections.push({
name: 'core.settings.about',
path: 'about',
icon: 'fas-id-card',
});
return sections;
}
}
/**
* Helper class to manage sections.
*/
class CoreSettingsSectionsManager extends CorePageItemsListManager<CoreSettingsSection> {
/**
* @inheritdoc
*/
protected getItemPath(section: CoreSettingsSection): string {
return section.path;
}
/**
* @inheritdoc
*/
protected getItemQueryParams(section: CoreSettingsSection): Params {
return section.params || {};
}
}
/**
* Settings section.
*/
export type CoreSettingsSection = {
name: string;
path: string;
icon: string;
params?: Params;
};

View File

@ -13,10 +13,9 @@
// limitations under the License.
import { AfterViewInit, Component, OnDestroy, ViewChild } from '@angular/core';
import { Params } from '@angular/router';
import { IonRefresher } from '@ionic/angular';
import { CoreSettingsDelegate, CoreSettingsHandlerToDisplay } from '../../services/settings-delegate';
import { CoreSettingsHandlerToDisplay } from '../../services/settings-delegate';
import { CoreEventObserver, CoreEvents } from '@singletons/events';
import { CoreSites } from '@services/sites';
import { CoreDomUtils } from '@services/utils/dom';
@ -24,8 +23,10 @@ import { CoreSettingsHelper, CoreSiteSpaceUsage } from '../../services/settings-
import { CoreApp } from '@services/app';
import { Translate } from '@singletons';
import { CoreNavigator } from '@services/navigator';
import { CorePageItemsListManager } from '@classes/page-items-list-manager';
import { CoreSplitViewComponent } from '@components/split-view/split-view';
import { CoreListItemsManager } from '@classes/items-management/list-items-manager';
import { CoreRoutedItemsManagerSourcesTracker } from '@classes/items-management/routed-items-manager-sources-tracker';
import { CoreSettingsHandlersSource } from '@features/settings/classes/settings-handlers-source';
/**
* Page that displays the list of site settings pages.
@ -38,7 +39,7 @@ export class CoreSitePreferencesPage implements AfterViewInit, OnDestroy {
@ViewChild(CoreSplitViewComponent) splitView!: CoreSplitViewComponent;
handlers: CoreSettingsSitePreferencesManager;
handlers: CoreListItemsManager<CoreSettingsHandlerToDisplay>;
isIOS: boolean;
siteId: string;
@ -51,10 +52,12 @@ export class CoreSitePreferencesPage implements AfterViewInit, OnDestroy {
protected isDestroyed = false;
constructor() {
this.isIOS = CoreApp.isIOS();
this.siteId = CoreSites.getCurrentSiteId();
this.handlers = new CoreSettingsSitePreferencesManager(CoreSitePreferencesPage);
const source = CoreRoutedItemsManagerSourcesTracker.getOrCreateSource(CoreSettingsHandlersSource, []);
this.handlers = new CoreListItemsManager(source, CoreSitePreferencesPage);
this.sitesObserver = CoreEvents.on(CoreEvents.SITE_UPDATED, () => {
this.refreshData();
@ -70,14 +73,14 @@ export class CoreSitePreferencesPage implements AfterViewInit, OnDestroy {
try {
await this.fetchData();
} finally {
const handler = pageToOpen ? this.handlers.items.find(handler => handler.page == pageToOpen) : undefined;
if (handler) {
this.handlers.select(handler);
this.handlers.watchSplitViewOutlet(this.splitView);
await this.handlers.select(handler);
} else {
this.handlers.start(this.splitView);
await this.handlers.start(this.splitView);
}
}
}
@ -86,7 +89,7 @@ export class CoreSitePreferencesPage implements AfterViewInit, OnDestroy {
* Fetch Data.
*/
protected async fetchData(): Promise<void> {
this.handlers.setItems(CoreSettingsDelegate.getHandlers());
await this.handlers.load();
this.spaceUsage = await CoreSettingsHelper.getSiteSpaceUsage(this.siteId);
}
@ -122,6 +125,7 @@ export class CoreSitePreferencesPage implements AfterViewInit, OnDestroy {
* @param refresher Refresher.
*/
refreshData(refresher?: IonRefresher): void {
this.handlers.getSource().setDirty(true);
this.fetchData().finally(() => {
refresher?.complete();
});
@ -171,24 +175,3 @@ export class CoreSitePreferencesPage implements AfterViewInit, OnDestroy {
}
}
/**
* Helper class to manage sections.
*/
class CoreSettingsSitePreferencesManager extends CorePageItemsListManager<CoreSettingsHandlerToDisplay> {
/**
* @inheritdoc
*/
protected getItemPath(handler: CoreSettingsHandlerToDisplay): string {
return handler.page;
}
/**
* @inheritdoc
*/
protected getItemQueryParams(handler: CoreSettingsHandlerToDisplay): Params {
return handler.params || {};
}
}