import 'dart:io'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:kooltab2/utils/app_update_util.dart'; import 'app_config.dart'; import '../l10n/app_localizations.dart'; class UpdateChecker { static bool _hasChecked = false; static Future check(BuildContext context) async { if (!Platform.isAndroid) return; if (_hasChecked) { debugPrint("Update already checked this boot"); return; } _hasChecked = true; final updater = AppUpdateUtil(serverUrl: kUpdateServerUrl); try { final update = await updater.checkForUpdate(); if (update == null) { debugPrint("No update available"); return; } debugPrint("Update available: ${update.version}"); if (!context.mounted) return; _showUpdateDialog(context, update); } catch (e) { debugPrint("Update check failed: $e"); } } static void _showUpdateDialog(BuildContext context, UpdateInfo update) { final l10n = AppLocalizations.of(context); showDialog( context: context, barrierDismissible: !update.mandatory, builder: (dialogContext) => AlertDialog( title: Text(l10n.updateAvailable), content: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(l10n.versionAvailable(update.version)), const SizedBox(height: 12), Text(update.notes), ], ), actions: [ if (!update.mandatory) TextButton( onPressed: () => Navigator.pop(dialogContext), child: Text(l10n.later), ), FilledButton( onPressed: () { Navigator.pop(dialogContext); context.push('/update-progress', extra: update); }, child: Text(l10n.update), ), ], ), ); } }