Skip to content

Commit 52ad9ea

Browse files
author
Egor Komarov
committed
refactor(EWM-589): replace NtpTime.now() with NtpService.now()
1 parent 6f05c5a commit 52ad9ea

File tree

21 files changed

+210
-173
lines changed

21 files changed

+210
-173
lines changed

lib/app/service/connection/data/connection_data/connection_data.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'package:app/app/service/connection/data/network_type.dart';
2-
import 'package:app/utils/utils.dart';
32
import 'package:freezed_annotation/freezed_annotation.dart';
43
import 'package:uuid/uuid.dart';
54

@@ -55,7 +54,7 @@ sealed class ConnectionData with _$ConnectionData {
5554
nativeTokenTicker: nativeTokenTicker,
5655
isPreset: false,
5756
canBeEdited: true,
58-
sortingOrder: NtpTime.now().millisecondsSinceEpoch.toDouble(),
57+
sortingOrder: DateTime.now().millisecondsSinceEpoch.toDouble(),
5958
latencyDetectionInterval: latencyDetectionInterval,
6059
maxLatency: maxLatency,
6160
endpointSelectionRetryCount: endpointSelectionRetryCount,
@@ -118,7 +117,7 @@ sealed class ConnectionData with _$ConnectionData {
118117
nativeTokenTicker: '',
119118
isPreset: false,
120119
canBeEdited: true,
121-
sortingOrder: NtpTime.now().millisecondsSinceEpoch.toDouble(),
120+
sortingOrder: DateTime.now().millisecondsSinceEpoch.toDouble(),
122121
latencyDetectionInterval: latencyDetectionInterval,
123122
maxLatency: maxLatency,
124123
endpointSelectionRetryCount: endpointSelectionRetryCount,
@@ -164,7 +163,7 @@ sealed class ConnectionData with _$ConnectionData {
164163
nativeTokenTicker: nativeTokenTicker,
165164
isPreset: false,
166165
canBeEdited: true,
167-
sortingOrder: NtpTime.now().millisecondsSinceEpoch.toDouble(),
166+
sortingOrder: DateTime.now().millisecondsSinceEpoch.toDouble(),
168167
isUsedOnStart: isUsedOnStart,
169168
nativeTokenDecimals: nativeTokenDecimals,
170169
);
@@ -212,7 +211,7 @@ sealed class ConnectionData with _$ConnectionData {
212211
nativeTokenTicker: '',
213212
isPreset: false,
214213
canBeEdited: true,
215-
sortingOrder: NtpTime.now().millisecondsSinceEpoch.toDouble(),
214+
sortingOrder: DateTime.now().millisecondsSinceEpoch.toDouble(),
216215
isUsedOnStart: isUsedOnStart,
217216
nativeTokenDecimals: nativeTokenDecimals,
218217
);
@@ -255,7 +254,7 @@ sealed class ConnectionData with _$ConnectionData {
255254
nativeTokenTicker: nativeTokenTicker,
256255
isPreset: false,
257256
canBeEdited: true,
258-
sortingOrder: NtpTime.now().millisecondsSinceEpoch.toDouble(),
257+
sortingOrder: DateTime.now().millisecondsSinceEpoch.toDouble(),
259258
isUsedOnStart: isUsedOnStart,
260259
nativeTokenDecimals: nativeTokenDecimals,
261260
);
@@ -303,7 +302,7 @@ sealed class ConnectionData with _$ConnectionData {
303302
nativeTokenTicker: '',
304303
isPreset: false,
305304
canBeEdited: true,
306-
sortingOrder: NtpTime.now().millisecondsSinceEpoch.toDouble(),
305+
sortingOrder: DateTime.now().millisecondsSinceEpoch.toDouble(),
307306
isUsedOnStart: isUsedOnStart,
308307
nativeTokenDecimals: nativeTokenDecimals,
309308
);

lib/app/service/password_service.dart

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@ import 'dart:async';
22
import 'dart:convert';
33

44
import 'package:app/app/service/service.dart';
5-
import 'package:app/utils/utils.dart';
65
import 'package:injectable/injectable.dart';
76
import 'package:logging/logging.dart';
87
import 'package:nekoton_repository/nekoton_repository.dart';
98
import 'package:rxdart/rxdart.dart';
109

1110
@singleton
1211
final class PasswordService {
13-
PasswordService(this._storageService, this._nekotonRepository);
12+
PasswordService(
13+
this._storageService,
14+
this._nekotonRepository,
15+
this._ntpService,
16+
);
1417

1518
static final _logger = Logger('PasswordService');
1619

1720
final NekotonRepository _nekotonRepository;
1821
final SecureStorageService _storageService;
22+
final NtpService _ntpService;
1923

2024
/// Master key to state map
2125
final _state = <PublicKey, _State>{};
@@ -31,7 +35,14 @@ final class PasswordService {
3135
for (final entry in map.entries) {
3236
try {
3337
final masterKey = PublicKey(publicKey: entry.key);
34-
final state = _State.fromJson(entry.value as Map<String, dynamic>);
38+
final json = entry.value as Map<String, dynamic>;
39+
final state = _State(
40+
_ntpService,
41+
attempts: json['attempts'] as int,
42+
lastAttempt: json['lastAttempt'] != null
43+
? DateTime.tryParse(json['lastAttempt'] as String)
44+
: null,
45+
);
3546

3647
_state[masterKey] = state;
3748
} catch (e, s) {
@@ -77,7 +88,7 @@ final class PasswordService {
7788
final masterKey = _getMasterKey(publicKey);
7889

7990
// invalid state, should not happen
80-
if (masterKey == null) return _State.unlocked();
91+
if (masterKey == null) return _State(_ntpService);
8192

8293
return _getLockState(masterKey);
8394
}
@@ -123,7 +134,7 @@ final class PasswordService {
123134
}
124135

125136
_State _getLockState(PublicKey masterKey) {
126-
return _state[masterKey] ??= _State.unlocked();
137+
return _state[masterKey] ??= _State(_ntpService);
127138
}
128139

129140
PublicKey? _getMasterKey(PublicKey publicKey) => _nekotonRepository.seedList
@@ -151,20 +162,11 @@ abstract class PasswordLockState {
151162
}
152163

153164
final class _State implements PasswordLockState {
154-
_State._({this.lastAttempt, this.attempts = 0}) {
165+
_State(this._ntpService, {this.lastAttempt, this.attempts = 0}) {
155166
_update();
156167
}
157168

158-
factory _State.unlocked() = _State._;
159-
160-
factory _State.fromJson(Map<String, dynamic> json) {
161-
return _State._(
162-
attempts: json['attempts'] as int,
163-
lastAttempt: json['lastAttempt'] != null
164-
? DateTime.tryParse(json['lastAttempt'] as String)
165-
: null,
166-
);
167-
}
169+
final NtpService _ntpService;
168170

169171
@override
170172
int attempts;
@@ -202,7 +204,7 @@ final class _State implements PasswordLockState {
202204
bool get isLocked {
203205
final lockUntil = this.lockUntil;
204206
if (lockUntil == null) return false;
205-
return NtpTime.now().isBefore(lockUntil);
207+
return _ntpService.now().isBefore(lockUntil);
206208
}
207209

208210
@override
@@ -216,7 +218,7 @@ final class _State implements PasswordLockState {
216218
}
217219

218220
void attempt() {
219-
lastAttempt = NtpTime.now();
221+
lastAttempt = _ntpService.now();
220222
attempts += 1;
221223

222224
_update();
@@ -243,7 +245,7 @@ final class _State implements PasswordLockState {
243245
_unlockTimer = null;
244246

245247
if (isLocked && lockUntil != null) {
246-
final now = NtpTime.now();
248+
final now = _ntpService.now();
247249
final timeToWait = lockUntil.difference(now) + const Duration(seconds: 1);
248250

249251
if (!timeToWait.isNegative) {

lib/data/models/browser_bookmark_item.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import 'package:app/data/models/browser_item.dart';
2-
import 'package:app/utils/utils.dart';
32
import 'package:freezed_annotation/freezed_annotation.dart';
43
import 'package:uuid/uuid.dart';
54

65
part 'browser_bookmark_item.freezed.dart';
7-
86
part 'browser_bookmark_item.g.dart';
97

108
@freezed
@@ -28,6 +26,6 @@ abstract class BrowserBookmarkItem
2826
id: const Uuid().v4(),
2927
title: title,
3028
url: url,
31-
sortingOrder: NtpTime.now().millisecondsSinceEpoch.toDouble(),
29+
sortingOrder: DateTime.now().millisecondsSinceEpoch.toDouble(),
3230
);
3331
}

lib/data/models/browser_history_item.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'package:app/data/models/browser_item.dart';
2-
import 'package:app/utils/utils.dart';
32
import 'package:freezed_annotation/freezed_annotation.dart';
43
import 'package:uuid/uuid.dart';
54

@@ -22,7 +21,7 @@ abstract class BrowserHistoryItem
2221
id: const Uuid().v4(),
2322
title: url.host,
2423
url: url,
25-
visitTime: NtpTime.now(),
24+
visitTime: DateTime.now(),
2625
);
2726

2827
const BrowserHistoryItem._();

0 commit comments

Comments
 (0)