Skip to content

Commit e0af134

Browse files
committed
refactor: Support in-memory auth for demo env
- Added auth in-memory client - Uses in-memory for demo env - Refactored client initialization
1 parent fe53dba commit e0af134

File tree

1 file changed

+46
-44
lines changed

1 file changed

+46
-44
lines changed

lib/bootstrap.dart

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import 'package:flutter/foundation.dart' show kIsWeb;
22
import 'package:flutter/material.dart';
33
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';
47
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';
510
import 'package:ht_auth_repository/ht_auth_repository.dart';
611
import 'package:ht_data_api/ht_data_api.dart';
712
import 'package:ht_data_client/ht_data_client.dart';
@@ -14,11 +19,10 @@ import 'package:ht_main/app/config/config.dart' as app_config;
1419
import 'package:ht_main/bloc_observer.dart';
1520
import 'package:ht_main/shared/localization/ar_timeago_messages.dart';
1621
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';
1823
import 'package:timeago/timeago.dart' as timeago;
1924

2025
Future<void> bootstrap(app_config.AppConfig appConfig) async {
21-
// Use prefixed AppConfig here
2226
WidgetsFlutterBinding.ensureInitialized();
2327
Bloc.observer = const AppBlocObserver();
2428

@@ -27,36 +31,40 @@ Future<void> bootstrap(app_config.AppConfig appConfig) async {
2731

2832
final kvStorage = await HtKvStorageSharedPreferences.getInstance();
2933

34+
late final HtAuthClient authClient;
3035
late final HtAuthRepository authenticationRepository;
36+
HtHttpClient? httpClient;
3137

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+
);
3456
}
3557

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-
4858
// Conditional data client instantiation based on environment
4959
HtDataClient<Headline> headlinesClient;
5060
HtDataClient<Category> categoriesClient;
5161
HtDataClient<Country> countriesClient;
5262
HtDataClient<Source> sourcesClient;
5363
HtDataClient<UserContentPreferences> userContentPreferencesClient;
5464
HtDataClient<UserAppSettings> userAppSettingsClient;
55-
HtDataClient<AppConfig>
56-
appConfigClient; // This AppConfig refers to the shared model
65+
HtDataClient<AppConfig> appConfigClient;
5766

