Skip to content

Commit

Permalink
v2.0 #40
Browse files Browse the repository at this point in the history
  • Loading branch information
jogboms authored Mar 5, 2021
2 parents e2e80ad + 1c48074 commit cb465f5
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 123 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [2.0.0]

- Migrate to null-safety

## [1.0.0]

- Improve network and wifi detection
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A tidy utility to handle offline/online connectivity like a Boss. It provides su

```yaml
dependencies:
flutter_offline: "^1.0.0"
flutter_offline: "^2.0.0"
```
### ⚡️ Import
Expand Down
2 changes: 1 addition & 1 deletion example/.flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity-2.0.2/","dependencies":[]},{"name":"wifi_info_flutter","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/wifi_info_flutter-1.0.4/","dependencies":[]}],"android":[{"name":"connectivity","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity-2.0.2/","dependencies":[]},{"name":"wifi_info_flutter","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/wifi_info_flutter-1.0.4/","dependencies":[]}],"macos":[{"name":"connectivity_macos","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity_macos-0.1.0+7/","dependencies":[]}],"linux":[],"windows":[],"web":[{"name":"connectivity_for_web","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity_for_web-0.3.1+4/","dependencies":[]}]},"dependencyGraph":[{"name":"connectivity","dependencies":["connectivity_macos","connectivity_for_web"]},{"name":"connectivity_for_web","dependencies":[]},{"name":"connectivity_macos","dependencies":[]},{"name":"wifi_info_flutter","dependencies":[]}],"date_created":"2021-03-05 06:20:56.934344","version":"2.1.0-10.0.pre"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity-3.0.2/","dependencies":[]},{"name":"wifi_info_flutter","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/wifi_info_flutter-2.0.0/","dependencies":[]}],"android":[{"name":"connectivity","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity-3.0.2/","dependencies":[]},{"name":"wifi_info_flutter","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/wifi_info_flutter-2.0.0/","dependencies":[]}],"macos":[{"name":"connectivity_macos","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity_macos-0.2.0/","dependencies":[]}],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"connectivity","dependencies":["connectivity_macos"]},{"name":"connectivity_macos","dependencies":[]},{"name":"wifi_info_flutter","dependencies":[]}],"date_created":"2021-03-05 06:49:22.633197","version":"2.1.0-10.0.pre"}
4 changes: 2 additions & 2 deletions example/lib/demo_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import 'package:flutter/material.dart';

