feat: enhance receipt alias management with global scope support and update validation
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
@@ -23,6 +25,9 @@ class _AdminAliasesPanelState extends ConsumerState<AdminAliasesPanel> {
|
||||
String _search = '';
|
||||
List<ReceiptAlias> _aliases = [];
|
||||
List<AdminProduct> _products = [];
|
||||
int? _scopeChangedAliasId;
|
||||
bool? _scopeChangedToGlobal;
|
||||
Timer? _scopeChangedTimer;
|
||||
|
||||
final TextEditingController _aliasController = TextEditingController();
|
||||
int? _selectedProductId;
|
||||
@@ -35,10 +40,26 @@ class _AdminAliasesPanelState extends ConsumerState<AdminAliasesPanel> {
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_scopeChangedTimer?.cancel();
|
||||
_aliasController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _markScopeChanged(int aliasId, bool isGlobal) {
|
||||
_scopeChangedTimer?.cancel();
|
||||
setState(() {
|
||||
_scopeChangedAliasId = aliasId;
|
||||
_scopeChangedToGlobal = isGlobal;
|
||||
});
|
||||
_scopeChangedTimer = Timer(const Duration(seconds: 6), () {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_scopeChangedAliasId = null;
|
||||
_scopeChangedToGlobal = null;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _load() async {
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
@@ -109,6 +130,7 @@ class _AdminAliasesPanelState extends ConsumerState<AdminAliasesPanel> {
|
||||
Future<void> _editAlias(ReceiptAlias alias) async {
|
||||
String aliasName = alias.receiptName;
|
||||
int selectedProductId = alias.productId;
|
||||
bool isGlobal = alias.isGlobal;
|
||||
final nameController = TextEditingController(text: alias.receiptName);
|
||||
|
||||
final result = await showDialog<bool>(
|
||||
@@ -147,6 +169,23 @@ class _AdminAliasesPanelState extends ConsumerState<AdminAliasesPanel> {
|
||||
setDialogState(() => selectedProductId = value);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
SwitchListTile.adaptive(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
title: const Text('Globalt alias'),
|
||||
subtitle: Text(
|
||||
alias.isGlobal
|
||||
? 'Aliaset är redan globalt.'
|
||||
: 'Du kan göra privata alias globala.',
|
||||
),
|
||||
value: isGlobal,
|
||||
onChanged: alias.isGlobal
|
||||
? null
|
||||
: (value) {
|
||||
if (!value) return;
|
||||
setDialogState(() => isGlobal = true);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
@@ -167,6 +206,7 @@ class _AdminAliasesPanelState extends ConsumerState<AdminAliasesPanel> {
|
||||
if (result != true || !mounted) return;
|
||||
|
||||
final trimmedAlias = aliasName.trim();
|
||||
final scopeChanged = isGlobal != alias.isGlobal;
|
||||
if (trimmedAlias.isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Aliasnamn kan inte vara tomt.')),
|
||||
@@ -180,10 +220,14 @@ class _AdminAliasesPanelState extends ConsumerState<AdminAliasesPanel> {
|
||||
alias.id,
|
||||
receiptName: trimmedAlias,
|
||||
productId: selectedProductId,
|
||||
isGlobal: isGlobal,
|
||||
);
|
||||
if (!mounted) return;
|
||||
await _load();
|
||||
if (!mounted) return;
|
||||
if (scopeChanged) {
|
||||
_markScopeChanged(alias.id, isGlobal);
|
||||
}
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Alias uppdaterat.')),
|
||||
);
|
||||
@@ -296,6 +340,16 @@ class _AdminAliasesPanelState extends ConsumerState<AdminAliasesPanel> {
|
||||
visualDensity: VisualDensity.compact,
|
||||
label: Text(alias.isGlobal ? 'Global' : 'Privat'),
|
||||
),
|
||||
if (_scopeChangedAliasId == alias.id && _scopeChangedToGlobal != null)
|
||||
Chip(
|
||||
visualDensity: VisualDensity.compact,
|
||||
avatar: const Icon(Icons.sync_alt, size: 16),
|
||||
label: Text(
|
||||
_scopeChangedToGlobal == true
|
||||
? 'Bytt till Global'
|
||||
: 'Bytt till Privat',
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
trailing: Row(
|
||||
|
||||
Reference in New Issue
Block a user