58-
if (appConfig.environment == app_config.AppEnvironment.developmentInMemory) {
59-
// Use prefixed AppEnvironment
67+
if (appConfig.environment == app_config.AppEnvironment.demo) {
6068
headlinesClient = HtDataInMemoryClient<Headline>(
6169
toJson: (i) => i.toJson(),
6270
getId: (i) => i.id,
@@ -86,102 +94,97 @@ Future<void> bootstrap(app_config.AppConfig appConfig) async {
8694
getId: (i) => i.id,
8795
);
8896
appConfigClient = HtDataInMemoryClient<AppConfig>(
89-
// This AppConfig refers to the shared model
9097
toJson: (i) => i.toJson(),
9198
getId: (i) => i.id,
9299
initialData: [
93100
AppConfig.fromJson(appConfigFixtureData),
94-
], // This AppConfig refers to the shared model
101+
],
95102
);
96-
} else if (appConfig.environment ==
97-
app_config.AppEnvironment.developmentApi) {
98-
// Use prefixed AppEnvironment
103+
} else if (appConfig.environment == app_config.AppEnvironment.development) {
99104
headlinesClient = HtDataApi<Headline>(
100-
httpClient: httpClient,
105+
httpClient: httpClient!,
101106
modelName: 'headline',
102107
fromJson: Headline.fromJson,
103108
toJson: (headline) => headline.toJson(),
104109
);
105110
categoriesClient = HtDataApi<Category>(
106-
httpClient: httpClient,
111+
httpClient: httpClient!,
107112
modelName: 'category',
108113
fromJson: Category.fromJson,
109114
toJson: (category) => category.toJson(),
110115
);
111116
countriesClient = HtDataApi<Country>(
112-
httpClient: httpClient,
117+
httpClient: httpClient!,
113118
modelName: 'country',
114119
fromJson: Country.fromJson,
115120
toJson: (country) => country.toJson(),
116121
);
117122
sourcesClient = HtDataApi<Source>(
118-
httpClient: httpClient,
123+
httpClient: httpClient!,
119124
modelName: 'source',
120125
fromJson: Source.fromJson,
121126
toJson: (source) => source.toJson(),
122127
);
123128
userContentPreferencesClient = HtDataApi<UserContentPreferences>(
124-
httpClient: httpClient,
129+
httpClient: httpClient!,
125130
modelName: 'user_content_preferences',
126131
fromJson: UserContentPreferences.fromJson,
127132
toJson: (prefs) => prefs.toJson(),
128133
);
129134
userAppSettingsClient = HtDataApi<UserAppSettings>(
130-
httpClient: httpClient,
135+
httpClient: httpClient!,
131136
modelName: 'user_app_settings',
132137
fromJson: UserAppSettings.fromJson,
133138
toJson: (settings) => settings.toJson(),
134139
);
135140
appConfigClient = HtDataApi<AppConfig>(
136-
// This AppConfig refers to the shared model
137-
httpClient: httpClient,
141+
httpClient: httpClient!,
138142
modelName: 'app_config',
139-
fromJson: AppConfig.fromJson, // This AppConfig refers to the shared model
143+
fromJson: AppConfig.fromJson,
140144
toJson: (config) => config.toJson(),
141145
);
142146
} else {
143147
// Default to API clients for production
144148
headlinesClient = HtDataApi<Headline>(
145-
httpClient: httpClient,
149+
httpClient: httpClient!,
146150
modelName: 'headline',
147151
fromJson: Headline.fromJson,
148152
toJson: (headline) => headline.toJson(),
149153
);
150154
categoriesClient = HtDataApi<Category>(
151-
httpClient: httpClient,
155+
httpClient: httpClient!,
152156
modelName: 'category',
153157
fromJson: Category.fromJson,
154158
toJson: (category) => category.toJson(),
155159
);
156160
countriesClient = HtDataApi<Country>(
157-
httpClient: httpClient,
161+
httpClient: httpClient!,
158162
modelName: 'country',
159163
fromJson: Country.fromJson,
160164
toJson: (country) => country.toJson(),
161165
);
162166
sourcesClient = HtDataApi<Source>(
163-
httpClient: httpClient,
167+
httpClient: httpClient!,
164168
modelName: 'source',
165169
fromJson: Source.fromJson,
166170
toJson: (source) => source.toJson(),
167171
);
168172
userContentPreferencesClient = HtDataApi<UserContentPreferences>(
169-
httpClient: httpClient,
173+
httpClient: httpClient!,
170174
modelName: 'user_content_preferences',
171175
fromJson: UserContentPreferences.fromJson,
172176
toJson: (prefs) => prefs.toJson(),
173177
);
174178
userAppSettingsClient = HtDataApi<UserAppSettings>(
175-
httpClient: httpClient,
179+
httpClient: httpClient!,
176180
modelName: 'user_app_settings',
177181
fromJson: UserAppSettings.fromJson,
178182
toJson: (settings) => settings.toJson(),
179183
);
180184
appConfigClient = HtDataApi<AppConfig>(
181-
// This AppConfig refers to the shared model
182-
httpClient: httpClient,
185+
httpClient: httpClient!,
183186
modelName: 'app_config',
184-
fromJson: AppConfig.fromJson, // This AppConfig refers to the shared model
187+
fromJson: AppConfig.fromJson,
185188
toJson: (config) => config.toJson(),
186189
);
187190
}
@@ -204,7 +207,6 @@ Future<void> bootstrap(app_config.AppConfig appConfig) async {
204207
dataClient: userAppSettingsClient,
205208
);
206209
final appConfigRepository = HtDataRepository<AppConfig>(
207-
// This AppConfig refers to the shared model
208210
dataClient: appConfigClient,
209211
);
210212

0 commit comments

Comments
 (0)