class DemoPage extends StatelessWidget {
const DemoPage({
Key key,
@required this.child,
Key? key,
required this.child,
}) : super(key: key);

final Widget child;
Expand Down
5 changes: 3 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ version: 1.0.0+1
publish_to: none

environment:
sdk: ">=2.0.0 <3.0.0"
sdk: '>=2.12.0 <3.0.0'
flutter: ">=2.0.0"

dependencies:
flutter:
Expand All @@ -16,7 +17,7 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
pedantic: ^1.9.0
pedantic: ^1.11.0

flutter:
uses-material-design: true
40 changes: 19 additions & 21 deletions lib/src/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import 'package:flutter_offline/src/utils.dart';
import 'package:wifi_info_flutter/wifi_info_flutter.dart';

const kOfflineDebounceDuration = Duration(seconds: 3);
typedef ValueWidgetBuilder<T> = Widget Function(BuildContext context, T value, Widget child);

class OfflineBuilder extends StatefulWidget {
factory OfflineBuilder({
Key key,
@required ValueWidgetBuilder<ConnectivityResult> connectivityBuilder,
Key? key,
required ValueWidgetBuilder<ConnectivityResult> connectivityBuilder,
Duration debounceDuration = kOfflineDebounceDuration,
WidgetBuilder builder,
Widget child,
WidgetBuilder errorBuilder,
WidgetBuilder? builder,
Widget? child,
WidgetBuilder? errorBuilder,
}) {
return OfflineBuilder.initialize(
key: key,
Expand All @@ -23,25 +24,22 @@ class OfflineBuilder extends StatefulWidget {
wifiInfo: WifiInfo(),
debounceDuration: debounceDuration,
builder: builder,
child: child,
errorBuilder: errorBuilder,
child: child,
);
}

@visibleForTesting
OfflineBuilder.initialize({
Key key,
@required this.connectivityBuilder,
@required this.connectivityService,
@required this.wifiInfo,
Key? key,
required this.connectivityBuilder,
required this.connectivityService,
required this.wifiInfo,
this.debounceDuration = kOfflineDebounceDuration,
this.builder,
this.child,
this.errorBuilder,
}) : assert(connectivityBuilder != null, 'connectivityBuilder cannot be null'),
assert(debounceDuration != null, 'debounceDuration cannot be null'),
assert(connectivityService != null, 'connectivityService cannot be null'),
assert(!(builder is WidgetBuilder && child is Widget) && !(builder == null && child == null),
}) : assert(!(builder is WidgetBuilder && child is Widget) && !(builder == null && child == null),
'You should specify either a builder or a child'),
super(key: key);

Expand All @@ -57,20 +55,20 @@ class OfflineBuilder extends StatefulWidget {
final ValueWidgetBuilder<ConnectivityResult> connectivityBuilder;

/// Used for building the child widget
final WidgetBuilder builder;
final WidgetBuilder? builder;

/// The widget below this widget in the tree.
final Widget child;
final Widget? child;

/// Used for building the error widget incase of any platform errors
final WidgetBuilder errorBuilder;
final WidgetBuilder? errorBuilder;

@override
OfflineBuilderState createState() => OfflineBuilderState();
}

class OfflineBuilderState extends State<OfflineBuilder> {
Stream<ConnectivityResult> _connectivityStream;
late Stream<ConnectivityResult> _connectivityStream;

@override
void initState() {
Expand All @@ -92,12 +90,12 @@ class OfflineBuilderState extends State<OfflineBuilder> {

if (snapshot.hasError) {
if (widget.errorBuilder != null) {
return widget.errorBuilder(context);
return widget.errorBuilder!(context);
}
throw OfflineBuilderError(snapshot.error);
throw OfflineBuilderError(snapshot.error!);
}

return widget.connectivityBuilder(context, snapshot.data, widget.child ?? widget.builder(context));
return widget.connectivityBuilder(context, snapshot.data!, widget.child ?? widget.builder!(context));
},
);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ StreamTransformer<ConnectivityResult, ConnectivityResult> debounce(
Duration debounceDuration,
) {
var _seenFirstData = false;
Timer _debounceTimer;
Timer? _debounceTimer;

return StreamTransformer<ConnectivityResult, ConnectivityResult>.fromHandlers(
handleData: (ConnectivityResult data, EventSink<ConnectivityResult> sink) {
Expand All @@ -33,13 +33,13 @@ StreamTransformer<ConnectivityResult, ConnectivityResult> startsWith(
Stream<ConnectivityResult> input,
bool cancelOnError,
) {
StreamController<ConnectivityResult> controller;
StreamSubscription<ConnectivityResult> subscription;
StreamController<ConnectivityResult>? controller;
late StreamSubscription<ConnectivityResult> subscription;

controller = StreamController<ConnectivityResult>(
sync: true,
onListen: () => controller?.add(data),
onPause: ([Future<dynamic> resumeSignal]) => subscription.pause(resumeSignal),
onPause: ([Future<dynamic>? resumeSignal]) => subscription.pause(resumeSignal),
onResume: () => subscription.resume(),
onCancel: () => subscription.cancel(),
);
Expand Down
37 changes: 9 additions & 28 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,21 @@ packages:
name: connectivity
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
connectivity_for_web:
dependency: transitive
description:
name: connectivity_for_web
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.1+4"
version: "3.0.2"
connectivity_macos:
dependency: transitive
description:
name: connectivity_macos
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.0+7"
version: "0.2.0"
connectivity_platform_interface:
dependency: transitive
description:
name: connectivity_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.6"
version: "2.0.1"
fake_async:
dependency: transitive
description:
Expand All @@ -88,18 +81,6 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
js:
dependency: transitive
description:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.3"
matcher:
dependency: transitive
description:
Expand Down Expand Up @@ -127,14 +108,14 @@ packages:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.2"
version: "1.11.0"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
version: "2.0.0"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -202,14 +183,14 @@ packages:
name: wifi_info_flutter
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
version: "2.0.0"
wifi_info_flutter_platform_interface:
dependency: transitive
description:
name: wifi_info_flutter_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
version: "2.0.1"
sdks:
dart: ">=2.12.0-0.0 <3.0.0"
flutter: ">=1.20.0"
dart: ">=2.12.0 <3.0.0"
flutter: ">=2.0.0"
11 changes: 6 additions & 5 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
name: flutter_offline
description: A tidy utility to handle offline/online connectivity like a Boss.
version: 1.0.0
version: 2.0.0
homepage: https://github.com/jogboms/flutter_offline

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: '>=2.12.0 <3.0.0'
flutter: ">=2.0.0"

dependencies:
flutter:
sdk: flutter
connectivity: ^2.0.0
wifi_info_flutter: ^1.0.0
connectivity: ^3.0.2
wifi_info_flutter: ^2.0.0

dev_dependencies:
flutter_test:
sdk: flutter
pedantic: ^1.9.0
pedantic: ^1.11.0
48 changes: 7 additions & 41 deletions test/flutter_offline_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,40 +46,6 @@ void main() {
});

group('Test Assertions', () {
testWidgets('Test no debounceDuration param', (WidgetTester tester) async {
expect(() {
OfflineBuilder.initialize(
connectivityService: TestConnectivityService(ConnectivityResult.none),
wifiInfo: TestWifiInfoService(),
connectivityBuilder: (_, __, Widget child) => child,
debounceDuration: null,
builder: (BuildContext context) => Text('builder_result'),
);
}, throwsAssertionError);
});

testWidgets('Test no connectivityBuilder param', (WidgetTester tester) async {
expect(() {
OfflineBuilder.initialize(
connectivityService: TestConnectivityService(ConnectivityResult.none),
wifiInfo: TestWifiInfoService(),
connectivityBuilder: null,
child: const Text('child_result'),
);
}, throwsAssertionError);
});

testWidgets('Test no connectivityService param', (WidgetTester tester) async {
expect(() {
OfflineBuilder.initialize(
connectivityService: null,
wifiInfo: null,
connectivityBuilder: (_, __, Widget child) => child,
child: const Text('child_result'),
);
}, throwsAssertionError);
});

testWidgets('Test builder & child param', (WidgetTester tester) async {
expect(() {
OfflineBuilder.initialize(
Expand Down Expand Up @@ -257,9 +223,9 @@ void main() {
connectivityService: service,
wifiInfo: TestWifiInfoService(),
connectivityBuilder: (_, ConnectivityResult connectivity, __) => Text('$connectivity'),
child: const SizedBox(),
debounceDuration: Duration.zero,
errorBuilder: (context) => Text('Error'),
child: const SizedBox(),
),
));

Expand All @@ -274,16 +240,16 @@ void main() {
}

class TestConnectivityService implements Connectivity {
TestConnectivityService([this.initialConnection]) {
_result = initialConnection;
TestConnectivityService([this.initialConnection]) : _result = initialConnection ?? ConnectivityResult.none {
controller = StreamController<ConnectivityResult>.broadcast(
onListen: () => controller.add(_result),
);
}

StreamController<ConnectivityResult> controller;
ConnectivityResult _result = ConnectivityResult.none;
final ConnectivityResult initialConnection;
late final StreamController<ConnectivityResult> controller;
final ConnectivityResult? initialConnection;

ConnectivityResult _result;

set result(ConnectivityResult result) {
_result = result;
Expand All @@ -297,7 +263,7 @@ class TestConnectivityService implements Connectivity {

@override
Future<ConnectivityResult> checkConnectivity() {
return Future.delayed(Duration.zero, () => initialConnection);
return Future.delayed(Duration.zero, () => initialConnection!);
}
}

Expand Down
Loading

0 comments on commit cb465f5

Please sign in to comment.