From 17440ca3d48f81e4dd253582ee057de59dc62712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Tue, 12 Jul 2022 10:40:01 +0200 Subject: [PATCH] MOBILE-4047 settings: Change sync on WiFi behavior --- scripts/create_langindex.sh | 2 + scripts/langindex.json | 5 +- src/core/features/settings/lang.json | 5 +- .../features/settings/pages/site/site.html | 11 +- src/core/features/settings/pages/site/site.ts | 30 +++- .../synchronization/synchronization.html | 131 ++++++++++-------- .../pages/synchronization/synchronization.ts | 44 +++++- src/core/services/network.ts | 60 +++++--- 8 files changed, 194 insertions(+), 94 deletions(-) diff --git a/scripts/create_langindex.sh b/scripts/create_langindex.sh index 3171c270a..a34fe6f6e 100755 --- a/scripts/create_langindex.sh +++ b/scripts/create_langindex.sh @@ -68,6 +68,8 @@ function do_match { print_message "$2" tput setaf 6 grep "$match" $LANGPACKSFOLDER/en/*.php + else + coincidence=0 fi } diff --git a/scripts/langindex.json b/scripts/langindex.json index ebdb17c76..5a015724d 100644 --- a/scripts/langindex.json +++ b/scripts/langindex.json @@ -2181,6 +2181,8 @@ "core.settings.colorscheme-system": "local_moodlemobileapp", "core.settings.colorscheme-system-notice": "local_moodlemobileapp", "core.settings.compilationinfo": "local_moodlemobileapp", + "core.settings.connecttosync": "local_moodlemobileapp", + "core.settings.connectwifitosync": "local_moodlemobileapp", "core.settings.copyinfo": "local_moodlemobileapp", "core.settings.cordovadevicemodel": "local_moodlemobileapp", "core.settings.cordovadeviceosversion": "local_moodlemobileapp", @@ -2202,7 +2204,6 @@ "core.settings.enablefirebaseanalyticsdescription": "local_moodlemobileapp", "core.settings.enablerichtexteditor": "local_moodlemobileapp", "core.settings.enablerichtexteditordescription": "local_moodlemobileapp", - "core.settings.enablesyncwifi": "local_moodlemobileapp", "core.settings.entriesincache": "local_moodlemobileapp", "core.settings.estimatedfreespace": "local_moodlemobileapp", "core.settings.filesystemroot": "local_moodlemobileapp", @@ -2220,6 +2221,7 @@ "core.settings.locationhref": "local_moodlemobileapp", "core.settings.loggedin": "message", "core.settings.loggedoff": "message", + "core.settings.logintosync": "local_moodlemobileapp", "core.settings.navigatorlanguage": "local_moodlemobileapp", "core.settings.navigatoruseragent": "local_moodlemobileapp", "core.settings.networkstatus": "local_moodlemobileapp", @@ -2236,6 +2238,7 @@ "core.settings.sites": "moodle", "core.settings.sitesyncfailed": "local_moodlemobileapp", "core.settings.spaceusage": "local_moodlemobileapp", + "core.settings.syncdatasaver": "local_moodlemobileapp", "core.settings.synchronization": "local_moodlemobileapp", "core.settings.synchronizenow": "local_moodlemobileapp", "core.settings.synchronizenowhelp": "local_moodlemobileapp", diff --git a/src/core/features/settings/lang.json b/src/core/features/settings/lang.json index f9388e862..1a6550718 100644 --- a/src/core/features/settings/lang.json +++ b/src/core/features/settings/lang.json @@ -14,6 +14,8 @@ "colorscheme-system": "System default", "colorscheme": "Color Scheme", "compilationinfo": "Compilation info", + "connectwifitosync": "Connect to a Wi-Fi network or turn off Data saver to synchronise sites.", + "connecttosync": "Your device is offline. Connect to the internet to synchronise sites.", "copyinfo": "Copy device info on the clipboard", "cordovadevicemodel": "Cordova device model", "cordovadeviceosversion": "Cordova device OS version", @@ -35,7 +37,6 @@ "enablefirebaseanalyticsdescription": "If enabled, the app will collect anonymous data usage.", "enablerichtexteditor": "Enable text editor", "enablerichtexteditordescription": "If enabled, a text editor will be available when entering content.", - "enablesyncwifi": "Allow sync only when on Wi-Fi", "entriesincache": "{{$a}} entries in cache", "estimatedfreespace": "Estimated free space", "filesystemroot": "File system root", @@ -53,6 +54,7 @@ "locationhref": "Web view URL", "loggedin": "Online", "loggedoff": "Offline", + "logintosync": "Log in to synchronise", "navigatorlanguage": "Navigator language", "navigatoruseragent": "Navigator userAgent", "networkstatus": "Internet connection status", @@ -69,6 +71,7 @@ "sites": "Sites", "sitesyncfailed": "Site synchronisation failed", "spaceusage": "Space usage", + "syncdatasaver": "Data saver: Synchronise only when on Wi-Fi", "synchronization": "Synchronisation", "synchronizenow": "Synchronise now", "synchronizenowhelp": "Synchronising a site will send pending changes and all offline activity stored in the device and will synchronise some data like messages and notifications.", diff --git a/src/core/features/settings/pages/site/site.html b/src/core/features/settings/pages/site/site.html index 0470d5026..215d5f82e 100644 --- a/src/core/features/settings/pages/site/site.html +++ b/src/core/features/settings/pages/site/site.html @@ -37,11 +37,20 @@

- + + + + + + {{ 'core.settings.connectwifitosync' | translate }} + {{ 'core.settings.connecttosync' | translate }} + + diff --git a/src/core/features/settings/pages/site/site.ts b/src/core/features/settings/pages/site/site.ts index a88532235..4a017185b 100644 --- a/src/core/features/settings/pages/site/site.ts +++ b/src/core/features/settings/pages/site/site.ts @@ -25,6 +25,11 @@ import { CoreRoutedItemsManagerSourcesTracker } from '@classes/items-management/ import { CoreSettingsHandlersSource } from '@features/settings/classes/settings-handlers-source'; import { CoreSettingsHelper } from '@features/settings/services/settings-helper'; import { CoreDomUtils } from '@services/utils/dom'; +import { CoreNetwork } from '@services/network'; +import { Subscription } from 'rxjs'; +import { NgZone } from '@singletons'; +import { CoreConstants } from '@/core/constants'; +import { CoreConfig } from '@services/config'; /** * Page that displays the list of site settings pages. @@ -39,8 +44,13 @@ export class CoreSitePreferencesPage implements AfterViewInit, OnDestroy { handlers: CoreListItemsManager; + dataSaver = false; + limitedConnection = false; + isOnline = true; + protected siteId: string; protected sitesObserver: CoreEventObserver; + protected networkObserver: Subscription; protected isDestroyed = false; constructor() { @@ -53,12 +63,25 @@ export class CoreSitePreferencesPage implements AfterViewInit, OnDestroy { this.sitesObserver = CoreEvents.on(CoreEvents.SITE_UPDATED, () => { this.refreshData(); }, this.siteId); + + this.isOnline = CoreNetwork.isOnline(); + this.limitedConnection = this.isOnline && CoreNetwork.isNetworkAccessLimited(); + + this.networkObserver = CoreNetwork.onChange().subscribe(() => { + // Execute the callback in the Angular zone, so change detection doesn't stop working. + NgZone.run(() => { + this.isOnline = CoreNetwork.isOnline(); + this.limitedConnection = this.isOnline && CoreNetwork.isNetworkAccessLimited(); + }); + }); } /** - * View loaded. + * @inheritdoc */ async ngAfterViewInit(): Promise { + this.dataSaver = await CoreConfig.get(CoreConstants.SETTINGS_SYNC_ONLY_ON_WIFI, true); + const pageToOpen = CoreNavigator.getRouteParam('page'); try { @@ -121,11 +144,12 @@ export class CoreSitePreferencesPage implements AfterViewInit, OnDestroy { } /** - * Page destroyed. + * @inheritdoc */ ngOnDestroy(): void { this.isDestroyed = true; - this.sitesObserver?.off(); + this.sitesObserver.off(); + this.networkObserver.unsubscribe(); this.handlers.destroy(); } diff --git a/src/core/features/settings/pages/synchronization/synchronization.html b/src/core/features/settings/pages/synchronization/synchronization.html index 03ee791e9..00c94086f 100644 --- a/src/core/features/settings/pages/synchronization/synchronization.html +++ b/src/core/features/settings/pages/synchronization/synchronization.html @@ -17,88 +17,97 @@ - +

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

- {{ 'core.settings.enablesyncwifi' | translate }} - + +

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

+
+
- - -

{{ 'core.accounts' | translate }}

-
-
- - + + + -

- -

-

{{ - accountsList.currentSite.siteUrlWithoutProtocol }} -

+ + {{ 'core.settings.connectwifitosync' | translate }} + {{ 'core.settings.connecttosync' | translate }}
-
- - - - {{ 'core.pictureof' | translate:{$a: accountsList.currentSite.fullName} }} - - -

{{accountsList.currentSite.fullName}}

-
- - - - -
- -
- - + + -

- -

-

{{ sites[0].siteUrlWithoutProtocol }}

+

{{ 'core.accounts' | translate }}

- -
+ + + +

+ +

+

{{ + accountsList.currentSite.siteUrlWithoutProtocol }} +

+
+
+ + + + + + + +
+ + + + +

+ +

+

{{ sites[0].siteUrlWithoutProtocol }}

+
+
+ + + + +
+
- - - - - {{ 'core.pictureof' | translate:{$a: site.fullName} }} - - -

{{site.fullName}}

-
- - - - - -
+ + + + {{ 'core.pictureof' | translate:{$a: site.fullName} }} + + +

{{site.fullName}}

+

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

+
+ + + + + + + +
diff --git a/src/core/features/settings/pages/synchronization/synchronization.ts b/src/core/features/settings/pages/synchronization/synchronization.ts index cf64335b1..47e3b6f3f 100644 --- a/src/core/features/settings/pages/synchronization/synchronization.ts +++ b/src/core/features/settings/pages/synchronization/synchronization.ts @@ -20,8 +20,11 @@ import { CoreSites } from '@services/sites'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreConfig } from '@services/config'; import { CoreSettingsHelper } from '@features/settings/services/settings-helper'; -import { Translate } from '@singletons'; +import { NgZone, Translate } from '@singletons'; import { CoreAccountsList, CoreLoginHelper } from '@features/login/services/login-helper'; +import { CoreNetwork } from '@services/network'; +import { Subscription } from 'rxjs'; +import { CoreNavigator } from '@services/navigator'; /** * Page that displays the synchronization settings. @@ -40,9 +43,13 @@ export class CoreSettingsSynchronizationPage implements OnInit, OnDestroy { }; sitesLoaded = false; - syncOnlyOnWifi = false; + dataSaver = false; + limitedConnection = false; + isOnline = true; + protected isDestroyed = false; protected sitesObserver: CoreEventObserver; + protected networkObserver: Subscription; constructor() { @@ -80,6 +87,18 @@ export class CoreSettingsSynchronizationPage implements OnInit, OnDestroy { siteEntry.fullName = siteInfo.fullname; } }); + + this.isOnline = CoreNetwork.isOnline(); + this.limitedConnection = this.isOnline && CoreNetwork.isNetworkAccessLimited(); + + this.networkObserver = CoreNetwork.onChange().subscribe(() => { + // Execute the callback in the Angular zone, so change detection doesn't stop working. + NgZone.run(() => { + this.isOnline = CoreNetwork.isOnline(); + this.limitedConnection = this.isOnline && CoreNetwork.isNetworkAccessLimited(); + }); + }); + } /** @@ -96,18 +115,18 @@ export class CoreSettingsSynchronizationPage implements OnInit, OnDestroy { this.sitesLoaded = true; - this.syncOnlyOnWifi = await CoreConfig.get(CoreConstants.SETTINGS_SYNC_ONLY_ON_WIFI, true); + this.dataSaver = await CoreConfig.get(CoreConstants.SETTINGS_SYNC_ONLY_ON_WIFI, true); } /** * Called when sync only on wifi setting is enabled or disabled. */ syncOnlyOnWifiChanged(): void { - CoreConfig.set(CoreConstants.SETTINGS_SYNC_ONLY_ON_WIFI, this.syncOnlyOnWifi ? 1 : 0); + CoreConfig.set(CoreConstants.SETTINGS_SYNC_ONLY_ON_WIFI, this.dataSaver ? 1 : 0); } /** - * Syncrhonizes a site. + * Synchronizes a site. * * @param siteId Site ID. */ @@ -124,6 +143,16 @@ export class CoreSettingsSynchronizationPage implements OnInit, OnDestroy { } } + /** + * Changes site. + * + * @param siteId Site ID. + */ + async login(siteId: string): Promise { + // This navigation will logout and navigate to the site home. + await CoreNavigator.navigateToSiteHome({ preferCurrentTab: false , siteId }); + } + /** * Returns true if site is beeing synchronized. * @@ -145,11 +174,12 @@ export class CoreSettingsSynchronizationPage implements OnInit, OnDestroy { } /** - * Page destroyed. + * @inheritdoc */ ngOnDestroy(): void { this.isDestroyed = true; - this.sitesObserver?.off(); + this.sitesObserver.off(); + this.networkObserver.unsubscribe(); } } diff --git a/src/core/services/network.ts b/src/core/services/network.ts index d8a2f9a64..44cb095e3 100644 --- a/src/core/services/network.ts +++ b/src/core/services/network.ts @@ -18,6 +18,17 @@ import { Network } from '@ionic-native/network/ngx'; import { makeSingleton } from '@singletons'; import { Observable, Subject, merge } from 'rxjs'; +enum CoreNetworkConnection { + UNKNOWN = 'unknown', + ETHERNET = 'ethernet', + WIFI = 'wifi', + CELL_2G = '2g', + CELL_3G = '3g', + CELL_4G = '4g', + CELL = 'cellular', + NONE = 'none', +}; + /** * Service to manage network connections. */ @@ -38,20 +49,25 @@ export class CoreNetworkService extends Network { this.checkOnline(); if (CorePlatform.isMobile()) { - this.onChange().subscribe(() => { + // We cannot directly listen to onChange because it depends on + // onConnect and onDisconnect that have been already overriden. + super.onConnect().subscribe(() => { + this.fireObservable(); + }); + super.onDisconnect().subscribe(() => { this.fireObservable(); }); } else { // eslint-disable-next-line @typescript-eslint/no-explicit-any ( window).Connection = { - UNKNOWN: 'unknown', // eslint-disable-line @typescript-eslint/naming-convention - ETHERNET: 'ethernet', // eslint-disable-line @typescript-eslint/naming-convention - WIFI: 'wifi', // eslint-disable-line @typescript-eslint/naming-convention - CELL_2G: '2g', // eslint-disable-line @typescript-eslint/naming-convention - CELL_3G: '3g', // eslint-disable-line @typescript-eslint/naming-convention - CELL_4G: '4g', // eslint-disable-line @typescript-eslint/naming-convention - CELL: 'cellular', // eslint-disable-line @typescript-eslint/naming-convention - NONE: 'none', // eslint-disable-line @typescript-eslint/naming-convention + UNKNOWN: CoreNetworkConnection.UNKNOWN, // eslint-disable-line @typescript-eslint/naming-convention + ETHERNET: CoreNetworkConnection.ETHERNET, // eslint-disable-line @typescript-eslint/naming-convention + WIFI: CoreNetworkConnection.WIFI, // eslint-disable-line @typescript-eslint/naming-convention + CELL_2G: CoreNetworkConnection.CELL_2G, // eslint-disable-line @typescript-eslint/naming-convention + CELL_3G: CoreNetworkConnection.CELL_3G, // eslint-disable-line @typescript-eslint/naming-convention + CELL_4G: CoreNetworkConnection.CELL_4G, // eslint-disable-line @typescript-eslint/naming-convention + CELL: CoreNetworkConnection.CELL, // eslint-disable-line @typescript-eslint/naming-convention + NONE: CoreNetworkConnection.NONE, // eslint-disable-line @typescript-eslint/naming-convention }; window.addEventListener('online', () => { @@ -91,18 +107,22 @@ export class CoreNetworkService extends Network { checkOnline(): void { if (this.forceOffline) { this.online = false; + this.type = CoreNetworkConnection.NONE; return; } if (!CorePlatform.isMobile()) { this.online = navigator.onLine; + this.type = this.online + ? CoreNetworkConnection.WIFI + : CoreNetworkConnection.NONE; return; } - let online = this.type !== null && this.type != this.Connection.NONE && - this.type != this.Connection.UNKNOWN; + let online = this.type !== null && this.type !== CoreNetworkConnection.NONE && + this.type !== CoreNetworkConnection.UNKNOWN; // Double check we are not online because we cannot rely 100% in Cordova APIs. if (!online && navigator.onLine) { @@ -123,6 +143,7 @@ export class CoreNetworkService extends Network { /** * Returns an observable to notify when the app is connected. + * It will also be fired when connection type changes. * * @return Observable. */ @@ -143,12 +164,11 @@ export class CoreNetworkService extends Network { * Fires the correct observable depending on the connection status. */ protected fireObservable(): void { - const previousOnline = this.online; - this.checkOnline(); - if (this.online && !previousOnline) { + + if (this.online) { this.connectObservable.next('connected'); - } else if (!this.online && previousOnline) { + } else { this.disconnectObservable.next('disconnected'); } } @@ -163,11 +183,11 @@ export class CoreNetworkService extends Network { return false; } - const limited = [ - this.Connection.CELL_2G, - this.Connection.CELL_3G, - this.Connection.CELL_4G, - this.Connection.CELL, + const limited: string[] = [ + CoreNetworkConnection.CELL_2G, + CoreNetworkConnection.CELL_3G, + CoreNetworkConnection.CELL_4G, + CoreNetworkConnection.CELL, ]; return limited.indexOf(this.type) > -1;