From adf5de823fc991695d2296a2a3634489a5feaba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Thu, 12 Nov 2020 09:43:34 +0100 Subject: [PATCH] MOBILE-3565 settings: Add site preferences --- src/app/core/mainmenu/pages/more/more.html | 3 +- src/app/core/settings/pages/site/site.html | 80 +++++++ .../settings/pages/site/site.page.module.ts | 48 ++++ src/app/core/settings/pages/site/site.page.ts | 217 ++++++++++++++++++ src/app/core/settings/settings-init.module.ts | 4 + .../core/settings/settings-routing.module.ts | 6 +- 6 files changed, 354 insertions(+), 4 deletions(-) create mode 100644 src/app/core/settings/pages/site/site.html create mode 100644 src/app/core/settings/pages/site/site.page.module.ts create mode 100644 src/app/core/settings/pages/site/site.page.ts diff --git a/src/app/core/mainmenu/pages/more/more.html b/src/app/core/mainmenu/pages/more/more.html index 96b7bbbd4..1fe0bd121 100644 --- a/src/app/core/mainmenu/pages/more/more.html +++ b/src/app/core/mainmenu/pages/more/more.html @@ -70,7 +70,8 @@

{{ 'core.mainmenu.help' | translate }}

- +

{{ 'core.settings.preferences' | translate }}

diff --git a/src/app/core/settings/pages/site/site.html b/src/app/core/settings/pages/site/site.html new file mode 100644 index 000000000..b10ce9c28 --- /dev/null +++ b/src/app/core/settings/pages/site/site.html @@ -0,0 +1,80 @@ + + + + + + {{ 'core.settings.preferences' | translate}} + + + + + + + + + + + + +

{{siteInfo!.fullname}}

+

+ +

+

{{ siteUrl }}

+
+
+ + + + +

{{ 'core.sharedfiles.sharedfiles' | translate }}

+
+ {{ iosSharedFiles }} +
+ + + + + +

{{ handler.title | translate}}

+
+
+ + + + +

{{ 'core.settings.spaceusage' | translate }} +

+

{{ spaceUsage.spaceUsage | coreBytesToSize }}

+
+ + + +
+ + +

{{ 'core.settings.synchronizenow' | translate }} + +

