Fixed wallets load #304

Merged
tech merged 1 commits from wallets-303 into main 2026-01-22 17:18:04 +00:00
9 changed files with 135 additions and 167 deletions

View File

@@ -12,9 +12,9 @@ class WalletsController with ChangeNotifier {
String? _orgRef; String? _orgRef;
/// UI-only: which wallets are allowed to be visible /// UI-only: which wallets are allowed to be visible
final Set<String> _visibleWalletIds = <String>{}; final Set<String> _visibleWalletRefs = <String>{};
String? _selectedWalletId; String? _selectedWalletRef;
bool get isLoading => _wallets.isLoading; bool get isLoading => _wallets.isLoading;
Exception? get error => _wallets.error; Exception? get error => _wallets.error;
@@ -27,64 +27,60 @@ class WalletsController with ChangeNotifier {
if (orgChanged) { if (orgChanged) {
_orgRef = nextOrgRef; _orgRef = nextOrgRef;
_visibleWalletIds.clear(); // All wallets hidden on org change _visibleWalletRefs.clear(); // All wallets hidden on org change
_selectedWalletId = null; _selectedWalletRef = null;
} }
// Remove ids that no longer exist // Remove ids that no longer exist
final ids = wallets.wallets.map((w) => w.id).toSet(); final ids = wallets.wallets.map((w) => w.id).toSet();
_visibleWalletIds.removeWhere((id) => !ids.contains(id)); _visibleWalletRefs.removeWhere((id) => !ids.contains(id));
final beforeSelected = _selectedWalletId; _selectedWalletRef = _resolveSelectedId(
currentRef: _selectedWalletRef,
_selectedWalletId = _resolveSelectedId(
currentId: _selectedWalletId,
wallets: wallets.wallets, wallets: wallets.wallets,
visibleIds: _visibleWalletIds, visibleRefs: _visibleWalletRefs,
); );
if (beforeSelected != _selectedWalletId || orgChanged) { notifyListeners();
notifyListeners();
}
} }
List<Wallet> get wallets => _wallets.wallets; List<Wallet> get wallets => _wallets.wallets;
bool isVisible(String walletId) => _visibleWalletIds.contains(walletId); bool isVisible(String walletRef) => _visibleWalletRefs.contains(walletRef);
bool isHidden(String walletId) => !isVisible(walletId); bool isHidden(String walletRef) => !isVisible(walletRef);
List<Wallet> get visibleWallets => List<Wallet> get visibleWallets =>
wallets.where((w) => _visibleWalletIds.contains(w.id)).toList(growable: false); wallets.where((w) => _visibleWalletRefs.contains(w.id)).toList(growable: false);
Wallet? get selectedWallet { Wallet? get selectedWallet {
final id = _selectedWalletId; final id = _selectedWalletRef;
if (id == null) return null; if (id == null) return null;
return wallets.firstWhereOrNull((w) => w.id == id); return wallets.firstWhereOrNull((w) => w.id == id);
} }
String? get selectedWalletId => _selectedWalletId; String? get selectedWalletRef => _selectedWalletRef;
void selectWallet(Wallet wallet) => selectWalletId(wallet.id); void selectWallet(Wallet wallet) => selectWalletByRef(wallet.id);
void selectWalletId(String walletId) { void selectWalletByRef(String walletRef) {
if (_selectedWalletId == walletId) return; if (_selectedWalletRef == walletRef) return;
// Prevent selecting a hidden wallet // Prevent selecting a hidden wallet
if (!_visibleWalletIds.contains(walletId)) return; if (!_visibleWalletRefs.contains(walletRef)) return;
_selectedWalletId = walletId; _selectedWalletRef = walletRef;
notifyListeners(); notifyListeners();
} }
/// Toggle wallet visibility /// Toggle wallet visibility
void toggleVisibility(String walletId) { void toggleVisibility(String walletId) {
final existed = _visibleWalletIds.remove(walletId); final existed = _visibleWalletRefs.remove(walletId);
if (!existed) _visibleWalletIds.add(walletId); if (!existed) _visibleWalletRefs.add(walletId);
_selectedWalletId = _resolveSelectedId( _selectedWalletRef = _resolveSelectedId(
currentId: _selectedWalletId, currentRef: _selectedWalletRef,
wallets: wallets, wallets: wallets,
visibleIds: _visibleWalletIds, visibleRefs: _visibleWalletRefs,
); );
notifyListeners(); notifyListeners();
@@ -92,36 +88,36 @@ class WalletsController with ChangeNotifier {
/// Show all wallets (bulk action) /// Show all wallets (bulk action)
void showAll() { void showAll() {
final allIds = wallets.map((w) => w.id); final allRefs = wallets.map((w) => w.id);
_visibleWalletIds _visibleWalletRefs
..clear() ..clear()
..addAll(allIds); ..addAll(allRefs);
_selectedWalletId = _resolveSelectedId( _selectedWalletRef = _resolveSelectedId(
currentId: _selectedWalletId, currentRef: _selectedWalletRef,
wallets: wallets, wallets: wallets,
visibleIds: _visibleWalletIds, visibleRefs: _visibleWalletRefs,
); );
notifyListeners(); notifyListeners();
} }
String? _resolveSelectedId({ String? _resolveSelectedId({
required String? currentId, required String? currentRef,
required List<Wallet> wallets, required List<Wallet> wallets,
required Set<String> visibleIds, required Set<String> visibleRefs,
}) { }) {
if (wallets.isEmpty) return null; if (wallets.isEmpty) return null;
// Keep current selection if it still exists and is visible // Keep current selection if it still exists and is visible
if (currentId != null && if (currentRef != null &&
visibleIds.contains(currentId) && visibleRefs.contains(currentRef) &&
wallets.any((w) => w.id == currentId)) { wallets.any((w) => w.id == currentRef)) {
return currentId; return currentRef;
} }
// Select the first visible wallet // Select the first visible wallet
final firstVisible = wallets.firstWhereOrNull((w) => visibleIds.contains(w.id)); final firstVisible = wallets.firstWhereOrNull((w) => visibleRefs.contains(w.id));
return firstVisible?.id; return firstVisible?.id;
} }
} }

View File

@@ -8,6 +8,7 @@ import 'package:provider/provider.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:pshared/config/constants.dart'; import 'package:pshared/config/constants.dart';
import 'package:pshared/controllers/wallets.dart';
import 'package:pshared/provider/locale.dart'; import 'package:pshared/provider/locale.dart';
import 'package:pshared/provider/permissions.dart'; import 'package:pshared/provider/permissions.dart';
import 'package:pshared/provider/account.dart'; import 'package:pshared/provider/account.dart';
@@ -20,10 +21,8 @@ import 'package:pshared/provider/invitations.dart';
import 'package:pshared/service/payment/wallets.dart'; import 'package:pshared/service/payment/wallets.dart';
import 'package:pweb/app/app.dart'; import 'package:pweb/app/app.dart';
import 'package:pshared/controllers/wallets.dart';
import 'package:pweb/pages/invitations/widgets/list/view_model.dart'; import 'package:pweb/pages/invitations/widgets/list/view_model.dart';
import 'package:pweb/app/timeago.dart'; import 'package:pweb/app/timeago.dart';
import 'package:pweb/providers/carousel.dart';
import 'package:pweb/providers/operatioins.dart'; import 'package:pweb/providers/operatioins.dart';
import 'package:pweb/providers/two_factor.dart'; import 'package:pweb/providers/two_factor.dart';
import 'package:pweb/providers/upload_history.dart'; import 'package:pweb/providers/upload_history.dart';
@@ -75,8 +74,6 @@ void main() async {
create: (_) => EmployeesProvider(), create: (_) => EmployeesProvider(),
update: (context, organizations, provider) => provider!..updateProviders(organizations), update: (context, organizations, provider) => provider!..updateProviders(organizations),
), ),
ChangeNotifierProvider(create: (_) => CarouselIndexProvider()),
ChangeNotifierProvider( ChangeNotifierProvider(
create: (_) => UploadHistoryProvider(service: MockUploadHistoryService())..load(), create: (_) => UploadHistoryProvider(service: MockUploadHistoryService())..load(),
), ),

View File

@@ -1,10 +1,10 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:pshared/controllers/wallets.dart'; import 'package:pshared/controllers/wallets.dart';
import 'package:pshared/utils/currency.dart';
import 'package:pshared/models/payment/wallet.dart'; import 'package:pshared/models/payment/wallet.dart';
import 'package:pshared/utils/currency.dart';
class BalanceAmount extends StatelessWidget { class BalanceAmount extends StatelessWidget {

View File

@@ -6,6 +6,7 @@ import 'package:pshared/controllers/wallets.dart';
import 'package:pshared/models/payment/wallet.dart'; import 'package:pshared/models/payment/wallet.dart';
import 'package:pweb/pages/dashboard/buttons/balance/carousel.dart'; import 'package:pweb/pages/dashboard/buttons/balance/carousel.dart';
import 'package:pweb/pages/dashboard/buttons/balance/controller.dart';
import 'package:pweb/generated/i18n/app_localizations.dart'; import 'package:pweb/generated/i18n/app_localizations.dart';
@@ -13,26 +14,45 @@ import 'package:pweb/generated/i18n/app_localizations.dart';
class BalanceWidget extends StatelessWidget { class BalanceWidget extends StatelessWidget {
final ValueChanged<Wallet> onTopUp; final ValueChanged<Wallet> onTopUp;
const BalanceWidget({super.key, required this.onTopUp}); const BalanceWidget({
super.key,
required this.onTopUp,
});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final walletsController = context.watch<WalletsController>(); final walletsController = context.watch<WalletsController>();
final carousel = context.watch<CarouselIndexController>();
final loc = AppLocalizations.of(context)!; final loc = AppLocalizations.of(context)!;
if (walletsController.isLoading) { if (walletsController.isLoading) {
return const Center(child: CircularProgressIndicator()); return const Center(child: CircularProgressIndicator());
} }
final wallets = walletsController.wallets; final wallets = walletsController.wallets;
if (wallets.isEmpty) { if (wallets.isEmpty) {
return Center(child: Text(loc.noWalletsAvailable)); return Center(child: Text(loc.noWalletsAvailable));
} }
// Ensure index is always valid when wallets list changes
carousel.setIndex(carousel.index, wallets.length);
final index = carousel.index;
final wallet = wallets[index];
// Single source of truth: controller
if (walletsController.selectedWallet?.id != wallet.id) {
walletsController.selectWallet(wallet);
}
return WalletCarousel( return WalletCarousel(
wallets: wallets, wallets: wallets,
onWalletChanged: walletsController.selectWallet, currentIndex: index,
onIndexChanged: (i) {
carousel.setIndex(i, wallets.length);
walletsController.selectWallet(wallets[i]);
},
onTopUp: onTopUp, onTopUp: onTopUp,
); );
} }

View File

@@ -1,101 +1,44 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:pshared/models/payment/wallet.dart'; import 'package:pshared/models/payment/wallet.dart';
import 'package:pweb/pages/dashboard/buttons/balance/card.dart'; import 'package:pweb/pages/dashboard/buttons/balance/card.dart';
import 'package:pweb/pages/dashboard/buttons/balance/config.dart'; import 'package:pweb/pages/dashboard/buttons/balance/config.dart';
import 'package:pweb/pages/dashboard/buttons/balance/indicator.dart'; import 'package:pweb/pages/dashboard/buttons/balance/indicator.dart';
import 'package:pweb/providers/carousel.dart';
class WalletCarousel extends StatefulWidget { class WalletCarousel extends StatelessWidget {
final List<Wallet> wallets; final List<Wallet> wallets;
final ValueChanged<Wallet> onWalletChanged; final int currentIndex;
final ValueChanged<int> onIndexChanged;
final ValueChanged<Wallet> onTopUp; final ValueChanged<Wallet> onTopUp;
const WalletCarousel({ const WalletCarousel({
super.key, super.key,
required this.wallets, required this.wallets,
required this.onWalletChanged, required this.currentIndex,
required this.onIndexChanged,
required this.onTopUp, required this.onTopUp,
}); });
@override
State<WalletCarousel> createState() => _WalletCarouselState();
}
class _WalletCarouselState extends State<WalletCarousel> {
late final PageController _pageController;
int _currentPage = 0;
@override
void initState() {
super.initState();
_pageController = PageController(
viewportFraction: WalletCardConfig.viewportFraction,
);
WidgetsBinding.instance.addPostFrameCallback((_) {
if (widget.wallets.isNotEmpty) {
widget.onWalletChanged(widget.wallets[_currentPage]);
}
});
}
@override
void dispose() {
_pageController.dispose();
super.dispose();
}
void _onPageChanged(int index) {
setState(() {
_currentPage = index;
});
context.read<CarouselIndexProvider>().updateIndex(index);
widget.onWalletChanged(widget.wallets[index]);
}
void _goToPreviousPage() {
if (_currentPage > 0) {
_pageController.animateToPage(
_currentPage - 1,
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
);
}
}
void _goToNextPage() {
if (_currentPage < widget.wallets.length - 1) {
_pageController.animateToPage(
_currentPage + 1,
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
);
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (wallets.isEmpty) {
return const SizedBox.shrink();
}
final safeIndex = currentIndex.clamp(0, wallets.length - 1);
final wallet = wallets[safeIndex];
return Column( return Column(
children: [ children: [
SizedBox( SizedBox(
height: WalletCardConfig.cardHeight, height: WalletCardConfig.cardHeight,
child: PageView.builder( child: Padding(
controller: _pageController, padding: WalletCardConfig.cardPadding,
physics: const NeverScrollableScrollPhysics(), child: WalletCard(
itemCount: widget.wallets.length, wallet: wallet,
onPageChanged: _onPageChanged, onTopUp: () => onTopUp(wallet),
itemBuilder: (context, index) { ),
return Padding(
padding: WalletCardConfig.cardPadding,
child: WalletCard(
wallet: widget.wallets[index],
onTopUp: () => widget.onTopUp(widget.wallets[index]),
),
);
},
), ),
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
@@ -103,15 +46,20 @@ class _WalletCarouselState extends State<WalletCarousel> {
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
IconButton( IconButton(
onPressed: _currentPage > 0 ? _goToPreviousPage : null, onPressed: safeIndex > 0
? () => onIndexChanged(safeIndex - 1)
: null,
icon: const Icon(Icons.arrow_back), icon: const Icon(Icons.arrow_back),
), ),
const SizedBox(width: 16), const SizedBox(width: 16),
CarouselIndicator(itemCount: widget.wallets.length), CarouselIndicator(
itemCount: wallets.length,
index: safeIndex,
),
const SizedBox(width: 16), const SizedBox(width: 16),
IconButton( IconButton(
onPressed: _currentPage < widget.wallets.length - 1 onPressed: safeIndex < wallets.length - 1
? _goToNextPage ? () => onIndexChanged(safeIndex + 1)
: null, : null,
icon: const Icon(Icons.arrow_forward), icon: const Icon(Icons.arrow_forward),
), ),

View File

@@ -0,0 +1,21 @@
import 'package:flutter/foundation.dart';
class CarouselIndexController with ChangeNotifier {
int _index = 0;
int get index => _index;
void setIndex(int value, int max) {
final next = value.clamp(0, max > 0 ? max - 1 : 0);
if (next == _index) return;
_index = next;
notifyListeners();
}
void reset() {
if (_index == 0) return;
_index = 0;
notifyListeners();
}
}

View File

@@ -1,31 +1,26 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:pweb/pages/dashboard/buttons/balance/config.dart'; import 'package:pweb/pages/dashboard/buttons/balance/config.dart';
import 'package:pweb/providers/carousel.dart';
class CarouselIndicator extends StatelessWidget { class CarouselIndicator extends StatelessWidget {
final int itemCount; final int itemCount;
final int index;
const CarouselIndicator({ const CarouselIndicator({
super.key, super.key,
required this.itemCount, required this.itemCount,
required this.index,
}); });
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => Row(
final currentIndex = context.watch<CarouselIndexProvider>().currentIndex; mainAxisAlignment: MainAxisAlignment.center,
children: List.generate(
return Row( itemCount,
mainAxisAlignment: MainAxisAlignment.center, (i) => _Dot(isActive: i == index),
children: List.generate( ),
itemCount, );
(index) => _Dot(isActive: currentIndex == index),
),
);
}
} }
class _Dot extends StatelessWidget { class _Dot extends StatelessWidget {
@@ -35,15 +30,15 @@ class _Dot extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final color = Theme.of(context).colorScheme.primary;
return Container( return Container(
width: WalletCardConfig.dotSize, width: WalletCardConfig.dotSize,
height: WalletCardConfig.dotSize, height: WalletCardConfig.dotSize,
margin: WalletCardConfig.dotMargin, margin: WalletCardConfig.dotMargin,
decoration: BoxDecoration( decoration: BoxDecoration(
shape: BoxShape.circle, shape: BoxShape.circle,
color: isActive color: isActive ? color : color.withAlpha(64),
? Theme.of(context).colorScheme.primary
: Theme.of(context).colorScheme.primary.withAlpha(60),
), ),
); );
} }

View File

@@ -1,10 +1,13 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:pshared/models/payment/type.dart'; import 'package:pshared/models/payment/type.dart';
import 'package:pshared/models/recipient/recipient.dart'; import 'package:pshared/models/recipient/recipient.dart';
import 'package:pshared/models/payment/wallet.dart'; import 'package:pshared/models/payment/wallet.dart';
import 'package:pweb/pages/dashboard/buttons/balance/balance.dart'; import 'package:pweb/pages/dashboard/buttons/balance/balance.dart';
import 'package:pweb/pages/dashboard/buttons/balance/controller.dart';
import 'package:pweb/pages/dashboard/buttons/buttons.dart'; import 'package:pweb/pages/dashboard/buttons/buttons.dart';
import 'package:pweb/pages/dashboard/payouts/multiple/title.dart'; import 'package:pweb/pages/dashboard/payouts/multiple/title.dart';
import 'package:pweb/pages/dashboard/payouts/multiple/widget.dart'; import 'package:pweb/pages/dashboard/payouts/multiple/widget.dart';
@@ -80,8 +83,11 @@ class _DashboardPageState extends State<DashboardPage> {
], ],
), ),
const SizedBox(height: AppSpacing.medium), const SizedBox(height: AppSpacing.medium),
BalanceWidget( ChangeNotifierProvider(
onTopUp: widget.onTopUp, create: (_) => CarouselIndexController(),
child: BalanceWidget(
onTopUp: widget.onTopUp,
),
), ),
const SizedBox(height: AppSpacing.small), const SizedBox(height: AppSpacing.small),
if (_showContainerMultiple) TitleMultiplePayout(), if (_showContainerMultiple) TitleMultiplePayout(),

View File

@@ -1,15 +0,0 @@
import 'package:flutter/material.dart';
class CarouselIndexProvider extends ChangeNotifier {
int _currentIndex = 0;
int get currentIndex => _currentIndex;
void updateIndex(int index) {
if (_currentIndex != index) {
_currentIndex = index;
notifyListeners();
}
}
}