mirror of
https://github.com/koloideal/Casha.git
synced 2026-06-10 18:35:28 +03:00
update
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../core/constants.dart';
|
||||
import '../../shared/models/transaction.dart';
|
||||
|
||||
class AddTransactionState {
|
||||
@@ -8,6 +9,7 @@ class AddTransactionState {
|
||||
final DateTime date;
|
||||
final String note;
|
||||
final bool isSubmitting;
|
||||
final String? editingId;
|
||||
|
||||
const AddTransactionState({
|
||||
this.amount,
|
||||
@@ -16,6 +18,7 @@ class AddTransactionState {
|
||||
required this.date,
|
||||
this.note = '',
|
||||
this.isSubmitting = false,
|
||||
this.editingId,
|
||||
});
|
||||
|
||||
AddTransactionState copyWith({
|
||||
@@ -25,6 +28,7 @@ class AddTransactionState {
|
||||
DateTime? date,
|
||||
String? note,
|
||||
bool? isSubmitting,
|
||||
String? editingId,
|
||||
}) =>
|
||||
AddTransactionState(
|
||||
amount: amount ?? this.amount,
|
||||
@@ -33,7 +37,10 @@ class AddTransactionState {
|
||||
date: date ?? this.date,
|
||||
note: note ?? this.note,
|
||||
isSubmitting: isSubmitting ?? this.isSubmitting,
|
||||
editingId: editingId ?? this.editingId,
|
||||
);
|
||||
|
||||
bool get isEditing => editingId != null;
|
||||
}
|
||||
|
||||
class AddTransactionNotifier extends StateNotifier<AddTransactionState> {
|
||||
@@ -41,11 +48,32 @@ class AddTransactionNotifier extends StateNotifier<AddTransactionState> {
|
||||
: super(AddTransactionState(date: DateTime.now()));
|
||||
|
||||
void setAmount(double? v) => state = state.copyWith(amount: v);
|
||||
|
||||
void setCategory(String v) => state = state.copyWith(category: v);
|
||||
void setType(TransactionType v) => state = state.copyWith(type: v);
|
||||
|
||||
void setType(TransactionType v) {
|
||||
// Reset category to first item of new type
|
||||
final newCategory = AppCategories.forType(v).first;
|
||||
state = state.copyWith(type: v, category: newCategory);
|
||||
}
|
||||
|
||||
void setDate(DateTime v) => state = state.copyWith(date: v);
|
||||
|
||||
void setNote(String v) => state = state.copyWith(note: v);
|
||||
|
||||
void setSubmitting(bool v) => state = state.copyWith(isSubmitting: v);
|
||||
|
||||
void initializeForEdit(Transaction transaction) {
|
||||
state = AddTransactionState(
|
||||
amount: transaction.amount,
|
||||
category: transaction.category,
|
||||
type: transaction.type,
|
||||
date: transaction.date,
|
||||
note: transaction.note ?? '',
|
||||
editingId: transaction.id,
|
||||
);
|
||||
}
|
||||
|
||||
void reset() => state = AddTransactionState(date: DateTime.now());
|
||||
}
|
||||
|
||||
@@ -53,3 +81,9 @@ final addTransactionProvider =
|
||||
StateNotifierProvider.autoDispose<AddTransactionNotifier, AddTransactionState>(
|
||||
(ref) => AddTransactionNotifier(),
|
||||
);
|
||||
|
||||
// Reactive categories based on selected type
|
||||
final availableCategoriesProvider = Provider.autoDispose<List<String>>((ref) {
|
||||
final type = ref.watch(addTransactionProvider.select((s) => s.type));
|
||||
return AppCategories.forType(type);
|
||||
});
|
||||
|
||||
@@ -12,7 +12,9 @@ import 'provider.dart';
|
||||
const _uuid = Uuid();
|
||||
|
||||
class AddTransactionScreen extends ConsumerStatefulWidget {
|
||||
const AddTransactionScreen({super.key});
|
||||
final Transaction? initial;
|
||||
|
||||
const AddTransactionScreen({super.key, this.initial});
|
||||
|
||||
@override
|
||||
ConsumerState<AddTransactionScreen> createState() =>
|
||||
@@ -24,6 +26,18 @@ class _AddTransactionScreenState extends ConsumerState<AddTransactionScreen> {
|
||||
final _amountController = TextEditingController();
|
||||
final _noteController = TextEditingController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
if (widget.initial != null) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
ref.read(addTransactionProvider.notifier).initializeForEdit(widget.initial!);
|
||||
_amountController.text = widget.initial!.amount.toString();
|
||||
_noteController.text = widget.initial!.note ?? '';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_amountController.dispose();
|
||||
@@ -37,7 +51,7 @@ class _AddTransactionScreenState extends ConsumerState<AddTransactionScreen> {
|
||||
ref.read(addTransactionProvider.notifier).setSubmitting(true);
|
||||
|
||||
final tx = Transaction(
|
||||
id: _uuid.v4(),
|
||||
id: state.editingId ?? _uuid.v4(),
|
||||
amount: state.amount!,
|
||||
category: state.category,
|
||||
type: state.type,
|
||||
@@ -45,7 +59,12 @@ class _AddTransactionScreenState extends ConsumerState<AddTransactionScreen> {
|
||||
note: state.note.isEmpty ? null : state.note,
|
||||
);
|
||||
|
||||
await ref.read(transactionsProvider.notifier).add(tx);
|
||||
if (state.isEditing) {
|
||||
await ref.read(transactionsProvider.notifier).update(tx);
|
||||
} else {
|
||||
await ref.read(transactionsProvider.notifier).add(tx);
|
||||
}
|
||||
|
||||
ref.read(addTransactionProvider.notifier).setSubmitting(false);
|
||||
|
||||
if (mounted) context.pop();
|
||||
@@ -76,11 +95,12 @@ class _AddTransactionScreenState extends ConsumerState<AddTransactionScreen> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final state = ref.watch(addTransactionProvider);
|
||||
final categories = ref.watch(availableCategoriesProvider);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: AppColors.background,
|
||||
appBar: AppBar(
|
||||
title: const Text('Add Transaction'),
|
||||
title: Text(state.isEditing ? 'Edit Transaction' : 'Add Transaction'),
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.close_rounded),
|
||||
onPressed: () => context.pop(),
|
||||
@@ -139,6 +159,7 @@ class _AddTransactionScreenState extends ConsumerState<AddTransactionScreen> {
|
||||
_SectionLabel('Category'),
|
||||
const SizedBox(height: 8),
|
||||
_CategoryPicker(
|
||||
categories: categories,
|
||||
selected: state.category,
|
||||
onChanged: (c) =>
|
||||
ref.read(addTransactionProvider.notifier).setCategory(c),
|
||||
@@ -200,7 +221,7 @@ class _AddTransactionScreenState extends ConsumerState<AddTransactionScreen> {
|
||||
color: Colors.white,
|
||||
),
|
||||
)
|
||||
: const Text('Save Transaction'),
|
||||
: Text(state.isEditing ? 'Update Transaction' : 'Save Transaction'),
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -309,16 +330,21 @@ class _TypeOption extends StatelessWidget {
|
||||
}
|
||||
|
||||
class _CategoryPicker extends StatelessWidget {
|
||||
final List<String> categories;
|
||||
final String selected;
|
||||
final ValueChanged<String> onChanged;
|
||||
const _CategoryPicker({required this.selected, required this.onChanged});
|
||||
const _CategoryPicker({
|
||||
required this.categories,
|
||||
required this.selected,
|
||||
required this.onChanged,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: AppCategories.all.map((cat) {
|
||||
children: categories.map((cat) {
|
||||
final isSelected = cat == selected;
|
||||
final color = AppCategories.colors[cat] ?? AppColors.accent;
|
||||
final icon = AppCategories.icons[cat] ?? Icons.category_rounded;
|
||||
|
||||
Reference in New Issue
Block a user