added version checking

This commit is contained in:
Vaibhav Surve 2025-12-11 15:43:14 +05:30
parent e8fd420d51
commit 22b61b7024
3 changed files with 398 additions and 72 deletions

View File

@ -0,0 +1,291 @@
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:flutter_in_store_app_version_checker/flutter_in_store_app_version_checker.dart';
import 'package:on_field_work/helpers/services/app_logger.dart';
import 'package:on_field_work/helpers/utils/mixins/ui_mixin.dart';
import 'package:on_field_work/images.dart';
import 'package:on_field_work/helpers/widgets/wave_background.dart';
class MandatoryUpdateScreen extends StatefulWidget {
final String newVersion;
final InStoreAppVersionCheckerResult? updateResult;
const MandatoryUpdateScreen({
super.key,
required this.newVersion,
this.updateResult,
});
@override
State<MandatoryUpdateScreen> createState() => _MandatoryUpdateScreenState();
}
class _MandatoryUpdateScreenState extends State<MandatoryUpdateScreen>
with SingleTickerProviderStateMixin, UIMixin {
late AnimationController _controller;
late Animation<double> _logoAnimation;
static const double _kMaxContentWidth = 480.0;
Color get _primaryColor => contentTheme.primary;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 800),
);
_logoAnimation = CurvedAnimation(
parent: _controller,
curve: Curves.elasticOut,
);
_controller.forward();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
Future<void> _launchStoreUrl() async {
final url = widget.updateResult?.appURL;
if (url != null && url.isNotEmpty) {
final uri = Uri.parse(url);
try {
if (await canLaunchUrl(uri)) {
await launchUrl(uri, mode: LaunchMode.externalApplication);
} else {
logSafe("Could not launch store URL: $url");
}
} catch (e, stack) {
logSafe(
"Error launching store URL: $url",
error: e,
stackTrace: stack,
level: LogLevel.error,
);
}
}
}
@override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.ltr,
child: Scaffold(
body: Stack(
children: [
RedWaveBackground(brandRed: _primaryColor),
SafeArea(
child: Center(
child: ConstrainedBox(
constraints:
const BoxConstraints(maxWidth: _kMaxContentWidth),
child: SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: 24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const SizedBox(height: 32),
ScaleTransition(
scale: _logoAnimation,
child: Container(
width: 100,
height: 100,
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.1),
blurRadius: 20,
offset: const Offset(0, 8),
),
],
),
padding: const EdgeInsets.all(20),
child: Image.asset(Images.logoDark),
),
),
const SizedBox(height: 32),
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.05),
blurRadius: 10,
offset: const Offset(0, 5),
),
],
),
padding: const EdgeInsets.all(32),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Title
Text(
"Update Required",
textAlign: TextAlign.center,
style: Theme.of(context)
.textTheme
.headlineMedium
?.copyWith(
fontWeight: FontWeight.bold,
color: Colors.black87,
),
),
const SizedBox(height: 12),
// Subtitle/Message
Text(
"A mandatory update (version ${widget.newVersion}) is available to continue using the application. Please update now for uninterrupted access.",
textAlign: TextAlign.center,
style: Theme.of(context)
.textTheme
.bodyLarge
?.copyWith(
color: Colors.black54,
height: 1.4,
),
),
const SizedBox(height: 32),
// Prominent Action Button
ElevatedButton.icon(
onPressed: _launchStoreUrl,
icon: const Icon(
Icons.system_update_alt,
color: Colors.white,
),
label: Text(
"UPDATE NOW",
style: Theme.of(context)
.textTheme
.labelLarge
?.copyWith(
fontWeight: FontWeight.w700,
color: Colors.white,
fontSize: 16,
),
),
style: ElevatedButton.styleFrom(
padding:
const EdgeInsets.symmetric(vertical: 18),
backgroundColor: _primaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
elevation: 5,
),
),
const SizedBox(height: 32),
// Why Update Section
Text(
"Why updating is important:",
textAlign: TextAlign.start,
style: Theme.of(context)
.textTheme
.titleMedium
?.copyWith(
fontWeight: FontWeight.w600,
color: Colors.black87,
),
),
const SizedBox(height: 12),
const Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
BulletPoint(
text:
"Access new features and improvements"),
BulletPoint(
text:
"Fix critical bugs and security issues"),
BulletPoint(
text:
"Ensure smooth app performance and stability"),
BulletPoint(
text:
"Stay compatible with latest operating system and services"),
],
),
const SizedBox(height: 12),
Text(
"Thank you for keeping your app up to date!",
textAlign: TextAlign.center,
style: Theme.of(context)
.textTheme
.bodySmall
?.copyWith(
color: Colors.black45,
),
),
],
),
),
],
),
),
),
),
),
],
),
),
);
}
}
class BulletPoint extends StatelessWidget {
final String text;
final Color bulletColor;
const BulletPoint({
super.key,
required this.text,
this.bulletColor = const Color(0xFF555555),
});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(bottom: 8),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(right: 8, top: 4),
child: Icon(
Icons.circle,
size: 6,
color: bulletColor,
),
),
Expanded(
child: Text(
text,
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: Colors.black87,
height: 1.4,
),
),
),
],
),
);
}
}

