77 lines
2.4 KiB
Dart
77 lines
2.4 KiB
Dart
import 'dart:async';
|
|
import 'dart:math';
|
|
import 'package:drift/drift.dart' show Value;
|
|
import '../database/database.dart' as db;
|
|
import '../models/category.dart';
|
|
import '../utils/category_utils.dart';
|
|
|
|
class ExpensesController {
|
|
final db.AppDatabase database;
|
|
|
|
// State variables
|
|
int _selectedPieIndex = -1;
|
|
bool _isPieChartExpanded = true;
|
|
bool _isFilterVisible = false;
|
|
String _selectedFilter = 'All';
|
|
|
|
ExpensesController(this.database);
|
|
|
|
// Getters
|
|
int get selectedPieIndex => _selectedPieIndex;
|
|
bool get isPieChartExpanded => _isPieChartExpanded;
|
|
bool get isFilterVisible => _isFilterVisible;
|
|
String get selectedFilter => _selectedFilter;
|
|
|
|
// Stream that provides transactions based on the selected filter
|
|
Stream<List<db.Transaction>> watchTransactions() {
|
|
return database.watchFilteredTransactions(_selectedFilter);
|
|
}
|
|
|
|
// Stream that provides category totals calculated from transactions
|
|
Stream<List<Category>> watchCategoryTotals() {
|
|
return database.calculateCategoryTotals();
|
|
}
|
|
|
|
// Handles selection of a pie chart slice
|
|
void selectPieCategory(int index) {
|
|
_selectedPieIndex = (_selectedPieIndex == index) ? -1 : index;
|
|
}
|
|
|
|
// Toggles the visibility of the pie chart section
|
|
void togglePieChartVisibility() {
|
|
_isPieChartExpanded = !_isPieChartExpanded;
|
|
}
|
|
|
|
// Toggles the visibility of the filter chip row
|
|
void toggleFilterVisibility() {
|
|
_isFilterVisible = !_isFilterVisible;
|
|
}
|
|
|
|
// Applies the selected filter to the transaction list
|
|
void applyFilter(String filter) {
|
|
_selectedFilter = filter;
|
|
}
|
|
|
|
// Adds a sample transaction for testing/demo
|
|
Future<void> addSampleTransaction() async {
|
|
final random = Random();
|
|
final categories = CategoryUtils.getAllCategoryNames();
|
|
final randomCategory = categories[random.nextInt(categories.length)];
|
|
final randomAmount = (random.nextDouble() * 100) + 5;
|
|
final randomDay = random.nextInt(7);
|
|
final randomHour = random.nextInt(24);
|
|
final randomMerchant = [
|
|
'Amazon', 'Local Cafe', 'Gas Station', 'Online Store', 'Supermarket'
|
|
][random.nextInt(5)];
|
|
|
|
final newTransaction = db.TransactionsCompanion.insert(
|
|
categoryName: randomCategory,
|
|
amount: randomAmount,
|
|
date: DateTime.now().subtract(Duration(days: randomDay, hours: randomHour)),
|
|
merchant: randomMerchant,
|
|
);
|
|
|
|
await database.addTransaction(newTransaction);
|
|
}
|
|
}
|