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' ;
1616import { CorePlatform } from '@services/platform' ;
1717import { Network } from '@awesome-cordova-plugins/network/ngx' ;
1818import { makeSingleton } from '@singletons' ;
1919import { Observable , Subject , merge } from 'rxjs' ;
2020import { 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}
0 commit comments