Skip to content

Commit

Permalink
Merge pull request #4
Browse files Browse the repository at this point in the history
v1.2.0
  • Loading branch information
MAlazhariy authored Aug 24, 2024
2 parents 4790aa9 + d0189e1 commit 1b1f69f
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .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_plus","path":"/Users/malazhariy/.pub-cache/hosted/pub.dev/connectivity_plus-6.0.5/","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"android":[{"name":"connectivity_plus","path":"/Users/malazhariy/.pub-cache/hosted/pub.dev/connectivity_plus-6.0.5/","native_build":true,"dependencies":[]}],"macos":[{"name":"connectivity_plus","path":"/Users/malazhariy/.pub-cache/hosted/pub.dev/connectivity_plus-6.0.5/","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"linux":[{"name":"connectivity_plus","path":"/Users/malazhariy/.pub-cache/hosted/pub.dev/connectivity_plus-6.0.5/","native_build":false,"dependencies":[]}],"windows":[{"name":"connectivity_plus","path":"/Users/malazhariy/.pub-cache/hosted/pub.dev/connectivity_plus-6.0.5/","native_build":true,"dependencies":[]}],"web":[{"name":"connectivity_plus","path":"/Users/malazhariy/.pub-cache/hosted/pub.dev/connectivity_plus-6.0.5/","dependencies":[]}]},"dependencyGraph":[{"name":"connectivity_plus","dependencies":[]}],"date_created":"2024-08-22 14:08:33.093585","version":"3.24.0","swift_package_manager_enabled":false}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity_plus","path":"/Users/malazhariy/.pub-cache/hosted/pub.dev/connectivity_plus-6.0.5/","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"android":[{"name":"connectivity_plus","path":"/Users/malazhariy/.pub-cache/hosted/pub.dev/connectivity_plus-6.0.5/","native_build":true,"dependencies":[]}],"macos":[{"name":"connectivity_plus","path":"/Users/malazhariy/.pub-cache/hosted/pub.dev/connectivity_plus-6.0.5/","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"linux":[{"name":"connectivity_plus","path":"/Users/malazhariy/.pub-cache/hosted/pub.dev/connectivity_plus-6.0.5/","native_build":false,"dependencies":[]}],"windows":[{"name":"connectivity_plus","path":"/Users/malazhariy/.pub-cache/hosted/pub.dev/connectivity_plus-6.0.5/","native_build":true,"dependencies":[]}],"web":[{"name":"connectivity_plus","path":"/Users/malazhariy/.pub-cache/hosted/pub.dev/connectivity_plus-6.0.5/","dependencies":[]}]},"dependencyGraph":[{"name":"connectivity_plus","dependencies":[]}],"date_created":"2024-08-24 14:36:07.069101","version":"3.24.0","swift_package_manager_enabled":false}
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ Add the package to your `pubspec.yaml` under `dependencies:`:
internet_state_manager:
git:
url: https://github.com/MAlazhariy/internet_state_manager.git
ref: v1.1.0
ref: v1.2.0
```
### Android Configuration
#### Android Configuration
To ensure proper functionality on Android, especially in release mode, you need to add `INTERNET` and `ACCESS_NETWORK_STATE` permissions to your `AndroidManifest.xml`:

Expand All @@ -46,6 +46,8 @@ To ensure proper functionality on Android, especially in release mode, you need
...
```

----

### Usage

1. **Initialization**
Expand Down Expand Up @@ -106,7 +108,7 @@ To ensure proper functionality on Android, especially in release mode, you need
// Access the connection status through state.status
return Scaffold(
body: Center(
child: state.status == InternetStatus.connected // or use state.status.isConnected (bool)
child: state.status.isConnected
? Text('You are connected to the internet')
: Text('No internet connection'),
),
Expand All @@ -127,11 +129,12 @@ Here's an example:
```dart
return InternetStateManager(
onRestoreInternetConnection: () {
// Your custom logic here to execute when the internet connection is restored.
setState(() {
initData(); // Your logic here to execute when the internet connection is restored.
initData();
});
},
child: // widget,
child: // your widget
);
```
In this example, the onRestoreInternetConnection callback is used to reinitialize data or update the UI when the internet connection is restored. This allows you to handle any necessary updates or actions that should occur once connectivity is regained.
Expand Down
82 changes: 77 additions & 5 deletions examples/testing_internet_package/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ void main() {
runApp(
InternetStateManagerInitializer.init(
options: InternetStateOptions(
checkConnectionPeriodic: const Duration(seconds: 5),
checkConnectionPeriodic: const Duration(seconds: 3),
disconnectionCheckPeriodic: const Duration(seconds: 1),
showLogs: true,
),
child: const MyApp(),
),
Expand All @@ -15,15 +17,13 @@ void main() {
class MyApp extends StatelessWidget {
const MyApp({super.key});

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Internet state manager',
debugShowCheckedModeBanner: false,
home: InternetStateManager(
child: MyHomePage(),
),
builder: (context, child) => InternetStateManager(child: child!),
home: const MyHomePage(),
);
}
}
Expand Down Expand Up @@ -93,6 +93,10 @@ class _MyHomePageState extends State<MyHomePage> {
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
const Btn(
title: "Go to first screen",
screen: FirstScreen(),
),
],
),
),
Expand All @@ -104,3 +108,71 @@ class _MyHomePageState extends State<MyHomePage> {
);
}
}

class FirstScreen extends StatelessWidget {
const FirstScreen({super.key});

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('First screen'),
),
body: const Btn(
title: "Go to second screen",
screen: SecondScreen(),
),
);
}
}

class SecondScreen extends StatefulWidget {
const SecondScreen({super.key});

@override
State<SecondScreen> createState() => _SecondScreenState();
}

