Skip to content

Commit

Permalink
minor code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
aldrinzigmundv committed Oct 13, 2023
1 parent f56ac6c commit a81da57
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 75 deletions.
7 changes: 2 additions & 5 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import 'package:flutter/material.dart';
import 'package:thirdbank/pages/loadingpage.dart';

void main() => runApp(
MaterialApp(
const MaterialApp(
title: "Third Bank",
initialRoute: '/',
routes: {
'/': (context) => const LoadingPage(),
},
home: LoadingPage(),
));
16 changes: 8 additions & 8 deletions lib/pages/homepage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class _HomePageState extends State<HomePage> {

String _transactionAreaMessage = "Your transactions will appear here.";

refreshWallet() async {
_refreshWallet() async {
try {
await wallet.syncWallet();
await wallet.getBlockchainHeight();
Expand All @@ -40,11 +40,11 @@ class _HomePageState extends State<HomePage> {
});
}
} catch (_) {
showFailedRefreshingWalletError();
_showFailedRefreshingWalletError();
}
}

showTransactionInfo(int index, int blockHeight, int finalFee) {
_showTransactionInfo(int index, int blockHeight, int finalFee) {
showDialog(
context: context,
builder: (BuildContext context) {
Expand Down Expand Up @@ -76,7 +76,7 @@ class _HomePageState extends State<HomePage> {
);
}

showFailedRefreshingWalletError() {
_showFailedRefreshingWalletError() {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content:
Text("Something went wrong. Please, check your internet connection."),
Expand All @@ -89,7 +89,7 @@ class _HomePageState extends State<HomePage> {
super.initState();
wallet = widget.wallet;
storage = widget.storage;
refreshWallet();
_refreshWallet();
}

@override
Expand Down Expand Up @@ -125,7 +125,7 @@ class _HomePageState extends State<HomePage> {
Padding(
padding: const EdgeInsets.all(9.0),
child: ElevatedButton(
onPressed: () => refreshWallet(),
onPressed: () => _refreshWallet(),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.yellowAccent),
Expand Down Expand Up @@ -205,7 +205,7 @@ class _HomePageState extends State<HomePage> {
format.getFee(wallet.transactions[index].fee);
return GestureDetector(
onTap: () =>
showTransactionInfo(index, blockHeight, finalFee),
_showTransactionInfo(index, blockHeight, finalFee),
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 3.0, horizontal: 9.0),
Expand Down Expand Up @@ -245,7 +245,7 @@ class _HomePageState extends State<HomePage> {
format.getFee(wallet.transactions[index].fee);
return GestureDetector(
onTap: () =>
showTransactionInfo(index, blockHeight, finalFee),
_showTransactionInfo(index, blockHeight, finalFee),
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 3.0, horizontal: 9.0),
Expand Down
14 changes: 7 additions & 7 deletions lib/pages/loadingpage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ class _LoadingPageState extends State<LoadingPage> {

String _loadingpagetext = "Please wait...";

void startup() async {
void _startup() async {
Future.delayed(const Duration(seconds: 1), () async {
await storage.initialize();
await auth.initialize();
if (await storage.read(key: "setupdone") == "true" &&
await storage.read(key: "lock") == "true") {
if (await auth.authenticate()) {
startWallet();
_startWallet();
} else {
displayError();
_displayError();
}
} else if (await storage.read(key: "setupdone") == "true" &&
await storage.read(key: "lock") == "false") {
startWallet();
_startWallet();
} else {
if (context.mounted) {
goToMnemonicQuestionPage(
Expand All @@ -42,7 +42,7 @@ class _LoadingPageState extends State<LoadingPage> {
});
}

void startWallet() async {
void _startWallet() async {
try {
wallet.mnemonic = await storage.read(key: "mnemonic");
wallet.walletAddress = await storage.read(key: "address");
Expand All @@ -60,7 +60,7 @@ class _LoadingPageState extends State<LoadingPage> {
}
}

displayError() {
_displayError() {
setState(() {
_loadingpagetext =
"Something went wrong. Either phone authentication failed or bad internet connection...";
Expand All @@ -70,7 +70,7 @@ class _LoadingPageState extends State<LoadingPage> {
@override
void initState() {
super.initState();
startup();
_startup();
}

@override
Expand Down
8 changes: 4 additions & 4 deletions lib/pages/setup pages/authenticationquestionpage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ class _AuthenticationQuestionPageState

final AuthenticationProvider authentication = AuthenticationProvider();

addLocalAuth() async {
_addLocalAuth() async {
await storage.write(key: "setupdone", value: "true");
await storage.write(key: "lock", value: "true");
if (context.mounted) {
goToHomePage(context: context, wallet: wallet, storage: storage);
}
}

noLocalAuth() async {
_noLocalAuth() async {
await storage.write(key: "setupdone", value: "true");
await storage.write(key: "lock", value: "false");
if (context.mounted) {
Expand Down Expand Up @@ -77,7 +77,7 @@ class _AuthenticationQuestionPageState
Padding(
padding: const EdgeInsets.all(9.0),
child: ElevatedButton(
onPressed: () => addLocalAuth(),
onPressed: () => _addLocalAuth(),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.yellowAccent),
Expand All @@ -93,7 +93,7 @@ class _AuthenticationQuestionPageState
Padding(
padding: const EdgeInsets.all(9.0),
child: ElevatedButton(
onPressed: () => noLocalAuth(),
onPressed: () => _noLocalAuth(),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.yellowAccent),
Expand Down
20 changes: 11 additions & 9 deletions lib/pages/setup pages/generatemnemonicpage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ class _GenerateMnemonicPageState extends State<GenerateMnemonicPage> {

final AuthenticationProvider authentication = AuthenticationProvider();

proceedWithCreatedMnemonic() async {
_regenarateMnemonic() {
wallet.generateMnemonic();
}

_proceedWithCreatedMnemonic() async {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text("Creating Wallet..."),
duration: Duration(seconds: 2),
Expand All @@ -38,13 +42,13 @@ class _GenerateMnemonicPageState extends State<GenerateMnemonicPage> {
wallet.mnemonic = "";
await wallet.getNewAddress();
await storage.write(key: "address", value: wallet.walletAddress);
evaluateNextPage();
_evaluateNextPage();
} catch (_) {
showFailedCreatingWalletError();
_showFailedCreatingWalletError();
}
}

evaluateNextPage() async {
_evaluateNextPage() async {
final localAuthAvailable =
await authentication.checkAuthenticationAvailability();
if (!localAuthAvailable) {
Expand All @@ -61,7 +65,7 @@ class _GenerateMnemonicPageState extends State<GenerateMnemonicPage> {
}
}

showFailedCreatingWalletError() {
_showFailedCreatingWalletError() {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content:
Text("Something went wrong. Please, check your internet connection."),
Expand Down Expand Up @@ -143,7 +147,7 @@ class _GenerateMnemonicPageState extends State<GenerateMnemonicPage> {
Padding(
padding: const EdgeInsets.all(9.0),
child: ElevatedButton(
onPressed: () => proceedWithCreatedMnemonic(),
onPressed: () => _proceedWithCreatedMnemonic(),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.yellowAccent),
Expand All @@ -159,9 +163,7 @@ class _GenerateMnemonicPageState extends State<GenerateMnemonicPage> {
Padding(
padding: const EdgeInsets.all(9.0),
child: ElevatedButton(
onPressed: () {
wallet.generateMnemonic();
},
onPressed: () => _regenarateMnemonic(),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.yellowAccent),
Expand Down
15 changes: 11 additions & 4 deletions lib/pages/setup pages/mnemonicquestionpage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ class MnemonicQuestionPage extends StatelessWidget {
final WalletProvider wallet;
final StorageProvider storage;

_goToGenerateMnemonicPage(context) {
goToGenerateMnemonicPage(
context: context, wallet: wallet, storage: storage);
}

_goToRestoreMnemonicPage(context) {
goToRestoreMnemonicPage(context: context, wallet: wallet, storage: storage);
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -37,8 +46,7 @@ class MnemonicQuestionPage extends StatelessWidget {
Padding(
padding: const EdgeInsets.all(9.0),
child: ElevatedButton(
onPressed: () => goToGenerateMnemonicPage(
context: context, wallet: wallet, storage: storage),
onPressed: () => _goToGenerateMnemonicPage(context),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.yellowAccent),
Expand All @@ -55,8 +63,7 @@ class MnemonicQuestionPage extends StatelessWidget {
Padding(
padding: const EdgeInsets.all(9.0),
child: ElevatedButton(
onPressed: () => goToRestoreMnemonicPage(
context: context, wallet: wallet, storage: storage),
onPressed: () => _goToRestoreMnemonicPage(context),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.yellowAccent),
Expand Down
20 changes: 10 additions & 10 deletions lib/pages/setup pages/restoremnemonicpage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ class _RestoreMnemonicPageState extends State<RestoreMnemonicPage> {

final TextEditingController _mnemonic = TextEditingController();

bool checkMnemonicLength(String input) {
bool _checkMnemonicLength(String input) {
final List<String> words = input.split(' ');
final int wordCount = words.length;

return wordCount == 12 || wordCount == 18 || wordCount == 24;
}

restoreOldWallet() async {
if (checkMnemonicLength(_mnemonic.text)) {
_restoreOldWallet() async {
if (_checkMnemonicLength(_mnemonic.text)) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text("Restoring..."),
duration: Duration(seconds: 2),
Expand All @@ -45,16 +45,16 @@ class _RestoreMnemonicPageState extends State<RestoreMnemonicPage> {
wallet.mnemonic = "";
await wallet.getNewAddress();
await storage.write(key: "address", value: wallet.walletAddress);
evaluateNextPage();
_evaluateNextPage();
} catch (_) {
showFailedRestoringWalletError();
_showFailedRestoringWalletError();
}
} else {
showBadPassphraseError();
_showBadPassphraseError();
}
}

evaluateNextPage() async {
_evaluateNextPage() async {
final localAuthAvailable =
await authentication.checkAuthenticationAvailability();
if (!localAuthAvailable) {
Expand All @@ -71,15 +71,15 @@ class _RestoreMnemonicPageState extends State<RestoreMnemonicPage> {
}
}

showFailedRestoringWalletError() {
_showFailedRestoringWalletError() {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text(
"Failed restoring wallet. Please, check passphrase or your internet connection."),
duration: Duration(seconds: 2),
));
}

showBadPassphraseError() {
_showBadPassphraseError() {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text("Failed restoring wallet. Please, check passphrase."),
duration: Duration(seconds: 2),
Expand Down Expand Up @@ -142,7 +142,7 @@ class _RestoreMnemonicPageState extends State<RestoreMnemonicPage> {
Padding(
padding: const EdgeInsets.all(9.0),
child: ElevatedButton(
onPressed: () => restoreOldWallet(),
onPressed: () => _restoreOldWallet(),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.yellowAccent),
Expand Down
16 changes: 9 additions & 7 deletions lib/pages/transactionpages/receivepage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class _ReceivePageState extends State<ReceivePage> {

late WalletProvider wallet;

_copyWalletAddressToClipboard() {
Clipboard.setData(ClipboardData(text: wallet.walletAddress));
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text("Wallet address copied to clipboard."),
duration: Duration(seconds: 2),
));
}

@override
void initState() {
super.initState();
Expand All @@ -38,13 +46,7 @@ class _ReceivePageState extends State<ReceivePage> {
)),
),
body: GestureDetector(
onTap: () {
Clipboard.setData(ClipboardData(text: wallet.walletAddress));
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text("Wallet address copied to clipboard."),
duration: Duration(seconds: 2),
));
},
onTap: () => _copyWalletAddressToClipboard(),
child: Center(
child: SingleChildScrollView(
child: Column(
Expand Down
Loading

0 comments on commit a81da57

Please sign in to comment.