Skip to content

Commit

Permalink
Add Edit Custom Token Page
Browse files Browse the repository at this point in the history
  • Loading branch information
konstantinullrich committed Jun 28, 2024
1 parent a079a3f commit 78cd4d7
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 8 deletions.
4 changes: 3 additions & 1 deletion lib/src/entities/custom_erc20_token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class CustomErc20Token {

@ignore
Image? get icon {
if (iconUrl == null) return null;
if (iconUrl == null) {
return Image.asset("assets/images/shrug.png", width: 40);
}

if (iconUrl!.startsWith("assets/images/")) {
return Image.asset(iconUrl!, width: 40);
Expand Down
13 changes: 13 additions & 0 deletions lib/src/screens/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:frankencoin_wallet/src/core/wallet_connect/walletconnect_service
import 'package:frankencoin_wallet/src/di.dart';
import 'package:frankencoin_wallet/src/entities/address_book_entry.dart';
import 'package:frankencoin_wallet/src/entities/crypto_currency.dart';
import 'package:frankencoin_wallet/src/entities/custom_erc20_token.dart';
import 'package:frankencoin_wallet/src/screens/address_book/add_contact_page.dart';
import 'package:frankencoin_wallet/src/screens/address_book/address_book_page.dart';
import 'package:frankencoin_wallet/src/screens/asset/asset_details_page.dart';
Expand All @@ -22,6 +23,7 @@ import 'package:frankencoin_wallet/src/screens/restore/restore_options_page.dart
import 'package:frankencoin_wallet/src/screens/routes.dart';
import 'package:frankencoin_wallet/src/screens/send/send_frankencoin_pay_page.dart';
import 'package:frankencoin_wallet/src/screens/send/send_page.dart';
import 'package:frankencoin_wallet/src/screens/settings/edit_custom_token_page.dart';
import 'package:frankencoin_wallet/src/screens/settings/edit_node_page.dart';
import 'package:frankencoin_wallet/src/screens/settings/manage_custom_tokens_page.dart';
import 'package:frankencoin_wallet/src/screens/settings/manage_nodes_page.dart';
Expand Down Expand Up @@ -136,6 +138,17 @@ Route<dynamic> createRoute(RouteSettings settings) {
getIt.get<CustomErc20TokenStore>(),
getIt.get<BottomSheetService>()));

case Routes.settingsCustomTokensEdit:
final customErc20Token = settings.arguments as CustomErc20Token?;

return MaterialPageRoute<void>(
builder: (_) => EditCustomTokenPage(
getIt.get<AppStore>(),
getIt.get<CustomErc20TokenStore>(),
getIt.get<BottomSheetService>(),
customToken: customErc20Token,
));

case Routes.settingsNodes:
return MaterialPageRoute<void>(
builder: (_) => ManageNodesPage(getIt.get<AppStore>()));
Expand Down
191 changes: 191 additions & 0 deletions lib/src/screens/settings/edit_custom_token_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import 'package:erc20/erc20.dart';
import 'package:flutter/cupertino.dart';
import 'package:frankencoin_wallet/generated/i18n.dart';
import 'package:frankencoin_wallet/src/colors.dart';
import 'package:frankencoin_wallet/src/core/bottom_sheet_service.dart';
import 'package:frankencoin_wallet/src/entities/blockchain.dart';
import 'package:frankencoin_wallet/src/entities/custom_erc20_token.dart';
import 'package:frankencoin_wallet/src/screens/base_page.dart';
import 'package:frankencoin_wallet/src/screens/send/widgets/blockchain_selector.dart';
import 'package:frankencoin_wallet/src/stores/app_store.dart';
import 'package:frankencoin_wallet/src/stores/custom_erc20_token_store.dart';
import 'package:walletconnect_flutter_v2/walletconnect_flutter_v2.dart';

class EditCustomTokenPage extends BasePage {
EditCustomTokenPage(
this.appStore, this.customErc20TokenStore, this.bottomSheetService,
{required this.customToken, super.key});

@override
String? get title =>
customToken != null ? S.current.edit_asset : S.current.new_asset;

final AppStore appStore;
final CustomErc20TokenStore customErc20TokenStore;
final BottomSheetService bottomSheetService;
final CustomErc20Token? customToken;

@override
Widget body(BuildContext context) => _EditCustomTokenPageBody(
appStore: appStore,
customErc20TokenStore: customErc20TokenStore,
bottomSheetService: bottomSheetService,
customToken: customToken ??
CustomErc20Token(
chainId: 1,
address: '',
symbol: '',
name: '',
decimals: 18,
),
);
}

class _EditCustomTokenPageBody extends StatefulWidget {
final AppStore appStore;
final CustomErc20TokenStore customErc20TokenStore;
final BottomSheetService bottomSheetService;
final CustomErc20Token customToken;

const _EditCustomTokenPageBody({
required this.appStore,
required this.customErc20TokenStore,
required this.bottomSheetService,
required this.customToken,
});

@override
State<StatefulWidget> createState() => _EditCustomTokenPageBodyState();
}

class _EditCustomTokenPageBodyState extends State<_EditCustomTokenPageBody> {
final TextEditingController _addressController = TextEditingController();
final TextEditingController _nameController = TextEditingController();
final TextEditingController _symbolController = TextEditingController();
final TextEditingController _decimalsController = TextEditingController();
final TextEditingController _iconUrlController = TextEditingController();
bool _isLoading = false;

int _chainId = 1;

@override
void initState() {
_addressController.text = widget.customToken.address;
_nameController.text = widget.customToken.name;
_symbolController.text = widget.customToken.symbol;
_decimalsController.text = widget.customToken.decimals.toString();
_iconUrlController.text = widget.customToken.iconUrl ?? "";

_chainId = widget.customToken.chainId;

_addressController.addListener(() {
final address = _addressController.text;
if (address.length == 42) _loadTokenInfos(address);
});

super.initState();
}

Future<void> _loadTokenInfos(String address) async {
final erc20Token = ERC20(
address: EthereumAddress.fromHex(address),
client: widget.appStore.getClient(_chainId),
);

_nameController.text = await erc20Token.name();
_symbolController.text = await erc20Token.symbol();
_decimalsController.text = (await erc20Token.decimals()).toString();
}

@override
Widget build(BuildContext context) {
return SafeArea(child: SingleChildScrollView(child: Column(
children: [
Padding(
padding:
const EdgeInsets.only(left: 26, right: 26, top: 10),
child: CupertinoTextField(
controller: _addressController,
placeholder: S.of(context).address,
suffix: const SizedBox(height: 52),
),
),
Padding(
padding:
const EdgeInsets.only(left: 26, right: 26),
child: BlockchainSelector(
bottomSheetService: widget.bottomSheetService,
blockchain: Blockchain.getFromChainId(_chainId),
onSelect: (blockchain) =>
setState(() => _chainId = blockchain.chainId),
),
),
Padding(
padding:
const EdgeInsets.only(left: 26, right: 26, top: 20, bottom: 10),
child: CupertinoTextField(
controller: _nameController,
placeholder: S.of(context).name,
suffix: const SizedBox(height: 52),
),
),
Padding(
padding:
const EdgeInsets.only(left: 26, right: 26, top: 10, bottom: 10),
child: CupertinoTextField(
controller: _symbolController,
placeholder: S.of(context).token_symbol,
suffix: const SizedBox(height: 52),
),
),
Padding(
padding:
const EdgeInsets.only(left: 26, right: 26, top: 10, bottom: 10),
child: CupertinoTextField(
controller: _decimalsController,
keyboardType: const TextInputType.numberWithOptions(decimal: false),
placeholder: S.of(context).token_decimals,
suffix: const SizedBox(height: 52),
),
),
Padding(
padding:
const EdgeInsets.only(left: 26, right: 26, top: 10, bottom: 10),
child: CupertinoTextField(
controller: _iconUrlController,
placeholder: S.of(context).token_icon_url,
suffix: const SizedBox(height: 52),
),
),
Padding(
padding: const EdgeInsets.only(top: 20, bottom: 20),
child: CupertinoButton(
onPressed: _isLoading ? null : _save,
color: FrankencoinColors.frRed,
child: _isLoading
? const CupertinoActivityIndicator()
: Text(
S.of(context).save,
style: const TextStyle(fontSize: 16),
),
),
),
],
),));
}

Future<void> _save() async {
setState(() => _isLoading = true);
widget.customToken.address = _addressController.text.trim();
widget.customToken.name = _nameController.text.trim();
widget.customToken.symbol = _symbolController.text.trim();
widget.customToken.decimals = int.parse(_decimalsController.text.trim());

if (_iconUrlController.text.trim().isNotEmpty) {
widget.customToken.iconUrl = _iconUrlController.text;
}

await widget.customErc20TokenStore.updateToken(widget.customToken);
setState(() => _isLoading = false);
}
}
10 changes: 4 additions & 6 deletions lib/src/screens/settings/manage_custom_tokens_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ class ManageCustomTokensPage extends BasePage {
overlayColor: MaterialStateColor.resolveWith(
(states) => Colors.transparent),
),
onPressed: null,
// onPressed: () => Navigator.of(context)
// .pushNamed(Routes.settingsCustomTokensEdit),
onPressed: () => Navigator.of(context)
.pushNamed(Routes.settingsCustomTokensEdit, arguments: null),
child: Icon(
Icons.add,
color: pageIconColor(context),
Expand Down Expand Up @@ -91,9 +90,8 @@ class _ManageCustomTokensPageBodyState
subtitle: e.address,
canEdit: e.editable,
onTap: e.editable
? (_) => Navigator.of(context).pushNamed(
Routes.settingsNodesEdit,
arguments: e.chainId)
? (_) => Navigator.of(context)
.pushNamed(Routes.settingsCustomTokensEdit, arguments: e)
: (_) => _alertNotEditable(e),
),
),
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ publish_to: 'none'
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.0.3+17
version: 1.0.3+19

environment:
sdk: '>=3.2.0 <4.0.0'
Expand Down
4 changes: 4 additions & 0 deletions res/values/strings_de.arb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"methods": "Funktionen",
"more_assets": "Mehr Assets",
"name": "Name",
"new_asset": "Neues Asset",
"new_wallet_connect_connection": "Neue Verbindung",
"nodes": "Nodes",
"not_enough_token": "Sie haben nicht genug ${tokenSymbol} in diesem Wallet",
Expand Down Expand Up @@ -97,6 +98,9 @@
"swap_provided_by": "Dieser Swap wird über ${providerName} ausgeführt",
"swap_route_title": "Swap Route Info",
"to": "an ${name}",
"token_symbol": "Token-Symbol",
"token_decimals": "Token-Dezimale",
"token_icon_url": "Token-Icon",
"total_supply": "Im Umlauf",
"trade": "Handeln",
"transaction_id": "Transaktions-ID",
Expand Down
4 changes: 4 additions & 0 deletions res/values/strings_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"methods": "Methods",
"more_assets": "More assets",
"name": "Name",
"new_asset": "New asset",
"new_wallet_connect_connection": "New Connection",
"node": "Node",
"nodes": "Nodes",
Expand Down Expand Up @@ -98,6 +99,9 @@
"swap_provided_by": "This Swap is provided by ${providerName}",
"swap_route_title": "Swap Route Info",
"to": "to ${name}",
"token_symbol": "Tokensymbol",
"token_decimals": "Tokendecimals",
"token_icon_url": "Tokenicon",
"total_supply": "Total supply",
"trade": "Trade",
"transaction_id": "Transactions-id",
Expand Down

0 comments on commit 78cd4d7

Please sign in to comment.