Skip to content

Commit

Permalink
update: public APIs doc & performance
Browse files Browse the repository at this point in the history
  • Loading branch information
csof3cen committed Jun 29, 2023
1 parent 69bee58 commit dc4a4c2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
5 changes: 5 additions & 0 deletions lib/src/connectivity_builder.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import 'package:flutter/material.dart';
import 'package:internet_connectivity_checker/internet_connectivity_checker.dart';

/// Connectivity builder widget
class ConnectivityBuilder extends StatefulWidget {
/// A builder that enables you to easily handle connectivity state
const ConnectivityBuilder({
this.interval,
required this.builder,
Key? key,
}) : super(key: key);

/// The duration interval to recheck connectivity state
final Duration? interval;

/// Connectivity builder function
final Widget Function(ConnectivityStatus status) builder;

@override
Expand Down
25 changes: 14 additions & 11 deletions lib/src/connectivity_checker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ bool _isDurationALongerThanB(Duration a, Duration b) {
}
}

/// Connectivity checker class
class ConnectivityChecker {
/// A list of hosts to lookup every [interval]
final List<String> _urlsToCheck = const [
'https://google.com',
'https://github.com'
];
final List<String> _urlsToCheck = const ['https://google.com', 'https://github.com'];

/// The time to wait between each internet connectivity verification
/// The default [interval] is set to 5 seconds
final Duration? interval;

final StreamController<bool> _streamController = StreamController<bool>();

Stream<bool> get stream =>
_streamController.stream.asBroadcastStream().timeout(
/// Connectivity stream
Stream<bool> get stream => _streamController.stream.asBroadcastStream().timeout(
interval != null
? Duration(
seconds: _isDurationALongerThanB(
interval!, const Duration(seconds: 2))
interval!,
const Duration(seconds: 2),
)
? (interval!.inSeconds + 2)
: 2)
: const Duration(seconds: 5),
Expand All @@ -36,6 +36,7 @@ class ConnectivityChecker {
},
);

/// Initialize an connectivity checker object
ConnectivityChecker({
this.interval,
}) {
Expand All @@ -44,7 +45,8 @@ class ConnectivityChecker {
.add(const Duration(seconds: 5))
.isAfter(DateTime.now().add(interval!))) {
throw Exception(
'interval cannot be smaller than 5 seconds because of lookups time. ${interval!.inMilliseconds}ms given.');
'''interval cannot be smaller than 5 seconds because of lookups time.
${interval!.inMilliseconds}ms given.''');
}
}

Expand All @@ -57,7 +59,7 @@ class ConnectivityChecker {
int successfulLookupsNum = 0;
int failedLookupsNum = 0;

for (var url in _urlsToCheck) {
for (final url in _urlsToCheck) {
try {
final request = await HttpClient().headUrl(Uri.parse(url));
final response = await request.close();
Expand All @@ -75,8 +77,9 @@ class ConnectivityChecker {
}
}

_streamController.sink
.add(successfulLookupsNum >= failedLookupsNum ? true : false);
_streamController.sink.add(
successfulLookupsNum >= failedLookupsNum ? true : false,
);
// print(successfulLookupsNum >= failedLookupsNum ? 'Success' : 'Failed');
}

Expand Down
12 changes: 11 additions & 1 deletion lib/src/connectivity_status.dart
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
enum ConnectivityStatus { online, offline, checking }
/// Connectivity status
enum ConnectivityStatus {
/// Device is online
online,

/// Device is offline
offline,

/// Package is checking... internet state
checking,
}

0 comments on commit dc4a4c2

Please sign in to comment.