controllo versione

This commit is contained in:
2026-05-19 18:53:24 +02:00
parent d3b1e52d88
commit 659963beb0
6 changed files with 237 additions and 7 deletions

View File

@@ -4,6 +4,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flux/core/utils/version_check_service.dart';
import 'package:flux/features/attachments/data/attachments_repository.dart';
import 'package:flux/features/auth/bloc/auth_cubit.dart';
import 'package:flux/features/company/data/company_repository.dart';
@@ -38,6 +39,7 @@ import 'package:flux/features/master_data/store/bloc/store_cubit.dart';
import 'package:flux/features/master_data/store/data/store_repository.dart';
import 'package:flux/features/settings/settings.dart';
import 'package:flutter_web_plugins/url_strategy.dart';
import 'package:url_launcher/url_launcher.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
@@ -159,19 +161,14 @@ class _FluxAppState extends State<FluxApp> {
@override
Widget build(BuildContext context) {
// Il BlocConsumer unisce Listener e Builder in un colpo solo!
return BlocConsumer<SessionCubit, SessionState>(
// --- PARTE LISTENER (Il colpo di clacson in background) ---
listenWhen: (previous, current) =>
previous.status != SessionStatus.authenticated &&
current.status == SessionStatus.authenticated,
listener: (context, state) {
// BAM! L'utente è dentro. Pre-carichiamo i Cubit leggeri.
context.read<StoreCubit>().loadStores();
context.read<StaffCubit>().loadAllStaff();
},
// --- PARTE BUILDER (La UI che viene disegnata a schermo) ---
builder: (context, sessionState) {
if (sessionState.status == SessionStatus.initial) {
return _buildLoadingScreen();
@@ -189,6 +186,11 @@ class _FluxAppState extends State<FluxApp> {
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
locale: const Locale('it'),
// 🥷 ECCO LA MAGIA: Avvolgiamo tutta l'app nel nostro checker!
builder: (context, child) {
return GlobalUpdateChecker(child: child!);
},
);
},
);
@@ -222,3 +224,146 @@ class _FluxAppState extends State<FluxApp> {
);
}
}
// --- IL WIDGET GUARDIANO DEGLI AGGIORNAMENTI ---
class GlobalUpdateChecker extends StatefulWidget {
final Widget child;
const GlobalUpdateChecker({super.key, required this.child});
@override
State<GlobalUpdateChecker> createState() => _GlobalUpdateCheckerState();
}
class _GlobalUpdateCheckerState extends State<GlobalUpdateChecker> {
bool _mustUpdate = false;
String? _updateUrl;
@override
void initState() {
super.initState();
_checkVersionAndBlock();
}
Future<void> _checkVersionAndBlock() async {
final updateUrl = await VersionCheckService().checkForceUpdate();
if (updateUrl != null && mounted) {
// Invece di aprire un dialog, cambiamo lo stato e attiviamo lo "Scudo"
setState(() {
_mustUpdate = true;
_updateUrl = updateUrl;
});
}
}
@override
Widget build(BuildContext context) {
// 1. Se l'app è aggiornata, mostriamo solo l'app normale
if (!_mustUpdate) return widget.child;
// 2. Se l'app è vecchia, sovrapponiamo il blocco con uno Stack
return Stack(
children: [
// L'app sotto continua ad esistere, ma è inaccessibile
widget.child,
// IL BLOCCO INVALICABILE SOPRA A TUTTO
Positioned.fill(
child: Container(
color: Colors.black.withValues(alpha: 0.85), // Sfondo oscurante
child: Center(
// Usiamo Material per ereditare correttamente temi, font e colori
child: Material(
color: Colors.transparent,
child: Container(
margin: const EdgeInsets.all(24),
padding: const EdgeInsets.all(24),
constraints: const BoxConstraints(maxWidth: 400),
decoration: BoxDecoration(
color: Theme.of(context).scaffoldBackgroundColor,
borderRadius: BorderRadius.circular(16),
boxShadow: const [
BoxShadow(
color: Colors.black54,
blurRadius: 20,
offset: Offset(0, 10),
),
],
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
kIsWeb ? Icons.cached : Icons.system_update,
color: Colors.orange,
size: 32,
),
const SizedBox(width: 12),
Text(
kIsWeb
? "Aggiornamento"
: "Aggiornamento Obbligatorio",
style: Theme.of(context).textTheme.titleLarge
?.copyWith(fontWeight: FontWeight.bold),
),
],
),
const SizedBox(height: 16),
Text(
kIsWeb
? "È stata rilasciata una nuova versione dell'applicazione. Ricarica la pagina per continuare."
: "Per continuare ad utilizzare l'applicazione è necessario scaricare e installare l'ultimo aggiornamento.",
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 16),
),
const SizedBox(height: 24),
if (kIsWeb)
FilledButton.icon(
icon: const Icon(Icons.refresh),
label: const Text("RICARICA ORA"),
style: FilledButton.styleFrom(
minimumSize: const Size(double.infinity, 50),
),
onPressed: () async {
// Trick cross-platform per fare il reload
await launchUrl(
Uri.parse(Uri.base.toString()),
webOnlyWindowName: '_self',
);
},
)
else
FilledButton.icon(
icon: const Icon(Icons.download),
label: const Text("SCARICA AGGIORNAMENTO"),
style: FilledButton.styleFrom(
minimumSize: const Size(double.infinity, 50),
backgroundColor: Colors.blue,
),
onPressed: () async {
if (_updateUrl != null) {
final url = Uri.parse(_updateUrl!);
if (await canLaunchUrl(url)) {
await launchUrl(
url,
mode: LaunchMode.externalApplication,
);
}
}
},
),
],
),
),
),
),
),
),
],
);
}
}