Skip to content

Commit 22a914b

Browse files
authored
hamed/refactor_server_time_cubit (#221)
- refactor server time cubit
1 parent 50741e6 commit 22a914b

File tree

4 files changed

+60
-78
lines changed

4 files changed

+60
-78
lines changed

lib/state/server_time/server_time_bloc.dart

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import 'dart:async';
2+
3+
import 'package:flutter_bloc/flutter_bloc.dart';
4+
5+
import 'package:flutter_deriv_api/api/response/time_response_result.dart';
6+
import 'package:flutter_deriv_api/helpers/helpers.dart';
7+
import 'package:flutter_deriv_api/state/connection/connection_cubit.dart';
8+
9+
part 'server_time_state.dart';
10+
11+
/// A cubit to fetch and sync machine time with server time.
12+
class ServerTimeCubit extends Cubit<ServerTimeState> {
13+
/// Initializes [ServerTimeCubit].
14+
ServerTimeCubit(ConnectionCubit connectionCubit)
15+
: super(InitialServerTime()) {
16+
_connectionSubscription =
17+
connectionCubit.stream.listen(_connectionListener);
18+
}
19+
20+
late final StreamSubscription<ConnectionState> _connectionSubscription;
21+
22+
final Duration _fetchServerTimeDuration = const Duration(seconds: 90);
23+
final Duration _fetchServerTimeTimeoutDuration = const Duration(seconds: 5);
24+
25+
Timer? _serverTimeInterval;
26+
27+
void _connectionListener(ConnectionState state) {
28+
_fetchServerTime(state);
29+
30+
_serverTimeInterval = Timer.periodic(
31+
_fetchServerTimeDuration,
32+
(Timer timer) => _fetchServerTime(state),
33+
);
34+
}
35+
36+
Future<void> _fetchServerTime(ConnectionState state) async {
37+
if (state is ConnectionConnectedState) {
38+
try {
39+
final TimeResponse serverTime = await TimeResponse.fetchTime()
40+
.timeout(_fetchServerTimeTimeoutDuration);
41+
42+
emit(ServerTimeFetched(serverTime: serverTime.time));
43+
} on Exception catch (e) {
44+
emit(ServerTimeError('$e'));
45+
}
46+
} else {
47+
_serverTimeInterval?.cancel();
48+
}
49+
}
50+
51+
@override
52+
Future<void> close() async {
53+
await _connectionSubscription.cancel();
54+
55+
return super.close();
56+
}
57+
}

lib/state/server_time/server_time_event.dart

Lines changed: 0 additions & 10 deletions
This file was deleted.

lib/state/server_time/server_time_state.dart

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of 'server_time_bloc.dart';
1+
part of 'server_time_cubit.dart';
22

33
/// Server time base states.
44
abstract class ServerTimeState {
@@ -17,19 +17,13 @@ class InitialServerTime extends ServerTimeState {
1717
String toString() => 'ServerTimeState: InitialServerTime';
1818
}
1919

20-
/// Shows that we are in the process of fetching server time.
21-
class FetchingServerTime extends ServerTimeState {
22-
@override
23-
String toString() => 'ServerTimeState: FetchingServerTime';
24-
}
25-
2620
/// Server time fetched state.
2721
class ServerTimeFetched extends ServerTimeState {
2822
/// Initializes server time fetched state.
2923
ServerTimeFetched({this.serverTime, int? timeDifference})
3024
: super(
31-
timeDifference =
32-
getSecondsSinceEpochDateTime(serverTime)! - getCurrentLocalEpoch(),
25+
timeDifference = getSecondsSinceEpochDateTime(serverTime)! -
26+
getCurrentLocalEpoch(),
3327
);
3428

3529
/// Fetched server time.

0 commit comments

Comments
 (0)