-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathmain.dart
103 lines (89 loc) · 3.46 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import 'dart:io';
import 'package:cupertino_http/cupertino_http.dart';
import 'package:disposebag/disposebag.dart';
import 'package:flutter/foundation.dart'
show debugPrint, debugPrintSynchronously, kReleaseMode;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc_pattern/flutter_bloc_pattern.dart';
import 'package:flutter_provider/flutter_provider.dart';
import 'package:http/http.dart' as http;
import 'package:http_client_hoc081098/http_client_hoc081098.dart';
import 'package:node_auth/app.dart';
import 'package:node_auth/data/local/local_data_source.dart';
import 'package:node_auth/data/local/method_channel_crypto_impl.dart';
import 'package:node_auth/data/local/shared_pref_util.dart';
import 'package:node_auth/data/remote/api_service.dart';
import 'package:node_auth/data/remote/auth_interceptor.dart';
import 'package:node_auth/data/remote/remote_data_source.dart';
import 'package:node_auth/data/user_repository_imp.dart';
import 'package:node_auth/domain/repositories/user_repository.dart';
import 'package:rx_shared_preferences/rx_shared_preferences.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
RxStreamBuilder.checkStateStreamEnabled = !kReleaseMode;
_setupLoggers();
await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
// Create http client
late final Func0<Future<void>> onUnauthorized;
final simpleHttpClient = createSimpleHttpClient(() => onUnauthorized());
// construct RemoteDataSource
final RemoteDataSource remoteDataSource = ApiService(simpleHttpClient);
// construct LocalDataSource
final rxPrefs = RxSharedPreferences.getInstance();
final crypto = MethodChannelCryptoImpl();
final LocalDataSource localDataSource = SharedPrefUtil(rxPrefs, crypto);
onUnauthorized = () => localDataSource.removeUserAndToken().first;
// construct UserRepository
final UserRepository userRepository = UserRepositoryImpl(
remoteDataSource,
localDataSource,
);
runApp(
Provider<UserRepository>.value(
userRepository,
child: const MyApp(),
),
);
}
SimpleHttpClient createSimpleHttpClient(
Func0<Future<void>> onUnauthorized,
) {
final authInterceptor = AuthInterceptor(onUnauthorized: onUnauthorized);
final loggingInterceptor = SimpleLoggingInterceptor(
SimpleLogger(
loggerFunction: print,
level: kReleaseMode ? SimpleLogLevel.none : SimpleLogLevel.body,
headersToRedact: {
ApiService.xAccessToken,
HttpHeaders.authorizationHeader,
},
),
);
final simpleHttpClient = SimpleHttpClient(
client: Platform.isIOS || Platform.isMacOS
? CupertinoClient.defaultSessionConfiguration()
: http.Client(),
timeout: const Duration(seconds: 20),
requestInterceptors: [
authInterceptor.requestInterceptor,
// others interceptors above this line
loggingInterceptor.requestInterceptor,
],
responseInterceptors: [
loggingInterceptor.responseInterceptor,
// others interceptors below this line
authInterceptor.responseInterceptor,
],
);
return simpleHttpClient;
}
void _setupLoggers() {
// set loggers to `null` to disable logging.
DisposeBagConfigs.logger = kReleaseMode ? null : disposeBagDefaultLogger;
RxSharedPreferencesConfigs.logger =
kReleaseMode ? null : const RxSharedPreferencesDefaultLogger();
debugPrint = kReleaseMode
? (String? message, {int? wrapWidth}) {}
: debugPrintSynchronously;
}