Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ class _BuildAppState extends State<BuildApp> {
void initState() {
super.initState();
_loadIdleTimeFromServer();
final connectivity = Provider.of<ConnectivityProvider>(context, listen: false);
connectivity.startAutoNetworkCheck();

}

Future<void> _loadIdleTimeFromServer() async {
Expand Down
18 changes: 18 additions & 0 deletions lib/provider/connectivity_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*
*/

import 'dart:async';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
Expand All @@ -20,6 +22,22 @@ class ConnectivityProvider extends ChangeNotifier {
bool get isConnected => _isConnected;
bool get isGPSEnabled => _isGPSEnabled;

Timer? _autoCheckTimer;

void startAutoNetworkCheck() {
_autoCheckTimer?.cancel();

_autoCheckTimer = Timer.periodic(const Duration(seconds: 3), (timer) {
checkNetworkConnection();
});
Comment on lines +30 to +32
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Consider increasing the polling interval to reduce resource consumption.

A 3-second network check interval may be excessive and could impact battery life and performance, especially on mobile devices. Consider increasing it to 10-15 seconds or using connectivity change listeners instead of polling.

Additionally, checkNetworkConnection() is async but not awaited in the timer callback. If a network check takes longer than 3 seconds, multiple concurrent calls could stack up, potentially overwhelming the network service.

Apply this diff to increase the interval and await the network check:

-    _autoCheckTimer = Timer.periodic(const Duration(seconds: 3), (timer) {
-      checkNetworkConnection();
+    _autoCheckTimer = Timer.periodic(const Duration(seconds: 10), (timer) async {
+      await checkNetworkConnection();
     });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
_autoCheckTimer = Timer.periodic(const Duration(seconds: 3), (timer) {
checkNetworkConnection();
});
_autoCheckTimer = Timer.periodic(const Duration(seconds: 10), (timer) async {
await checkNetworkConnection();
});
🤖 Prompt for AI Agents
In lib/provider/connectivity_provider.dart around lines 30 to 32, the timer
currently polls every 3 seconds and calls the async checkNetworkConnection()
without awaiting it; increase the polling interval to a more conservative value
(e.g., 15 seconds) and await the async network check inside the timer callback
to prevent overlapping calls (or replace polling with connectivity change
listeners later); update the Timer.periodic to use Duration(seconds: 15) and
make the callback async so it awaits checkNetworkConnection() to avoid
concurrent executions.

}

@override
void dispose() {
_autoCheckTimer?.cancel();
super.dispose();
}

checkNetworkConnection() async {
String response = await networkService.checkInternetConnection();
if(response == "200") {
Expand Down
27 changes: 27 additions & 0 deletions lib/utils/inactivity_tracker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:registration_client/provider/global_provider.dart';
import 'package:registration_client/provider/sync_provider.dart';

import '../main.dart';
import '../provider/connectivity_provider.dart';

class InactivityTracker extends StatefulWidget {
final Widget child;
Expand Down Expand Up @@ -41,6 +42,17 @@ class _InactivityTrackerState extends State<InactivityTracker> with WidgetsBindi
super.initState();
globalProvider = Provider.of<GlobalProvider>(context, listen: false);
WidgetsBinding.instance.addObserver(this);

final connectivity = Provider.of<ConnectivityProvider>(context, listen: false);

connectivity.addListener(() {
if (!connectivity.isConnected) {
Future.microtask(() {
if (mounted) _showWarningDialog();
});
}
});

_startInactivityTimer();
}

Expand All @@ -63,7 +75,22 @@ class _InactivityTrackerState extends State<InactivityTracker> with WidgetsBindi
}

void _startInactivityTimer() {
final connectivity = Provider.of<ConnectivityProvider>(context, listen: false);
if (!connectivity.isConnected) {
Future.microtask(() {
if (mounted) _showWarningDialog();
});
return;
}

if (!widget.isUserLoggedIn) return;

if (!connectivity.isConnected) {
Future.microtask(() {
if (mounted) _showWarningDialog();
});
return;
}
_inactivityTimer = Timer(widget.timeout, _showWarningDialog);
}

Expand Down