+
+ + + + +
+
+
+
+
diff --git a/src/app/core/settings/pages/site/site.page.module.ts b/src/app/core/settings/pages/site/site.page.module.ts new file mode 100644 index 000000000..d4b8d8366 --- /dev/null +++ b/src/app/core/settings/pages/site/site.page.module.ts @@ -0,0 +1,48 @@ +// (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 { NgModule } from '@angular/core'; +import { TranslateModule } from '@ngx-translate/core'; +import { RouterModule, Routes } from '@angular/router'; +import { CommonModule } from '@angular/common'; +import { IonicModule } from '@ionic/angular'; + +import { CoreComponentsModule } from '@components/components.module'; +import { CoreDirectivesModule } from '@directives/directives.module'; +import { CorePipesModule } from '@pipes/pipes.module'; + +import { CoreSitePreferencesPage } from './site.page'; + +const routes: Routes = [ + { + path: '', + component: CoreSitePreferencesPage, + }, +]; + +@NgModule({ + declarations: [ + CoreSitePreferencesPage, + ], + imports: [ + RouterModule.forChild(routes), + CommonModule, + IonicModule, + TranslateModule.forChild(), + CoreComponentsModule, + CoreDirectivesModule, + CorePipesModule, + ], +}) +export class CoreSitePreferencesPageModule {} diff --git a/src/app/core/settings/pages/site/site.page.ts b/src/app/core/settings/pages/site/site.page.ts new file mode 100644 index 000000000..0ac690b20 --- /dev/null +++ b/src/app/core/settings/pages/site/site.page.ts @@ -0,0 +1,217 @@ +// (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 { Component, OnDestroy, OnInit } from '@angular/core'; +import { ActivatedRoute, Params, Router } from '@angular/router'; +import { IonRefresher } from '@ionic/angular'; + +import { CoreSettingsDelegate, CoreSettingsHandlerData } from '../../services/settings.delegate'; +import { CoreEventObserver, CoreEvents, CoreEventSiteUpdatedData } from '@singletons/events'; +import { CoreSites } from '@services/sites'; +import { CoreDomUtils } from '@services/utils/dom'; +// import { CoreSplitViewComponent } from '@components/split-view/split-view'; +// import { CoreSharedFiles } from '@core/sharedfiles/providers/sharedfiles'; +import { CoreSettingsHelper, CoreSiteSpaceUsage } from '../../services/settings.helper'; +import { CoreApp } from '@services/app'; +import { CoreSiteInfo } from '@classes/site'; +import { Translate } from '@singletons/core.singletons'; + +/** + * Page that displays the list of site settings pages. + */ +@Component({ + selector: 'page-core-site-preferences', + templateUrl: 'site.html', +}) +export class CoreSitePreferencesPage implements OnInit, OnDestroy { + + // @ViewChild(CoreSplitViewComponent) splitviewCtrl?: CoreSplitViewComponent; + + isIOS: boolean; + selectedPage?: string; + + handlers: CoreSettingsHandlerData[] = []; + siteId: string; + siteInfo?: CoreSiteInfo; + siteName?: string; + siteUrl?: string; + spaceUsage: CoreSiteSpaceUsage = { + cacheEntries: 0, + spaceUsage: 0, + }; + + loaded = false; + iosSharedFiles = 0; + protected sitesObserver: CoreEventObserver; + protected isDestroyed = false; + + constructor( + protected settingsDelegate: CoreSettingsDelegate, + protected route: ActivatedRoute, + protected router: Router, // Will be removed when splitview is implemented + ) { + + this.isIOS = CoreApp.instance.isIOS(); + this.siteId = CoreSites.instance.getCurrentSiteId(); + + this.selectedPage = route.snapshot.paramMap.get('page') || undefined; + + this.sitesObserver = CoreEvents.on(CoreEvents.SITE_UPDATED, (data: CoreEventSiteUpdatedData) => { + if (data.siteId == this.siteId) { + this.refreshData(); + } + }); + } + + /** + * View loaded. + */ + ngOnInit(): void { + this.fetchData().finally(() => { + this.loaded = true; + + if (this.selectedPage) { + this.openHandler(this.selectedPage); + } /* else if (this.splitviewCtrl.isOn()) { + if (this.isIOS) { + this.openHandler('CoreSharedFilesListPage', { manage: true, siteId: this.siteId, hideSitePicker: true }); + } else if (this.handlers.length > 0) { + this.openHandler(this.handlers[0].page, this.handlers[0].params); + } + }*/ + }); + } + + /** + * Fetch Data. + */ + protected async fetchData(): Promise { + this.handlers = this.settingsDelegate.getHandlers(); + + const currentSite = CoreSites.instance.getCurrentSite(); + this.siteInfo = currentSite!.getInfo(); + this.siteName = currentSite!.getSiteName(); + this.siteUrl = currentSite!.getURL(); + + const promises: Promise[] = []; + + promises.push(CoreSettingsHelper.instance.getSiteSpaceUsage(this.siteId) + .then((spaceUsage) => { + this.spaceUsage = spaceUsage; + + return; + })); + + /* if (this.isIOS) { + promises.push(CoreSharedFiles.instance.getSiteSharedFiles(this.siteId) + .then((files) => { + this.iosSharedFiles = files.length; + + return; + })); + }*/ + + await Promise.all(promises); + } + + /** + * Syncrhonizes the site. + */ + async synchronize(): Promise { + try { + // Using syncOnlyOnWifi false to force manual sync. + await CoreSettingsHelper.instance.synchronizeSite(false, this.siteId); + } catch (error) { + if (this.isDestroyed) { + return; + } + CoreDomUtils.instance.showErrorModalDefault(error, 'core.settings.errorsyncsite', true); + } + + } + + /** + * Returns true if site is beeing synchronized. + * + * @return True if site is beeing synchronized, false otherwise. + */ + isSynchronizing(): boolean { + return !!CoreSettingsHelper.instance.getSiteSyncPromise(this.siteId); + } + + /** + * Refresh the data. + * + * @param refresher Refresher. + */ + refreshData(refresher?: CustomEvent): void { + this.fetchData().finally(() => { + refresher?.detail.complete(); + }); + } + + /** + * Deletes files of a site and the tables that can be cleared. + * + * @param siteData Site object with space usage. + */ + async deleteSiteStorage(): Promise { + try { + this.spaceUsage = await CoreSettingsHelper.instance.deleteSiteStorage(this.siteName || '', this.siteId); + } catch { + // Ignore cancelled confirmation modal. + } + } + + /** + * Open a handler. + * + * @param page Page to open. + * @param params Params of the page to open. + */ + openHandler(page: string, params?: Params): void { + this.selectedPage = page; + // this.splitviewCtrl.push(page, params); + this.router.navigate([page], { relativeTo: this.route, queryParams: params }); + } + + /** + * Show information about space usage actions. + */ + showSpaceInfo(): void { + CoreDomUtils.instance.showAlert( + Translate.instance.instant('core.help'), + Translate.instance.instant('core.settings.spaceusagehelp'), + ); + } + + /** + * Show information about sync actions. + */ + showSyncInfo(): void { + CoreDomUtils.instance.showAlert( + Translate.instance.instant('core.help'), + Translate.instance.instant('core.settings.synchronizenowhelp'), + ); + } + + /** + * Page destroyed. + */ + ngOnDestroy(): void { + this.isDestroyed = true; + this.sitesObserver?.off(); + } + +} diff --git a/src/app/core/settings/settings-init.module.ts b/src/app/core/settings/settings-init.module.ts index 2f0534f1d..70c559c98 100644 --- a/src/app/core/settings/settings-init.module.ts +++ b/src/app/core/settings/settings-init.module.ts @@ -23,6 +23,10 @@ const routes: Routes = [ path: 'settings', loadChildren: () => import('@core/settings/settings.module').then(m => m.CoreSettingsModule), }, + { + path: 'preferences', + loadChildren: () => import('@core/settings/pages/site/site.page.module').then(m => m.CoreSitePreferencesPageModule), + }, ]; @NgModule({ diff --git a/src/app/core/settings/settings-routing.module.ts b/src/app/core/settings/settings-routing.module.ts index 5c0508893..19d726001 100644 --- a/src/app/core/settings/settings-routing.module.ts +++ b/src/app/core/settings/settings-routing.module.ts @@ -18,11 +18,11 @@ import { RouterModule, Routes } from '@angular/router'; const routes: Routes = [ { path: 'about', - loadChildren: () => import('./pages/about/about.page.module').then( m => m.CoreSettingsAboutPageModule), + loadChildren: () => import('./pages/about/about.page.module').then(m => m.CoreSettingsAboutPageModule), }, { path: 'general', - loadChildren: () => import('./pages/general/general.page.module').then( m => m.CoreSettingsGeneralPageModule), + loadChildren: () => import('./pages/general/general.page.module').then(m => m.CoreSettingsGeneralPageModule), }, { path: 'spaceusage', @@ -38,7 +38,7 @@ const routes: Routes = [ }, { path: '', - loadChildren: () => import('./pages/app/app.page.module').then( m => m.CoreSettingsAppPageModule), + loadChildren: () => import('./pages/app/app.page.module').then(m => m.CoreSettingsAppPageModule), }, ];