creazione nuovo negozio
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:equatable/equatable.dart';
|
import 'package:equatable/equatable.dart';
|
||||||
import 'package:flux/core/enums/enums.dart';
|
import 'package:flux/core/enums/enums.dart';
|
||||||
import 'package:flux/features/settings/settings.dart';
|
import 'package:flux/features/company/models/company_model.dart';
|
||||||
|
import 'package:flux/features/store/models/store_model.dart';
|
||||||
import 'package:get_it/get_it.dart';
|
import 'package:get_it/get_it.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
@@ -14,7 +15,7 @@ class SessionBloc extends Bloc<SessionEvent, SessionState> {
|
|||||||
final SupabaseClient _supabase = GetIt.I.get<SupabaseClient>();
|
final SupabaseClient _supabase = GetIt.I.get<SupabaseClient>();
|
||||||
StreamSubscription<AuthState>? _authSubscription;
|
StreamSubscription<AuthState>? _authSubscription;
|
||||||
|
|
||||||
SessionBloc() : super(const SessionState.unknown()) {
|
SessionBloc() : super(const SessionState(status: SessionStatus.unknown)) {
|
||||||
on<AppStarted>((event, emit) {
|
on<AppStarted>((event, emit) {
|
||||||
// 1. Controlla la sessione attuale al boot
|
// 1. Controlla la sessione attuale al boot
|
||||||
final session = _supabase.auth.currentSession;
|
final session = _supabase.auth.currentSession;
|
||||||
@@ -32,33 +33,47 @@ class SessionBloc extends Bloc<SessionEvent, SessionState> {
|
|||||||
});
|
});
|
||||||
|
|
||||||
on<UserChanged>((event, emit) async {
|
on<UserChanged>((event, emit) async {
|
||||||
GetIt.I.get<AppSettings>().setCurrentUserId(event.userId);
|
|
||||||
if (event.userId == null) {
|
if (event.userId == null) {
|
||||||
emit(SessionState.unauthenticated());
|
emit(SessionState(status: SessionStatus.unauthenticated));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 1. Controlla se l'utente ha una Company
|
// 1. Controlla se l'utente ha una Company
|
||||||
final company = await _supabase
|
final companyJson = await _supabase
|
||||||
.from('company')
|
.from('company')
|
||||||
.select()
|
.select()
|
||||||
.eq('user_id', event.userId!)
|
.eq('user_id', event.userId!)
|
||||||
.maybeSingle();
|
.maybeSingle();
|
||||||
|
|
||||||
if (company == null) {
|
if (companyJson == null) {
|
||||||
emit(SessionState.authenticatedNoCompany(event.userId!));
|
emit(
|
||||||
|
SessionState(
|
||||||
|
status: SessionStatus.authenticatedNoCompany,
|
||||||
|
userId: event.userId,
|
||||||
|
),
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
CompanyModel company = CompanyModel.fromJson(companyJson);
|
||||||
|
|
||||||
// 2. Controlla i negozi
|
// 2. Controlla i negozi
|
||||||
final stores = await _supabase
|
final stores = await _supabase
|
||||||
.from('store')
|
.from('store')
|
||||||
.select()
|
.select()
|
||||||
.eq('company_id', company['id']);
|
.eq('company_id', companyJson['id']);
|
||||||
|
|
||||||
if (stores.isEmpty) {
|
if (stores.isEmpty) {
|
||||||
emit(SessionState.authenticatedNoStore(event.userId!, company['id']));
|
emit(
|
||||||
|
SessionState(
|
||||||
|
status: SessionStatus.authenticatedNoStore,
|
||||||
|
userId: event.userId,
|
||||||
|
company: company,
|
||||||
|
),
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
final availableStores = stores
|
||||||
|
.map((s) => StoreModel.fromJson(s))
|
||||||
|
.toList();
|
||||||
|
|
||||||
// 3. Tutto ok, gestiamo le SharedPreferences per il negozio
|
// 3. Tutto ok, gestiamo le SharedPreferences per il negozio
|
||||||
final prefs = GetIt.I.get<SharedPreferences>();
|
final prefs = GetIt.I.get<SharedPreferences>();
|
||||||
@@ -69,6 +84,18 @@ class SessionBloc extends Bloc<SessionEvent, SessionState> {
|
|||||||
lastStoreId = stores.first['id'];
|
lastStoreId = stores.first['id'];
|
||||||
await prefs.setString('last_store_id', lastStoreId!);
|
await prefs.setString('last_store_id', lastStoreId!);
|
||||||
}
|
}
|
||||||
|
final selectedStore = StoreModel.fromJson(
|
||||||
|
stores.firstWhere((s) => s['id'] == lastStoreId),
|
||||||
|
);
|
||||||
|
emit(
|
||||||
|
SessionState(
|
||||||
|
status: SessionStatus.ready,
|
||||||
|
userId: event.userId,
|
||||||
|
company: company,
|
||||||
|
selectedStore: selectedStore,
|
||||||
|
availableStores: availableStores,
|
||||||
|
),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,5 +105,3 @@ class SessionBloc extends Bloc<SessionEvent, SessionState> {
|
|||||||
return super.close();
|
return super.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class SharedPreferencesKeys {}
|
|
||||||
|
|||||||
@@ -11,27 +11,41 @@ enum SessionStatus {
|
|||||||
class SessionState extends Equatable {
|
class SessionState extends Equatable {
|
||||||
final SessionStatus status;
|
final SessionStatus status;
|
||||||
final String? userId;
|
final String? userId;
|
||||||
final String? companyId;
|
final CompanyModel? company;
|
||||||
|
final StoreModel? selectedStore;
|
||||||
|
final List<StoreModel> availableStores; // Utile per uno switcher in futuro
|
||||||
|
|
||||||
const SessionState._({
|
const SessionState({
|
||||||
this.status = SessionStatus.unknown,
|
this.status = SessionStatus.unknown,
|
||||||
this.userId,
|
this.userId,
|
||||||
this.companyId,
|
this.company,
|
||||||
|
this.selectedStore,
|
||||||
|
this.availableStores = const [],
|
||||||
});
|
});
|
||||||
const SessionState.unknown() : this._();
|
|
||||||
const SessionState.unauthenticated()
|
|
||||||
: this._(status: SessionStatus.unauthenticated);
|
|
||||||
const SessionState.authenticatedNoCompany(String userId)
|
|
||||||
: this._(status: SessionStatus.authenticatedNoCompany, userId: userId);
|
|
||||||
const SessionState.authenticatedNoStore(String userId, String companyId)
|
|
||||||
: this._(
|
|
||||||
status: SessionStatus.authenticatedNoStore,
|
|
||||||
userId: userId,
|
|
||||||
companyId: companyId,
|
|
||||||
);
|
|
||||||
const SessionState.ready(String userId)
|
|
||||||
: this._(status: SessionStatus.ready, userId: userId);
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<Object?> get props => [status, userId];
|
List<Object?> get props => [
|
||||||
|
status,
|
||||||
|
userId,
|
||||||
|
company,
|
||||||
|
selectedStore,
|
||||||
|
availableStores,
|
||||||
|
];
|
||||||
|
|
||||||
|
// copyWith per aggiornare solo un pezzo (es. quando cambi negozio)
|
||||||
|
SessionState copyWith({
|
||||||
|
SessionStatus? status,
|
||||||
|
String? userId,
|
||||||
|
CompanyModel? company,
|
||||||
|
StoreModel? selectedStore,
|
||||||
|
List<StoreModel>? availableStores,
|
||||||
|
}) {
|
||||||
|
return SessionState(
|
||||||
|
status: status ?? this.status,
|
||||||
|
userId: userId ?? this.userId,
|
||||||
|
company: company ?? this.company,
|
||||||
|
selectedStore: selectedStore ?? this.selectedStore,
|
||||||
|
availableStores: availableStores ?? this.availableStores,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ class _CreateCompanyScreenState extends State<CreateCompanyScreen> {
|
|||||||
void _onSave() {
|
void _onSave() {
|
||||||
if (_formKey.currentState!.validate()) {
|
if (_formKey.currentState!.validate()) {
|
||||||
// Recuperiamo l'ID utente attuale da Supabase o dal SessionBloc
|
// Recuperiamo l'ID utente attuale da Supabase o dal SessionBloc
|
||||||
final userId = GetIt.I.get<AppSettings>().currentUserId!;
|
final userId = context.read<SessionBloc>().state.userId!;
|
||||||
|
|
||||||
final company = CompanyModel(
|
final company = CompanyModel(
|
||||||
userId: userId,
|
userId: userId,
|
||||||
@@ -91,7 +91,7 @@ class _CreateCompanyScreenState extends State<CreateCompanyScreen> {
|
|||||||
listener: (context, state) {
|
listener: (context, state) {
|
||||||
if (state.status == CompanyStatus.success && state.company != null) {
|
if (state.status == CompanyStatus.success && state.company != null) {
|
||||||
// 1. Aggiorniamo la singleton con i dati reali (ID incluso)
|
// 1. Aggiorniamo la singleton con i dati reali (ID incluso)
|
||||||
GetIt.I.get<AppSettings>().setCurrentCompany(state.company);
|
//GetIt.I.get<AppSettings>().setCurrentCompany(state.company);
|
||||||
|
|
||||||
// 2. Notifichiamo il SessionBloc per cambiare pagina
|
// 2. Notifichiamo il SessionBloc per cambiare pagina
|
||||||
context.read<SessionBloc>().add(AppStarted());
|
context.read<SessionBloc>().add(AppStarted());
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
import 'package:flux/features/company/models/company_model.dart';
|
import 'package:flux/core/enums/enums.dart';
|
||||||
import 'package:get_it/get_it.dart';
|
import 'package:get_it/get_it.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
class AppSettings {
|
class AppSettings {
|
||||||
late String _themeModeSetting;
|
late String _themeModeSetting;
|
||||||
late String? _currentUserId;
|
|
||||||
late SharedPreferences _prefs;
|
late SharedPreferences _prefs;
|
||||||
late CompanyModel? _currentCompany;
|
|
||||||
|
|
||||||
// Singleton
|
// Singleton
|
||||||
|
|
||||||
@@ -25,18 +23,6 @@ class AppSettings {
|
|||||||
|
|
||||||
void setThemeModeSetting(String value) {
|
void setThemeModeSetting(String value) {
|
||||||
_themeModeSetting = value;
|
_themeModeSetting = value;
|
||||||
_prefs.setString('theme', value);
|
_prefs.setString(PrefKeys.theme.value, value);
|
||||||
}
|
|
||||||
|
|
||||||
String? get currentUserId => _currentUserId;
|
|
||||||
|
|
||||||
void setCurrentUserId(String? value) {
|
|
||||||
_currentUserId = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
CompanyModel? get currentCompany => _currentCompany;
|
|
||||||
|
|
||||||
void setCurrentCompany(CompanyModel? value) {
|
|
||||||
_currentCompany = value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ class _CreateStoreScreenState extends State<CreateStoreScreen> {
|
|||||||
|
|
||||||
/// Funzione magica per copiare i dati dall'azienda salvata in GetIt
|
/// Funzione magica per copiare i dati dall'azienda salvata in GetIt
|
||||||
void _useCompanyAddress() {
|
void _useCompanyAddress() {
|
||||||
final company = _settings.currentCompany;
|
final company = context.read<SessionBloc>().state.company;
|
||||||
if (company != null) {
|
if (company != null) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_indirizzoController.text = company.indirizzo;
|
_indirizzoController.text = company.indirizzo;
|
||||||
@@ -61,18 +61,18 @@ class _CreateStoreScreenState extends State<CreateStoreScreen> {
|
|||||||
|
|
||||||
void _onSave() {
|
void _onSave() {
|
||||||
if (_formKey.currentState!.validate()) {
|
if (_formKey.currentState!.validate()) {
|
||||||
final settings = GetIt.I<AppSettings>();
|
final company = context.read<SessionBloc>().state.company;
|
||||||
|
|
||||||
if (_settings.currentCompany?.id == null) {
|
if (company == null) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
const SnackBar(content: Text('Errore: ID Azienda non trovato')),
|
const SnackBar(content: Text('Errore: Azienda non trovata')),
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final store = StoreModel(
|
final store = StoreModel(
|
||||||
nome: _nomeController.text.trim(),
|
nome: _nomeController.text.trim(),
|
||||||
companyId: _settings.currentCompany!.id,
|
companyId: company.id,
|
||||||
indirizzo: _indirizzoController.text.trim(),
|
indirizzo: _indirizzoController.text.trim(),
|
||||||
cap: _capController.text.trim(),
|
cap: _capController.text.trim(),
|
||||||
comune: _comuneController.text.trim(),
|
comune: _comuneController.text.trim(),
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import 'package:flux/features/auth/ui/auth_screen.dart';
|
|||||||
import 'package:flux/features/company/bloc/company_bloc.dart';
|
import 'package:flux/features/company/bloc/company_bloc.dart';
|
||||||
import 'package:flux/features/company/data/company_repository.dart';
|
import 'package:flux/features/company/data/company_repository.dart';
|
||||||
import 'package:flux/features/company/ui/create_company_screen.dart';
|
import 'package:flux/features/company/ui/create_company_screen.dart';
|
||||||
|
import 'package:flux/features/store/bloc/store_bloc.dart';
|
||||||
import 'package:flux/features/store/data/store_repository.dart';
|
import 'package:flux/features/store/data/store_repository.dart';
|
||||||
import 'package:flux/features/store/ui/create_store_screen.dart';
|
import 'package:flux/features/store/ui/create_store_screen.dart';
|
||||||
import 'package:flux/ui/home_screen.dart';
|
import 'package:flux/ui/home_screen.dart';
|
||||||
@@ -31,6 +32,7 @@ void main() async {
|
|||||||
),
|
),
|
||||||
BlocProvider<AuthBloc>(create: (context) => AuthBloc()),
|
BlocProvider<AuthBloc>(create: (context) => AuthBloc()),
|
||||||
BlocProvider<CompanyBloc>(create: (context) => CompanyBloc()),
|
BlocProvider<CompanyBloc>(create: (context) => CompanyBloc()),
|
||||||
|
BlocProvider<StoreBloc>(create: (context) => StoreBloc()),
|
||||||
],
|
],
|
||||||
child: const FluxApp(),
|
child: const FluxApp(),
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user