Fix issues from code review

This commit is contained in:
Godwin Asuquo 2022-12-09 17:08:52 +01:00
parent 75463912d0
commit 9ef1186c45
4 changed files with 99 additions and 100 deletions

View file

@ -42,25 +42,27 @@ class AuthService with Store {
return decodedPin == pin; return decodedPin == pin;
} }
void saveLastAuthTime(){ void saveLastAuthTime() {
int timestamp = DateTime.now().millisecondsSinceEpoch; int timestamp = DateTime.now().millisecondsSinceEpoch;
sharedPreferences.setInt(PreferencesKey.lastAuthTimeMilliseconds, timestamp); sharedPreferences.setInt(PreferencesKey.lastAuthTimeMilliseconds, timestamp);
} }
bool requireAuth(){ bool requireAuth() {
final timestamp = sharedPreferences.getInt(PreferencesKey.lastAuthTimeMilliseconds);
final duration = _durationToRequireAuth(timestamp ?? 0); final timestamp = sharedPreferences.getInt(PreferencesKey.lastAuthTimeMilliseconds);
final requiredPinInterval = getIt.get<SettingsStore>().pinTimeOutDuration; final duration = _durationToRequireAuth(timestamp ?? 0);
final requiredPinInterval = getIt.get<SettingsStore>().pinTimeOutDuration;
return duration >= requiredPinInterval.value; return duration >= requiredPinInterval.value;
} }
int _durationToRequireAuth(int timestamp){ int _durationToRequireAuth(int timestamp) {
DateTime before = DateTime.fromMillisecondsSinceEpoch(timestamp); DateTime before = DateTime.fromMillisecondsSinceEpoch(timestamp);
DateTime now = DateTime.now(); DateTime now = DateTime.now();
Duration timeDifference = now.difference(before); Duration timeDifference = now.difference(before);
return timeDifference.inMinutes; return timeDifference.inMinutes;
} }
} }

View file