class _SecondScreenState extends State<SecondScreen> {
String title = 'Second screen';

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: InternetStateManager(
child: const FirstScreen(),
onRestoreInternetConnection: (){
setState(() {
title = "Internet connection restored";
});
},
),
);
}
}

class Btn extends StatelessWidget {
const Btn({
super.key,
required this.title,
required this.screen,
});

final String title;
final Widget screen;

@override
Widget build(BuildContext context) {
return Center(
child: TextButton(
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => screen));
},
child: Text(title),
),
);
}
}
19 changes: 13 additions & 6 deletions lib/src/bloc/internet_manager_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class InternetManagerCubit extends Cubit<InternetManagerState> {
Timer? _timer;

bool _connectionChanged = false;
bool _loading = false;

/// Returns [TRUE] if there is an internet connection after the
/// internet connection was offline. This means that the **connection
Expand All @@ -46,15 +47,17 @@ class InternetManagerCubit extends Cubit<InternetManagerState> {
});
}

final _showLog = false;

Future<void> checkConnection() async {
if (state.loading) return;
if (_loading) return;
_timer?.cancel();
_connectionChanged = false;
emit(state._loading());
_loading = true;
if (state.status.isDisconnected && !disconnectedToLocalNetwork) {
emit(state._loading());
}

if (_showLog) debugPrint('>> Checking for connection...');
if (getOptions.showLogs) debugPrint('>> Checking for connection...');

// check internet connection if there status connection
bool result = false;
Expand All @@ -73,17 +76,21 @@ class InternetManagerCubit extends Cubit<InternetManagerState> {
),
);

if (_showLog) {
if (getOptions.showLogs) {
debugPrint(
'connection: ${_localConnectionResult.map((e) => e.name).join(', ')} - ${state.status.isConnected ? "connected ✅" : "not connected ❌"}');
}
_loading = false;
_startTimer();
}

void _startTimer() {
if (getOptions.autoCheckConnection) {
final duration = state.status.isConnected || getOptions.disconnectionCheckPeriodic == null
? getOptions.checkConnectionPeriodic
: getOptions.disconnectionCheckPeriodic!;
_timer = Timer(
getOptions.checkConnectionPeriodic,
duration,
checkConnection,
);
}
Expand Down
25 changes: 20 additions & 5 deletions lib/src/utils/internet_state_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ import 'package:flutter/material.dart';
class InternetStateOptions {
/// ### The duration periodic between each auto internet check.
///
/// If null it returns `12 seconds` Duration.
/// The default is `12 seconds` Duration.
final Duration checkConnectionPeriodic;

/// ### The duration periodic between each auto internet check when disconnected state.
///
/// Used when internet connection is lost, the check periodic will be set to this value
/// otherwise if null it will be set to [checkConnectionPeriodic].
final Duration? disconnectionCheckPeriodic;

/// ### Set to [TRUE] to auto check internet connection periodically.
///
/// If set to [FALSE] it will check the internet connection only
Expand All @@ -30,14 +36,24 @@ class InternetStateOptions {
/// If [NULL] it returns default values.
late final InternetStateLabels labels;

/// ### Show logs.
///
/// Default is [FALSE].
final bool showLogs;

InternetStateOptions({
InternetStateLabels? translations,
/// ### The labels shown when internet disconnected.
///
/// If [NULL] it returns default values.
InternetStateLabels? labels,
this.errorBackgroundColor,
this.onBackgroundColor,
this.checkConnectionPeriodic = const Duration(seconds: 12),
this.autoCheckConnection = true,
this.showLogs = false,
this.disconnectionCheckPeriodic,
}) {
labels = translations ?? InternetStateLabels.defaultValues;
this.labels = labels ?? InternetStateLabels.defaultValues;
}
}

Expand All @@ -63,8 +79,7 @@ class InternetStateLabels {
required this.tryAgainText,
});

static InternetStateLabels get defaultValues =>
InternetStateLabels(
static InternetStateLabels get defaultValues => InternetStateLabels(
noInternetTitle: () => 'No internet connection',
descriptionText: () => 'Check your internet connection.',
tryAgainText: () => 'Try again',
Expand Down
12 changes: 10 additions & 2 deletions lib/src/widgets/internet_state_manager_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class InternetStateManager extends StatefulWidget {
super.key,
required this.builder,
this.onRestoreInternetConnection,
}) : child = null, noInternetScreen = null;
}) : child = null,
noInternetScreen = null;

final Widget? child;
final Widget Function(
Expand Down Expand Up @@ -50,6 +51,8 @@ class InternetStateManager extends StatefulWidget {
}

class _InternetStateManagerState extends State<InternetStateManager> {
int counter = 0;

@override
void initState() {
InternetStateManagerController.checkInstanceIsCreated();
Expand All @@ -68,10 +71,15 @@ class _InternetStateManagerState extends State<InternetStateManager> {
Widget build(BuildContext context) {
return BlocBuilder<InternetManagerCubit, InternetManagerState>(
builder: (context, state) {
if (getOptions.showLogs) {
debugPrint('> state changed: $state - ${counter++}');
}
if (state.status.isDisconnected) {
return widget.noInternetScreen ?? _DisconnectedWidget(parent: widget);
} else if (context.read<InternetManagerCubit>().connectionRestored && widget.onRestoreInternetConnection != null) {
debugPrint("internet connection restored ..");
if (getOptions.showLogs) {
debugPrint("internet connection restored ..");
}
WidgetsBinding.instance.addPostFrameCallback((_) {
widget.onRestoreInternetConnection?.call();
context.read<InternetManagerCubit>().onRestoreInternetConnectionCalled();
Expand Down

0 comments on commit 1b1f69f

Please sign in to comment.