Skip to content

Commit bf71209

Browse files
committed
feat: bootstrap app with data repositories
- Init data clients based on env - Added repositories for data access
1 parent 9794830 commit bf71209

File tree

1 file changed

+215
-0
lines changed

1 file changed

+215
-0
lines changed

lib/bootstrap.dart

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
import 'package:flutter/foundation.dart' show kIsWeb;
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_bloc/flutter_bloc.dart';
4+
import 'package:ht_auth_api/ht_auth_api.dart';
5+
import 'package:ht_auth_repository/ht_auth_repository.dart';
6+
import 'package:ht_data_api/ht_data_api.dart';
7+
import 'package:ht_data_client/ht_data_client.dart';
8+
import 'package:ht_data_inmemory/ht_data_inmemory.dart';
9+
import 'package:ht_data_repository/ht_data_repository.dart';
10+
import 'package:ht_http_client/ht_http_client.dart';
11+
import 'package:ht_kv_storage_shared_preferences/ht_kv_storage_shared_preferences.dart';
12+
import 'package:ht_main/app/app.dart';
13+
import 'package:ht_main/app/config/config.dart' as app_config;
14+
import 'package:ht_main/bloc_observer.dart';
15+
import 'package:ht_main/shared/localization/ar_timeago_messages.dart';
16+
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
18+
import 'package:timeago/timeago.dart' as timeago;
19+
20+
Future<void> bootstrap(app_config.AppConfig appConfig) async { // Use prefixed AppConfig here
21+
WidgetsFlutterBinding.ensureInitialized();
22+
Bloc.observer = const AppBlocObserver();
23+
24+
timeago.setLocaleMessages('en', EnTimeagoMessages());
25+
timeago.setLocaleMessages('ar', ArTimeagoMessages());
26+
27+
final kvStorage = await HtKvStorageSharedPreferences.getInstance();
28+
29+
late final HtAuthRepository authenticationRepository;
30+
31+
Future<String?> tokenProvider() async {
32+
return authenticationRepository.getAuthToken();
33+
}
34+
35+
final httpClient = HtHttpClient(
36+
baseUrl: appConfig.baseUrl,
37+
tokenProvider: tokenProvider,
38+
isWeb: kIsWeb,
39+
);
40+
41+
final authClient = HtAuthApi(httpClient: httpClient);
42+
authenticationRepository = HtAuthRepository(
43+
authClient: authClient,
44+
storageService: kvStorage,
45+
);
46+
47+
// Conditional data client instantiation based on environment
48+
HtDataClient<Headline> headlinesClient;
49+
HtDataClient<Category> categoriesClient;
50+
HtDataClient<Country> countriesClient;
51+
HtDataClient<Source> sourcesClient;
52+
HtDataClient<UserContentPreferences> userContentPreferencesClient;
53+
HtDataClient<UserAppSettings> userAppSettingsClient;
54+
HtDataClient<AppConfig> appConfigClient; // This AppConfig refers to the shared model
55+
56+
if (appConfig.environment == app_config.AppEnvironment.developmentInMemory) { // Use prefixed AppEnvironment
57+
headlinesClient = HtDataInMemoryClient<Headline>(
58+
toJson: (i) => i.toJson(),
59+
getId: (i) => i.id,
60+
initialData: headlinesFixturesData.map(Headline.fromJson).toList(),
61+
);
62+
categoriesClient = HtDataInMemoryClient<Category>(
63+
toJson: (i) => i.toJson(),
64+
getId: (i) => i.id,
65+
initialData: categoriesFixturesData.map(Category.fromJson).toList(),
66+
);
67+
countriesClient = HtDataInMemoryClient<Country>(
68+
toJson: (i) => i.toJson(),
69+
getId: (i) => i.id,
70+
initialData: countriesFixturesData.map(Country.fromJson).toList(),
71+
);
72+
sourcesClient = HtDataInMemoryClient<Source>(
73+
toJson: (i) => i.toJson(),
74+
getId: (i) => i.id,
75+
initialData: sourcesFixturesData.map(Source.fromJson).toList(),
76+
);
77+
userContentPreferencesClient = HtDataInMemoryClient<UserContentPreferences>(
78+
toJson: (i) => i.toJson(),
79+
getId: (i) => i.id,
80+
);
81+
userAppSettingsClient = HtDataInMemoryClient<UserAppSettings>(
82+
toJson: (i) => i.toJson(),
83+
getId: (i) => i.id,
84+
);
85+
appConfigClient = HtDataInMemoryClient<AppConfig>( // This AppConfig refers to the shared model
86+
toJson: (i) => i.toJson(),
87+
getId: (i) => i.id,
88+
initialData: [AppConfig.fromJson(appConfigFixtureData)], // This AppConfig refers to the shared model
89+
);
90+
} else if (appConfig.environment == app_config.AppEnvironment.developmentApi) { // Use prefixed AppEnvironment
91+
headlinesClient = HtDataApi<Headline>(
92+
httpClient: httpClient,
93+
modelName: 'headline',
94+
fromJson: Headline.fromJson,
95+
toJson: (headline) => headline.toJson(),
96+
);
97+
categoriesClient = HtDataApi<Category>(
98+
httpClient: httpClient,
99+
modelName: 'category',
100+
fromJson: Category.fromJson,
101+
toJson: (category) => category.toJson(),
102+
);
103+
countriesClient = HtDataApi<Country>(
104+
httpClient: httpClient,
105+
modelName: 'country',
106+
fromJson: Country.fromJson,
107+
toJson: (country) => country.toJson(),
108+
);
109+
sourcesClient = HtDataApi<Source>(
110+
httpClient: httpClient,
111+
modelName: 'source',
112+
fromJson: Source.fromJson,
113+
toJson: (source) => source.toJson(),
114+
);
115+
userContentPreferencesClient = HtDataApi<UserContentPreferences>(
116+
httpClient: httpClient,
117+
modelName: 'user_content_preferences',
118+
fromJson: UserContentPreferences.fromJson,
119+
toJson: (prefs) => prefs.toJson(),
120+
);
121+
userAppSettingsClient = HtDataApi<UserAppSettings>(
122+
httpClient: httpClient,
123+
modelName: 'user_app_settings',
124+
fromJson: UserAppSettings.fromJson,
125+
toJson: (settings) => settings.toJson(),
126+
);
127+
appConfigClient = HtDataApi<AppConfig>( // This AppConfig refers to the shared model
128+
httpClient: httpClient,
129+
modelName: 'app_config',
130+
fromJson: AppConfig.fromJson, // This AppConfig refers to the shared model
131+
toJson: (config) => config.toJson(),
132+
);
133+
} else {
134+
// Default to API clients for production
135+
headlinesClient = HtDataApi<Headline>(
136+
httpClient: httpClient,
137+
modelName: 'headline',
138+
fromJson: Headline.fromJson,
139+
toJson: (headline) => headline.toJson(),
140+
);
141+
categoriesClient = HtDataApi<Category>(
142+
httpClient: httpClient,
143+
modelName: 'category',
144+
fromJson: Category.fromJson,
145+
toJson: (category) => category.toJson(),
146+
);
147+
countriesClient = HtDataApi<Country>(
148+
httpClient: httpClient,
149+
modelName: 'country',
150+
fromJson: Country.fromJson,
151+
toJson: (country) => country.toJson(),
152+
);
153+
sourcesClient = HtDataApi<Source>(
154+
httpClient: httpClient,
155+
modelName: 'source',
156+
fromJson: Source.fromJson,
157+
toJson: (source) => source.toJson(),
158+
);
159+
userContentPreferencesClient = HtDataApi<UserContentPreferences>(
160+
httpClient: httpClient,
161+
modelName: 'user_content_preferences',
162+
fromJson: UserContentPreferences.fromJson,
163+
toJson: (prefs) => prefs.toJson(),
164+
);
165+
userAppSettingsClient = HtDataApi<UserAppSettings>(
166+
httpClient: httpClient,
167+
modelName: 'user_app_settings',
168+
fromJson: UserAppSettings.fromJson,
169+
toJson: (settings) => settings.toJson(),
170+
);
171+
appConfigClient = HtDataApi<AppConfig>( // This AppConfig refers to the shared model
172+
httpClient: httpClient,
173+
modelName: 'app_config',
174+
fromJson: AppConfig.fromJson, // This AppConfig refers to the shared model
175+
toJson: (config) => config.toJson(),
176+
);
177+
}
178+
179+
final headlinesRepository = HtDataRepository<Headline>(
180+
dataClient: headlinesClient,
181+
);
182+
final categoriesRepository = HtDataRepository<Category>(
183+
dataClient: categoriesClient,
184+
);
185+
final countriesRepository = HtDataRepository<Country>(
186+
dataClient: countriesClient,
187+
);
188+
final sourcesRepository = HtDataRepository<Source>(
189+
dataClient: sourcesClient,
190+
);
191+
final userContentPreferencesRepository =
192+
HtDataRepository<UserContentPreferences>(
193+
dataClient: userContentPreferencesClient,
194+
);
195+
final userAppSettingsRepository = HtDataRepository<UserAppSettings>(
196+
dataClient: userAppSettingsClient,
197+
);
198+
final appConfigRepository = HtDataRepository<AppConfig>( // This AppConfig refers to the shared model
199+
dataClient: appConfigClient,
200+
);
201+
202+
runApp(
203+
App(
204+
htAuthenticationRepository: authenticationRepository,
205+
htHeadlinesRepository: headlinesRepository,
206+
htCategoriesRepository: categoriesRepository,
207+
htCountriesRepository: countriesRepository,
208+
htSourcesRepository: sourcesRepository,
209+
htUserAppSettingsRepository: userAppSettingsRepository,
210+
htUserContentPreferencesRepository: userContentPreferencesRepository,
211+
htAppConfigRepository: appConfigRepository,
212+
kvStorageService: kvStorage,
213+
),
214+
);
215+
}

0 commit comments

Comments
 (0)