2026-04-26 10:15:34 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
2026-05-04 19:32:14 +02:00
|
|
|
import 'package:firebase_core/firebase_core.dart';
|
2026-04-26 10:15:34 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
2026-04-04 14:43:29 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2026-04-04 19:25:55 +02:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2026-04-12 19:21:54 +02:00
|
|
|
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
2026-05-04 15:36:42 +02:00
|
|
|
import 'package:flux/features/attachments/data/attachments_repository.dart';
|
2026-04-22 11:06:02 +02:00
|
|
|
import 'package:flux/features/auth/bloc/auth_cubit.dart';
|
2026-05-04 15:36:42 +02:00
|
|
|
import 'package:flux/features/operations/data/operations_repository.dart';
|
2026-05-04 19:32:14 +02:00
|
|
|
import 'package:flux/firebase_options.dart';
|
2026-05-04 15:36:42 +02:00
|
|
|
import 'package:flux/l10n/app_localizations.dart';
|
2026-04-22 11:06:02 +02:00
|
|
|
import 'package:get_it/get_it.dart';
|
|
|
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
|
|
|
import 'package:flux/core/blocs/session/session_cubit.dart';
|
|
|
|
|
import 'package:flux/core/data/core_repository.dart';
|
2026-04-09 18:17:48 +02:00
|
|
|
import 'package:flux/core/routes/app_router.dart';
|
2026-04-07 11:30:22 +02:00
|
|
|
import 'package:flux/core/theme/theme.dart';
|
|
|
|
|
import 'package:flux/core/theme/bloc/theme_bloc.dart';
|
2026-05-04 15:36:42 +02:00
|
|
|
import 'package:flux/features/customers/blocs/customers_cubit.dart';
|
2026-04-10 10:47:56 +02:00
|
|
|
import 'package:flux/features/customers/data/customer_repository.dart';
|
2026-04-14 11:29:49 +02:00
|
|
|
import 'package:flux/features/master_data/products/blocs/product_cubit.dart';
|
|
|
|
|
import 'package:flux/features/master_data/products/data/product_repository.dart';
|
2026-04-17 10:34:23 +02:00
|
|
|
import 'package:flux/features/master_data/providers/blocs/provider_cubit.dart';
|
|
|
|
|
import 'package:flux/features/master_data/providers/data/provider_repository.dart';
|
2026-04-14 11:29:49 +02:00
|
|
|
import 'package:flux/features/master_data/staff/blocs/staff_cubit.dart';
|
|
|
|
|
import 'package:flux/features/master_data/staff/data/staff_repository.dart';
|
2026-04-15 10:05:07 +02:00
|
|
|
import 'package:flux/features/master_data/store/bloc/store_cubit.dart';
|
2026-04-14 11:29:49 +02:00
|
|
|
import 'package:flux/features/master_data/store/data/store_repository.dart';
|
2026-05-04 15:36:42 +02:00
|
|
|
import 'package:flux/features/operations/blocs/operations_cubit.dart';
|
2026-04-09 11:30:57 +02:00
|
|
|
import 'package:flux/features/settings/settings.dart';
|
2026-04-04 14:43:29 +02:00
|
|
|
|
2026-04-04 19:25:55 +02:00
|
|
|
void main() async {
|
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
2026-04-12 19:21:54 +02:00
|
|
|
await dotenv.load(fileName: ".env");
|
2026-04-22 11:06:02 +02:00
|
|
|
|
|
|
|
|
// Inizializza le dipendenze PRIMA di lanciare l'app
|
2026-04-09 11:30:57 +02:00
|
|
|
await setupLocator();
|
2026-04-06 10:55:56 +02:00
|
|
|
|
2026-04-05 10:06:26 +02:00
|
|
|
runApp(
|
2026-04-06 10:55:56 +02:00
|
|
|
MultiBlocProvider(
|
|
|
|
|
providers: [
|
2026-04-22 11:06:02 +02:00
|
|
|
BlocProvider<AuthCubit>(create: (context) => AuthCubit()),
|
2026-04-07 11:55:01 +02:00
|
|
|
BlocProvider<ThemeBloc>(
|
|
|
|
|
create: (context) => ThemeBloc()..add(LoadThemeEvent()),
|
|
|
|
|
),
|
2026-04-22 11:06:02 +02:00
|
|
|
// Il Vigile Urbano viene inizializzato!
|
|
|
|
|
BlocProvider<SessionCubit>(create: (_) => GetIt.I<SessionCubit>()),
|
|
|
|
|
|
|
|
|
|
// Cubit delle feature
|
|
|
|
|
BlocProvider<StoreCubit>(create: (_) => StoreCubit()),
|
2026-05-04 15:36:42 +02:00
|
|
|
BlocProvider<CustomersCubit>(create: (_) => CustomersCubit()),
|
|
|
|
|
BlocProvider<ProductsCubit>(create: (_) => ProductsCubit()),
|
2026-04-22 11:06:02 +02:00
|
|
|
BlocProvider<StaffCubit>(create: (_) => StaffCubit()),
|
2026-05-04 15:36:42 +02:00
|
|
|
BlocProvider<OperationsCubit>(create: (_) => OperationsCubit()),
|
2026-04-22 11:06:02 +02:00
|
|
|
BlocProvider<ProvidersCubit>(create: (_) => ProvidersCubit()),
|
2026-04-06 10:55:56 +02:00
|
|
|
],
|
2026-04-05 10:06:26 +02:00
|
|
|
child: const FluxApp(),
|
|
|
|
|
),
|
|
|
|
|
);
|
2026-04-04 14:43:29 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-09 11:30:57 +02:00
|
|
|
Future<void> setupLocator() async {
|
|
|
|
|
final GetIt getIt = GetIt.instance;
|
2026-04-22 11:06:02 +02:00
|
|
|
|
2026-04-09 11:30:57 +02:00
|
|
|
getIt.registerSingleton<SharedPreferences>(
|
|
|
|
|
await SharedPreferences.getInstance(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await Supabase.initialize(
|
2026-04-12 19:21:54 +02:00
|
|
|
url: dotenv.env['SUPABASE_URL'] ?? '',
|
|
|
|
|
anonKey: dotenv.env['SUPABASE_ANON_KEY'] ?? '',
|
2026-04-09 11:30:57 +02:00
|
|
|
);
|
2026-04-22 11:06:02 +02:00
|
|
|
//await Supabase.instance.client.auth.signOut();
|
2026-04-09 11:30:57 +02:00
|
|
|
getIt.registerSingleton<SupabaseClient>(Supabase.instance.client);
|
2026-04-22 11:06:02 +02:00
|
|
|
|
|
|
|
|
// Settings
|
2026-04-09 11:30:57 +02:00
|
|
|
getIt.registerLazySingleton<AppSettings>(() => AppSettings());
|
2026-04-22 11:06:02 +02:00
|
|
|
|
|
|
|
|
// Repositories
|
|
|
|
|
getIt.registerLazySingleton<CoreRepository>(
|
|
|
|
|
() => CoreRepository(),
|
|
|
|
|
); // <-- NUOVO
|
2026-04-09 11:30:57 +02:00
|
|
|
getIt.registerLazySingleton<StoreRepository>(() => StoreRepository());
|
2026-04-10 10:47:56 +02:00
|
|
|
getIt.registerLazySingleton<CustomerRepository>(() => CustomerRepository());
|
2026-04-12 19:21:54 +02:00
|
|
|
getIt.registerLazySingleton<ProductRepository>(() => ProductRepository());
|
2026-04-13 15:18:37 +02:00
|
|
|
getIt.registerLazySingleton<StaffRepository>(() => StaffRepository());
|
2026-05-04 15:36:42 +02:00
|
|
|
getIt.registerLazySingleton<OperationsRepository>(
|
|
|
|
|
() => OperationsRepository(),
|
|
|
|
|
);
|
2026-04-17 10:34:23 +02:00
|
|
|
getIt.registerLazySingleton<ProviderRepository>(() => ProviderRepository());
|
2026-05-04 15:36:42 +02:00
|
|
|
getIt.registerLazySingleton<AttachmentsRepository>(
|
|
|
|
|
() => AttachmentsRepository(),
|
|
|
|
|
);
|
2026-04-22 11:06:02 +02:00
|
|
|
|
|
|
|
|
// NOTA: CompanyRepository l'ho tolto perché la logica della Company
|
|
|
|
|
// ora è gestita dal CoreRepository durante l'Onboarding.
|
|
|
|
|
// Se ti serve per altro, rimettilo pure!
|
|
|
|
|
|
|
|
|
|
// Inizializziamo il SessionCubit (che prende CoreRepository e SharedPreferences)
|
|
|
|
|
// Usiamo registerSingleton così viene creato subito e inizia ad ascoltare Supabase Auth.
|
|
|
|
|
getIt.registerSingleton<SessionCubit>(
|
|
|
|
|
SessionCubit(getIt<CoreRepository>(), getIt<SharedPreferences>()),
|
|
|
|
|
);
|
2026-05-04 19:32:14 +02:00
|
|
|
//TODO rimuovere dopo gli import
|
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
|
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
|
2026-04-09 11:30:57 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-12 19:21:54 +02:00
|
|
|
class FluxApp extends StatefulWidget {
|
2026-04-05 10:06:26 +02:00
|
|
|
const FluxApp({super.key});
|
2026-04-04 14:43:29 +02:00
|
|
|
|
|
|
|
|
@override
|
2026-04-12 19:21:54 +02:00
|
|
|
State<FluxApp> createState() => _FluxAppState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _FluxAppState extends State<FluxApp> {
|
|
|
|
|
late final GoRouter _router;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
2026-04-22 11:06:02 +02:00
|
|
|
// Creiamo il router passandogli il Cubit per i redirect
|
|
|
|
|
_router = AppRouter.createRouter(context.read<SessionCubit>());
|
2026-04-26 10:15:34 +02:00
|
|
|
GetIt.I.get<SessionCubit>().setIsMobileDevice(isMobileDevice(context));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isMobileDevice(BuildContext context) {
|
|
|
|
|
if (kIsWeb) {
|
|
|
|
|
return false; // Il web non lo consideriamo "mobile nativo" per i deep link
|
|
|
|
|
}
|
|
|
|
|
return Platform.isAndroid || Platform.isIOS;
|
2026-04-12 19:21:54 +02:00
|
|
|
}
|
2026-04-06 10:55:56 +02:00
|
|
|
|
2026-04-12 19:21:54 +02:00
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2026-04-26 10:15:34 +02:00
|
|
|
// 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();
|
|
|
|
|
context.read<ProvidersCubit>().loadProviders();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// --- PARTE BUILDER (La UI che viene disegnata a schermo) ---
|
2026-04-22 11:06:02 +02:00
|
|
|
builder: (context, sessionState) {
|
|
|
|
|
if (sessionState.status == SessionStatus.initial) {
|
2026-04-20 16:52:20 +02:00
|
|
|
return _buildLoadingScreen();
|
|
|
|
|
}
|
2026-04-22 11:06:02 +02:00
|
|
|
|
2026-04-20 16:52:20 +02:00
|
|
|
return BlocBuilder<ThemeBloc, ThemeState>(
|
2026-04-22 11:06:02 +02:00
|
|
|
builder: (context, themeState) {
|
2026-04-20 16:52:20 +02:00
|
|
|
return MaterialApp.router(
|
|
|
|
|
title: 'FLUX Gestionale',
|
|
|
|
|
debugShowCheckedModeBanner: false,
|
|
|
|
|
theme: fluxLightTheme,
|
|
|
|
|
darkTheme: fluxDarkTheme,
|
2026-04-22 11:06:02 +02:00
|
|
|
themeMode: themeState.currentTheme.themeMode,
|
|
|
|
|
routerConfig: _router,
|
2026-05-04 15:36:42 +02:00
|
|
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
|
|
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
|
|
|
|
locale: const Locale('it'),
|
2026-04-20 16:52:20 +02:00
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildLoadingScreen() {
|
|
|
|
|
return MaterialApp(
|
|
|
|
|
debugShowCheckedModeBanner: false,
|
|
|
|
|
home: Scaffold(
|
|
|
|
|
body: Center(
|
|
|
|
|
child: Column(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
|
children: [
|
|
|
|
|
const Icon(Icons.bolt, size: 64, color: Colors.blue),
|
|
|
|
|
const SizedBox(height: 24),
|
|
|
|
|
const CircularProgressIndicator(),
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
const Text(
|
|
|
|
|
"Inizializzazione sessione...",
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
color: Colors.grey,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
2026-04-17 10:34:23 +02:00
|
|
|
),
|
2026-04-12 19:21:54 +02:00
|
|
|
),
|
2026-04-06 10:55:56 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|