Fix issues from code review
Godwin Asuquo committed
Dec 9, 2022 at 17:08 UTC
9ef1186c453256de7e3e6a2ac48a0ab0a239e336
4 files changed
+98
-99
lib/core/auth_service.dart
+14
-12
@@ -42,25 +42,27 @@ class AuthService with Store {
42
return decodedPin == pin;
43
}
44
45
- void saveLastAuthTime(){
45
+ void saveLastAuthTime() {
46
+
47
int timestamp = DateTime.now().millisecondsSinceEpoch;
48
sharedPreferences.setInt(PreferencesKey.lastAuthTimeMilliseconds, timestamp);
49
}
50
50
- bool requireAuth(){
51
- final timestamp = sharedPreferences.getInt(PreferencesKey.lastAuthTimeMilliseconds);
52
- final duration = _durationToRequireAuth(timestamp ?? 0);
53
- final requiredPinInterval = getIt.get<SettingsStore>().pinTimeOutDuration;
51
+ bool requireAuth() {
52
+
53
+ final timestamp = sharedPreferences.getInt(PreferencesKey.lastAuthTimeMilliseconds);
54
+ final duration = _durationToRequireAuth(timestamp ?? 0);
55
+ final requiredPinInterval = getIt.get<SettingsStore>().pinTimeOutDuration;
56
55
- return duration >= requiredPinInterval.value;
56
- }
57
+ return duration >= requiredPinInterval.value;
58
+ }
59
58
- int _durationToRequireAuth(int timestamp){
60
+ int _durationToRequireAuth(int timestamp) {
61
60
- DateTime before = DateTime.fromMillisecondsSinceEpoch(timestamp);
61
- DateTime now = DateTime.now();
62
- Duration timeDifference = now.difference(before);
62
+ DateTime before = DateTime.fromMillisecondsSinceEpoch(timestamp);
63
+ DateTime now = DateTime.now();
64
+ Duration timeDifference = now.difference(before);
65
64
- return timeDifference.inMinutes;
66
+ return timeDifference.inMinutes;
67
}
68
}
lib/src/screens/root/root.dart
+7
-5
@@ -1,6 +1,5 @@
1
import 'dart:async';
2
import 'package:cake_wallet/core/auth_service.dart';
3
-import 'package:cake_wallet/di.dart';
3
import 'package:flutter/material.dart';
4
import 'package:cake_wallet/routes.dart';
5
import 'package:cake_wallet/src/screens/auth/auth_page.dart';
@@ -14,12 +13,15 @@ class Root extends StatefulWidget {
13
required this.authenticationStore,
14
required this.appStore,
15
required this.child,
17
- required this.navigatorKey})
16
+ required this.navigatorKey,
17
+ required this.authService,
18
+ })
19
: super(key: key);
20
21
final AuthenticationStore authenticationStore;
22
final AppStore appStore;
23
final GlobalKey<NavigatorState> navigatorKey;
24
+ final AuthService authService;
25
final Widget child;
26
27
@override
@@ -30,7 +32,7 @@ class RootState extends State<Root> with WidgetsBindingObserver {
32
RootState()
33
: _isInactiveController = StreamController<bool>.broadcast(),
34
_isInactive = false,
33
- _requestAuth = getIt.get<AuthService>().requireAuth(),
35
+ _requestAuth = true,
36
_postFrameCallback = false;
37
38
Stream<bool> get isInactive => _isInactiveController.stream;
@@ -41,7 +43,7 @@ class RootState extends State<Root> with WidgetsBindingObserver {
43
44
@override
45
void initState() {
44
-
46
+ _requestAuth = widget.authService.requireAuth();
47
_isInactiveController = StreamController<bool>.broadcast();
48
_isInactive = false;
49
_postFrameCallback = false;
@@ -58,7 +60,7 @@ class RootState extends State<Root> with WidgetsBindingObserver {
60
}
61
62
setState(() {
61
- _requestAuth = getIt.get<AuthService>().requireAuth();
63
+ _requestAuth = widget.authService.requireAuth();
64
});
65
66
if (!_isInactive &&
lib/src/screens/wallet_list/wallet_list_page.dart
+75
-80
@@ -220,93 +220,88 @@ class WalletListBodyState extends State<WalletListBody> {
220
}
221
222
Future<void> _loadWallet(WalletListItem wallet) async {
223
- if(await widget.walletListViewModel.checkIfAuthRequired()){
224
- await Navigator.of(context).pushNamed(Routes.auth, arguments:
225
- (bool isAuthenticatedSuccessfully, AuthPageState auth) async {
226
- if (!isAuthenticatedSuccessfully) {
227
- return;
228
- }
223
+ if (await widget.walletListViewModel.checkIfAuthRequired()) {
224
+ await Navigator.of(context).pushNamed(Routes.auth,
225
+ arguments: (bool isAuthenticatedSuccessfully, AuthPageState auth) async {
226
+ if (!isAuthenticatedSuccessfully) {
227
+ return;
228
+ }
229
230
- try {
231
- auth.changeProcessText(
232
- S.of(context).wallet_list_loading_wallet(wallet.name));
233
- await widget.walletListViewModel.loadWallet(wallet);
234
- auth.hideProgressText();
235
- auth.close();
236
- WidgetsBinding.instance.addPostFrameCallback((_) {
237
- Navigator.of(context).pop();
238
- });
239
- } catch (e) {
240
- auth.changeProcessText(S
241
- .of(context)
242
- .wallet_list_failed_to_load(wallet.name, e.toString()));
243
- }
244
- });
245
- }else{
230
+ try {
231
+ auth.changeProcessText(S.of(context).wallet_list_loading_wallet(wallet.name));
232
+ await widget.walletListViewModel.loadWallet(wallet);
233
+ auth.hideProgressText();
234
+ auth.close();
235
+ WidgetsBinding.instance.addPostFrameCallback((_) {
236
+ Navigator.of(context).pop();
237
+ });
238
+ } catch (e) {
239
+ auth.changeProcessText(
240
+ S.of(context).wallet_list_failed_to_load(wallet.name, e.toString()));
241
+ }
242
+ });
243
+ } else {
244
try {
245
changeProcessText(S.of(context).wallet_list_loading_wallet(wallet.name));
246
await widget.walletListViewModel.loadWallet(wallet);
247
hideProgressText();
250
- Navigator.of(context).pop();
248
+ Navigator.of(context).pop();
249
} catch (e) {
252
- changeProcessText(S
253
- .of(context)
254
- .wallet_list_failed_to_load(wallet.name, e.toString()));
250
+ changeProcessText(S.of(context).wallet_list_failed_to_load(wallet.name, e.toString()));
251
}
252
}
253
}
254
255
Future<void> _removeWallet(WalletListItem wallet) async {
260
- if(widget.walletListViewModel.checkIfAuthRequired()){
261
- await Navigator.of(context).pushNamed(Routes.auth, arguments:
262
- (bool isAuthenticatedSuccessfully, AuthPageState auth) async {
263
- if (!isAuthenticatedSuccessfully) {
264
- return;
265
- }
266
- _onSuccessfulAuth(wallet, auth);
267
- });
268
- }else{
256
+ if (widget.walletListViewModel.checkIfAuthRequired()) {
257
+ await Navigator.of(context).pushNamed(Routes.auth,
258
+ arguments: (bool isAuthenticatedSuccessfully, AuthPageState auth) async {
259
+ if (!isAuthenticatedSuccessfully) {
260
+ return;
261
+ }
262
+ _onSuccessfulAuth(wallet, auth);
263
+ });
264
+ } else {
265
_onSuccessfulAuth(wallet, null);
266
}
267
}
268
273
- _onSuccessfulAuth(WalletListItem wallet, AuthPageState? auth)async{
269
+ void _onSuccessfulAuth(WalletListItem wallet, AuthPageState? auth) async {
270
bool confirmed = false;
275
- await showPopUp<void>(
276
- context: context,
277
- builder: (BuildContext context) {
278
- return AlertWithTwoActions(
279
- alertTitle: S.of(context).delete_wallet,
280
- alertContent: S.of(context).delete_wallet_confirm_message(wallet.name),
281
- leftButtonText: S.of(context).cancel,
282
- rightButtonText: S.of(context).delete,
283
- actionLeftButton: () => Navigator.of(context).pop(),
284
- actionRightButton: () {
285
- confirmed = true;
286
- Navigator.of(context).pop();
287
- },
288
- );
289
- });
290
-
291
- if (confirmed) {
292
- try {
293
- auth != null ?
294
- auth.changeProcessText(
295
- S.of(context).wallet_list_removing_wallet(wallet.name))
296
- : changeProcessText( S.of(context).wallet_list_removing_wallet(wallet.name));
297
- await widget.walletListViewModel.remove(wallet);
298
- } catch (e) {
299
- auth != null ?
300
- auth.changeProcessText(
301
- S.of(context).wallet_list_failed_to_remove(wallet.name, e.toString()),
302
- )
303
- : changeProcessText(
304
- S.of(context).wallet_list_failed_to_remove(wallet.name, e.toString()),
271
+ await showPopUp<void>(
272
+ context: context,
273
+ builder: (BuildContext context) {
274
+ return AlertWithTwoActions(
275
+ alertTitle: S.of(context).delete_wallet,
276
+ alertContent: S.of(context).delete_wallet_confirm_message(wallet.name),
277
+ leftButtonText: S.of(context).cancel,
278
+ rightButtonText: S.of(context).delete,
279
+ actionLeftButton: () => Navigator.of(context).pop(),
280
+ actionRightButton: () {
281
+ confirmed = true;
282
+ Navigator.of(context).pop();
283
+ },
284
);
306
- }
285
+ });
286
+
287
+ if (confirmed) {
288
+ try {
289
+ auth != null
290
+ ? auth.changeProcessText(S.of(context).wallet_list_removing_wallet(wallet.name))
291
+ : changeProcessText(S.of(context).wallet_list_removing_wallet(wallet.name));
292
+ await widget.walletListViewModel.remove(wallet);
293
+ } catch (e) {
294
+ auth != null
295
+ ? auth.changeProcessText(
296
+ S.of(context).wallet_list_failed_to_remove(wallet.name, e.toString()),
297
+ )
298
+ : changeProcessText(
299
+ S.of(context).wallet_list_failed_to_remove(wallet.name, e.toString()),
300
+ );
301
}
302
+ }
303
309
- auth?.close();
304
+ auth?.close();
305
}
306
307
void changeProcessText(String text) {
@@ -319,16 +314,16 @@ class WalletListBodyState extends State<WalletListBody> {
314
}
315
316
ActionPane _actionPane(WalletListItem wallet) => ActionPane(
322
- motion: const ScrollMotion(),
323
- extentRatio: 0.3,
324
- children: [
325
- SlidableAction(
326
- onPressed: (_) => _removeWallet(wallet),
327
- backgroundColor: Colors.red,
328
- foregroundColor: Colors.white,
329
- icon: CupertinoIcons.delete,
330
- label: S.of(context).delete,
331
- ),
332
- ],
333
- );
317
+ motion: const ScrollMotion(),
318
+ extentRatio: 0.3,
319
+ children: [
320
+ SlidableAction(
321
+ onPressed: (_) => _removeWallet(wallet),
322
+ backgroundColor: Colors.red,
323
+ foregroundColor: Colors.white,
324
+ icon: CupertinoIcons.delete,
325
+ label: S.of(context).delete,
326
+ ),
327
+ ],
328
+ );
329
}
lib/view_model/auth_view_model.dart
+2
-2
@@ -121,8 +121,8 @@ abstract class AuthViewModelBase with Store {
121
}
122
}
123
124
- void _saveLastAuthTime(ExecutionState state){
125
- if(state is ExecutedSuccessfullyState){
124
+ void _saveLastAuthTime(ExecutionState state) {
125
+ if(state is ExecutedSuccessfullyState) {
126
_authService.saveLastAuthTime();
127
}
128
}