v1.1.6
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
@@ -44,112 +45,109 @@ class SessionCubit extends Cubit<SessionState> {
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. CHI È QUESTO UTENTE? (Vediamo se ha un profilo staff, che sia Invitato o Admin)
|
||||
StaffMemberModel? staff = await _repository.getStaffMemberByUserId(
|
||||
user.id,
|
||||
);
|
||||
CompanyModel? company;
|
||||
if (staff != null) {
|
||||
// --- LA MAGIA DEL SENSORE ---
|
||||
if (staff.hasJoined == false) {
|
||||
// È la primissima volta che entra! Aggiorniamo il DB.
|
||||
await _repository.updateStaffMember(staff.id!, {'has_joined': true});
|
||||
// Aggiorniamo anche il nostro modello in memoria per questa sessione
|
||||
staff = staff.copyWith(hasJoined: true);
|
||||
// Riportiamo lo stato su initial per far girare lo spinner se stiamo riprovando
|
||||
emit(state.copyWith(status: SessionStatus.initial, errorMessage: null));
|
||||
|
||||
// WRAP DELLA LOGICA IN UN BLOCCO PROTETTO DA TIMEOUT (10 Secondi)
|
||||
await Future(() async {
|
||||
StaffMemberModel? staff = await _repository.getStaffMemberByUserId(
|
||||
user.id,
|
||||
);
|
||||
CompanyModel? company;
|
||||
|
||||
if (staff != null) {
|
||||
if (staff.hasJoined == false) {
|
||||
await _repository.updateStaffMember(staff.id!, {
|
||||
'has_joined': true,
|
||||
});
|
||||
staff = staff.copyWith(hasJoined: true);
|
||||
}
|
||||
company = await _repository.getCompanyById(staff.companyId);
|
||||
} else {
|
||||
company = await _repository.getCompanyByOwnerId(user.id);
|
||||
}
|
||||
|
||||
company = await _repository.getCompanyById(staff.companyId);
|
||||
} else {
|
||||
// È l'Admin in onboarding
|
||||
company = await _repository.getCompanyByOwnerId(user.id);
|
||||
}
|
||||
// 1. Controllo Azienda
|
||||
if (company == null) {
|
||||
return emit(
|
||||
state.copyWith(
|
||||
status: SessionStatus.onboardingRequired,
|
||||
user: user,
|
||||
onboardingStep: OnboardingStep.company,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
emit(state.copyWith(company: company));
|
||||
}
|
||||
|
||||
if (staff != null) {
|
||||
// L'utente esiste già nel sistema! Carichiamo l'azienda per cui lavora
|
||||
company = await _repository.getCompanyById(staff.companyId);
|
||||
} else {
|
||||
// L'utente non ha profilo. Probabilmente è l'Admin che ha appena
|
||||
// fatto Sign Up e sta iniziando l'Onboarding
|
||||
company = await _repository.getCompanyByOwnerId(user.id);
|
||||
}
|
||||
if (company == null) {
|
||||
return emit(
|
||||
state.copyWith(
|
||||
status: SessionStatus.onboardingRequired,
|
||||
user: user,
|
||||
onboardingStep: OnboardingStep.company,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
emit(state.copyWith(company: company));
|
||||
}
|
||||
final stores = await _repository.getStoresByCompanyId(company.id!);
|
||||
if (stores.isEmpty) {
|
||||
return emit(
|
||||
state.copyWith(
|
||||
status: SessionStatus.onboardingRequired,
|
||||
user: user,
|
||||
company: company,
|
||||
onboardingStep: OnboardingStep.store,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
emit(state.copyWith(currentStore: stores.first));
|
||||
}
|
||||
|
||||
// 2. Controllo Negozi
|
||||
final stores = await _repository.getStoresByCompanyId(company.id!);
|
||||
if (stores.isEmpty) {
|
||||
return emit(
|
||||
if (staff == null) {
|
||||
return emit(
|
||||
state.copyWith(
|
||||
status: SessionStatus.onboardingRequired,
|
||||
user: user,
|
||||
company: company,
|
||||
onboardingStep: OnboardingStep.staff,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final lastStoreId = _prefs.getString(_lastStoreKey);
|
||||
final activeStore =
|
||||
stores.firstWhereOrNull((s) => s.id == lastStoreId) ?? stores.first;
|
||||
|
||||
if (lastStoreId != activeStore.id && activeStore.id != null) {
|
||||
await _prefs.setString(_lastStoreKey, activeStore.id!);
|
||||
}
|
||||
|
||||
setIsSingleUserMode(_prefs.getBool('isSingleUserMode') ?? false);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: SessionStatus.onboardingRequired,
|
||||
status: SessionStatus.authenticated,
|
||||
user: user,
|
||||
company: company,
|
||||
onboardingStep: OnboardingStep.store,
|
||||
currentStore: activeStore,
|
||||
currentStaffMember: staff,
|
||||
onboardingStep: OnboardingStep.none,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
emit(state.copyWith(currentStore: stores.first));
|
||||
}
|
||||
|
||||
// 3. Controllo Staff (Paziente Zero)
|
||||
if (staff == null) {
|
||||
return emit(
|
||||
state.copyWith(
|
||||
status: SessionStatus.onboardingRequired,
|
||||
user: user,
|
||||
company: company,
|
||||
onboardingStep: OnboardingStep.staff,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// --- TUTTO COMPLETATO: LOGICA DEL NEGOZIO DI DEFAULT ---
|
||||
|
||||
// Leggiamo l'ultimo negozio dalle SharedPreferences
|
||||
final lastStoreId = _prefs.getString(_lastStoreKey);
|
||||
|
||||
// Cerchiamo quel negozio nella lista. Se non c'è (magari è stato eliminato), prendiamo il primo.
|
||||
final activeStore =
|
||||
stores.firstWhereOrNull((s) => s.id == lastStoreId) ?? stores.first;
|
||||
|
||||
// Se non avevamo il lastStoreId salvato, salviamolo ora
|
||||
if (lastStoreId != activeStore.id && activeStore.id != null) {
|
||||
await _prefs.setString(_lastStoreKey, activeStore.id!);
|
||||
}
|
||||
|
||||
setIsSingleUserMode(_prefs.getBool('isSingleUserMode') ?? false);
|
||||
|
||||
// 4. BENVENUTO A BORDO
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: SessionStatus.authenticated,
|
||||
user: user,
|
||||
company: company,
|
||||
currentStore: activeStore,
|
||||
currentStaffMember: staff,
|
||||
onboardingStep: OnboardingStep.none, // Svuotiamo l'onboarding
|
||||
),
|
||||
// FCM è fuori dall'await principale, quindi va bene così
|
||||
_registerFcmToken(companyId: company.id!, staffId: staff.id!);
|
||||
}).timeout(
|
||||
const Duration(seconds: 10), // Tempo massimo concesso al server
|
||||
onTimeout: () {
|
||||
throw TimeoutException(
|
||||
'Il server di FLUX non risponde. Controlla la connessione.',
|
||||
);
|
||||
},
|
||||
);
|
||||
} on TimeoutException catch (e) {
|
||||
// 🎯 BINGO! IL TIMEOUT È SCATTATO
|
||||
debugPrint("Timeout Inizializzazione: ${e.message}");
|
||||
emit(
|
||||
state.copyWith(status: SessionStatus.error, errorMessage: e.message),
|
||||
);
|
||||
// --- REGISTRAZIONE DISPOSITIVO PER NOTIFICHE PUSH ---
|
||||
// Lo chiamiamo SENZA 'await' in modo che il caricamento dell'app non si blocchi.
|
||||
// L'utente entrerà subito nell'app e poi vedrà comparire il popup di sistema
|
||||
// per accettare i permessi delle notifiche.
|
||||
_registerFcmToken(companyId: company.id!, staffId: staff.id!);
|
||||
} catch (e) {
|
||||
// Se esplode il database, non lasciamo l'app freezata in 'initial'
|
||||
// Altri errori generici del DB o di rete
|
||||
debugPrint("Errore Inizializzazione: $e");
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: SessionStatus
|
||||
.unauthenticated, // O un nuovo stato SessionStatus.error
|
||||
status: SessionStatus.error,
|
||||
errorMessage: "Si è verificato un errore di connessione imprevisto.",
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ enum SessionStatus {
|
||||
unauthenticated,
|
||||
onboardingRequired,
|
||||
authenticated,
|
||||
error,
|
||||
}
|
||||
|
||||
/// Definisce lo step esatto dell'onboarding (Paranoia Mode)
|
||||
@@ -26,6 +27,7 @@ class SessionState extends Equatable {
|
||||
final OnboardingStep onboardingStep;
|
||||
final bool isMobileDevice;
|
||||
final bool isSingleUserMode;
|
||||
final String? errorMessage;
|
||||
|
||||
const SessionState({
|
||||
this.status = SessionStatus.initial,
|
||||
@@ -36,6 +38,7 @@ class SessionState extends Equatable {
|
||||
this.onboardingStep = OnboardingStep.none,
|
||||
this.isMobileDevice = false,
|
||||
this.isSingleUserMode = false,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
/// Metodo per creare una copia dello stato modificando solo i campi necessari
|
||||
@@ -48,6 +51,7 @@ class SessionState extends Equatable {
|
||||
OnboardingStep? onboardingStep,
|
||||
bool? isMobileDevice,
|
||||
bool? isSingleUserMode,
|
||||
String? errorMessage,
|
||||
}) {
|
||||
return SessionState(
|
||||
status: status ?? this.status,
|
||||
@@ -58,6 +62,7 @@ class SessionState extends Equatable {
|
||||
onboardingStep: onboardingStep ?? this.onboardingStep,
|
||||
isMobileDevice: isMobileDevice ?? this.isMobileDevice,
|
||||
isSingleUserMode: isSingleUserMode ?? this.isSingleUserMode,
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -71,6 +76,7 @@ class SessionState extends Equatable {
|
||||
onboardingStep,
|
||||
isMobileDevice,
|
||||
isSingleUserMode,
|
||||
errorMessage,
|
||||
];
|
||||
|
||||
// Helper rapidi per la UI
|
||||
|
||||
@@ -66,10 +66,15 @@ import 'package:get_it/get_it.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
class AppRouter {
|
||||
// 1. CREIAMO LA CHIAVE GLOBALE DEL NAVIGATORE
|
||||
static final GlobalKey<NavigatorState> rootNavigatorKey =
|
||||
GlobalKey<NavigatorState>();
|
||||
static GoRouter createRouter(SessionCubit sessionCubit) {
|
||||
return GoRouter(
|
||||
navigatorKey: rootNavigatorKey,
|
||||
initialLocation: '/',
|
||||
refreshListenable: GoRouterRefreshStream(sessionCubit.stream),
|
||||
|
||||
redirect: (context, state) {
|
||||
final sessionState = sessionCubit.state;
|
||||
final isGoingToLogin = state.matchedLocation == '/login';
|
||||
|
||||
36
lib/core/services/notification_service.dart
Normal file
36
lib/core/services/notification_service.dart
Normal file
@@ -0,0 +1,36 @@
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flux/core/routes/app_router.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
// Chiamala dopo l'autenticazione o nel main()
|
||||
Future<void> setupInteractedMessage() async {
|
||||
// CASO A: L'app era completamente CHIUSA e viene aperta tappando la notifica
|
||||
RemoteMessage? initialMessage = await FirebaseMessaging.instance
|
||||
.getInitialMessage();
|
||||
if (initialMessage != null) {
|
||||
_handleNotificationTap(initialMessage);
|
||||
}
|
||||
|
||||
// CASO B: L'app era in BACKGROUND (minimizzata) e l'utente tappa la notifica
|
||||
FirebaseMessaging.onMessageOpenedApp.listen(_handleNotificationTap);
|
||||
}
|
||||
|
||||
void _handleNotificationTap(RemoteMessage message) {
|
||||
// Verifichiamo che tipo di notifica è e prendiamo l'ID
|
||||
final eventType = message.data['eventType'];
|
||||
final referenceId = message.data['referenceId'];
|
||||
|
||||
if (eventType == 'task_assigned' && referenceId != null) {
|
||||
// Navighiamo verso il form del Task usando la GlobalKey!
|
||||
// Assicuriamoci che il context sia disponibile
|
||||
final context = AppRouter.rootNavigatorKey.currentContext;
|
||||
if (context != null) {
|
||||
// Usiamo .push perché è una rotta di dettaglio fuori dalla shell
|
||||
// Il path è /tasks/form/:id (vedi il tuo AppRouter)
|
||||
context.push('/tasks/form/$referenceId');
|
||||
} else {
|
||||
debugPrint("Attenzione: Context non trovato per il Deep Link!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,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/services/notification_service.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';
|
||||
@@ -61,6 +62,7 @@ void main() async {
|
||||
await Firebase.initializeApp(
|
||||
options: DefaultFirebaseOptions.currentPlatform,
|
||||
);
|
||||
await setupInteractedMessage();
|
||||
} catch (e) {
|
||||
debugPrint('Errore inizializzazione Firebase: $e');
|
||||
}
|
||||
@@ -194,6 +196,13 @@ class _FluxAppState extends State<FluxApp> {
|
||||
return _buildLoadingScreen();
|
||||
}
|
||||
|
||||
if (sessionState.status == SessionStatus.error) {
|
||||
return _buildSessionErrorScreen(
|
||||
state: sessionState,
|
||||
context: context,
|
||||
);
|
||||
}
|
||||
|
||||
return BlocBuilder<ThemeBloc, ThemeState>(
|
||||
builder: (context, themeState) {
|
||||
return MaterialApp.router(
|
||||
@@ -245,6 +254,42 @@ class _FluxAppState extends State<FluxApp> {
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildSessionErrorScreen({
|
||||
required SessionState state,
|
||||
required BuildContext context,
|
||||
}) {
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24.0),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(Icons.wifi_off_rounded, size: 64, color: Colors.grey),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
state.errorMessage ?? 'Errore nella connessione',
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(fontSize: 16),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
ElevatedButton.icon(
|
||||
onPressed: () {
|
||||
// Il ninja riprova a lanciare l'inizializzazione
|
||||
context.read<SessionCubit>().initializeSession();
|
||||
},
|
||||
icon: const Icon(Icons.refresh),
|
||||
label: const Text("Riprova a connetterti"),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// --- IL WIDGET GUARDIANO DEGLI AGGIORNAMENTI ---
|
||||
class GlobalUpdateChecker extends StatefulWidget {
|
||||
final Widget child;
|
||||
|
||||
Reference in New Issue
Block a user