From 546bd0dc864a4e83366de958be35c3603d5b9fbd Mon Sep 17 00:00:00 2001 From: kolo Date: Tue, 24 Mar 2026 15:54:38 +0300 Subject: [PATCH] update --- lib/features/dashboard/screen.dart | 82 +++++++++++++++---- .../widgets/account_editor_overlay.dart | 9 +- .../widgets/balance_card_carousel.dart | 57 +++++++------ 3 files changed, 103 insertions(+), 45 deletions(-) diff --git a/lib/features/dashboard/screen.dart b/lib/features/dashboard/screen.dart index e27e5da..bb23b37 100644 --- a/lib/features/dashboard/screen.dart +++ b/lib/features/dashboard/screen.dart @@ -2,9 +2,11 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import 'package:intl/intl.dart'; +import 'package:drift/drift.dart' as drift; import '../../core/l10n/locale_provider.dart'; import '../../core/services/card_color_service.dart'; import '../../core/services/haptic_service.dart'; +import '../../data/database/app_database.dart' hide Account; import '../../shared/models/account.dart'; import '../settings/provider.dart'; import 'provider.dart'; @@ -52,6 +54,7 @@ class _DashboardScreenState extends ConsumerState { Account? editingAccount; String tempAccountName = ''; String tempAccountCurrency = 'USD'; + bool isAddingAccount = false; void _onCardLongPress() { final colors = ref.read(cardColorsProvider); @@ -130,26 +133,73 @@ class _DashboardScreenState extends ConsumerState { Overlay.of(context, rootOverlay: true).insert(overlayEntry!); } + void _onAddAccountTapped() { + final colors = ref.read(cardColorsProvider); + savedPrimary = colors.primary; + savedSecondary = colors.secondary; + savedPrimaryHSV = HSVColor.fromColor(colors.primary); + savedSecondaryHSV = HSVColor.fromColor(colors.secondary); + savedGradientType = colors.gradientType; + tempPrimary = colors.primary; + tempSecondary = colors.secondary; + tempPrimaryHSV = HSVColor.fromColor(colors.primary); + tempSecondaryHSV = HSVColor.fromColor(colors.secondary); + tempGradientType = colors.gradientType; + + setState(() { + isAddingAccount = true; + editingAccount = null; + tempAccountName = ''; + tempAccountCurrency = ref.read(currencyProvider).code; + editingCard = true; + editingPrimary = true; + }); + _showAccountOverlay(); + } + void closeAccountOverlay({required bool apply}) async { - if (apply && editingAccount != null) { + if (apply && tempAccountName.trim().isNotEmpty) { HapticService.medium(); - // Save colors - await ref - .read(accountCardColorsProvider(editingAccount!.id).notifier) - .save(tempPrimary, tempSecondary, tempGradientType); + if (isAddingAccount) { + // Create new account + final newId = DateTime.now().millisecondsSinceEpoch; - // Update account name and currency - final updatedAccount = Account( - id: editingAccount!.id, - name: tempAccountName, - isMain: editingAccount!.isMain, - sortOrder: editingAccount!.sortOrder, - currency: tempAccountCurrency, - createdAt: editingAccount!.createdAt, - ); + // Insert the new account using Drift's insert method + final db = ref.read(appDatabaseProvider); + await db.into(db.accounts).insert( + AccountsCompanion.insert( + id: drift.Value(newId), + name: tempAccountName.trim(), + isMain: const drift.Value(false), + currency: drift.Value(tempAccountCurrency), + sortOrder: const drift.Value(99), + ), + ); - await ref.read(accountRepositoryProvider).update(updatedAccount); + // Save the chosen colors for the newly created account + await ref + .read(accountCardColorsProvider(newId).notifier) + .save(tempPrimary, tempSecondary, tempGradientType); + } else if (editingAccount != null) { + // Existing edit logic + // Save colors + await ref + .read(accountCardColorsProvider(editingAccount!.id).notifier) + .save(tempPrimary, tempSecondary, tempGradientType); + + // Update account name and currency + final updatedAccount = Account( + id: editingAccount!.id, + name: tempAccountName.trim(), + isMain: editingAccount!.isMain, + sortOrder: editingAccount!.sortOrder, + currency: tempAccountCurrency, + createdAt: editingAccount!.createdAt, + ); + + await ref.read(accountRepositoryProvider).update(updatedAccount); + } } else { // Restore original values on cancel setState(() { @@ -168,6 +218,7 @@ class _DashboardScreenState extends ConsumerState { setState(() { editingCard = false; editingAccount = null; + isAddingAccount = false; }); } @@ -287,6 +338,7 @@ class _DashboardScreenState extends ConsumerState { currencyInfo: currencyInfo, onLongPress: _onCardLongPress, onAccountLongPress: _onAccountCardLongPress, + onAddAccountTap: _onAddAccountTapped, previewPrimary: editingCard ? tempPrimary : null, previewSecondary: editingCard ? tempSecondary : null, previewGradientType: editingCard diff --git a/lib/features/dashboard/widgets/account_editor_overlay.dart b/lib/features/dashboard/widgets/account_editor_overlay.dart index ffa05d0..1be546c 100644 --- a/lib/features/dashboard/widgets/account_editor_overlay.dart +++ b/lib/features/dashboard/widgets/account_editor_overlay.dart @@ -345,7 +345,7 @@ class _AccountEditorOverlayState extends State { border: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: BorderSide( - color: _showLimitError + color: (_showLimitError || _nameController.text.trim().isEmpty) ? Colors.red : Theme.of(widget.context).colorScheme.onSurface.withOpacity(0.15), width: 1.5, @@ -354,7 +354,7 @@ class _AccountEditorOverlayState extends State { enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: BorderSide( - color: _showLimitError + color: (_showLimitError || _nameController.text.trim().isEmpty) ? Colors.red : Theme.of(widget.context).colorScheme.onSurface.withOpacity(0.15), width: 1.5, @@ -363,7 +363,7 @@ class _AccountEditorOverlayState extends State { focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: BorderSide( - color: _showLimitError + color: (_showLimitError || _nameController.text.trim().isEmpty) ? Colors.red : const Color(0xFF7C6DED), width: 1.5, @@ -879,7 +879,8 @@ class _AccountEditorOverlayState extends State { shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12)), ), - child: Text(s.apply, + child: Text( + dash.isAddingAccount ? 'Создать счёт' : s.apply, style: const TextStyle( fontWeight: FontWeight.w700, fontSize: 14)), ), diff --git a/lib/features/dashboard/widgets/balance_card_carousel.dart b/lib/features/dashboard/widgets/balance_card_carousel.dart index 28fa051..310725a 100644 --- a/lib/features/dashboard/widgets/balance_card_carousel.dart +++ b/lib/features/dashboard/widgets/balance_card_carousel.dart @@ -13,6 +13,7 @@ class BalanceCardCarousel extends ConsumerStatefulWidget { final CurrencyInfo currencyInfo; final VoidCallback? onLongPress; final void Function(Account)? onAccountLongPress; + final VoidCallback? onAddAccountTap; final Color? previewPrimary; final Color? previewSecondary; final GradientType? previewGradientType; @@ -23,6 +24,7 @@ class BalanceCardCarousel extends ConsumerStatefulWidget { required this.currencyInfo, this.onLongPress, this.onAccountLongPress, + this.onAddAccountTap, this.previewPrimary, this.previewSecondary, this.previewGradientType, @@ -117,7 +119,7 @@ class _BalanceCardCarouselState extends ConsumerState { ); } else { cardWidget = AddAccountCard( - onTap: () {}, + onTap: widget.onAddAccountTap, ); } @@ -177,33 +179,36 @@ class AddAccountCard extends StatelessWidget { Widget build(BuildContext context) { return GestureDetector( onTap: onTap, - child: CustomPaint( - painter: _DashedBorderPainter(), - child: Container( - width: double.infinity, - height: 220, - decoration: BoxDecoration( - color: Theme.of(context).colorScheme.surface.withOpacity(0.4), - borderRadius: BorderRadius.circular(20), - ), - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Icon( - Icons.add_rounded, - size: 32, - color: Theme.of(context).colorScheme.onSurface.withOpacity(0.5), - ), - const SizedBox(height: 8), - Text( - 'Add account', - style: TextStyle( - fontSize: 14, + child: Container( + margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 15), // makes it smaller + child: CustomPaint( + painter: _DashedBorderPainter(), + child: Container( + width: double.infinity, + height: 190, // reduced from 220 + decoration: BoxDecoration( + color: Theme.of(context).colorScheme.surface.withOpacity(0.4), + borderRadius: BorderRadius.circular(20), + ), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Icon( + Icons.add_rounded, + size: 32, color: Theme.of(context).colorScheme.onSurface.withOpacity(0.5), - fontWeight: FontWeight.w500, ), - ), - ], + const SizedBox(height: 8), + Text( + 'Add account', + style: TextStyle( + fontSize: 14, + color: Theme.of(context).colorScheme.onSurface.withOpacity(0.5), + fontWeight: FontWeight.w500, + ), + ), + ], + ), ), ), ),