mirror of
https://github.com/koloideal/Casha.git
synced 2026-06-10 10:25: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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user