fix-general (#6)
Reviewed-on: http://catelliub.zapto.org:3000/brontomark/flux/pulls/6 Co-authored-by: Mark M2 Macbook <marco@catelli.it> Co-committed-by: Mark M2 Macbook <marco@catelli.it>
This commit is contained in:
@@ -2,6 +2,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flux/core/enums/enums.dart';
|
||||
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';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
@@ -13,6 +14,7 @@ part 'session_state.dart';
|
||||
|
||||
class SessionBloc extends Bloc<SessionEvent, SessionState> {
|
||||
final SupabaseClient _supabase = GetIt.I.get<SupabaseClient>();
|
||||
final StoreRepository _storeRepository = GetIt.I.get<StoreRepository>();
|
||||
StreamSubscription<AuthState>? _authSubscription;
|
||||
|
||||
SessionBloc() : super(const SessionState(status: SessionStatus.unknown)) {
|
||||
@@ -56,10 +58,7 @@ class SessionBloc extends Bloc<SessionEvent, SessionState> {
|
||||
CompanyModel company = CompanyModel.fromJson(companyJson);
|
||||
|
||||
// 2. Controlla i negozi
|
||||
final stores = await _supabase
|
||||
.from('store')
|
||||
.select()
|
||||
.eq('company_id', companyJson['id']);
|
||||
final stores = await _storeRepository.fetchAllCompanyStores(company.id);
|
||||
|
||||
if (stores.isEmpty) {
|
||||
emit(
|
||||
@@ -71,27 +70,24 @@ class SessionBloc extends Bloc<SessionEvent, SessionState> {
|
||||
);
|
||||
return;
|
||||
}
|
||||
final availableStores = stores.map((s) => StoreModel.fromMap(s)).toList();
|
||||
|
||||
// 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'];
|
||||
if (lastStoreId == null || !stores.any((s) => s.id == lastStoreId)) {
|
||||
lastStoreId = stores.first.id;
|
||||
await prefs.setString('last_store_id', lastStoreId!);
|
||||
}
|
||||
final selectedStore = StoreModel.fromMap(
|
||||
stores.firstWhere((s) => s['id'] == lastStoreId),
|
||||
);
|
||||
final selectedStore = stores.firstWhere((s) => s.id == lastStoreId);
|
||||
emit(
|
||||
SessionState(
|
||||
status: SessionStatus.ready,
|
||||
userId: event.userId,
|
||||
company: company,
|
||||
selectedStore: selectedStore,
|
||||
availableStores: availableStores,
|
||||
availableStores: stores,
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flux/core/theme/theme.dart';
|
||||
|
||||
class FluxTextField extends StatelessWidget {
|
||||
class FluxTextField extends StatefulWidget {
|
||||
final String label;
|
||||
final IconData icon;
|
||||
final bool isPassword;
|
||||
@@ -30,20 +30,38 @@ class FluxTextField extends StatelessWidget {
|
||||
this.maxLength,
|
||||
});
|
||||
|
||||
@override
|
||||
State<FluxTextField> createState() => _FluxTextFieldState();
|
||||
}
|
||||
|
||||
class _FluxTextFieldState extends State<FluxTextField> {
|
||||
late bool _obscureText;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_obscureText = widget.isPassword;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return TextField(
|
||||
controller: controller,
|
||||
obscureText: isPassword,
|
||||
keyboardType: keyboardType,
|
||||
autofocus: autoFocus,
|
||||
minLines: minLines,
|
||||
controller: widget.controller,
|
||||
obscureText: _obscureText,
|
||||
enableSuggestions: !widget.isPassword,
|
||||
autocorrect: !widget.isPassword,
|
||||
keyboardType: widget.keyboardType,
|
||||
autofocus: widget.autoFocus,
|
||||
minLines: widget.minLines,
|
||||
// Se minLines è impostato, maxLines deve essere almeno uguale o null (espandibile)
|
||||
maxLines: minLines != null ? null : maxLines,
|
||||
maxLines: widget.minLines != null ? null : widget.maxLines,
|
||||
style: TextStyle(color: context.primaryText),
|
||||
decoration: InputDecoration(
|
||||
prefixIcon: Icon(icon, color: context.accent.withValues(alpha: 0.6)),
|
||||
labelText: label,
|
||||
prefixIcon: Icon(
|
||||
widget.icon,
|
||||
color: context.accent.withValues(alpha: 0.6),
|
||||
),
|
||||
labelText: widget.label,
|
||||
labelStyle: TextStyle(color: context.secondaryText, fontSize: 14),
|
||||
filled: true,
|
||||
fillColor: context.surface.withValues(alpha: 0.5),
|
||||
@@ -61,10 +79,25 @@ class FluxTextField extends StatelessWidget {
|
||||
horizontal: 16,
|
||||
vertical: 16,
|
||||
),
|
||||
suffixIcon: widget.isPassword
|
||||
? IconButton(
|
||||
icon: Icon(
|
||||
// Cambiamo icona in base allo stato
|
||||
_obscureText ? Icons.visibility_off : Icons.visibility,
|
||||
color: Colors.grey,
|
||||
),
|
||||
onPressed: () {
|
||||
// Quando l'utente clicca, invertiamo lo stato e ridisegniamo
|
||||
setState(() {
|
||||
_obscureText = !_obscureText;
|
||||
});
|
||||
},
|
||||
)
|
||||
: null, // Se non è una password, niente icona
|
||||
),
|
||||
onSubmitted: onSubmitted,
|
||||
onChanged: onChanged,
|
||||
maxLength: maxLength,
|
||||
onSubmitted: widget.onSubmitted,
|
||||
onChanged: widget.onChanged,
|
||||
maxLength: widget.maxLength,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user