Files
flux/lib/core/blocs/session/session_bloc.dart

102 lines
3.2 KiB
Dart
Raw Normal View History

2026-04-06 10:55:56 +02:00
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:equatable/equatable.dart';
2026-04-07 11:55:01 +02:00
import 'package:flux/core/enums/enums.dart';
2026-04-09 16:01:57 +02:00
import 'package:flux/features/company/models/company_model.dart';
import 'package:flux/features/master_data/store/data/store_repository.dart';
import 'package:flux/features/master_data/store/models/store_model.dart';
2026-04-06 10:55:56 +02:00
import 'package:get_it/get_it.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';
import 'package:supabase_flutter/supabase_flutter.dart';
part 'session_events.dart';
part 'session_state.dart';
class SessionBloc extends Bloc<SessionEvent, SessionState> {
final SupabaseClient _supabase = GetIt.I.get<SupabaseClient>();
final StoreRepository _storeRepository = GetIt.I.get<StoreRepository>();
2026-04-06 10:55:56 +02:00
StreamSubscription<AuthState>? _authSubscription;
2026-04-09 16:01:57 +02:00
SessionBloc() : super(const SessionState(status: SessionStatus.unknown)) {
2026-04-06 10:55:56 +02:00
on<AppStarted>((event, emit) {
// 1. Controlla la sessione attuale al boot
final session = _supabase.auth.currentSession;
if (session != null) {
add(UserChanged(session.user.id));
} else {
add(UserChanged(null));
}
// 2. Ascolta i cambiamenti futuri (login, logout, token scaduto)
_authSubscription = _supabase.auth.onAuthStateChange.listen((data) {
final userId = data.session?.user.id;
add(UserChanged(userId));
});
});
on<UserChanged>((event, emit) async {
if (event.userId == null) {
2026-04-09 16:01:57 +02:00
emit(SessionState(status: SessionStatus.unauthenticated));
2026-04-06 10:55:56 +02:00
return;
}
// 1. Controlla se l'utente ha una Company
2026-04-09 16:01:57 +02:00
final companyJson = await _supabase
2026-04-06 10:55:56 +02:00
.from('company')
.select()
.eq('user_id', event.userId!)
.maybeSingle();
2026-04-09 16:01:57 +02:00
if (companyJson == null) {
emit(
SessionState(
status: SessionStatus.authenticatedNoCompany,
userId: event.userId,
),
);
2026-04-06 10:55:56 +02:00
return;
}
2026-04-09 16:01:57 +02:00
CompanyModel company = CompanyModel.fromJson(companyJson);
2026-04-06 10:55:56 +02:00
// 2. Controlla i negozi
final stores = await _storeRepository.fetchAllCompanyStores(company.id);
2026-04-06 10:55:56 +02:00
if (stores.isEmpty) {
2026-04-09 16:01:57 +02:00
emit(
SessionState(
status: SessionStatus.authenticatedNoStore,
userId: event.userId,
company: company,
),
);
2026-04-06 10:55:56 +02:00
return;
}
// 3. Tutto ok, gestiamo le SharedPreferences per il negozio
final prefs = GetIt.I.get<SharedPreferences>();
String? lastStoreId = prefs.getString(PrefKeys.lastStore.value);
// Se non c'è nelle SharedPreferences, prendi il primo della lista
if (lastStoreId == null || !stores.any((s) => s.id == lastStoreId)) {
lastStoreId = stores.first.id;
2026-04-06 10:55:56 +02:00
await prefs.setString('last_store_id', lastStoreId!);
}
final selectedStore = stores.firstWhere((s) => s.id == lastStoreId);
2026-04-09 16:01:57 +02:00
emit(
SessionState(
status: SessionStatus.ready,
userId: event.userId,
company: company,
selectedStore: selectedStore,
availableStores: stores,
2026-04-09 16:01:57 +02:00
),
);
2026-04-06 10:55:56 +02:00
});
}
@override
Future<void> close() {
_authSubscription?.cancel();
return super.close();
}
}