feat(app): gestione server preferiti, disconnessione inattività e avvio/risveglio su selezione PC
This commit is contained in:
1 parent
87539c0ac5
commit
91f55dc9a2
20 files changed
+1205
-71
No files matched your search
@@ -15,6 +15,9 @@ class AppConstants {
|
||||
static const String keyPort = 'server_port';
|
||||
static const String keyPin = 'server_pin';
|
||||
static const String keyServerName = 'server_name';
|
||||
static const String keyFavoriteServers = 'favorite_servers_list';
|
||||
static const String keyAutoConnect = 'auto_connect_enabled';
|
||||
static const String keyInactivityTimeout = 'inactivity_timeout_minutes';
|
||||
static const String keySuffix = 'server_suffix';
|
||||
static const String keyContinuous = 'continuous_mode';
|
||||
static const String keyVibrate = 'vibrate_enabled';
|
||||
@@ -25,5 +28,7 @@ class AppConstants {
|
||||
static const String suffixEnter = 'enter';
|
||||
static const String suffixTab = 'tab';
|
||||
static const String suffixNone = 'none';
|
||||
}
|
||||
|
||||
// Default timeout in minutes
|
||||
static const int defaultInactivityTimeoutMinutes = 5;
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'dart:convert';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../../../../core/constants/app_constants.dart';
|
||||
import '../../domain/entities/server_connection_info.dart';
|
||||
@@ -29,5 +30,58 @@ class ConnectionLocalDataSource {
|
||||
await _prefs.setString(AppConstants.keyServerName, info.serverName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<ServerConnectionInfo> loadFavoriteServers() {
|
||||
final rawList = _prefs.getStringList(AppConstants.keyFavoriteServers) ?? [];
|
||||
final List<ServerConnectionInfo> favorites = [];
|
||||
|
||||
for (final item in rawList) {
|
||||
try {
|
||||
final Map<String, dynamic> json = jsonDecode(item);
|
||||
favorites.add(ServerConnectionInfo.fromJson(json));
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
// Se la lista è vuota ma abbiamo un server precedentemente configurato, aggiungiamolo come primo preferito
|
||||
if (favorites.isEmpty) {
|
||||
final current = loadConnectionInfo();
|
||||
if (current.isConfigured) {
|
||||
favorites.add(current);
|
||||
saveFavoriteServersList(favorites);
|
||||
}
|
||||
}
|
||||
|
||||
return favorites;
|
||||
}
|
||||
|
||||
Future<void> saveFavoriteServersList(List<ServerConnectionInfo> list) async {
|
||||
final encoded = list.map((e) => jsonEncode(e.toJson())).toList();
|
||||
await _prefs.setStringList(AppConstants.keyFavoriteServers, encoded);
|
||||
}
|
||||
|
||||
Future<void> saveFavoriteServer(ServerConnectionInfo server) async {
|
||||
if (!server.isConfigured) return;
|
||||
final favorites = loadFavoriteServers();
|
||||
|
||||
final existingIndex = favorites.indexWhere(
|
||||
(s) =>
|
||||
(server.effectiveId.isNotEmpty && s.effectiveId == server.effectiveId) ||
|
||||
(s.ip == server.ip && s.port == server.port),
|
||||
);
|
||||
|
||||
if (existingIndex >= 0) {
|
||||
favorites[existingIndex] = server;
|
||||
} else {
|
||||
favorites.add(server);
|
||||
}
|
||||
|
||||
await saveFavoriteServersList(favorites);
|
||||
}
|
||||
|
||||
Future<void> removeFavoriteServer(String id) async {
|
||||
final favorites = loadFavoriteServers();
|
||||
favorites.removeWhere((s) =>
|
||||
s.effectiveId == id || s.id == id || '${s.ip}:${s.port}' == id);
|
||||
await saveFavoriteServersList(favorites);
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,9 @@ class ConnectionRepositoryImpl implements ConnectionRepository {
|
||||
_serverNameSub = remoteDataSource.serverNameStream.listen((name) async {
|
||||
final current = localDataSource.loadConnectionInfo();
|
||||
if (current.serverName != name) {
|
||||
await localDataSource.saveConnectionInfo(current.copyWith(serverName: name));
|
||||
final updated = current.copyWith(serverName: name);
|
||||
await localDataSource.saveConnectionInfo(updated);
|
||||
await localDataSource.saveFavoriteServer(updated);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -43,11 +45,27 @@ class ConnectionRepositoryImpl implements ConnectionRepository {
|
||||
@override
|
||||
Future<void> saveConnection(ServerConnectionInfo info) async {
|
||||
await localDataSource.saveConnectionInfo(info);
|
||||
await localDataSource.saveFavoriteServer(info);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<ServerConnectionInfo>> loadFavoriteServers() async {
|
||||
return localDataSource.loadFavoriteServers();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> saveFavoriteServer(ServerConnectionInfo server) async {
|
||||
await localDataSource.saveFavoriteServer(server);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> removeFavoriteServer(String id) async {
|
||||
await localDataSource.removeFavoriteServer(id);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> connect(ServerConnectionInfo info) async {
|
||||
await localDataSource.saveConnectionInfo(info);
|
||||
await saveConnection(info);
|
||||
await remoteDataSource.connect(info);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,31 +1,55 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class ServerConnectionInfo extends Equatable {
|
||||
final String id;
|
||||
final String ip;
|
||||
final int port;
|
||||
final String pin;
|
||||
final String serverName;
|
||||
|
||||
const ServerConnectionInfo({
|
||||
this.id = '',
|
||||
this.ip = '',
|
||||
this.port = 8765,
|
||||
this.pin = '',
|
||||
this.serverName = '',
|
||||
});
|
||||
|
||||
String get effectiveId =>
|
||||
id.isNotEmpty ? id : (ip.isNotEmpty ? '$ip:$port' : '');
|
||||
|
||||
String get displayName => serverName.isNotEmpty
|
||||
? serverName
|
||||
: (ip.isNotEmpty ? ip : 'Nessun PC');
|
||||
|
||||
bool get isConfigured => ip.isNotEmpty;
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'id': effectiveId,
|
||||
'ip': ip,
|
||||
'port': port,
|
||||
'pin': pin,
|
||||
'serverName': serverName,
|
||||
};
|
||||
|
||||
factory ServerConnectionInfo.fromJson(Map<String, dynamic> json) =>
|
||||
ServerConnectionInfo(
|
||||
id: json['id'] as String? ?? '',
|
||||
ip: json['ip'] as String? ?? '',
|
||||
port: json['port'] as int? ?? 8765,
|
||||
pin: json['pin'] as String? ?? '',
|
||||
serverName: json['serverName'] as String? ?? '',
|
||||
);
|
||||
|
||||
ServerConnectionInfo copyWith({
|
||||
String? id,
|
||||
String? ip,
|
||||
int? port,
|
||||
String? pin,
|
||||
String? serverName,
|
||||
}) {
|
||||
return ServerConnectionInfo(
|
||||
id: id ?? this.id,
|
||||
ip: ip ?? this.ip,
|
||||
port: port ?? this.port,
|
||||
pin: pin ?? this.pin,
|
||||
@@ -34,6 +58,5 @@ class ServerConnectionInfo extends Equatable {
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [ip, port, pin, serverName];
|
||||
List<Object?> get props => [effectiveId, ip, port, pin, serverName];
|
||||
}
|
||||
|
||||
@@ -16,6 +16,10 @@ abstract class ConnectionRepository {
|
||||
Future<ServerConnectionInfo> loadSavedConnection();
|
||||
Future<void> saveConnection(ServerConnectionInfo info);
|
||||
|
||||
Future<List<ServerConnectionInfo>> loadFavoriteServers();
|
||||
Future<void> saveFavoriteServer(ServerConnectionInfo server);
|
||||
Future<void> removeFavoriteServer(String id);
|
||||
|
||||
Future<void> connect(ServerConnectionInfo info);
|
||||
Future<void> disconnect();
|
||||
|
||||
@@ -27,8 +31,8 @@ abstract class ConnectionRepository {
|
||||
int? port,
|
||||
});
|
||||
|
||||
Future<List<DiscoveredServer>> discoverServers({Duration timeout = const Duration(seconds: 3)});
|
||||
Future<List<DiscoveredServer>> discoverServers(
|
||||
{Duration timeout = const Duration(seconds: 3)});
|
||||
|
||||
void dispose();
|
||||
}
|
||||
|
||||
@@ -9,7 +9,9 @@ class ConnectionCubit extends Cubit<ServerConnectionState> {
|
||||
StreamSubscription? _statusSubscription;
|
||||
StreamSubscription? _messageSubscription;
|
||||
Timer? _reconnectTimer;
|
||||
Timer? _inactivityTimer;
|
||||
bool _userExplicitlyDisconnected = false;
|
||||
int _activeTimeoutMinutes = 0;
|
||||
|
||||
ConnectionCubit(this._repository) : super(const ServerConnectionState()) {
|
||||
_statusSubscription = _repository.statusStream.listen(_onStatusChanged);
|
||||
@@ -17,11 +19,21 @@ class ConnectionCubit extends Cubit<ServerConnectionState> {
|
||||
}
|
||||
|
||||
void _onStatusChanged(ConnectionStatus status) {
|
||||
emit(state.copyWith(status: status));
|
||||
if (status == ConnectionStatus.disconnected &&
|
||||
!_userExplicitlyDisconnected &&
|
||||
state.info.isConfigured) {
|
||||
_scheduleReconnect();
|
||||
emit(state.copyWith(
|
||||
status: status,
|
||||
isDisconnectedByInactivity:
|
||||
status == ConnectionStatus.connected ? false : state.isDisconnectedByInactivity,
|
||||
));
|
||||
|
||||
if (status == ConnectionStatus.connected) {
|
||||
_startInactivityTimer();
|
||||
} else {
|
||||
_inactivityTimer?.cancel();
|
||||
if (status == ConnectionStatus.disconnected &&
|
||||
!_userExplicitlyDisconnected &&
|
||||
state.info.isConfigured) {
|
||||
_scheduleReconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,41 +44,100 @@ class ConnectionCubit extends Cubit<ServerConnectionState> {
|
||||
void _scheduleReconnect() {
|
||||
_reconnectTimer?.cancel();
|
||||
_reconnectTimer = Timer(const Duration(seconds: 4), () {
|
||||
if (!_userExplicitlyDisconnected && !state.isConnected && state.info.isConfigured) {
|
||||
connect(state.info);
|
||||
if (!_userExplicitlyDisconnected &&
|
||||
!state.isConnected &&
|
||||
state.info.isConfigured) {
|
||||
connect(state.info, timeoutMinutes: _activeTimeoutMinutes);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> initialize() async {
|
||||
Future<void> initialize({bool autoConnect = false, int timeoutMinutes = 5}) async {
|
||||
_activeTimeoutMinutes = timeoutMinutes;
|
||||
final info = await _repository.loadSavedConnection();
|
||||
emit(state.copyWith(info: info));
|
||||
if (info.isConfigured) {
|
||||
await connect(info);
|
||||
final favorites = await _repository.loadFavoriteServers();
|
||||
|
||||
emit(state.copyWith(
|
||||
info: info,
|
||||
favorites: favorites,
|
||||
));
|
||||
|
||||
if (autoConnect && info.isConfigured) {
|
||||
await connect(info, timeoutMinutes: timeoutMinutes);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> connect(ServerConnectionInfo info) async {
|
||||
Future<void> connect(ServerConnectionInfo info, {int timeoutMinutes = 5}) async {
|
||||
_userExplicitlyDisconnected = false;
|
||||
_activeTimeoutMinutes = timeoutMinutes;
|
||||
_reconnectTimer?.cancel();
|
||||
emit(state.copyWith(info: info, status: ConnectionStatus.connecting));
|
||||
_inactivityTimer?.cancel();
|
||||
|
||||
emit(state.copyWith(
|
||||
info: info,
|
||||
status: ConnectionStatus.connecting,
|
||||
isDisconnectedByInactivity: false,
|
||||
));
|
||||
|
||||
await _repository.connect(info);
|
||||
await reloadFavorites();
|
||||
}
|
||||
|
||||
Future<void> disconnect() async {
|
||||
Future<void> disconnect({bool byInactivity = false}) async {
|
||||
_userExplicitlyDisconnected = true;
|
||||
_reconnectTimer?.cancel();
|
||||
_inactivityTimer?.cancel();
|
||||
await _repository.disconnect();
|
||||
|
||||
emit(state.copyWith(
|
||||
status: ConnectionStatus.disconnected,
|
||||
statusMessage: 'Disconnesso manualmente',
|
||||
statusMessage: byInactivity
|
||||
? 'Disconnesso per inattività ($_activeTimeoutMinutes min)'
|
||||
: 'Disconnesso manualmente',
|
||||
isDisconnectedByInactivity: byInactivity,
|
||||
));
|
||||
}
|
||||
|
||||
void onUserActivity(int timeoutMinutes) {
|
||||
_activeTimeoutMinutes = timeoutMinutes;
|
||||
if (state.isConnected) {
|
||||
_startInactivityTimer();
|
||||
}
|
||||
}
|
||||
|
||||
void _startInactivityTimer() {
|
||||
_inactivityTimer?.cancel();
|
||||
if (_activeTimeoutMinutes > 0 && state.isConnected) {
|
||||
_inactivityTimer = Timer(Duration(minutes: _activeTimeoutMinutes), () {
|
||||
if (state.isConnected) {
|
||||
disconnect(byInactivity: true);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> reloadFavorites() async {
|
||||
final favorites = await _repository.loadFavoriteServers();
|
||||
emit(state.copyWith(favorites: favorites));
|
||||
}
|
||||
|
||||
Future<void> saveFavorite(ServerConnectionInfo server) async {
|
||||
await _repository.saveFavoriteServer(server);
|
||||
await reloadFavorites();
|
||||
}
|
||||
|
||||
Future<void> removeFavorite(String id) async {
|
||||
await _repository.removeFavoriteServer(id);
|
||||
await reloadFavorites();
|
||||
}
|
||||
|
||||
Future<bool> sendCode({
|
||||
required String code,
|
||||
required String suffix,
|
||||
}) async {
|
||||
// Notify activity to reset inactivity timer
|
||||
onUserActivity(_activeTimeoutMinutes);
|
||||
|
||||
return await _repository.sendCode(
|
||||
code: code,
|
||||
suffix: suffix,
|
||||
@@ -79,6 +150,7 @@ class ConnectionCubit extends Cubit<ServerConnectionState> {
|
||||
@override
|
||||
Future<void> close() {
|
||||
_reconnectTimer?.cancel();
|
||||
_inactivityTimer?.cancel();
|
||||
_statusSubscription?.cancel();
|
||||
_messageSubscription?.cancel();
|
||||
_repository.dispose();
|
||||
|
||||
@@ -6,11 +6,15 @@ class ServerConnectionState extends Equatable {
|
||||
final ConnectionStatus status;
|
||||
final String statusMessage;
|
||||
final ServerConnectionInfo info;
|
||||
final List<ServerConnectionInfo> favorites;
|
||||
final bool isDisconnectedByInactivity;
|
||||
|
||||
const ServerConnectionState({
|
||||
this.status = ConnectionStatus.disconnected,
|
||||
this.statusMessage = 'Non connesso',
|
||||
this.info = const ServerConnectionInfo(),
|
||||
this.favorites = const [],
|
||||
this.isDisconnectedByInactivity = false,
|
||||
});
|
||||
|
||||
bool get isConnected => status == ConnectionStatus.connected;
|
||||
@@ -20,14 +24,25 @@ class ServerConnectionState extends Equatable {
|
||||
ConnectionStatus? status,
|
||||
String? statusMessage,
|
||||
ServerConnectionInfo? info,
|
||||
List<ServerConnectionInfo>? favorites,
|
||||
bool? isDisconnectedByInactivity,
|
||||
}) {
|
||||
return ServerConnectionState(
|
||||
status: status ?? this.status,
|
||||
statusMessage: statusMessage ?? this.statusMessage,
|
||||
info: info ?? this.info,
|
||||
favorites: favorites ?? this.favorites,
|
||||
isDisconnectedByInactivity:
|
||||
isDisconnectedByInactivity ?? this.isDisconnectedByInactivity,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [status, statusMessage, info];
|
||||
List<Object?> get props => [
|
||||
status,
|
||||
statusMessage,
|
||||
info,
|
||||
favorites,
|
||||
isDisconnectedByInactivity,
|
||||
];
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../../../core/theme/app_theme.dart';
|
||||
import '../../domain/entities/server_connection_info.dart';
|
||||
import '../../domain/repositories/connection_repository.dart';
|
||||
import '../../../settings/presentation/cubit/settings_cubit.dart';
|
||||
import '../cubit/connection_cubit.dart';
|
||||
import '../cubit/connection_state.dart';
|
||||
import '../cubit/discovery_cubit.dart';
|
||||
@@ -17,6 +18,7 @@ class ConnectScreen extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _ConnectScreenState extends State<ConnectScreen> {
|
||||
late final TextEditingController _nameController;
|
||||
late final TextEditingController _ipController;
|
||||
late final TextEditingController _portController;
|
||||
late final TextEditingController _pinController;
|
||||
@@ -25,6 +27,7 @@ class _ConnectScreenState extends State<ConnectScreen> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
final info = context.read<ConnectionCubit>().state.info;
|
||||
_nameController = TextEditingController(text: info.serverName);
|
||||
_ipController = TextEditingController(text: info.ip);
|
||||
_portController = TextEditingController(text: info.port.toString());
|
||||
_pinController = TextEditingController(text: info.pin);
|
||||
@@ -35,6 +38,7 @@ class _ConnectScreenState extends State<ConnectScreen> {
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_nameController.dispose();
|
||||
_ipController.dispose();
|
||||
_portController.dispose();
|
||||
_pinController.dispose();
|
||||
@@ -42,6 +46,7 @@ class _ConnectScreenState extends State<ConnectScreen> {
|
||||
}
|
||||
|
||||
void _onManualConnect() {
|
||||
final name = _nameController.text.trim();
|
||||
final ip = _ipController.text.trim();
|
||||
final port = int.tryParse(_portController.text.trim()) ?? 8765;
|
||||
final pin = _pinController.text.trim();
|
||||
@@ -60,10 +65,19 @@ class _ConnectScreenState extends State<ConnectScreen> {
|
||||
ip: ip,
|
||||
port: port,
|
||||
pin: pin,
|
||||
serverName: '',
|
||||
serverName: name.isNotEmpty ? name : 'PC Negozio',
|
||||
);
|
||||
|
||||
context.read<ConnectionCubit>().connect(info);
|
||||
final timeout = context
|
||||
.read<SettingsCubit>()
|
||||
.state
|
||||
.settings
|
||||
.inactivityTimeoutMinutes;
|
||||
|
||||
context
|
||||
.read<ConnectionCubit>()
|
||||
.connect(info, timeoutMinutes: timeout);
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Connessione a $ip:$port in corso...'),
|
||||
@@ -75,13 +89,17 @@ class _ConnectScreenState extends State<ConnectScreen> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final timeout =
|
||||
context.watch<SettingsCubit>().state.settings.inactivityTimeoutMinutes;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Connessione PC'),
|
||||
title: const Text('Connessione Postazioni PC'),
|
||||
),
|
||||
body: BlocConsumer<ConnectionCubit, ServerConnectionState>(
|
||||
listener: (context, state) {
|
||||
if (state.info.ip != _ipController.text && state.info.ip.isNotEmpty) {
|
||||
_nameController.text = state.info.serverName;
|
||||
_ipController.text = state.info.ip;
|
||||
_portController.text = state.info.port.toString();
|
||||
_pinController.text = state.info.pin;
|
||||
@@ -89,6 +107,7 @@ class _ConnectScreenState extends State<ConnectScreen> {
|
||||
},
|
||||
builder: (context, connectionState) {
|
||||
final isConnected = connectionState.isConnected;
|
||||
final favorites = connectionState.favorites;
|
||||
|
||||
return ListView(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
|
||||
@@ -102,7 +121,8 @@ class _ConnectScreenState extends State<ConnectScreen> {
|
||||
style: FilledButton.styleFrom(
|
||||
backgroundColor: AppTheme.primary,
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(14)),
|
||||
),
|
||||
icon: const Icon(Icons.qr_code_scanner_rounded, size: 22),
|
||||
label: const Text(
|
||||
@@ -119,6 +139,95 @@ class _ConnectScreenState extends State<ConnectScreen> {
|
||||
),
|
||||
const SizedBox(height: 28),
|
||||
|
||||
// Sezione Postazioni Preferite Salve
|
||||
if (favorites.isNotEmpty) ...[
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text(
|
||||
'Postazioni Salvate nel Negozio',
|
||||
style: TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w700,
|
||||
color: AppTheme.darkTextPrimary,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'${favorites.length} preferiti',
|
||||
style: const TextStyle(
|
||||
fontSize: 12, color: AppTheme.darkTextSecondary),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Column(
|
||||
children: favorites.map((fav) {
|
||||
final isCurrent =
|
||||
isConnected && connectionState.info.ip == fav.ip;
|
||||
return Card(
|
||||
margin: const EdgeInsets.only(bottom: 8),
|
||||
child: ListTile(
|
||||
leading: Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: (isCurrent
|
||||
? AppTheme.success
|
||||
: AppTheme.primary)
|
||||
.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Icon(
|
||||
Icons.computer_rounded,
|
||||
color: isCurrent
|
||||
? AppTheme.success
|
||||
: AppTheme.primary,
|
||||
size: 20,
|
||||
),
|
||||
),
|
||||
title: Text(
|
||||
fav.displayName,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
subtitle: Text(
|
||||
'${fav.ip}:${fav.port} ${fav.pin.isNotEmpty ? "• PIN" : ""}',
|
||||
style: const TextStyle(
|
||||
fontFamily: 'monospace', fontSize: 11),
|
||||
),
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.delete_outline_rounded,
|
||||
size: 18),
|
||||
color: AppTheme.darkTextSecondary,
|
||||
onPressed: () {
|
||||
context
|
||||
.read<ConnectionCubit>()
|
||||
.removeFavorite(fav.effectiveId);
|
||||
},
|
||||
),
|
||||
FilledButton(
|
||||
style: FilledButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 12, vertical: 6),
|
||||
textStyle: const TextStyle(fontSize: 11),
|
||||
),
|
||||
onPressed: () {
|
||||
context.read<ConnectionCubit>().connect(fav,
|
||||
timeoutMinutes: timeout);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
child: Text(isCurrent ? 'Attivo' : 'Connetti'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
],
|
||||
|
||||
// Method 2: Wi-Fi Discovery
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
@@ -138,13 +247,15 @@ class _ConnectScreenState extends State<ConnectScreen> {
|
||||
? const SizedBox(
|
||||
width: 18,
|
||||
height: 18,
|
||||
child: CircularProgressIndicator(strokeWidth: 2, color: AppTheme.primary),
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2, color: AppTheme.primary),
|
||||
)
|
||||
: const Icon(Icons.refresh_rounded, size: 20),
|
||||
tooltip: 'Aggiorna ricerca',
|
||||
onPressed: discoveryState.isScanning
|
||||
? null
|
||||
: () => context.read<DiscoveryCubit>().scanForServers(),
|
||||
: () =>
|
||||
context.read<DiscoveryCubit>().scanForServers(),
|
||||
);
|
||||
},
|
||||
),
|
||||
@@ -154,7 +265,8 @@ class _ConnectScreenState extends State<ConnectScreen> {
|
||||
|
||||
BlocBuilder<DiscoveryCubit, DiscoveryState>(
|
||||
builder: (context, discoveryState) {
|
||||
if (discoveryState.isScanning && discoveryState.servers.isEmpty) {
|
||||
if (discoveryState.isScanning &&
|
||||
discoveryState.servers.isEmpty) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
@@ -173,7 +285,9 @@ class _ConnectScreenState extends State<ConnectScreen> {
|
||||
Expanded(
|
||||
child: Text(
|
||||
'Ricerca server PC in corso sulla rete...',
|
||||
style: TextStyle(color: AppTheme.darkTextSecondary, fontSize: 13),
|
||||
style: TextStyle(
|
||||
color: AppTheme.darkTextSecondary,
|
||||
fontSize: 13),
|
||||
),
|
||||
),
|
||||
],
|
||||
@@ -191,7 +305,10 @@ class _ConnectScreenState extends State<ConnectScreen> {
|
||||
),
|
||||
child: const Text(
|
||||
'Nessun server PC rilevato automaticamente sulla rete.\nAssicurati che il programma sul PC sia avviato e connesso allo stesso Wi-Fi.',
|
||||
style: TextStyle(color: AppTheme.darkTextSecondary, fontSize: 13, height: 1.4),
|
||||
style: TextStyle(
|
||||
color: AppTheme.darkTextSecondary,
|
||||
fontSize: 13,
|
||||
height: 1.4),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
);
|
||||
@@ -206,32 +323,41 @@ class _ConnectScreenState extends State<ConnectScreen> {
|
||||
return Card(
|
||||
margin: const EdgeInsets.only(bottom: 10),
|
||||
child: ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 16, vertical: 6),
|
||||
leading: Container(
|
||||
padding: const EdgeInsets.all(10),
|
||||
decoration: BoxDecoration(
|
||||
color: AppTheme.primary.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: const Icon(Icons.computer_rounded, color: AppTheme.primary),
|
||||
child: const Icon(Icons.computer_rounded,
|
||||
color: AppTheme.primary),
|
||||
),
|
||||
title: Text(
|
||||
srv.serverName,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 15),
|
||||
style: const TextStyle(
|
||||
fontWeight: FontWeight.bold, fontSize: 15),
|
||||
),
|
||||
subtitle: Text(
|
||||
'${srv.ip}:${srv.port} ${srv.pinRequired ? "• Richiede PIN" : ""}',
|
||||
style: const TextStyle(fontSize: 12, color: AppTheme.darkTextSecondary),
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: AppTheme.darkTextSecondary),
|
||||
),
|
||||
trailing: isCurrent
|
||||
? const Chip(
|
||||
label: Text('Connesso', style: TextStyle(fontSize: 12, color: AppTheme.success)),
|
||||
label: Text('Connesso',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: AppTheme.success)),
|
||||
backgroundColor: Color(0xFF064E3B),
|
||||
side: BorderSide.none,
|
||||
)
|
||||
: FilledButton(
|
||||
style: FilledButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 8),
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 14, vertical: 8),
|
||||
),
|
||||
onPressed: () {
|
||||
context.read<ConnectionCubit>().connect(
|
||||
@@ -241,6 +367,7 @@ class _ConnectScreenState extends State<ConnectScreen> {
|
||||
pin: _pinController.text.trim(),
|
||||
serverName: srv.serverName,
|
||||
),
|
||||
timeoutMinutes: timeout,
|
||||
);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
@@ -266,6 +393,16 @@ class _ConnectScreenState extends State<ConnectScreen> {
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
TextField(
|
||||
controller: _nameController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Nome Postazione / PC (opzionale)',
|
||||
hintText: 'Es. Cassa 1, Magazzino, Ufficio',
|
||||
prefixIcon: Icon(Icons.badge_outlined, size: 20),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
TextField(
|
||||
controller: _ipController,
|
||||
decoration: const InputDecoration(
|
||||
@@ -365,7 +502,8 @@ class _ConnectScreenState extends State<ConnectScreen> {
|
||||
decoration: BoxDecoration(
|
||||
color: AppTheme.darkCard,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
border: Border.all(color: badgeColor.withValues(alpha: 0.4), width: 1.5),
|
||||
border:
|
||||
Border.all(color: badgeColor.withValues(alpha: 0.4), width: 1.5),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: badgeColor.withValues(alpha: 0.1),
|
||||
|
||||
@@ -4,7 +4,7 @@ import '../../../../core/theme/app_theme.dart';
|
||||
import '../../domain/repositories/connection_repository.dart';
|
||||
import '../cubit/connection_cubit.dart';
|
||||
import '../cubit/connection_state.dart';
|
||||
import '../screens/connect_screen.dart';
|
||||
import 'server_selection_sheet.dart';
|
||||
|
||||
class ConnectionStatusChip extends StatelessWidget {
|
||||
const ConnectionStatusChip({super.key});
|
||||
@@ -37,9 +37,7 @@ class ConnectionStatusChip extends StatelessWidget {
|
||||
|
||||
return InkWell(
|
||||
onTap: () {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (_) => const ConnectScreen()),
|
||||
);
|
||||
ServerSelectionSheet.show(context);
|
||||
},
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
child: Container(
|
||||
|
||||
@@ -4,6 +4,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:mobile_scanner/mobile_scanner.dart';
|
||||
import '../../../../core/theme/app_theme.dart';
|
||||
import '../../domain/entities/server_connection_info.dart';
|
||||
import '../../../settings/presentation/cubit/settings_cubit.dart';
|
||||
import '../cubit/connection_cubit.dart';
|
||||
|
||||
class QrPairingModal extends StatelessWidget {
|
||||
@@ -75,6 +76,7 @@ class QrPairingModal extends StatelessWidget {
|
||||
final pin = json['pin'] as String? ?? '';
|
||||
final name = json['name'] as String? ?? 'PC Server';
|
||||
|
||||
final timeout = context.read<SettingsCubit>().state.settings.inactivityTimeoutMinutes;
|
||||
if (ip != null && ip.isNotEmpty) {
|
||||
context.read<ConnectionCubit>().connect(
|
||||
ServerConnectionInfo(
|
||||
@@ -83,6 +85,7 @@ class QrPairingModal extends StatelessWidget {
|
||||
pin: pin,
|
||||
serverName: name,
|
||||
),
|
||||
timeoutMinutes: timeout,
|
||||
);
|
||||
Navigator.pop(context);
|
||||
return;
|
||||
@@ -90,6 +93,7 @@ class QrPairingModal extends StatelessWidget {
|
||||
} catch (_) {
|
||||
final uri = Uri.tryParse(val);
|
||||
if (uri != null && uri.host.isNotEmpty) {
|
||||
final timeout = context.read<SettingsCubit>().state.settings.inactivityTimeoutMinutes;
|
||||
context.read<ConnectionCubit>().connect(
|
||||
ServerConnectionInfo(
|
||||
ip: uri.host,
|
||||
@@ -97,6 +101,7 @@ class QrPairingModal extends StatelessWidget {
|
||||
pin: '',
|
||||
serverName: 'PC Server',
|
||||
),
|
||||
timeoutMinutes: timeout,
|
||||
);
|
||||
Navigator.pop(context);
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,450 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../../../core/theme/app_theme.dart';
|
||||
import '../../domain/entities/server_connection_info.dart';
|
||||
import '../../../settings/presentation/cubit/settings_cubit.dart';
|
||||
import '../../../settings/presentation/cubit/settings_state.dart';
|
||||
import '../cubit/connection_cubit.dart';
|
||||
import '../cubit/connection_state.dart';
|
||||
import '../screens/connect_screen.dart';
|
||||
|
||||
class ServerSelectionSheet extends StatelessWidget {
|
||||
const ServerSelectionSheet({super.key});
|
||||
|
||||
static Future<void> show(BuildContext context) {
|
||||
return showModalBottomSheet(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
backgroundColor: Colors.transparent,
|
||||
builder: (_) => const ServerSelectionSheet(),
|
||||
);
|
||||
}
|
||||
|
||||
void _connectToServer(
|
||||
BuildContext context,
|
||||
ServerConnectionInfo server,
|
||||
int timeoutMinutes,
|
||||
) {
|
||||
context
|
||||
.read<ConnectionCubit>()
|
||||
.connect(server, timeoutMinutes: timeoutMinutes);
|
||||
Navigator.of(context).pop();
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Connessione a ${server.displayName}...'),
|
||||
backgroundColor: AppTheme.primary,
|
||||
duration: const Duration(seconds: 1),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _confirmDelete(
|
||||
BuildContext context, ServerConnectionInfo server) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (ctx) => AlertDialog(
|
||||
title: const Text('Rimuovere postazione?'),
|
||||
content: Text(
|
||||
'Vuoi eliminare "${server.displayName}" (${server.ip}) dai PC preferiti?',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx),
|
||||
child: const Text('Annulla'),
|
||||
),
|
||||
FilledButton(
|
||||
style: FilledButton.styleFrom(backgroundColor: AppTheme.error),
|
||||
onPressed: () {
|
||||
context.read<ConnectionCubit>().removeFavorite(server.effectiveId);
|
||||
Navigator.pop(ctx);
|
||||
},
|
||||
child: const Text('Rimuovi'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SettingsCubit, SettingsState>(
|
||||
builder: (context, settingsState) {
|
||||
final settings = settingsState.settings;
|
||||
|
||||
return BlocBuilder<ConnectionCubit, ServerConnectionState>(
|
||||
builder: (context, connectionState) {
|
||||
final favorites = connectionState.favorites;
|
||||
final currentIp = connectionState.info.ip;
|
||||
final isConnected = connectionState.isConnected;
|
||||
|
||||
return Container(
|
||||
constraints: BoxConstraints(
|
||||
maxHeight: MediaQuery.of(context).size.height * 0.82,
|
||||
),
|
||||
decoration: const BoxDecoration(
|
||||
color: AppTheme.darkSurface,
|
||||
borderRadius: BorderRadius.vertical(top: Radius.circular(24)),
|
||||
),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// Handle barra superiore
|
||||
Container(
|
||||
margin: const EdgeInsets.only(top: 12, bottom: 6),
|
||||
width: 44,
|
||||
height: 4,
|
||||
decoration: BoxDecoration(
|
||||
color: AppTheme.darkBorder,
|
||||
borderRadius: BorderRadius.circular(2),
|
||||
),
|
||||
),
|
||||
|
||||
// Intestazione
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(20, 8, 16, 12),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: AppTheme.primary.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: const Icon(Icons.storefront_rounded,
|
||||
color: AppTheme.primary, size: 24),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
const Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Seleziona Postazione PC',
|
||||
style: TextStyle(
|
||||
fontSize: 17,
|
||||
fontWeight: FontWeight.w700,
|
||||
color: AppTheme.darkTextPrimary,
|
||||
),
|
||||
),
|
||||
SizedBox(height: 2),
|
||||
Text(
|
||||
'Scegli il computer del negozio su cui scrivere',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: AppTheme.darkTextSecondary,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.close_rounded, size: 20),
|
||||
color: AppTheme.darkTextSecondary,
|
||||
onPressed: () => Navigator.pop(context),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// Alert inattività
|
||||
if (connectionState.isDisconnectedByInactivity)
|
||||
Container(
|
||||
margin: const EdgeInsets.symmetric(
|
||||
horizontal: 16, vertical: 4),
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 14, vertical: 8),
|
||||
decoration: BoxDecoration(
|
||||
color: AppTheme.warning.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(
|
||||
color: AppTheme.warning.withValues(alpha: 0.4)),
|
||||
),
|
||||
child: const Row(
|
||||
children: [
|
||||
Icon(Icons.timer_outlined,
|
||||
color: AppTheme.warning, size: 18),
|
||||
SizedBox(width: 10),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'Disconnesso per inattività. Seleziona la postazione prima di scansionare.',
|
||||
style: TextStyle(
|
||||
fontSize: 12, color: AppTheme.warning),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
const Divider(height: 1),
|
||||
|
||||
// Lista preferiti
|
||||
Flexible(
|
||||
child: favorites.isEmpty
|
||||
? Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.devices_other_rounded,
|
||||
size: 48,
|
||||
color: AppTheme.darkTextSecondary
|
||||
.withValues(alpha: 0.4)),
|
||||
const SizedBox(height: 12),
|
||||
const Text(
|
||||
'Nessun PC preferito salvato',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 15),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
const Text(
|
||||
'Aggiungi i computer del negozio per collegarti con 1 tocco ed evitare errori tra colleghi.',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: AppTheme.darkTextSecondary),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: ListView.separated(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16, vertical: 12),
|
||||
shrinkWrap: true,
|
||||
itemCount: favorites.length,
|
||||
separatorBuilder: (context, index) =>
|
||||
const SizedBox(height: 8),
|
||||
itemBuilder: (context, index) {
|
||||
final item = favorites[index];
|
||||
final isCurrent =
|
||||
isConnected && item.ip == currentIp;
|
||||
|
||||
return Card(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
side: BorderSide(
|
||||
color: isCurrent
|
||||
? AppTheme.success
|
||||
.withValues(alpha: 0.6)
|
||||
: AppTheme.darkBorder,
|
||||
width: isCurrent ? 1.5 : 1,
|
||||
),
|
||||
),
|
||||
child: InkWell(
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
onTap: () => _connectToServer(
|
||||
context,
|
||||
item,
|
||||
settings.inactivityTimeoutMinutes,
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 14, vertical: 10),
|
||||
child: Row(
|
||||
children: [
|
||||
// Icona PC
|
||||
Container(
|
||||
padding: const EdgeInsets.all(10),
|
||||
decoration: BoxDecoration(
|
||||
color: (isCurrent
|
||||
? AppTheme.success
|
||||
: AppTheme.primary)
|
||||
.withValues(alpha: 0.15),
|
||||
borderRadius:
|
||||
BorderRadius.circular(10),
|
||||
),
|
||||
child: Icon(
|
||||
Icons.computer_rounded,
|
||||
color: isCurrent
|
||||
? AppTheme.success
|
||||
: AppTheme.primary,
|
||||
size: 22,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
|
||||
// Informazioni
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment:
|
||||
CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
item.displayName,
|
||||
style: const TextStyle(
|
||||
fontWeight:
|
||||
FontWeight.bold,
|
||||
fontSize: 15,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow:
|
||||
TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
if (isCurrent)
|
||||
Container(
|
||||
padding: const EdgeInsets
|
||||
.symmetric(
|
||||
horizontal: 6,
|
||||
vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: AppTheme.success
|
||||
.withValues(
|
||||
alpha: 0.2),
|
||||
borderRadius:
|
||||
BorderRadius
|
||||
.circular(6),
|
||||
),
|
||||
child: const Text(
|
||||
'ATTIVO',
|
||||
style: TextStyle(
|
||||
color:
|
||||
AppTheme.success,
|
||||
fontSize: 10,
|
||||
fontWeight:
|
||||
FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
'${item.ip}:${item.port} ${item.pin.isNotEmpty ? "• Con PIN" : ""}',
|
||||
style: const TextStyle(
|
||||
fontFamily: 'monospace',
|
||||
fontSize: 12,
|
||||
color: AppTheme
|
||||
.darkTextSecondary,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
|
||||
// Tasto Elimina preferito
|
||||
IconButton(
|
||||
icon: const Icon(
|
||||
Icons.delete_outline_rounded,
|
||||
size: 20),
|
||||
color: AppTheme.darkTextSecondary,
|
||||
tooltip: 'Rimuovi dai preferiti',
|
||||
onPressed: () =>
|
||||
_confirmDelete(context, item),
|
||||
),
|
||||
|
||||
// Bottone Connetti
|
||||
FilledButton(
|
||||
style: FilledButton.styleFrom(
|
||||
backgroundColor: isCurrent
|
||||
? AppTheme.darkBorder
|
||||
: AppTheme.primary,
|
||||
foregroundColor: Colors.white,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 14, vertical: 8),
|
||||
textStyle: const TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.bold),
|
||||
),
|
||||
onPressed: () => _connectToServer(
|
||||
context,
|
||||
item,
|
||||
settings.inactivityTimeoutMinutes,
|
||||
),
|
||||
child: Text(isCurrent
|
||||
? 'Riconnetti'
|
||||
: 'Connetti'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
// Bottone Nuovo PC
|
||||
Padding(
|
||||
padding:
|
||||
const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
child: OutlinedButton.icon(
|
||||
style: OutlinedButton.styleFrom(
|
||||
minimumSize: const Size.fromHeight(46),
|
||||
side: const BorderSide(
|
||||
color: AppTheme.primary, width: 1.5),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12)),
|
||||
),
|
||||
icon: const Icon(Icons.add_rounded, size: 20),
|
||||
label: const Text(
|
||||
'Aggiungi Nuovo PC (Wi-Fi o QR Code)',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => const ConnectScreen()),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
const Divider(height: 1),
|
||||
|
||||
// Toggle Autocollegamento rapido
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 6, 16, 14),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.flash_auto_rounded,
|
||||
size: 20, color: AppTheme.secondary),
|
||||
const SizedBox(width: 10),
|
||||
const Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Collega automaticamente all\'avvio',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'Disattivato: richiede la selezione del PC (consigliato in negozio)',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: AppTheme.darkTextSecondary,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Switch(
|
||||
value: settings.autoConnect,
|
||||
activeThumbColor: AppTheme.primary,
|
||||
onChanged: (val) {
|
||||
context
|
||||
.read<SettingsCubit>()
|
||||
.updateAutoConnect(val);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,10 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:mobile_scanner/mobile_scanner.dart';
|
||||
import '../../../../core/theme/app_theme.dart';
|
||||
import '../../../connection/presentation/cubit/connection_cubit.dart';
|
||||
import '../../../connection/presentation/widgets/connection_status_chip.dart';
|
||||
import '../../../connection/presentation/widgets/server_selection_sheet.dart';
|
||||
import '../../../settings/presentation/cubit/settings_cubit.dart';
|
||||
import '../../../settings/presentation/screens/settings_screen.dart';
|
||||
import '../cubit/history_cubit.dart';
|
||||
import '../cubit/history_state.dart';
|
||||
@@ -22,6 +25,8 @@ class ScannerScreen extends StatefulWidget {
|
||||
|
||||
class _ScannerScreenState extends State<ScannerScreen> with WidgetsBindingObserver {
|
||||
late MobileScannerController _scannerController;
|
||||
DateTime? _pausedTime;
|
||||
bool _isSelectionSheetOpen = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -32,6 +37,34 @@ class _ScannerScreenState extends State<ScannerScreen> with WidgetsBindingObserv
|
||||
facing: CameraFacing.back,
|
||||
torchEnabled: false,
|
||||
);
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_checkStartupConnection();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _checkStartupConnection() async {
|
||||
final settingsCubit = context.read<SettingsCubit>();
|
||||
final connectionCubit = context.read<ConnectionCubit>();
|
||||
|
||||
final settings = settingsCubit.state.settings;
|
||||
await connectionCubit.initialize(
|
||||
autoConnect: settings.autoConnect,
|
||||
timeoutMinutes: settings.inactivityTimeoutMinutes,
|
||||
);
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
if (!settings.autoConnect && !connectionCubit.state.isConnected) {
|
||||
_openServerSelectionSheet();
|
||||
}
|
||||
}
|
||||
|
||||
void _openServerSelectionSheet() async {
|
||||
if (_isSelectionSheetOpen || !mounted) return;
|
||||
_isSelectionSheetOpen = true;
|
||||
await ServerSelectionSheet.show(context);
|
||||
_isSelectionSheetOpen = false;
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -46,8 +79,38 @@ class _ScannerScreenState extends State<ScannerScreen> with WidgetsBindingObserv
|
||||
if (!_scannerController.value.isInitialized) return;
|
||||
if (state == AppLifecycleState.inactive || state == AppLifecycleState.paused) {
|
||||
_scannerController.stop();
|
||||
_pausedTime = DateTime.now();
|
||||
} else if (state == AppLifecycleState.resumed) {
|
||||
_scannerController.start();
|
||||
_onAppResumed();
|
||||
}
|
||||
}
|
||||
|
||||
void _onAppResumed() {
|
||||
final settings = context.read<SettingsCubit>().state.settings;
|
||||
final connectionCubit = context.read<ConnectionCubit>();
|
||||
|
||||
final wasPausedFor = _pausedTime != null
|
||||
? DateTime.now().difference(_pausedTime!)
|
||||
: Duration.zero;
|
||||
|
||||
final timeoutMinutes = settings.inactivityTimeoutMinutes;
|
||||
final isTimeoutExceeded =
|
||||
timeoutMinutes > 0 && wasPausedFor.inMinutes >= timeoutMinutes;
|
||||
|
||||
if (isTimeoutExceeded && connectionCubit.state.isConnected) {
|
||||
connectionCubit.disconnect(byInactivity: true);
|
||||
}
|
||||
|
||||
if (!settings.autoConnect && !connectionCubit.state.isConnected) {
|
||||
_openServerSelectionSheet();
|
||||
} else if (settings.autoConnect && !connectionCubit.state.isConnected) {
|
||||
if (connectionCubit.state.info.isConfigured) {
|
||||
connectionCubit.connect(
|
||||
connectionCubit.state.info,
|
||||
timeoutMinutes: timeoutMinutes,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,16 +8,22 @@ class SettingsLocalDataSource {
|
||||
SettingsLocalDataSource(this._prefs);
|
||||
|
||||
AppSettings loadSettings() {
|
||||
final suffix = _prefs.getString(AppConstants.keySuffix) ?? AppConstants.suffixEnter;
|
||||
final suffix =
|
||||
_prefs.getString(AppConstants.keySuffix) ?? AppConstants.suffixEnter;
|
||||
final continuous = _prefs.getBool(AppConstants.keyContinuous) ?? false;
|
||||
final vibrate = _prefs.getBool(AppConstants.keyVibrate) ?? true;
|
||||
final sound = _prefs.getBool(AppConstants.keySound) ?? true;
|
||||
final autoConnect = _prefs.getBool(AppConstants.keyAutoConnect) ?? false;
|
||||
final inactivityTimeout = _prefs.getInt(AppConstants.keyInactivityTimeout) ??
|
||||
AppConstants.defaultInactivityTimeoutMinutes;
|
||||
|
||||
return AppSettings(
|
||||
suffix: suffix,
|
||||
continuousMode: continuous,
|
||||
vibrate: vibrate,
|
||||
sound: sound,
|
||||
autoConnect: autoConnect,
|
||||
inactivityTimeoutMinutes: inactivityTimeout,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -29,9 +35,17 @@ class SettingsLocalDataSource {
|
||||
await _prefs.setBool(AppConstants.keyContinuous, enabled);
|
||||
}
|
||||
|
||||
Future<void> saveFeedbackToggles({required bool vibrate, required bool sound}) async {
|
||||
Future<void> saveFeedbackToggles(
|
||||
{required bool vibrate, required bool sound}) async {
|
||||
await _prefs.setBool(AppConstants.keyVibrate, vibrate);
|
||||
await _prefs.setBool(AppConstants.keySound, sound);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> saveAutoConnect(bool enabled) async {
|
||||
await _prefs.setBool(AppConstants.keyAutoConnect, enabled);
|
||||
}
|
||||
|
||||
Future<void> saveInactivityTimeout(int minutes) async {
|
||||
await _prefs.setInt(AppConstants.keyInactivityTimeout, minutes);
|
||||
}
|
||||
}
|
||||
@@ -23,8 +23,18 @@ class SettingsRepositoryImpl implements SettingsRepository {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> saveFeedbackToggles({required bool vibrate, required bool sound}) async {
|
||||
Future<void> saveFeedbackToggles(
|
||||
{required bool vibrate, required bool sound}) async {
|
||||
await _localDataSource.saveFeedbackToggles(vibrate: vibrate, sound: sound);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> saveAutoConnect(bool enabled) async {
|
||||
await _localDataSource.saveAutoConnect(enabled);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> saveInactivityTimeout(int minutes) async {
|
||||
await _localDataSource.saveInactivityTimeout(minutes);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,21 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import '../../../../core/constants/app_constants.dart';
|
||||
|
||||
class AppSettings extends Equatable {
|
||||
final String suffix; // 'enter', 'tab', 'none'
|
||||
final bool continuousMode;
|
||||
final bool vibrate;
|
||||
final bool sound;
|
||||
final bool autoConnect; // Default: false (ideal for multi-PC store)
|
||||
final int inactivityTimeoutMinutes; // Default: 5 or 10 min, 0 = disabled
|
||||
|
||||
const AppSettings({
|
||||
this.suffix = 'enter',
|
||||
this.continuousMode = false,
|
||||
this.vibrate = true,
|
||||
this.sound = true,
|
||||
this.autoConnect = false,
|
||||
this.inactivityTimeoutMinutes = AppConstants.defaultInactivityTimeoutMinutes,
|
||||
});
|
||||
|
||||
AppSettings copyWith({
|
||||
@@ -18,16 +23,27 @@ class AppSettings extends Equatable {
|
||||
bool? continuousMode,
|
||||
bool? vibrate,
|
||||
bool? sound,
|
||||
bool? autoConnect,
|
||||
int? inactivityTimeoutMinutes,
|
||||
}) {
|
||||
return AppSettings(
|
||||
suffix: suffix ?? this.suffix,
|
||||
continuousMode: continuousMode ?? this.continuousMode,
|
||||
vibrate: vibrate ?? this.vibrate,
|
||||
sound: sound ?? this.sound,
|
||||
autoConnect: autoConnect ?? this.autoConnect,
|
||||
inactivityTimeoutMinutes:
|
||||
inactivityTimeoutMinutes ?? this.inactivityTimeoutMinutes,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [suffix, continuousMode, vibrate, sound];
|
||||
List<Object?> get props => [
|
||||
suffix,
|
||||
continuousMode,
|
||||
vibrate,
|
||||
sound,
|
||||
autoConnect,
|
||||
inactivityTimeoutMinutes,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -5,5 +5,6 @@ abstract class SettingsRepository {
|
||||
Future<void> saveSuffix(String suffix);
|
||||
Future<void> saveContinuousMode(bool enabled);
|
||||
Future<void> saveFeedbackToggles({required bool vibrate, required bool sound});
|
||||
Future<void> saveAutoConnect(bool enabled);
|
||||
Future<void> saveInactivityTimeout(int minutes);
|
||||
}
|
||||
|
||||
@@ -40,4 +40,16 @@ class SettingsCubit extends Cubit<SettingsState> {
|
||||
sound: updated.sound,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> updateAutoConnect(bool enabled) async {
|
||||
final updated = state.settings.copyWith(autoConnect: enabled);
|
||||
emit(state.copyWith(settings: updated));
|
||||
await _settingsRepository.saveAutoConnect(enabled);
|
||||
}
|
||||
|
||||
Future<void> updateInactivityTimeout(int minutes) async {
|
||||
final updated = state.settings.copyWith(inactivityTimeoutMinutes: minutes);
|
||||
emit(state.copyWith(settings: updated));
|
||||
await _settingsRepository.saveInactivityTimeout(minutes);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import '../../../../core/theme/app_theme.dart';
|
||||
import '../../../connection/presentation/cubit/connection_cubit.dart';
|
||||
import '../../../connection/presentation/cubit/connection_state.dart';
|
||||
import '../../../connection/presentation/screens/connect_screen.dart';
|
||||
import '../../../connection/presentation/widgets/server_selection_sheet.dart';
|
||||
import '../cubit/settings_cubit.dart';
|
||||
import '../cubit/settings_state.dart';
|
||||
|
||||
@@ -25,10 +26,11 @@ class SettingsScreen extends StatelessWidget {
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
children: [
|
||||
// PC Connection Section
|
||||
const _SectionHeader(title: 'CONNESSIONE PC'),
|
||||
const _SectionHeader(title: 'CONNESSIONE PC & NEGOZIO'),
|
||||
Card(
|
||||
child: Column(
|
||||
children: [
|
||||
// Server PC attivo
|
||||
BlocBuilder<ConnectionCubit, ServerConnectionState>(
|
||||
builder: (context, connState) {
|
||||
final isConnected = connState.isConnected;
|
||||
@@ -36,17 +38,21 @@ class SettingsScreen extends StatelessWidget {
|
||||
leading: Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: (isConnected ? AppTheme.success : AppTheme.darkTextSecondary)
|
||||
color: (isConnected
|
||||
? AppTheme.success
|
||||
: AppTheme.darkTextSecondary)
|
||||
.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Icon(
|
||||
Icons.computer_rounded,
|
||||
color: isConnected ? AppTheme.success : AppTheme.darkTextSecondary,
|
||||
color: isConnected
|
||||
? AppTheme.success
|
||||
: AppTheme.darkTextSecondary,
|
||||
size: 22,
|
||||
),
|
||||
),
|
||||
title: const Text('Server PC'),
|
||||
title: const Text('Stato Postazione'),
|
||||
subtitle: Text(
|
||||
isConnected
|
||||
? 'Connesso a ${connState.info.displayName}'
|
||||
@@ -59,13 +65,120 @@ class SettingsScreen extends StatelessWidget {
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (_) => const ConnectScreen()),
|
||||
MaterialPageRoute(
|
||||
builder: (_) => const ConnectScreen()),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
const Divider(height: 1),
|
||||
|
||||
// Postazioni e PC Preferiti
|
||||
BlocBuilder<ConnectionCubit, ServerConnectionState>(
|
||||
builder: (context, connState) {
|
||||
final count = connState.favorites.length;
|
||||
return ListTile(
|
||||
leading: Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: AppTheme.primary.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Icon(Icons.storefront_rounded,
|
||||
color: AppTheme.primary, size: 22),
|
||||
),
|
||||
title: const Text('Postazioni PC Preferite'),
|
||||
subtitle: Text(
|
||||
count > 0
|
||||
? '$count computer registrati nel negozio'
|
||||
: 'Nessun computer salvato',
|
||||
style: const TextStyle(fontSize: 13),
|
||||
),
|
||||
trailing: const Icon(Icons.chevron_right_rounded),
|
||||
onTap: () {
|
||||
ServerSelectionSheet.show(context);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
const Divider(height: 1),
|
||||
|
||||
// Toggle Connessione automatica all'avvio / risveglio
|
||||
SwitchListTile(
|
||||
secondary: Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: AppTheme.secondary.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Icon(Icons.flash_auto_rounded,
|
||||
color: AppTheme.secondary, size: 22),
|
||||
),
|
||||
title: const Text(
|
||||
'Autocollegamento all\'avvio/risveglio',
|
||||
style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600),
|
||||
),
|
||||
subtitle: const Text(
|
||||
'Disattivato: chiede sempre a quale PC connettersi (evita di sparare codici sul PC del collega)',
|
||||
style: TextStyle(fontSize: 12),
|
||||
),
|
||||
value: settings.autoConnect,
|
||||
onChanged: (val) {
|
||||
context.read<SettingsCubit>().updateAutoConnect(val);
|
||||
},
|
||||
),
|
||||
const Divider(height: 1),
|
||||
|
||||
// Timeout di inattività
|
||||
ListTile(
|
||||
leading: Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: AppTheme.warning.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Icon(Icons.timer_outlined,
|
||||
color: AppTheme.warning, size: 22),
|
||||
),
|
||||
title: const Text(
|
||||
'Disconnessione per inattività',
|
||||
style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600),
|
||||
),
|
||||
subtitle: Text(
|
||||
settings.inactivityTimeoutMinutes > 0
|
||||
? 'Interrompe la connessione dopo ${settings.inactivityTimeoutMinutes} minuti senza scansioni'
|
||||
: 'Disattivata (rimane sempre connesso)',
|
||||
style: const TextStyle(fontSize: 12),
|
||||
),
|
||||
trailing: DropdownButton<int>(
|
||||
value: settings.inactivityTimeoutMinutes,
|
||||
dropdownColor: AppTheme.darkCard,
|
||||
style: const TextStyle(
|
||||
color: AppTheme.darkTextPrimary,
|
||||
fontWeight: FontWeight.bold),
|
||||
underline: const SizedBox.shrink(),
|
||||
items: const [
|
||||
DropdownMenuItem(value: 5, child: Text('5 min (Consigliato)')),
|
||||
DropdownMenuItem(value: 10, child: Text('10 min')),
|
||||
DropdownMenuItem(value: 15, child: Text('15 min')),
|
||||
DropdownMenuItem(value: 0, child: Text('Disattivata')),
|
||||
],
|
||||
onChanged: (val) {
|
||||
if (val != null) {
|
||||
context
|
||||
.read<SettingsCubit>()
|
||||
.updateInactivityTimeout(val);
|
||||
context
|
||||
.read<ConnectionCubit>()
|
||||
.onUserActivity(val);
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
const Divider(height: 1),
|
||||
|
||||
// Test di digitazione
|
||||
ListTile(
|
||||
leading: Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
@@ -73,7 +186,8 @@ class SettingsScreen extends StatelessWidget {
|
||||
color: AppTheme.primary.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Icon(Icons.send_rounded, color: AppTheme.primary, size: 22),
|
||||
child: const Icon(Icons.send_rounded,
|
||||
color: AppTheme.primary, size: 22),
|
||||
),
|
||||
title: const Text('Invia Test di Digitazione'),
|
||||
subtitle: const Text(
|
||||
@@ -107,10 +221,13 @@ class SettingsScreen extends StatelessWidget {
|
||||
children: [
|
||||
_RadioOption(
|
||||
title: 'Tasto Invio (Enter)',
|
||||
subtitle: 'Preme Invio dopo ogni codice (consigliato per Excel, ERP, gestionali)',
|
||||
subtitle:
|
||||
'Preme Invio dopo ogni codice (consigliato per Excel, ERP, gestionali)',
|
||||
isSelected: settings.suffix == AppConstants.suffixEnter,
|
||||
onTap: () {
|
||||
context.read<SettingsCubit>().updateSuffix(AppConstants.suffixEnter);
|
||||
context
|
||||
.read<SettingsCubit>()
|
||||
.updateSuffix(AppConstants.suffixEnter);
|
||||
},
|
||||
),
|
||||
const Divider(height: 1),
|
||||
@@ -119,7 +236,9 @@ class SettingsScreen extends StatelessWidget {
|
||||
subtitle: 'Sposta il cursore al campo/cella successiva',
|
||||
isSelected: settings.suffix == AppConstants.suffixTab,
|
||||
onTap: () {
|
||||
context.read<SettingsCubit>().updateSuffix(AppConstants.suffixTab);
|
||||
context
|
||||
.read<SettingsCubit>()
|
||||
.updateSuffix(AppConstants.suffixTab);
|
||||
},
|
||||
),
|
||||
const Divider(height: 1),
|
||||
@@ -128,7 +247,9 @@ class SettingsScreen extends StatelessWidget {
|
||||
subtitle: 'Scrive solo la stringa pura del codice a barre',
|
||||
isSelected: settings.suffix == AppConstants.suffixNone,
|
||||
onTap: () {
|
||||
context.read<SettingsCubit>().updateSuffix(AppConstants.suffixNone);
|
||||
context
|
||||
.read<SettingsCubit>()
|
||||
.updateSuffix(AppConstants.suffixNone);
|
||||
},
|
||||
),
|
||||
],
|
||||
@@ -149,7 +270,8 @@ class SettingsScreen extends StatelessWidget {
|
||||
color: AppTheme.primary.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Icon(Icons.repeat_rounded, color: AppTheme.primary, size: 22),
|
||||
child: const Icon(Icons.repeat_rounded,
|
||||
color: AppTheme.primary, size: 22),
|
||||
),
|
||||
title: const Text('Scansione Continua'),
|
||||
subtitle: const Text(
|
||||
@@ -169,7 +291,8 @@ class SettingsScreen extends StatelessWidget {
|
||||
color: AppTheme.primary.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Icon(Icons.vibration_rounded, color: AppTheme.primary, size: 22),
|
||||
child: const Icon(Icons.vibration_rounded,
|
||||
color: AppTheme.primary, size: 22),
|
||||
),
|
||||
title: const Text('Vibrazione alla lettura'),
|
||||
subtitle: const Text(
|
||||
@@ -178,7 +301,9 @@ class SettingsScreen extends StatelessWidget {
|
||||
),
|
||||
value: settings.vibrate,
|
||||
onChanged: (val) {
|
||||
context.read<SettingsCubit>().updateFeedback(vibrate: val);
|
||||
context
|
||||
.read<SettingsCubit>()
|
||||
.updateFeedback(vibrate: val);
|
||||
},
|
||||
),
|
||||
const Divider(height: 1),
|
||||
@@ -189,7 +314,8 @@ class SettingsScreen extends StatelessWidget {
|
||||
color: AppTheme.primary.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Icon(Icons.volume_up_rounded, color: AppTheme.primary, size: 22),
|
||||
child: const Icon(Icons.volume_up_rounded,
|
||||
color: AppTheme.primary, size: 22),
|
||||
),
|
||||
title: const Text('Suono di conferma'),
|
||||
subtitle: const Text(
|
||||
@@ -219,10 +345,12 @@ class SettingsScreen extends StatelessWidget {
|
||||
color: AppTheme.primary.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Icon(Icons.business_rounded, color: AppTheme.primary, size: 22),
|
||||
child: const Icon(Icons.business_rounded,
|
||||
color: AppTheme.primary, size: 22),
|
||||
),
|
||||
title: const Text('Organizzazione'),
|
||||
subtitle: const Text(AppConstants.organization, style: TextStyle(fontSize: 13)),
|
||||
subtitle: const Text(AppConstants.organization,
|
||||
style: TextStyle(fontSize: 13)),
|
||||
),
|
||||
const Divider(height: 1),
|
||||
ListTile(
|
||||
@@ -232,7 +360,8 @@ class SettingsScreen extends StatelessWidget {
|
||||
color: AppTheme.primary.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Icon(Icons.info_outline_rounded, color: AppTheme.primary, size: 22),
|
||||
child: const Icon(Icons.info_outline_rounded,
|
||||
color: AppTheme.primary, size: 22),
|
||||
),
|
||||
title: const Text('Versione App'),
|
||||
subtitle: const Text(
|
||||
@@ -308,7 +437,9 @@ class _RadioOption extends StatelessWidget {
|
||||
style: TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: isSelected ? AppTheme.darkTextPrimary : AppTheme.darkTextSecondary,
|
||||
color: isSelected
|
||||
? AppTheme.darkTextPrimary
|
||||
: AppTheme.darkTextSecondary,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 3),
|
||||
|
||||
+1
-1
@@ -84,7 +84,7 @@ class BarcodeReaderApp extends StatelessWidget {
|
||||
create: (_) => SettingsCubit(settingsRepository)..loadSettings(),
|
||||
),
|
||||
BlocProvider<ConnectionCubit>(
|
||||
create: (_) => ConnectionCubit(connectionRepository)..initialize(),
|
||||
create: (_) => ConnectionCubit(connectionRepository),
|
||||
),
|
||||
BlocProvider<DiscoveryCubit>(
|
||||
create: (_) => DiscoveryCubit(connectionRepository),
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:barcode_reader_app/features/connection/data/datasources/connection_local_datasource.dart';
|
||||
import 'package:barcode_reader_app/features/connection/data/datasources/connection_remote_datasource.dart';
|
||||
import 'package:barcode_reader_app/features/connection/data/datasources/udp_discovery_datasource.dart';
|
||||
import 'package:barcode_reader_app/features/connection/data/repositories/connection_repository_impl.dart';
|
||||
import 'package:barcode_reader_app/features/connection/domain/entities/server_connection_info.dart';
|
||||
import 'package:barcode_reader_app/features/connection/domain/repositories/connection_repository.dart';
|
||||
import 'package:barcode_reader_app/features/connection/presentation/cubit/connection_cubit.dart';
|
||||
import 'package:barcode_reader_app/features/settings/data/datasources/settings_local_datasource.dart';
|
||||
import 'package:barcode_reader_app/features/settings/data/repositories/settings_repository_impl.dart';
|
||||
import 'package:barcode_reader_app/features/settings/presentation/cubit/settings_cubit.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
late SharedPreferences prefs;
|
||||
late SettingsRepositoryImpl settingsRepo;
|
||||
late ConnectionRepositoryImpl connectionRepo;
|
||||
|
||||
setUp(() async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
prefs = await SharedPreferences.getInstance();
|
||||
|
||||
settingsRepo =
|
||||
SettingsRepositoryImpl(SettingsLocalDataSource(prefs));
|
||||
connectionRepo = ConnectionRepositoryImpl(
|
||||
localDataSource: ConnectionLocalDataSource(prefs),
|
||||
remoteDataSource: ConnectionRemoteDataSource(),
|
||||
discoveryDataSource: const UdpDiscoveryDataSource(),
|
||||
);
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
connectionRepo.dispose();
|
||||
});
|
||||
|
||||
group('Favorites and Store Multi-PC Management', () {
|
||||
test('Favorite servers can be added and removed', () async {
|
||||
final cubit = ConnectionCubit(connectionRepo);
|
||||
await cubit.initialize(autoConnect: false);
|
||||
|
||||
expect(cubit.state.favorites, isEmpty);
|
||||
|
||||
const cassa1 = ServerConnectionInfo(
|
||||
ip: '192.168.1.10',
|
||||
port: 8765,
|
||||
serverName: 'Cassa 1 - Principale',
|
||||
);
|
||||
|
||||
const cassa2 = ServerConnectionInfo(
|
||||
ip: '192.168.1.11',
|
||||
port: 8765,
|
||||
serverName: 'Cassa 2 - Riparazioni',
|
||||
);
|
||||
|
||||
await cubit.saveFavorite(cassa1);
|
||||
await cubit.saveFavorite(cassa2);
|
||||
|
||||
expect(cubit.state.favorites.length, 2);
|
||||
expect(cubit.state.favorites.first.displayName, 'Cassa 1 - Principale');
|
||||
|
||||
await cubit.removeFavorite(cassa1.effectiveId);
|
||||
expect(cubit.state.favorites.length, 1);
|
||||
expect(cubit.state.favorites.first.displayName, 'Cassa 2 - Riparazioni');
|
||||
});
|
||||
|
||||
test('Initializes disconnected when autoConnect is false', () async {
|
||||
final cubit = ConnectionCubit(connectionRepo);
|
||||
// Salva un server
|
||||
await connectionRepo.saveConnection(const ServerConnectionInfo(
|
||||
ip: '192.168.1.10',
|
||||
port: 8765,
|
||||
serverName: 'Cassa 1',
|
||||
));
|
||||
|
||||
// Inizializza con autoconnect = false (default per negozio con più postazioni)
|
||||
await cubit.initialize(autoConnect: false);
|
||||
|
||||
expect(cubit.state.status, ConnectionStatus.disconnected);
|
||||
expect(cubit.state.isConnected, false);
|
||||
expect(cubit.state.info.displayName, 'Cassa 1');
|
||||
});
|
||||
|
||||
test('SettingsCubit handles autoConnect and inactivity timeout changes', () async {
|
||||
final cubit = SettingsCubit(settingsRepo);
|
||||
await cubit.loadSettings();
|
||||
|
||||
// Default: autoConnect è false
|
||||
expect(cubit.state.settings.autoConnect, false);
|
||||
expect(cubit.state.settings.inactivityTimeoutMinutes, 5);
|
||||
|
||||
await cubit.updateAutoConnect(true);
|
||||
expect(cubit.state.settings.autoConnect, true);
|
||||
|
||||
await cubit.updateInactivityTimeout(10);
|
||||
expect(cubit.state.settings.inactivityTimeoutMinutes, 10);
|
||||
|
||||
// Ricarica dal repository per verificare la persistenza
|
||||
final reloaded = await settingsRepo.loadSettings();
|
||||
expect(reloaded.autoConnect, true);
|
||||
expect(reloaded.inactivityTimeoutMinutes, 10);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user