View File

@ -3,8 +3,9 @@ import 'package:get/get.dart';
import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:flutter_in_store_app_version_checker/flutter_in_store_app_version_checker.dart';
import 'package:on_field_work/helpers/services/app_logger.dart'; import 'package:on_field_work/helpers/services/app_logger.dart';
import 'package:on_field_work/helpers/extensions/app_localization_delegate.dart';
import 'package:on_field_work/helpers/services/localizations/language.dart'; import 'package:on_field_work/helpers/services/localizations/language.dart';
import 'package:on_field_work/helpers/services/navigation_services.dart'; import 'package:on_field_work/helpers/services/navigation_services.dart';
import 'package:on_field_work/helpers/services/storage/local_storage.dart'; import 'package:on_field_work/helpers/services/storage/local_storage.dart';
@ -13,84 +14,120 @@ import 'package:on_field_work/helpers/theme/theme_customizer.dart';
import 'package:on_field_work/helpers/theme/app_notifier.dart'; import 'package:on_field_work/helpers/theme/app_notifier.dart';
import 'package:on_field_work/routes.dart'; import 'package:on_field_work/routes.dart';
class MyApp extends StatelessWidget { import 'package:on_field_work/view/mandatory_update_screen.dart';
final bool isOffline;
class MyApp extends StatefulWidget {
final bool isOffline;
const MyApp({super.key, required this.isOffline}); const MyApp({super.key, required this.isOffline});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _needsUpdate = false;
String? _newVersion;
InStoreAppVersionCheckerResult? _updateResult;
final InStoreAppVersionChecker _checker = InStoreAppVersionChecker(
androidStore: AndroidStore.googlePlayStore,
);
@override
void initState() {
super.initState();
_checkVersion();
}
/// -------------------------
/// Version Check
/// -------------------------
Future<void> _checkVersion() async {
try {
final result = await _checker.checkUpdate();
_updateResult = result;
logSafe("Version Check initiated...");
logSafe("Current App Version: ${_checker.currentVersion}");
logSafe("Result canUpdate: ${result.canUpdate}");
logSafe("Result newVersion: ${result.newVersion}");
logSafe("Result appURL: ${result.appURL}");
if (result.canUpdate) {
setState(() {
_needsUpdate = true;
_newVersion = result.newVersion ?? "";
});
logSafe("New version available → $_newVersion");
}
if (result.errorMessage != null) {
logSafe("VersionChecker Error: ${result.errorMessage}");
}
} catch (e, stack) {
logSafe(
"Version check exception",
error: e,
stackTrace: stack,
level: LogLevel.error,
);
}
}
/// -------------------------
/// Initial Route Logic
/// -------------------------
Future<String> _getInitialRoute() async { Future<String> _getInitialRoute() async {
try { try {
final token = LocalStorage.getJwtToken(); final token = LocalStorage.getJwtToken();
if (token == null || token.isEmpty) { if (token == null || token.isEmpty) {
logSafe("User not logged in. Routing to /auth/login-option");
return "/auth/login-option"; return "/auth/login-option";
} }
final bool hasMpin = LocalStorage.getIsMpin(); if (LocalStorage.getIsMpin()) {
if (hasMpin) {
await LocalStorage.setBool("mpin_verified", false); await LocalStorage.setBool("mpin_verified", false);
logSafe("Routing to /auth/mpin-auth");
return "/auth/mpin-auth"; return "/auth/mpin-auth";
} }
logSafe("No MPIN. Routing to /dashboard");
return "/dashboard"; return "/dashboard";
} catch (e, stacktrace) { } catch (e, stack) {
logSafe("Error determining initial route", logSafe(
level: LogLevel.error, error: e, stackTrace: stacktrace); "Initial route ERROR",
error: e,
stackTrace: stack,
level: LogLevel.error,
);
return "/auth/login-option"; return "/auth/login-option";
} }
} }
// REVISED: Helper Widget to show a full-screen, well-designed offline status /// -------------------------
Widget _buildConnectivityOverlay(BuildContext context) { /// Offline Overlay (Blocking)
// If not offline, return an empty widget. /// -------------------------
if (!isOffline) return const SizedBox.shrink(); Widget _buildOfflineOverlay() {
// Otherwise, return a full-screen overlay.
return Directionality( return Directionality(
textDirection: AppTheme.textDirection, textDirection: AppTheme.textDirection,
child: Scaffold( child: Scaffold(
backgroundColor: backgroundColor: Colors.grey.shade200,
Colors.grey.shade100, // Light background for the offline state
body: Center( body: Center(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
Icon( Icon(Icons.cloud_off, size: 100, color: Colors.red.shade600),
Icons.cloud_off, const SizedBox(height: 20),
color: Colors.red.shade700, // Prominent color
size: 100,
),
const SizedBox(height: 24),
const Text( const Text(
"You Are Offline", "You Are Offline",
style: TextStyle( style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
fontSize: 22,
fontWeight: FontWeight.bold,
color: Colors.black87,
),
), ),
const SizedBox(height: 8), const SizedBox(height: 10),
const Padding( const Padding(
padding: EdgeInsets.symmetric(horizontal: 40.0), padding: EdgeInsets.symmetric(horizontal: 32),
child: Text( child: Text(
"Please check your internet connection and try again.", "Please check your internet connection and try again.",
textAlign: TextAlign.center, textAlign: TextAlign.center,
style: TextStyle( style: TextStyle(fontSize: 16, color: Colors.black54),
fontSize: 16,
color: Colors.black54,
),
), ),
), ),
const SizedBox(height: 32),
// Optional: Add a button for the user to potentially refresh/retry
// ElevatedButton(
// onPressed: () {
// // Add logic to re-check connectivity or navigate (if possible)
// },
// child: const Text("RETRY"),
// ),
], ],
), ),
), ),
@ -98,21 +135,28 @@ class MyApp extends StatelessWidget {
); );
} }
/// -------------------------
/// Build
/// -------------------------
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (_needsUpdate && !widget.isOffline) {
// 4. USE THE NEW WIDGET HERE
return MandatoryUpdateScreen(
newVersion: _newVersion ?? "",
updateResult: _updateResult,
);
}
if (widget.isOffline) {
return _buildOfflineOverlay();
}
return Consumer<AppNotifier>( return Consumer<AppNotifier>(
builder: (_, notifier, __) { builder: (_, notifier, __) {
return FutureBuilder<String>( return FutureBuilder<String>(
future: _getInitialRoute(), future: _getInitialRoute(),
builder: (context, snapshot) { builder: (context, snapshot) {
if (snapshot.hasError) {
logSafe("FutureBuilder snapshot error",
level: LogLevel.error, error: snapshot.error);
return const MaterialApp(
home: Center(child: Text("Error determining route")),
);
}
if (!snapshot.hasData) { if (!snapshot.hasData) {
return const MaterialApp( return const MaterialApp(
home: Center(child: CircularProgressIndicator()), home: Center(child: CircularProgressIndicator()),
@ -127,29 +171,19 @@ class MyApp extends StatelessWidget {
navigatorKey: NavigationService.navigatorKey, navigatorKey: NavigationService.navigatorKey,
initialRoute: snapshot.data!, initialRoute: snapshot.data!,
getPages: getPageRoute(), getPages: getPageRoute(),
builder: (context, child) { supportedLocales: Language.getLocales(),
NavigationService.registerContext(context); localizationsDelegates: const [
// 💡 REVISED: Use a Stack to place the offline overlay ON TOP of the app content.
// This allows the full-screen view to cover everything, including the main app content.
return Stack(
children: [
Directionality(
textDirection: AppTheme.textDirection,
child: child ?? const SizedBox(),
),
// 2. The full-screen connectivity overlay, only visible when offline
_buildConnectivityOverlay(context),
],
);
},
localizationsDelegates: [
AppLocalizationsDelegate(context),
GlobalMaterialLocalizations.delegate, GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate, GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate, GlobalCupertinoLocalizations.delegate,
], ],
supportedLocales: Language.getLocales(), builder: (context, child) {
NavigationService.registerContext(context);
return Directionality(
textDirection: AppTheme.textDirection,
child: child ?? const SizedBox(),
);
},
); );
}, },
); );

View File

@ -56,7 +56,7 @@ dependencies:
syncfusion_flutter_calendar: ^31.2.18 syncfusion_flutter_calendar: ^31.2.18
syncfusion_flutter_maps: ^31.2.18 syncfusion_flutter_maps: ^31.2.18
http: ^1.6.0 http: ^1.6.0
geolocator: ^14.0.2 geolocator: ^14.0.1
permission_handler: ^12.0.1 permission_handler: ^12.0.1
image: ^4.0.17 image: ^4.0.17
image_picker: ^1.0.7 image_picker: ^1.0.7
@ -87,6 +87,7 @@ dependencies:
share_plus: ^12.0.1 share_plus: ^12.0.1
timeline_tile: ^2.0.0 timeline_tile: ^2.0.0
encrypt: ^5.0.3 encrypt: ^5.0.3
flutter_in_store_app_version_checker: ^1.10.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: