Skip to content

Commit

Permalink
Merge pull request #822 from threefoldtech/development_use_wallet_pro…
Browse files Browse the repository at this point in the history
…vider

Use wallet provider instead of fn propagation
  • Loading branch information
zaelgohary authored Jan 14, 2025
2 parents ebc973b + 933c84d commit 39e3b9b
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 105 deletions.
20 changes: 20 additions & 0 deletions app/lib/providers/wallets_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ class WalletsNotifier extends StateNotifier<List<Wallet>> {
});
}

Future<void> addWallet(Wallet wallet) async {
await _mutex.protect(() async {
state = [...state, wallet];
});
}

Future<void> editWallet(String oldName, String newName) async {
await _mutex.protect(() async {
final wallet = state.where((w) => w.name == oldName).firstOrNull;
if (wallet != null) {
wallet.name = newName;
}
state = [...state];
});
}

void reloadBalances() async {
if (!_reload) return await TFChainService.disconnect();
if (!_loading) {
Expand Down Expand Up @@ -70,6 +86,10 @@ class WalletsNotifier extends StateNotifier<List<Wallet>> {
_reload = true;
}

void clear() {
_isListed = false;
}

Wallet? getUpdatedWallet(String name) {
return state.where((w) => w.name == name).firstOrNull;
}
Expand Down
2 changes: 2 additions & 0 deletions app/lib/screens/preference_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:threebotlogin/events/events.dart';
import 'package:threebotlogin/helpers/environment.dart';
import 'package:threebotlogin/helpers/globals.dart';
import 'package:threebotlogin/helpers/logger.dart';
import 'package:threebotlogin/providers/wallets_provider.dart';

import 'package:threebotlogin/screens/authentication_screen.dart';
import 'package:threebotlogin/screens/change_pin_screen.dart';
Expand Down Expand Up @@ -324,6 +325,7 @@ class _PreferenceScreenState extends ConsumerState<PreferenceScreen> {
bool result = false;
if (deleted) {
result = await clearData();
ref.read(walletsNotifier.notifier).clear();
if (result) {
Navigator.pop(context);
await Navigator.pushReplacement(
Expand Down
2 changes: 1 addition & 1 deletion app/lib/screens/recover_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class _RecoverScreenState extends State<RecoverScreen> {
}

validateSeed(String seedPhrase, userInfoResult) async {
if (seedPhrase.isEmpty){
if (seedPhrase.isEmpty) {
throw ('Seed Phrase is required');
}
try {
Expand Down
15 changes: 7 additions & 8 deletions app/lib/screens/wallets/bridge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ import 'package:validators/validators.dart';
import 'package:threebotlogin/services/stellar_service.dart' as Stellar;
import 'package:threebotlogin/services/tfchain_service.dart' as TFChain;

class WalletBridgeScreen extends StatefulWidget {
const WalletBridgeScreen(
{super.key, required this.wallet, required this.allWallets});
class WalletBridgeScreen extends ConsumerStatefulWidget {
const WalletBridgeScreen({super.key, required this.wallet});
final Wallet wallet;
final List<Wallet> allWallets;

@override
State<WalletBridgeScreen> createState() => _WalletBridgeScreenState();
ConsumerState<WalletBridgeScreen> createState() => _WalletBridgeScreenState();
}

class _WalletBridgeScreenState extends State<WalletBridgeScreen> {
class _WalletBridgeScreenState extends ConsumerState<WalletBridgeScreen> {
final fromController = TextEditingController();
final toController = TextEditingController();
final amountController = TextEditingController();
Expand Down Expand Up @@ -171,6 +169,7 @@ class _WalletBridgeScreenState extends State<WalletBridgeScreen> {

@override
Widget build(BuildContext context) {
List<Wallet> wallets = ref.read(walletsNotifier);
final bool disableDeposit = widget.wallet.stellarBalance == '-1';
if (disableDeposit && !isWithdraw) {
onTransactionChange(BridgeOperation.Withdraw);
Expand Down Expand Up @@ -223,12 +222,12 @@ class _WalletBridgeScreenState extends State<WalletBridgeScreen> {
: ChainType.TFChain,
currentWalletAddress: fromController.text,
wallets: isWithdraw
? widget.allWallets
? wallets
.where((w) =>
double.parse(w.stellarBalance) >=
0)
.toList()
: widget.allWallets,
: wallets,
onSelectToAddress: _selectToAddress),
));
},
Expand Down
21 changes: 11 additions & 10 deletions app/lib/screens/wallets/send.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ import 'package:validators/validators.dart';
import 'package:threebotlogin/services/stellar_service.dart' as Stellar;
import 'package:threebotlogin/services/tfchain_service.dart' as TFChain;

class WalletSendScreen extends StatefulWidget {
const WalletSendScreen(
{super.key, required this.wallet, required this.allWallets});
class WalletSendScreen extends ConsumerStatefulWidget {
const WalletSendScreen({super.key, required this.wallet});
final Wallet wallet;
final List<Wallet> allWallets;

@override
State<WalletSendScreen> createState() => _WalletSendScreenState();
ConsumerState<WalletSendScreen> createState() => _WalletSendScreenState();
}

class _WalletSendScreenState extends State<WalletSendScreen> {
class _WalletSendScreenState extends ConsumerState<WalletSendScreen> {
final fromController = TextEditingController();
final toController = TextEditingController();
final amountController = TextEditingController();
Expand All @@ -39,9 +37,12 @@ class _WalletSendScreenState extends State<WalletSendScreen> {
bool reloadBalance = true;
final FocusNode textFieldFocusNode = FocusNode();
List percentages = [25, 50, 75, 100];
List<Wallet> wallets = [];

@override
void initState() {
fromController.text = widget.wallet.stellarAddress;
wallets = ref.read(walletsNotifier);
_reloadBalances();
super.initState();
}
Expand Down Expand Up @@ -127,8 +128,8 @@ class _WalletSendScreenState extends State<WalletSendScreen> {
return false;
}

final matchingWallets = widget.allWallets
.where((wallet) => wallet.stellarAddress == toAddress);
final matchingWallets =
wallets.where((wallet) => wallet.stellarAddress == toAddress);
final Wallet? wallet =
matchingWallets.isNotEmpty ? matchingWallets.first : null;
if (wallet != null && wallet.stellarBalance == '-1') {
Expand Down Expand Up @@ -268,13 +269,13 @@ class _WalletSendScreenState extends State<WalletSendScreen> {
currentWalletAddress:
fromController.text,
wallets: chainType == ChainType.Stellar
? widget.allWallets
? wallets
.where((w) =>
double.parse(
w.stellarBalance) >=
0)
.toList()
: widget.allWallets,
: wallets,
onSelectToAddress: _selectToAddress),
));
},
Expand Down
6 changes: 1 addition & 5 deletions app/lib/screens/wallets/wallet_assets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import 'package:threebotlogin/widgets/wallets/arrow_inward.dart';
import 'package:threebotlogin/widgets/wallets/balance_tile.dart';

class WalletAssetsWidget extends StatefulWidget {
const WalletAssetsWidget(
{super.key, required this.wallet, required this.allWallets});
const WalletAssetsWidget({super.key, required this.wallet});
final Wallet wallet;
final List<Wallet> allWallets;

@override
State<WalletAssetsWidget> createState() => _WalletAssetsWidgetState();
Expand Down Expand Up @@ -103,7 +101,6 @@ class _WalletAssetsWidgetState extends State<WalletAssetsWidget> {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => WalletSendScreen(
wallet: widget.wallet,
allWallets: widget.allWallets,
),
));
},
Expand Down Expand Up @@ -163,7 +160,6 @@ class _WalletAssetsWidgetState extends State<WalletAssetsWidget> {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => WalletBridgeScreen(
wallet: widget.wallet,
allWallets: widget.allWallets,
),
));
},
Expand Down
33 changes: 9 additions & 24 deletions app/lib/screens/wallets/wallet_details.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:threebotlogin/models/wallet.dart';
import 'package:threebotlogin/providers/wallets_provider.dart';
import 'package:threebotlogin/screens/wallets/transactions.dart';
import 'package:threebotlogin/screens/wallets/wallet_assets.dart';
import 'package:threebotlogin/screens/wallets/wallet_info.dart';

class WalletDetailsScreen extends StatefulWidget {
const WalletDetailsScreen(
{super.key,
required this.wallet,
required this.allWallets,
required this.onDeleteWallet,
required this.onEditWallet});
class WalletDetailsScreen extends ConsumerStatefulWidget {
const WalletDetailsScreen({super.key, required this.wallet});
final Wallet wallet;
final List<Wallet> allWallets;
final void Function(String name) onDeleteWallet;
final void Function(String oldName, String newName) onEditWallet;

@override
State<WalletDetailsScreen> createState() => _WalletDetailsScreenState();
ConsumerState<WalletDetailsScreen> createState() =>
_WalletDetailsScreenState();
}

class _WalletDetailsScreenState extends State<WalletDetailsScreen> {
class _WalletDetailsScreenState extends ConsumerState<WalletDetailsScreen> {
int currentScreenIndex = 0;

void _selectScreen(int index) {
Expand All @@ -29,28 +24,18 @@ class _WalletDetailsScreenState extends State<WalletDetailsScreen> {
});
}

void _onEditWallet(String oldName, String newName) {
widget.wallet.name = newName;
widget.onEditWallet(oldName, newName);
setState(() {});
}

@override
Widget build(BuildContext context) {
Widget content;
ref.watch(walletsNotifier).firstWhere((w) => w.name == widget.wallet.name);
if (currentScreenIndex == 1) {
content = WalletTransactionsWidget(
wallet: widget.wallet,
);
} else if (currentScreenIndex == 2) {
content = WalletDetailsWidget(
wallet: widget.wallet,
onDeleteWallet: widget.onDeleteWallet,
onEditWallet: _onEditWallet,
);
content = WalletDetailsWidget(wallet: widget.wallet);
} else {
content = WalletAssetsWidget(
allWallets: widget.allWallets,
wallet: widget.wallet,
);
}
Expand Down
28 changes: 16 additions & 12 deletions app/lib/screens/wallets/wallet_info.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:threebotlogin/helpers/logger.dart';
import 'package:threebotlogin/models/wallet.dart';
import 'package:threebotlogin/providers/wallets_provider.dart';
import 'package:threebotlogin/services/wallet_service.dart';
import 'package:threebotlogin/widgets/wallets/warning_dialog.dart';

class WalletDetailsWidget extends StatefulWidget {
const WalletDetailsWidget(
{super.key,
required this.wallet,
required this.onDeleteWallet,
required this.onEditWallet});
class WalletDetailsWidget extends ConsumerStatefulWidget {
const WalletDetailsWidget({super.key, required this.wallet});
final Wallet wallet;
final void Function(String name) onDeleteWallet;
final void Function(String oldName, String newName) onEditWallet;

@override
State<WalletDetailsWidget> createState() => _WalletDetailsWidgetState();
ConsumerState<WalletDetailsWidget> createState() =>
_WalletDetailsWidgetState();
}

class _WalletDetailsWidgetState extends State<WalletDetailsWidget> {
class _WalletDetailsWidgetState extends ConsumerState<WalletDetailsWidget> {
final stellarSecretController = TextEditingController();
final stellarAddressController = TextEditingController();
final tfchainSecretController = TextEditingController();
Expand All @@ -30,11 +27,18 @@ class _WalletDetailsWidgetState extends State<WalletDetailsWidget> {
bool showTfchainSecret = false;
bool showStellarSecret = false;
bool edit = false;
late WalletsNotifier walletsRef;

@override
void initState() {
super.initState();
walletsRef = ref.read(walletsNotifier.notifier);
}

Future<bool> _deleteWallet() async {
try {
await deleteWallet(walletNameController.text);
widget.onDeleteWallet(walletNameController.text);
await walletsRef.removeWallet(walletNameController.text);
return true;
} catch (e) {
logger.e('Failed to delete wallet due to $e');
Expand Down Expand Up @@ -68,7 +72,7 @@ class _WalletDetailsWidgetState extends State<WalletDetailsWidget> {
}
try {
await editWallet(walletName, newName);
widget.onEditWallet(walletName, newName);
await walletsRef.editWallet(walletName, newName);
walletName = newName;
widget.wallet.name = newName;
} catch (e) {
Expand Down
22 changes: 0 additions & 22 deletions app/lib/screens/wallets/wallet_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@ class _WalletScreenState extends ConsumerState<WalletScreen> {
List<Wallet> wallets = [];
late WalletsNotifier walletRef;

onDeleteWallet(String name) {
walletRef.removeWallet(name);
}

onEditWallet(String oldName, String newName) {
for (final w in wallets) {
if (w.name == oldName) {
w.name = newName;
}
}
setState(() {});
}

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -91,9 +78,6 @@ class _WalletScreenState extends ConsumerState<WalletScreen> {
final wallet = wallets[i];
return WalletCardWidget(
wallet: wallet,
allWallets: wallets,
onDeleteWallet: onDeleteWallet,
onEditWallet: onEditWallet,
);
}));
}
Expand Down Expand Up @@ -155,7 +139,6 @@ class _WalletScreenState extends ConsumerState<WalletScreen> {
constraints: const BoxConstraints(maxWidth: double.infinity),
context: context,
builder: (ctx) => NewWallet(
onAddWallet: _addWallet,
wallets: wallets,
));
}
Expand All @@ -172,11 +155,6 @@ class _WalletScreenState extends ConsumerState<WalletScreen> {
wallets.add(wallet);
}

void _addWallet(Wallet wallet) {
wallets.add(wallet);
setState(() {});
}

Future<void> handleRefresh() async {
try {
loading = true;
Expand Down
Loading

0 comments on commit 39e3b9b

Please sign in to comment.