@ -1,6 +1,5 @@
import 'dart:async'; import 'dart:async';
import 'package:cake_wallet/core/auth_service.dart'; import 'package:cake_wallet/core/auth_service.dart';
import 'package:cake_wallet/di.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:cake_wallet/routes.dart'; import 'package:cake_wallet/routes.dart';
import 'package:cake_wallet/src/screens/auth/auth_page.dart'; import 'package:cake_wallet/src/screens/auth/auth_page.dart';
@ -14,12 +13,15 @@ class Root extends StatefulWidget {
required this.authenticationStore, required this.authenticationStore,
required this.appStore, required this.appStore,
required this.child, required this.child,
required this.navigatorKey}) required this.navigatorKey,
required this.authService,
})
: super(key: key); : super(key: key);
final AuthenticationStore authenticationStore; final AuthenticationStore authenticationStore;
final AppStore appStore; final AppStore appStore;
final GlobalKey<NavigatorState> navigatorKey; final GlobalKey<NavigatorState> navigatorKey;
final AuthService authService;
final Widget child; final Widget child;
@override @override
@ -30,7 +32,7 @@ class RootState extends State<Root> with WidgetsBindingObserver {
RootState() RootState()
: _isInactiveController = StreamController<bool>.broadcast(), : _isInactiveController = StreamController<bool>.broadcast(),
_isInactive = false, _isInactive = false,
_requestAuth = getIt.get<AuthService>().requireAuth(), _requestAuth = true,
_postFrameCallback = false; _postFrameCallback = false;
Stream<bool> get isInactive => _isInactiveController.stream; Stream<bool> get isInactive => _isInactiveController.stream;
@ -41,7 +43,7 @@ class RootState extends State<Root> with WidgetsBindingObserver {
@override @override
void initState() { void initState() {
_requestAuth = widget.authService.requireAuth();
_isInactiveController = StreamController<bool>.broadcast(); _isInactiveController = StreamController<bool>.broadcast();
_isInactive = false; _isInactive = false;
_postFrameCallback = false; _postFrameCallback = false;
@ -58,7 +60,7 @@ class RootState extends State<Root> with WidgetsBindingObserver {
} }
setState(() { setState(() {
_requestAuth = getIt.get<AuthService>().requireAuth(); _requestAuth = widget.authService.requireAuth();
}); });
if (!_isInactive && if (!_isInactive &&

View file

@ -220,93 +220,88 @@ class WalletListBodyState extends State<WalletListBody> {
} }
Future<void> _loadWallet(WalletListItem wallet) async { Future<void> _loadWallet(WalletListItem wallet) async {
if(await widget.walletListViewModel.checkIfAuthRequired()){ if (await widget.walletListViewModel.checkIfAuthRequired()) {
await Navigator.of(context).pushNamed(Routes.auth, arguments: await Navigator.of(context).pushNamed(Routes.auth,
(bool isAuthenticatedSuccessfully, AuthPageState auth) async { arguments: (bool isAuthenticatedSuccessfully, AuthPageState auth) async {
if (!isAuthenticatedSuccessfully) { if (!isAuthenticatedSuccessfully) {
return; return;
} }
try { try {
auth.changeProcessText( auth.changeProcessText(S.of(context).wallet_list_loading_wallet(wallet.name));
S.of(context).wallet_list_loading_wallet(wallet.name)); await widget.walletListViewModel.loadWallet(wallet);
await widget.walletListViewModel.loadWallet(wallet); auth.hideProgressText();
auth.hideProgressText(); auth.close();
auth.close(); WidgetsBinding.instance.addPostFrameCallback((_) {
WidgetsBinding.instance.addPostFrameCallback((_) { Navigator.of(context).pop();
Navigator.of(context).pop(); });
}); } catch (e) {
} catch (e) { auth.changeProcessText(
auth.changeProcessText(S S.of(context).wallet_list_failed_to_load(wallet.name, e.toString()));
.of(context) }
.wallet_list_failed_to_load(wallet.name, e.toString())); });
} } else {
});
}else{
try { try {
changeProcessText(S.of(context).wallet_list_loading_wallet(wallet.name)); changeProcessText(S.of(context).wallet_list_loading_wallet(wallet.name));
await widget.walletListViewModel.loadWallet(wallet); await widget.walletListViewModel.loadWallet(wallet);
hideProgressText(); hideProgressText();
Navigator.of(context).pop(); Navigator.of(context).pop();
} catch (e) { } catch (e) {
changeProcessText(S changeProcessText(S.of(context).wallet_list_failed_to_load(wallet.name, e.toString()));
.of(context)
.wallet_list_failed_to_load(wallet.name, e.toString()));
} }
} }
} }
Future<void> _removeWallet(WalletListItem wallet) async { Future<void> _removeWallet(WalletListItem wallet) async {
if(widget.walletListViewModel.checkIfAuthRequired()){ if (widget.walletListViewModel.checkIfAuthRequired()) {
await Navigator.of(context).pushNamed(Routes.auth, arguments: await Navigator.of(context).pushNamed(Routes.auth,
(bool isAuthenticatedSuccessfully, AuthPageState auth) async { arguments: (bool isAuthenticatedSuccessfully, AuthPageState auth) async {
if (!isAuthenticatedSuccessfully) { if (!isAuthenticatedSuccessfully) {
return; return;
} }
_onSuccessfulAuth(wallet, auth); _onSuccessfulAuth(wallet, auth);
}); });
}else{ } else {
_onSuccessfulAuth(wallet, null); _onSuccessfulAuth(wallet, null);
} }
} }
_onSuccessfulAuth(WalletListItem wallet, AuthPageState? auth)async{ void _onSuccessfulAuth(WalletListItem wallet, AuthPageState? auth) async {
bool confirmed = false; bool confirmed = false;
await showPopUp<void>( await showPopUp<void>(
context: context, context: context,
builder: (BuildContext context) { builder: (BuildContext context) {
return AlertWithTwoActions( return AlertWithTwoActions(
alertTitle: S.of(context).delete_wallet, alertTitle: S.of(context).delete_wallet,
alertContent: S.of(context).delete_wallet_confirm_message(wallet.name), alertContent: S.of(context).delete_wallet_confirm_message(wallet.name),
leftButtonText: S.of(context).cancel, leftButtonText: S.of(context).cancel,
rightButtonText: S.of(context).delete, rightButtonText: S.of(context).delete,
actionLeftButton: () => Navigator.of(context).pop(), actionLeftButton: () => Navigator.of(context).pop(),
actionRightButton: () { actionRightButton: () {
confirmed = true; confirmed = true;
Navigator.of(context).pop(); 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()),
); );
} });
}
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) { void changeProcessText(String text) {
@ -319,16 +314,16 @@ class WalletListBodyState extends State<WalletListBody> {
} }
ActionPane _actionPane(WalletListItem wallet) => ActionPane( ActionPane _actionPane(WalletListItem wallet) => ActionPane(
motion: const ScrollMotion(), motion: const ScrollMotion(),
extentRatio: 0.3, extentRatio: 0.3,
children: [ children: [
SlidableAction( SlidableAction(
onPressed: (_) => _removeWallet(wallet), onPressed: (_) => _removeWallet(wallet),
backgroundColor: Colors.red, backgroundColor: Colors.red,
foregroundColor: Colors.white, foregroundColor: Colors.white,
icon: CupertinoIcons.delete, icon: CupertinoIcons.delete,
label: S.of(context).delete, label: S.of(context).delete,
), ),
], ],
); );
} }

View file

@ -121,8 +121,8 @@ abstract class AuthViewModelBase with Store {
} }
} }
void _saveLastAuthTime(ExecutionState state){ void _saveLastAuthTime(ExecutionState state) {
if(state is ExecutedSuccessfullyState){ if(state is ExecutedSuccessfullyState) {
_authService.saveLastAuthTime(); _authService.saveLastAuthTime();
} }
} }