1
1
import 'package:flutter/foundation.dart' show kIsWeb;
2
2
import 'package:flutter/material.dart' ;
3
3
import 'package:flutter_bloc/flutter_bloc.dart' ;
4
+ import 'package:flutter/foundation.dart' show kIsWeb;
5
+ import 'package:flutter/material.dart' ;
6
+ import 'package:flutter_bloc/flutter_bloc.dart' ;
4
7
import 'package:ht_auth_api/ht_auth_api.dart' ;
8
+ import 'package:ht_auth_client/ht_auth_client.dart' ;
9
+ import 'package:ht_auth_inmemory/ht_auth_inmemory.dart' ;
5
10
import 'package:ht_auth_repository/ht_auth_repository.dart' ;
6
11
import 'package:ht_data_api/ht_data_api.dart' ;
7
12
import 'package:ht_data_client/ht_data_client.dart' ;
@@ -14,11 +19,10 @@ import 'package:ht_main/app/config/config.dart' as app_config;
14
19
import 'package:ht_main/bloc_observer.dart' ;
15
20
import 'package:ht_main/shared/localization/ar_timeago_messages.dart' ;
16
21
import 'package:ht_main/shared/localization/en_timeago_messages.dart' ;
17
- import 'package:ht_shared/ht_shared.dart' ; // Keep this import as is for other shared models
22
+ import 'package:ht_shared/ht_shared.dart' ;
18
23
import 'package:timeago/timeago.dart' as timeago;
19
24
20
25
Future <void > bootstrap (app_config.AppConfig appConfig) async {
21
- // Use prefixed AppConfig here
22
26
WidgetsFlutterBinding .ensureInitialized ();
23
27
Bloc .observer = const AppBlocObserver ();
24
28
@@ -27,36 +31,40 @@ Future<void> bootstrap(app_config.AppConfig appConfig) async {
27
31
28
32
final kvStorage = await HtKvStorageSharedPreferences .getInstance ();
29
33
34
+ late final HtAuthClient authClient;
30
35
late final HtAuthRepository authenticationRepository;
36
+ HtHttpClient ? httpClient;
31
37
32
- Future <String ?> tokenProvider () async {
33
- return authenticationRepository.getAuthToken ();
38
+ if (appConfig.environment == app_config.AppEnvironment .demo) {
39
+ authClient = HtAuthInmemory ();
40
+ authenticationRepository = HtAuthRepository (
41
+ authClient: authClient,
42
+ storageService: kvStorage,
43
+ );
44
+ } else {
45
+ // For production and development environments, an HTTP client is needed.
46
+ httpClient = HtHttpClient (
47
+ baseUrl: appConfig.baseUrl,
48
+ tokenProvider: () => authenticationRepository.getAuthToken (),
49
+ isWeb: kIsWeb,
50
+ );
51
+ authClient = HtAuthApi (httpClient: httpClient);
52
+ authenticationRepository = HtAuthRepository (
53
+ authClient: authClient,
54
+ storageService: kvStorage,
55
+ );
34
56
}
35
57
36
- final httpClient = HtHttpClient (
37
- baseUrl: appConfig.baseUrl,
38
- tokenProvider: tokenProvider,
39
- isWeb: kIsWeb,
40
- );
41
-
42
- final authClient = HtAuthApi (httpClient: httpClient);
43
- authenticationRepository = HtAuthRepository (
44
- authClient: authClient,
45
- storageService: kvStorage,
46
- );
47
-
48
58
// Conditional data client instantiation based on environment
49
59
HtDataClient <Headline > headlinesClient;
50
60
HtDataClient <Category > categoriesClient;
51
61
HtDataClient <Country > countriesClient;
52
62
HtDataClient <Source > sourcesClient;
53
63
HtDataClient <UserContentPreferences > userContentPreferencesClient;
54
64
HtDataClient <UserAppSettings > userAppSettingsClient;
55
- HtDataClient <AppConfig >
56
- appConfigClient; // This AppConfig refers to the shared model
65
+ HtDataClient <AppConfig > appConfigClient;
57
66
58
- if (appConfig.environment == app_config.AppEnvironment .developmentInMemory) {
59
- // Use prefixed AppEnvironment
67
+ if (appConfig.environment == app_config.AppEnvironment .demo) {
60
68
headlinesClient = HtDataInMemoryClient <Headline >(
61
69
toJson: (i) => i.toJson (),
62
70
getId: (i) => i.id,
@@ -86,102 +94,97 @@ Future<void> bootstrap(app_config.AppConfig appConfig) async {
86
94
getId: (i) => i.id,
87
95
);
88
96
appConfigClient = HtDataInMemoryClient <AppConfig >(
89
- // This AppConfig refers to the shared model
90
97
toJson: (i) => i.toJson (),
91
98
getId: (i) => i.id,
92
99
initialData: [
93
100
AppConfig .fromJson (appConfigFixtureData),
94
- ], // This AppConfig refers to the shared model
101
+ ],
95
102
);
96
- } else if (appConfig.environment ==
97
- app_config.AppEnvironment .developmentApi) {
98
- // Use prefixed AppEnvironment
103
+ } else if (appConfig.environment == app_config.AppEnvironment .development) {
99
104
headlinesClient = HtDataApi <Headline >(
100
- httpClient: httpClient,
105
+ httpClient: httpClient! ,
101
106
modelName: 'headline' ,
102
107
fromJson: Headline .fromJson,
103
108
toJson: (headline) => headline.toJson (),
104
109
);
105
110
categoriesClient = HtDataApi <Category >(
106
- httpClient: httpClient,
111
+ httpClient: httpClient! ,
107
112
modelName: 'category' ,
108
113
fromJson: Category .fromJson,
109
114
toJson: (category) => category.toJson (),
110
115
);
111
116
countriesClient = HtDataApi <Country >(
112
- httpClient: httpClient,
117
+ httpClient: httpClient! ,
113
118
modelName: 'country' ,
114
119
fromJson: Country .fromJson,
115
120
toJson: (country) => country.toJson (),
116
121
);
117
122
sourcesClient = HtDataApi <Source >(
118
- httpClient: httpClient,
123
+ httpClient: httpClient! ,
119
124
modelName: 'source' ,
120
125
fromJson: Source .fromJson,
121
126
toJson: (source) => source.toJson (),
122
127
);
123
128
userContentPreferencesClient = HtDataApi <UserContentPreferences >(
124
- httpClient: httpClient,
129
+ httpClient: httpClient! ,
125
130
modelName: 'user_content_preferences' ,
126
131
fromJson: UserContentPreferences .fromJson,
127
132
toJson: (prefs) => prefs.toJson (),
128
133
);
129
134
userAppSettingsClient = HtDataApi <UserAppSettings >(
130
- httpClient: httpClient,
135
+ httpClient: httpClient! ,
131
136
modelName: 'user_app_settings' ,
132
137
fromJson: UserAppSettings .fromJson,
133
138
toJson: (settings) => settings.toJson (),
134
139
);
135
140
appConfigClient = HtDataApi <AppConfig >(
136
- // This AppConfig refers to the shared model
137
- httpClient: httpClient,
141
+ httpClient: httpClient! ,
138
142
modelName: 'app_config' ,
139
- fromJson: AppConfig .fromJson, // This AppConfig refers to the shared model
143
+ fromJson: AppConfig .fromJson,
140
144
toJson: (config) => config.toJson (),
141
145
);
142
146
} else {
143
147
// Default to API clients for production
144
148
headlinesClient = HtDataApi <Headline >(
145
- httpClient: httpClient,
149
+ httpClient: httpClient! ,
146
150
modelName: 'headline' ,
147
151
fromJson: Headline .fromJson,
148
152
toJson: (headline) => headline.toJson (),
149
153
);
150
154
categoriesClient = HtDataApi <Category >(
151
- httpClient: httpClient,
155
+ httpClient: httpClient! ,
152
156
modelName: 'category' ,
153
157
fromJson: Category .fromJson,
154
158
toJson: (category) => category.toJson (),
155
159
);
156
160
countriesClient = HtDataApi <Country >(
157
- httpClient: httpClient,
161
+ httpClient: httpClient! ,
158
162
modelName: 'country' ,
159
163
fromJson: Country .fromJson,
160
164
toJson: (country) => country.toJson (),
161
165
);
162
166
sourcesClient = HtDataApi <Source >(
163
- httpClient: httpClient,
167
+ httpClient: httpClient! ,
164
168
modelName: 'source' ,
165
169
fromJson: Source .fromJson,
166
170
toJson: (source) => source.toJson (),
167
171
);
168
172
userContentPreferencesClient = HtDataApi <UserContentPreferences >(
169
- httpClient: httpClient,
173
+ httpClient: httpClient! ,
170
174
modelName: 'user_content_preferences' ,
171
175
fromJson: UserContentPreferences .fromJson,
172
176
toJson: (prefs) => prefs.toJson (),
173
177
);
174
178
userAppSettingsClient = HtDataApi <UserAppSettings >(
175
- httpClient: httpClient,
179
+ httpClient: httpClient! ,
176
180
modelName: 'user_app_settings' ,
177
181
fromJson: UserAppSettings .fromJson,
178
182
toJson: (settings) => settings.toJson (),
179
183
);
180
184
appConfigClient = HtDataApi <AppConfig >(
181
- // This AppConfig refers to the shared model
182
- httpClient: httpClient,
185
+ httpClient: httpClient! ,
183
186
modelName: 'app_config' ,
184
- fromJson: AppConfig .fromJson, // This AppConfig refers to the shared model
187
+ fromJson: AppConfig .fromJson,
185
188
toJson: (config) => config.toJson (),
186
189
);
187
190
}
@@ -204,7 +207,6 @@ Future<void> bootstrap(app_config.AppConfig appConfig) async {
204
207
dataClient: userAppSettingsClient,
205
208
);
206
209
final appConfigRepository = HtDataRepository <AppConfig >(
207
- // This AppConfig refers to the shared model
208
210
dataClient: appConfigClient,
209
211
);
210
212
0 commit comments