diff --git a/src/core/features/settings/classes/settings-handlers-source.ts b/src/core/features/settings/classes/settings-handlers-source.ts new file mode 100644 index 000000000..30db670ce --- /dev/null +++ b/src/core/features/settings/classes/settings-handlers-source.ts @@ -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 { + + /** + * @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 || {}; + } + +} diff --git a/src/core/features/settings/classes/settings-sections-source.ts b/src/core/features/settings/classes/settings-sections-source.ts new file mode 100644 index 000000000..d3bcd02c9 --- /dev/null +++ b/src/core/features/settings/classes/settings-sections-source.ts @@ -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 { + + /** + * @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; +}; diff --git a/src/core/features/settings/pages/index/index.ts b/src/core/features/settings/pages/index/index.ts index 6e1653977..9e044dfe3 100644 --- a/src/core/features/settings/pages/index/index.ts +++ b/src/core/features/settings/pages/index/index.ts @@ -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; @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 { + 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 { - - /** - * @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; -}; diff --git a/src/core/features/settings/pages/site/site.ts b/src/core/features/settings/pages/site/site.ts index 77a366b73..4fc275b2e 100644 --- a/src/core/features/settings/pages/site/site.ts +++ b/src/core/features/settings/pages/site/site.ts @@ -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; 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 { - 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 { - - /** - * @inheritdoc - */ - protected getItemPath(handler: CoreSettingsHandlerToDisplay): string { - return handler.page; - } - - /** - * @inheritdoc - */ - protected getItemQueryParams(handler: CoreSettingsHandlerToDisplay): Params { - return handler.params || {}; - } - -}