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,10 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../../shared/services/exchange_rate_service.dart';
|
||||
import '../dashboard/provider.dart';
|
||||
|
||||
final budgetProvider = StateNotifierProvider<BudgetNotifier, double?>((ref) {
|
||||
@@ -16,3 +22,113 @@ class BudgetNotifier extends StateNotifier<double?> {
|
||||
state = budget;
|
||||
}
|
||||
}
|
||||
|
||||
// Currency info: symbol and code
|
||||
class CurrencyInfo {
|
||||
final String symbol;
|
||||
final String code;
|
||||
const CurrencyInfo(this.symbol, this.code);
|
||||
}
|
||||
|
||||
const Map<String, CurrencyInfo> currencyMap = {
|
||||
'USD': CurrencyInfo('\$', 'USD'),
|
||||
'EUR': CurrencyInfo('€', 'EUR'),
|
||||
'BYN': CurrencyInfo('Br', 'BYN'),
|
||||
'RUB': CurrencyInfo('₽', 'RUB'),
|
||||
};
|
||||
|
||||
class CurrencyNotifier extends StateNotifier<CurrencyInfo> {
|
||||
CurrencyNotifier() : super(currencyMap['USD']!) {
|
||||
_load();
|
||||
}
|
||||
|
||||
void _load() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final code = prefs.getString('currency_code') ?? 'USD';
|
||||
state = currencyMap[code] ?? currencyMap['USD']!;
|
||||
}
|
||||
|
||||
Future<void> setCurrency(String code) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
state = currencyMap[code] ?? currencyMap['USD']!;
|
||||
await prefs.setString('currency_code', code);
|
||||
}
|
||||
}
|
||||
|
||||
final currencyProvider = StateNotifierProvider<CurrencyNotifier, CurrencyInfo>(
|
||||
(ref) => CurrencyNotifier(),
|
||||
);
|
||||
|
||||
class ThemeModeNotifier extends StateNotifier<ThemeMode> {
|
||||
ThemeModeNotifier() : super(ThemeMode.dark) {
|
||||
_load();
|
||||
}
|
||||
|
||||
void _load() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
state = (prefs.getBool('dark_mode') ?? true) ? ThemeMode.dark : ThemeMode.light;
|
||||
}
|
||||
|
||||
Future<void> toggle() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
state = state == ThemeMode.dark ? ThemeMode.light : ThemeMode.dark;
|
||||
await prefs.setBool('dark_mode', state == ThemeMode.dark);
|
||||
}
|
||||
|
||||
Future<void> setThemeMode(bool isDark) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
state = isDark ? ThemeMode.dark : ThemeMode.light;
|
||||
await prefs.setBool('dark_mode', isDark);
|
||||
}
|
||||
}
|
||||
|
||||
final themeProvider = StateNotifierProvider<ThemeModeNotifier, ThemeMode>(
|
||||
(ref) => ThemeModeNotifier(),
|
||||
);
|
||||
|
||||
// Exchange rate service
|
||||
final exchangeRateServiceProvider = Provider<ExchangeRateService>((ref) {
|
||||
final prefs = ref.watch(sharedPreferencesProvider);
|
||||
return ExchangeRateService(prefs);
|
||||
});
|
||||
|
||||
final ratesInitProvider = FutureProvider<void>((ref) async {
|
||||
await ref.read(exchangeRateServiceProvider).fetchRates();
|
||||
});
|
||||
|
||||
final exportProvider = Provider<ExportService>((ref) {
|
||||
return ExportService(ref);
|
||||
});
|
||||
|
||||
class ExportService {
|
||||
final Ref _ref;
|
||||
|
||||
ExportService(this._ref);
|
||||
|
||||
Future<String> exportToCSV() async {
|
||||
final transactions = _ref.read(transactionsProvider);
|
||||
final currency = _ref.read(currencyProvider);
|
||||
|
||||
// CSV header
|
||||
final buffer = StringBuffer();
|
||||
buffer.writeln('Date,Type,Category,Amount,Currency,Note');
|
||||
|
||||
// CSV rows
|
||||
for (final tx in transactions) {
|
||||
final date = DateFormat('yyyy-MM-dd').format(tx.date);
|
||||
final type = tx.type.name;
|
||||
final category = tx.category;
|
||||
final amount = '${tx.currency}${tx.amount.toStringAsFixed(2)}';
|
||||
final note = tx.note?.replaceAll(',', ';') ?? '';
|
||||
buffer.writeln('$date,$type,$category,$amount,${tx.currencyCode},$note');
|
||||
}
|
||||
|
||||
// Save to Downloads
|
||||
final directory = await getApplicationDocumentsDirectory();
|
||||
final timestamp = DateFormat('yyyyMMdd_HHmmss').format(DateTime.now());
|
||||
final file = File('${directory.path}/transactions_$timestamp.csv');
|
||||
await file.writeAsString(buffer.toString());
|
||||
|
||||
return file.path;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import '../../core/constants.dart';
|
||||
import 'provider.dart';
|
||||
|
||||
final _currencyFmt = NumberFormat.currency(symbol: '\$', decimalDigits: 2);
|
||||
|
||||
class SettingsScreen extends ConsumerStatefulWidget {
|
||||
const SettingsScreen({super.key});
|
||||
|
||||
@@ -17,10 +16,13 @@ class SettingsScreen extends ConsumerStatefulWidget {
|
||||
class _SettingsScreenState extends ConsumerState<SettingsScreen> {
|
||||
final _budgetController = TextEditingController();
|
||||
bool _isEditing = false;
|
||||
late NumberFormat _currencyFmt;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final currencyInfo = ref.read(currencyProvider);
|
||||
_currencyFmt = NumberFormat.currency(symbol: currencyInfo.symbol, decimalDigits: 2);
|
||||
final budget = ref.read(budgetProvider);
|
||||
if (budget != null) {
|
||||
_budgetController.text = budget.toStringAsFixed(2);
|
||||
@@ -49,27 +51,196 @@ class _SettingsScreenState extends ConsumerState<SettingsScreen> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final budget = ref.watch(budgetProvider);
|
||||
final themeMode = ref.watch(themeProvider);
|
||||
final isDarkMode = themeMode == ThemeMode.dark;
|
||||
final currencyInfo = ref.watch(currencyProvider);
|
||||
|
||||
// Update currency format when it changes
|
||||
_currencyFmt = NumberFormat.currency(symbol: currencyInfo.symbol, decimalDigits: 2);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: AppColors.background,
|
||||
body: SafeArea(
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.fromLTRB(20, 24, 20, 20),
|
||||
appBar: AppBar(
|
||||
title: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Settings',
|
||||
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
|
||||
style: Theme.of(context).textTheme.titleLarge?.copyWith(
|
||||
fontWeight: FontWeight.w700,
|
||||
color: AppColors.textPrimary,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'Manage your preferences',
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: AppColors.textSecondary,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 32),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.add_circle_rounded),
|
||||
iconSize: 32,
|
||||
color: AppColors.accent,
|
||||
onPressed: () => context.push('/add'),
|
||||
tooltip: 'Add Transaction',
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SafeArea(
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.fromLTRB(20, 16, 20, 20),
|
||||
children: [
|
||||
|
||||
// Theme Toggle
|
||||
Container(
|
||||
padding: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surface,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
border: Border.all(color: AppColors.divider),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(10),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.accent.withOpacity(0.15),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Icon(
|
||||
isDarkMode ? Icons.dark_mode_rounded : Icons.light_mode_rounded,
|
||||
color: AppColors.accent,
|
||||
size: 20,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Dark Mode',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.textPrimary,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
isDarkMode ? 'Enabled' : 'Disabled',
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: AppColors.textSecondary,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Switch(
|
||||
value: isDarkMode,
|
||||
onChanged: (value) {
|
||||
ref.read(themeProvider.notifier).setThemeMode(value);
|
||||
},
|
||||
activeColor: AppColors.accent,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Currency Selector
|
||||
Container(
|
||||
padding: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surface,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
border: Border.all(color: AppColors.divider),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(10),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.accent.withOpacity(0.15),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.attach_money_rounded,
|
||||
color: AppColors.accent,
|
||||
size: 20,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'Currency',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.textPrimary,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: ['USD', 'EUR', 'BYN', 'RUB'].map((code) {
|
||||
final info = currencyMap[code]!;
|
||||
final isSelected = currencyInfo.code == code;
|
||||
return Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(right: 8),
|
||||
child: GestureDetector(
|
||||
onTap: () {
|
||||
ref.read(currencyProvider.notifier).setCurrency(code);
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||||
decoration: BoxDecoration(
|
||||
color: isSelected
|
||||
? AppColors.accent.withOpacity(0.2)
|
||||
: AppColors.background,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: isSelected ? AppColors.accent : AppColors.divider,
|
||||
width: isSelected ? 1.5 : 1,
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Text(
|
||||
info.symbol,
|
||||
style: Theme.of(context).textTheme.titleLarge?.copyWith(
|
||||
color: isSelected ? AppColors.accent : AppColors.textSecondary,
|
||||
fontWeight: isSelected ? FontWeight.w700 : FontWeight.normal,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
code,
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: isSelected ? AppColors.accent : AppColors.textSecondary,
|
||||
fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Budget Setting
|
||||
Container(
|
||||
padding: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
@@ -123,8 +294,8 @@ class _SettingsScreenState extends ConsumerState<SettingsScreen> {
|
||||
inputFormatters: [
|
||||
FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d{0,2}')),
|
||||
],
|
||||
decoration: const InputDecoration(
|
||||
prefixText: '\$ ',
|
||||
decoration: InputDecoration(
|
||||
prefixText: '${currencyInfo.symbol} ',
|
||||
hintText: '0.00',
|
||||
helperText: 'Leave empty to remove budget limit',
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user