Skip to content

Commit 3100a6f

Browse files
committed
MOBILE-4842 network: Simplify network type management
1 parent 68d3d04 commit 3100a6f

File tree

11 files changed

+72
-51
lines changed

11 files changed

+72
-51
lines changed

local_moodleappbehat/tests/behat/behat_app.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,15 +1179,16 @@ public function i_switch_offline_mode(string $offline) {
11791179
public function i_switch_network_connection(string $mode) {
11801180
switch ($mode) {
11811181
case 'wifi':
1182-
$this->runtime_js("network.setForceConnectionMode('$mode');");
1182+
$this->runtime_js("network.setForceConnectionMode('wifi');");
11831183
break;
11841184
case 'cellular':
1185-
$this->runtime_js("network.setForceConnectionMode('$mode');");
1185+
$this->runtime_js("network.setForceConnectionMode('cellular');");
11861186
break;
11871187
case 'offline':
1188-
$this->runtime_js("network.setForceConnectionMode('none');");
1188+
$this->runtime_js("network.setForceConnectionMode('offline');");
11891189
break;
11901190
default:
1191+
$this->runtime_js("network.setForceConnectionMode('unknown');");
11911192
break;
11921193
}
11931194
}

src/core/classes/sites/authenticated-site.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,7 @@ export class CoreAuthenticatedSite extends CoreUnauthenticatedSite {
15921592
let expirationDelay = CoreAuthenticatedSite.UPDATE_FREQUENCIES[updateFrequency] ||
15931593
CoreAuthenticatedSite.UPDATE_FREQUENCIES[CoreCacheUpdateFrequency.USUALLY];
15941594

1595-
if (CoreNetwork.isNetworkAccessLimited()) {
1595+
if (CoreNetwork.isCellular()) {
15961596
// Not WiFi, increase the expiration delay a 50% to decrease the data usage in this case.
15971597
expirationDelay *= 1.5;
15981598
}

src/core/directives/external-content.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,8 @@ export class CoreExternalContentDirective implements AfterViewInit, OnChanges, O
518518
clickableEl.addEventListener(eventName, () => {
519519
// User played media or opened a downloadable link.
520520
// Download the file if in wifi and it hasn't been downloaded already (for big files).
521-
if (state !== DownloadStatus.DOWNLOADED && state !== DownloadStatus.DOWNLOADING && CoreNetwork.isWifi()) {
521+
if (state !== DownloadStatus.DOWNLOADED && state !== DownloadStatus.DOWNLOADING &&
522+
CoreNetwork.isWifi()) {
522523
// We aren't using the result, so it doesn't matter which of the 2 functions we call.
523524
CoreFilepool.getUrlByUrl(site.getId(), url, this.component, this.componentId, 0, false);
524525
}

src/core/features/fileuploader/services/fileuploader-helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export class CoreFileUploaderHelperProvider {
140140

141141
if (size < 0) {
142142
return CoreAlerts.confirm(Translate.instant('core.fileuploader.confirmuploadunknownsize'));
143-
} else if (size >= wifiThreshold || (CoreNetwork.isNetworkAccessLimited() && size >= limitedThreshold)) {
143+
} else if (size >= wifiThreshold || (CoreNetwork.isCellular() && size >= limitedThreshold)) {
144144
const readableSize = CoreText.bytesToSize(size, 2);
145145

146146
return CoreAlerts.confirm(Translate.instant('core.fileuploader.confirmuploadfile', { size: readableSize }));

src/core/features/settings/pages/deviceinfo/deviceinfo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { CoreConfig } from '@services/config';
2525
import { CoreToasts } from '@services/overlays/toasts';
2626
import { CoreNavigator } from '@services/navigator';
2727
import { CorePlatform } from '@services/platform';
28-
import { CoreNetwork, CoreNetworkConnection } from '@services/network';
28+
import { CoreNetwork, CoreNetworkConnectionType } from '@services/network';
2929
import { CoreLoginHelper } from '@features/login/services/login-helper';
3030
import { CoreSitesFactory } from '@services/sites-factory';
3131
import { CoreText } from '@singletons/text';
@@ -96,7 +96,7 @@ export default class CoreSettingsDeviceInfoPage {
9696
compilationTime: CoreConstants.BUILD.compilationTime || 0,
9797
lastCommit: CoreConstants.BUILD.lastCommitHash || '',
9898
isOnline: CoreNetwork.onlineSignal(),
99-
wifiConnection: computed(() => CoreNetwork.connectionTypeSignal()() === CoreNetworkConnection.WIFI),
99+
wifiConnection: computed(() => CoreNetwork.connectionTypeSignal()() === CoreNetworkConnectionType.WIFI),
100100
localNotifAvailable: CoreLocalNotifications.isPluginAvailable() ? 'yes' : 'no',
101101
pushId: CorePushNotifications.getPushId(),
102102
deviceType: '',

src/core/features/settings/pages/site/site.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ export default class CoreSitePreferencesPage implements AfterViewInit, OnDestroy
7373
}, this.siteId);
7474

7575
this.isOnline = CoreNetwork.isOnline();
76-
this.limitedConnection = this.isOnline && CoreNetwork.isNetworkAccessLimited();
76+
this.limitedConnection = CoreNetwork.isCellular();
7777

7878
this.networkObserver = CoreNetwork.onChange().subscribe(() => {
7979
// Execute the callback in the Angular zone, so change detection doesn't stop working.
8080
NgZone.run(() => {
8181
this.isOnline = CoreNetwork.isOnline();
82-
this.limitedConnection = this.isOnline && CoreNetwork.isNetworkAccessLimited();
82+
this.limitedConnection = CoreNetwork.isCellular();
8383
});
8484
});
8585
}

src/core/features/settings/pages/synchronization/synchronization.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ export default class CoreSettingsSynchronizationPage implements OnInit, OnDestro
9393
});
9494

9595
this.isOnline = CoreNetwork.isOnline();
96-
this.limitedConnection = this.isOnline && CoreNetwork.isNetworkAccessLimited();
96+
this.limitedConnection = CoreNetwork.isCellular();
9797

9898
this.networkObserver = CoreNetwork.onChange().subscribe(() => {
9999
// Execute the callback in the Angular zone, so change detection doesn't stop working.
100100
NgZone.run(() => {
101101
this.isOnline = CoreNetwork.isOnline();
102-
this.limitedConnection = this.isOnline && CoreNetwork.isNetworkAccessLimited();
102+
this.limitedConnection = CoreNetwork.isCellular();
103103
});
104104
});
105105

src/core/features/settings/services/settings-helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export class CoreSettingsHelperProvider {
241241
} else if (hasSyncHandlers && !CoreNetwork.isOnline()) {
242242
// We need connection to execute sync.
243243
throw new CoreError(Translate.instant('core.settings.cannotsyncoffline'));
244-
} else if (hasSyncHandlers && syncOnlyOnWifi && CoreNetwork.isNetworkAccessLimited()) {
244+
} else if (hasSyncHandlers && syncOnlyOnWifi && CoreNetwork.isCellular()) {
245245
throw new CoreError(Translate.instant('core.settings.cannotsyncwithoutwifi'));
246246
}
247247

src/core/services/network.ts

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { computed, effect, Injectable, Signal, signal } from '@angular/core';
15+
import { effect, Injectable, Signal, signal } from '@angular/core';
1616
import { CorePlatform } from '@services/platform';
1717
import { Network } from '@awesome-cordova-plugins/network/ngx';
1818
import { makeSingleton } from '@singletons';
1919
import { Observable, Subject, merge } from 'rxjs';
2020
import { CoreHTMLClasses } from '@singletons/html-classes';
2121

22-
export enum CoreNetworkConnection {
22+
enum CoreNetworkConnection {
2323
UNKNOWN = 'unknown',
2424
ETHERNET = 'ethernet',
2525
WIFI = 'wifi',
@@ -30,6 +30,13 @@ export enum CoreNetworkConnection {
3030
NONE = 'none',
3131
}
3232

33+
export enum CoreNetworkConnectionType {
34+
UNKNOWN = 'unknown',
35+
WIFI = 'wifi', // Usually a non-metered connection.
36+
CELL = 'cellular', // Usually a metered connection.
37+
OFFLINE = 'offline',
38+
}
39+
3340
/**
3441
* Service to manage network connections.
3542
*/
@@ -41,11 +48,10 @@ export class CoreNetworkService extends Network {
4148
protected connectObservable = new Subject<'connected'>();
4249
protected connectStableObservable = new Subject<'connected'>();
4350
protected disconnectObservable = new Subject<'disconnected'>();
44-
protected forceConnectionMode?: CoreNetworkConnection;
45-
protected readonly online = signal(false);
51+
protected forceConnectionMode?: CoreNetworkConnectionType;
4652
protected connectStableTimeout?: number;
47-
private readonly _connectionType = signal(CoreNetworkConnection.UNKNOWN);
48-
private readonly _limitedConnection = computed(() => this.online() && CoreNetwork.isNetworkAccessLimited());
53+
protected readonly online = signal(false);
54+
private readonly _connectionType = signal(CoreNetworkConnectionType.UNKNOWN);
4955

5056
constructor() {
5157
super();
@@ -69,7 +75,7 @@ export class CoreNetworkService extends Network {
6975
});
7076
}
7177

72-
get connectionType(): CoreNetworkConnection {
78+
get connectionType(): CoreNetworkConnectionType {
7379
CoreNetwork.updateConnectionType();
7480

7581
return this._connectionType();
@@ -91,6 +97,7 @@ export class CoreNetworkService extends Network {
9197
this.fireObservable();
9298
});
9399
} else {
100+
// Match the Cordova constants to the ones used in the app.
94101
// eslint-disable-next-line @typescript-eslint/no-explicit-any
95102
(<any> window).Connection = {
96103
UNKNOWN: CoreNetworkConnection.UNKNOWN, // eslint-disable-line @typescript-eslint/naming-convention
@@ -130,7 +137,7 @@ export class CoreNetworkService extends Network {
130137
*
131138
* @param value Value to set.
132139
*/
133-
setForceConnectionMode(value: CoreNetworkConnection): void {
140+
setForceConnectionMode(value: CoreNetworkConnectionType): void {
134141
this.forceConnectionMode = value;
135142
this.fireObservable();
136143
}
@@ -151,7 +158,7 @@ export class CoreNetworkService extends Network {
151158
// Recalculate connection type.
152159
CoreNetwork.updateConnectionType();
153160

154-
if (this.forceConnectionMode === CoreNetworkConnection.NONE) {
161+
if (this.forceConnectionMode === CoreNetworkConnectionType.OFFLINE) {
155162
this.online.set(false);
156163

157164
return;
@@ -166,7 +173,7 @@ export class CoreNetworkService extends Network {
166173
}
167174

168175
const type = this._connectionType();
169-
let online = type !== null && type !== CoreNetworkConnection.NONE && type !== CoreNetworkConnection.UNKNOWN;
176+
let online = type !== null && type !== CoreNetworkConnectionType.OFFLINE && type !== CoreNetworkConnectionType.UNKNOWN;
170177

171178
// Double check we are not online because we cannot rely 100% in Cordova APIs.
172179
if (!online && navigator.onLine) {
@@ -187,12 +194,32 @@ export class CoreNetworkService extends Network {
187194
}
188195

189196
if (CorePlatform.isMobile()) {
190-
this._connectionType.set(this.type as CoreNetworkConnection);
191-
192-
return;
197+
switch (this.type) {
198+
case CoreNetworkConnection.WIFI:
199+
case CoreNetworkConnection.ETHERNET:
200+
this._connectionType.set(CoreNetworkConnectionType.WIFI);
201+
202+
return;
203+
case CoreNetworkConnection.CELL:
204+
case CoreNetworkConnection.CELL_2G:
205+
case CoreNetworkConnection.CELL_3G:
206+
case CoreNetworkConnection.CELL_4G:
207+
this._connectionType.set(CoreNetworkConnectionType.CELL);
208+
209+
return;
210+
case CoreNetworkConnection.NONE:
211+
this._connectionType.set(CoreNetworkConnectionType.OFFLINE);
212+
213+
return;
214+
default:
215+
case CoreNetworkConnection.UNKNOWN:
216+
this._connectionType.set(CoreNetworkConnectionType.UNKNOWN);
217+
218+
return;
219+
}
193220
}
194221

195-
this._connectionType.set(this.online() ? CoreNetworkConnection.WIFI : CoreNetworkConnection.NONE);
222+
this._connectionType.set(this.online() ? CoreNetworkConnectionType.WIFI : CoreNetworkConnectionType.OFFLINE);
196223
}
197224

198225
/**
@@ -213,21 +240,12 @@ export class CoreNetworkService extends Network {
213240
return this.online.asReadonly();
214241
}
215242

216-
/**
217-
* Returns a signal to watch limited connection status.
218-
*
219-
* @returns Signal.
220-
*/
221-
limitedConnectionSignal(): Signal<boolean> {
222-
return this._limitedConnection;
223-
}
224-
225243
/**
226244
* Returns a signal to watch connection type.
227245
*
228246
* @returns Signal.
229247
*/
230-
connectionTypeSignal(): Signal<CoreNetworkConnection> {
248+
connectionTypeSignal(): Signal<CoreNetworkConnectionType> {
231249
return this._connectionType.asReadonly();
232250
}
233251

@@ -284,18 +302,10 @@ export class CoreNetworkService extends Network {
284302
* Check if device uses a limited connection.
285303
*
286304
* @returns Whether the device uses a limited connection.
305+
* @deprecated since 5.1. Use isCellular instead.
287306
*/
288307
isNetworkAccessLimited(): boolean {
289-
const limited: CoreNetworkConnection[] = [
290-
CoreNetworkConnection.CELL_2G,
291-
CoreNetworkConnection.CELL_3G,
292-
CoreNetworkConnection.CELL_4G,
293-
CoreNetworkConnection.CELL,
294-
];
295-
296-
const type = this.connectionType;
297-
298-
return limited.indexOf(type) > -1;
308+
return this.isCellular();
299309
}
300310

301311
/**
@@ -304,7 +314,16 @@ export class CoreNetworkService extends Network {
304314
* @returns Whether the device uses a wifi connection.
305315
*/
306316
isWifi(): boolean {
307-
return this.isOnline() && !this.isNetworkAccessLimited();
317+
return this.connectionType === CoreNetworkConnectionType.WIFI;
318+
}
319+
320+
/**
321+
* Check if device uses a limited connection.
322+
*
323+
* @returns Whether the device uses a limited connection.
324+
*/
325+
isCellular(): boolean {
326+
return this.connectionType === CoreNetworkConnectionType.CELL;
308327
}
309328

310329
}

src/core/services/overlays/alerts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export class CoreAlertsService {
186186
const limitedThreshold = options.limitedThreshold ?? CoreConstants.DOWNLOAD_THRESHOLD;
187187

188188
let wifiPrefix = '';
189-
if (CoreNetwork.isNetworkAccessLimited()) {
189+
if (CoreNetwork.isCellular()) {
190190
wifiPrefix = Translate.instant('core.course.confirmlimiteddownload');
191191
}
192192

@@ -203,7 +203,7 @@ export class CoreAlertsService {
203203
{ size: readableSize, availableSpace: availableSpace },
204204
));
205205
} else if (options.alwaysConfirm || size.size >= wifiThreshold ||
206-
(CoreNetwork.isNetworkAccessLimited() && size.size >= limitedThreshold)) {
206+
(CoreNetwork.isCellular() && size.size >= limitedThreshold)) {
207207

208208
return this.confirm(wifiPrefix + Translate.instant(
209209
options.message ?? (size.size === 0 ? 'core.course.confirmdownloadzerosize' : 'core.course.confirmdownload'),

0 commit comments

Comments
 (0)