From 9ef1186c453256de7e3e6a2ac48a0ab0a239e336 Mon Sep 17 00:00:00 2001 From: Godwin Asuquo Date: Fri, 9 Dec 2022 17:08:52 +0100 Subject: [PATCH] Fix issues from code review --- lib/core/auth_service.dart | 26 +-- lib/src/screens/root/root.dart | 12 +- .../screens/wallet_list/wallet_list_page.dart | 157 +++++++++--------- lib/view_model/auth_view_model.dart | 4 +- 4 files changed, 99 insertions(+), 100 deletions(-) diff --git a/lib/core/auth_service.dart b/lib/core/auth_service.dart index 6b167fa40..c493cddcb 100644 --- a/lib/core/auth_service.dart +++ b/lib/core/auth_service.dart @@ -42,25 +42,27 @@ class AuthService with Store { return decodedPin == pin; } - void saveLastAuthTime(){ + void saveLastAuthTime() { + int timestamp = DateTime.now().millisecondsSinceEpoch; sharedPreferences.setInt(PreferencesKey.lastAuthTimeMilliseconds, timestamp); } - bool requireAuth(){ - final timestamp = sharedPreferences.getInt(PreferencesKey.lastAuthTimeMilliseconds); - final duration = _durationToRequireAuth(timestamp ?? 0); - final requiredPinInterval = getIt.get().pinTimeOutDuration; + bool requireAuth() { + + final timestamp = sharedPreferences.getInt(PreferencesKey.lastAuthTimeMilliseconds); + final duration = _durationToRequireAuth(timestamp ?? 0); + final requiredPinInterval = getIt.get().pinTimeOutDuration; - return duration >= requiredPinInterval.value; - } + return duration >= requiredPinInterval.value; + } - int _durationToRequireAuth(int timestamp){ + int _durationToRequireAuth(int timestamp) { - DateTime before = DateTime.fromMillisecondsSinceEpoch(timestamp); - DateTime now = DateTime.now(); - Duration timeDifference = now.difference(before); + DateTime before = DateTime.fromMillisecondsSinceEpoch(timestamp); + DateTime now = DateTime.now(); + Duration timeDifference = now.difference(before); - return timeDifference.inMinutes; + return timeDifference.inMinutes; } } diff --git a/lib/src/screens/root/root.dart b/lib/src/screens/root/root.dart index 8d2b61a65..206f485dd 100644 --- a/lib/src/screens/root/root.dart +++ b/lib/src/screens/root/root.dart @@ -1,6 +1,5 @@ import 'dart:async'; import 'package:cake_wallet/core/auth_service.dart'; -import 'package:cake_wallet/di.dart'; import 'package:flutter/material.dart'; import 'package:cake_wallet/routes.dart'; import 'package:cake_wallet/src/screens/auth/auth_page.dart'; @@ -14,12 +13,15 @@ class Root extends StatefulWidget { required this.authenticationStore, required this.appStore, required this.child, - required this.navigatorKey}) + required this.navigatorKey, + required this.authService, + }) : super(key: key); final AuthenticationStore authenticationStore; final AppStore appStore; final GlobalKey navigatorKey; + final AuthService authService; final Widget child; @override @@ -30,7 +32,7 @@ class RootState extends State with WidgetsBindingObserver { RootState() : _isInactiveController = StreamController.broadcast(), _isInactive = false, - _requestAuth = getIt.get().requireAuth(), + _requestAuth = true, _postFrameCallback = false; Stream get isInactive => _isInactiveController.stream; @@ -41,7 +43,7 @@ class RootState extends State with WidgetsBindingObserver { @override void initState() { - + _requestAuth = widget.authService.requireAuth(); _isInactiveController = StreamController.broadcast(); _isInactive = false; _postFrameCallback = false; @@ -58,7 +60,7 @@ class RootState extends State with WidgetsBindingObserver { } setState(() { - _requestAuth = getIt.get().requireAuth(); + _requestAuth = widget.authService.requireAuth(); }); if (!_isInactive && diff --git a/lib/src/screens/wallet_list/wallet_list_page.dart b/lib/src/screens/wallet_list/wallet_list_page.dart index 5d9650ef2..d76631b3f 100644 --- a/lib/src/screens/wallet_list/wallet_list_page.dart +++ b/lib/src/screens/wallet_list/wallet_list_page.dart @@ -220,93 +220,88 @@ class WalletListBodyState extends State { } Future _loadWallet(WalletListItem wallet) async { - if(await widget.walletListViewModel.checkIfAuthRequired()){ - await Navigator.of(context).pushNamed(Routes.auth, arguments: - (bool isAuthenticatedSuccessfully, AuthPageState auth) async { - if (!isAuthenticatedSuccessfully) { - return; - } + if (await widget.walletListViewModel.checkIfAuthRequired()) { + await Navigator.of(context).pushNamed(Routes.auth, + arguments: (bool isAuthenticatedSuccessfully, AuthPageState auth) async { + if (!isAuthenticatedSuccessfully) { + return; + } - try { - auth.changeProcessText( - S.of(context).wallet_list_loading_wallet(wallet.name)); - await widget.walletListViewModel.loadWallet(wallet); - auth.hideProgressText(); - auth.close(); - WidgetsBinding.instance.addPostFrameCallback((_) { - Navigator.of(context).pop(); - }); - } catch (e) { - auth.changeProcessText(S - .of(context) - .wallet_list_failed_to_load(wallet.name, e.toString())); - } - }); - }else{ + try { + auth.changeProcessText(S.of(context).wallet_list_loading_wallet(wallet.name)); + await widget.walletListViewModel.loadWallet(wallet); + auth.hideProgressText(); + auth.close(); + WidgetsBinding.instance.addPostFrameCallback((_) { + Navigator.of(context).pop(); + }); + } catch (e) { + auth.changeProcessText( + S.of(context).wallet_list_failed_to_load(wallet.name, e.toString())); + } + }); + } else { try { changeProcessText(S.of(context).wallet_list_loading_wallet(wallet.name)); await widget.walletListViewModel.loadWallet(wallet); hideProgressText(); - Navigator.of(context).pop(); + Navigator.of(context).pop(); } catch (e) { - changeProcessText(S - .of(context) - .wallet_list_failed_to_load(wallet.name, e.toString())); + changeProcessText(S.of(context).wallet_list_failed_to_load(wallet.name, e.toString())); } } } Future _removeWallet(WalletListItem wallet) async { - if(widget.walletListViewModel.checkIfAuthRequired()){ - await Navigator.of(context).pushNamed(Routes.auth, arguments: - (bool isAuthenticatedSuccessfully, AuthPageState auth) async { - if (!isAuthenticatedSuccessfully) { - return; - } - _onSuccessfulAuth(wallet, auth); - }); - }else{ + if (widget.walletListViewModel.checkIfAuthRequired()) { + await Navigator.of(context).pushNamed(Routes.auth, + arguments: (bool isAuthenticatedSuccessfully, AuthPageState auth) async { + if (!isAuthenticatedSuccessfully) { + return; + } + _onSuccessfulAuth(wallet, auth); + }); + } else { _onSuccessfulAuth(wallet, null); } } - _onSuccessfulAuth(WalletListItem wallet, AuthPageState? auth)async{ + void _onSuccessfulAuth(WalletListItem wallet, AuthPageState? auth) async { bool confirmed = false; - await showPopUp( - context: context, - builder: (BuildContext context) { - return AlertWithTwoActions( - alertTitle: S.of(context).delete_wallet, - alertContent: S.of(context).delete_wallet_confirm_message(wallet.name), - leftButtonText: S.of(context).cancel, - rightButtonText: S.of(context).delete, - actionLeftButton: () => Navigator.of(context).pop(), - actionRightButton: () { - confirmed = true; - Navigator.of(context).pop(); - }, - ); - }); - - if (confirmed) { - try { - auth != null ? - auth.changeProcessText( - S.of(context).wallet_list_removing_wallet(wallet.name)) - : changeProcessText( S.of(context).wallet_list_removing_wallet(wallet.name)); - await widget.walletListViewModel.remove(wallet); - } catch (e) { - auth != null ? - auth.changeProcessText( - S.of(context).wallet_list_failed_to_remove(wallet.name, e.toString()), - ) - : changeProcessText( - S.of(context).wallet_list_failed_to_remove(wallet.name, e.toString()), + await showPopUp( + context: context, + builder: (BuildContext context) { + return AlertWithTwoActions( + alertTitle: S.of(context).delete_wallet, + alertContent: S.of(context).delete_wallet_confirm_message(wallet.name), + leftButtonText: S.of(context).cancel, + rightButtonText: S.of(context).delete, + actionLeftButton: () => Navigator.of(context).pop(), + actionRightButton: () { + confirmed = true; + Navigator.of(context).pop(); + }, ); - } - } + }); - auth?.close(); + if (confirmed) { + try { + auth != null + ? auth.changeProcessText(S.of(context).wallet_list_removing_wallet(wallet.name)) + : changeProcessText(S.of(context).wallet_list_removing_wallet(wallet.name)); + await widget.walletListViewModel.remove(wallet); + } catch (e) { + auth != null + ? auth.changeProcessText( + S.of(context).wallet_list_failed_to_remove(wallet.name, e.toString()), + ) + : changeProcessText( + S.of(context).wallet_list_failed_to_remove(wallet.name, e.toString()), + ); + } + } + + auth?.close(); } void changeProcessText(String text) { @@ -319,16 +314,16 @@ class WalletListBodyState extends State { } ActionPane _actionPane(WalletListItem wallet) => ActionPane( - motion: const ScrollMotion(), - extentRatio: 0.3, - children: [ - SlidableAction( - onPressed: (_) => _removeWallet(wallet), - backgroundColor: Colors.red, - foregroundColor: Colors.white, - icon: CupertinoIcons.delete, - label: S.of(context).delete, - ), - ], - ); + motion: const ScrollMotion(), + extentRatio: 0.3, + children: [ + SlidableAction( + onPressed: (_) => _removeWallet(wallet), + backgroundColor: Colors.red, + foregroundColor: Colors.white, + icon: CupertinoIcons.delete, + label: S.of(context).delete, + ), + ], + ); } diff --git a/lib/view_model/auth_view_model.dart b/lib/view_model/auth_view_model.dart index 42201ddab..d45873230 100644 --- a/lib/view_model/auth_view_model.dart +++ b/lib/view_model/auth_view_model.dart @@ -121,8 +121,8 @@ abstract class AuthViewModelBase with Store { } } - void _saveLastAuthTime(ExecutionState state){ - if(state is ExecutedSuccessfullyState){ + void _saveLastAuthTime(ExecutionState state) { + if(state is ExecutedSuccessfullyState) { _authService.saveLastAuthTime(); } }