Skip to content

Commit 4878186

Browse files
committed
refactor: Use bootstrap for app initialization
- Simplifies main function - Introduces app config - Uses bootstrap function
1 parent bf71209 commit 4878186

File tree

1 file changed

+15
-138
lines changed

1 file changed

+15
-138
lines changed

lib/main.dart

Lines changed: 15 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,18 @@
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_repository/ht_data_repository.dart';
8-
import 'package:ht_http_client/ht_http_client.dart';
9-
import 'package:ht_kv_storage_shared_preferences/ht_kv_storage_shared_preferences.dart';
10-
import 'package:ht_main/app/app.dart';
11-
import 'package:ht_main/bloc_observer.dart';
12-
import 'package:ht_main/shared/localization/ar_timeago_messages.dart';
13-
import 'package:ht_main/shared/localization/en_timeago_messages.dart'; // Added
14-
import 'package:ht_shared/ht_shared.dart';
15-
import 'package:timeago/timeago.dart' as timeago;
1+
import 'package:ht_main/app/config/config.dart' as app_config;
2+
import 'package:ht_main/bootstrap.dart';
163

17-
void main() async {
18-
WidgetsFlutterBinding.ensureInitialized();
19-
Bloc.observer = const AppBlocObserver();
20-
21-
// Initialize timeago custom locale messages
22-
timeago.setLocaleMessages('en', EnTimeagoMessages()); // Added
23-
timeago.setLocaleMessages('ar', ArTimeagoMessages());
24-
25-
// 1. Instantiate KV Storage Service
26-
final kvStorage = await HtKvStorageSharedPreferences.getInstance();
27-
28-
// 2. Declare Auth Repository (will be initialized after TokenProvider)
29-
// This is necessary because the TokenProvider needs a reference to the
30-
// authenticationRepository instance before it's fully initialized.
31-
late final HtAuthRepository authenticationRepository;
32-
33-
// 3. Define Token Provider
34-
Future<String?> tokenProvider() async {
35-
return authenticationRepository.getAuthToken();
36-
}
37-
38-
// 4. Instantiate HTTP Client
39-
final httpClient = HtHttpClient(
40-
baseUrl: 'http://localhost:8080',
41-
tokenProvider: tokenProvider,
42-
isWeb: kIsWeb,
43-
);
44-
45-
// 5. Instantiate Auth Client and Repository
46-
// Concrete client implementation is HtAuthApi from ht_auth_api
47-
final authClient = HtAuthApi(httpClient: httpClient);
48-
// Initialize the authenticationRepository instance
49-
authenticationRepository = HtAuthRepository(
50-
authClient: authClient,
51-
storageService: kvStorage,
52-
);
53-
54-
// 6. Instantiate Data Clients and Repositories for each model type
55-
// Concrete client implementation is HtDataApi<T> from ht_data_api
56-
// Each client needs the httpClient, a modelName string, and fromJson/toJson functions.
4+
// Define the current application environment here.
5+
// Change this value to switch between environments for local development.
6+
const app_config.AppEnvironment currentEnvironment =
7+
app_config.AppEnvironment.developmentInMemory; // Or .developmentApi, or .production
578

58-
final headlinesClient = HtDataApi<Headline>(
59-
httpClient: httpClient,
60-
modelName: 'headline',
61-
fromJson: Headline.fromJson,
62-
toJson: (headline) => headline.toJson(),
63-
);
64-
final headlinesRepository = HtDataRepository<Headline>(
65-
dataClient: headlinesClient,
66-
);
67-
68-
final categoriesClient = HtDataApi<Category>(
69-
httpClient: httpClient,
70-
modelName: 'category',
71-
fromJson: Category.fromJson,
72-
toJson: (category) => category.toJson(),
73-
);
74-
final categoriesRepository = HtDataRepository<Category>(
75-
dataClient: categoriesClient,
76-
);
77-
78-
final countriesClient = HtDataApi<Country>(
79-
httpClient: httpClient,
80-
modelName: 'country',
81-
fromJson: Country.fromJson,
82-
toJson: (country) => country.toJson(),
83-
);
84-
final countriesRepository = HtDataRepository<Country>(
85-
dataClient: countriesClient,
86-
);
87-
88-
final sourcesClient = HtDataApi<Source>(
89-
httpClient: httpClient,
90-
modelName: 'source',
91-
fromJson: Source.fromJson,
92-
toJson: (source) => source.toJson(),
93-
);
94-
final sourcesRepository = HtDataRepository<Source>(dataClient: sourcesClient);
95-
96-
final userContentPreferencesClient = HtDataApi<UserContentPreferences>(
97-
httpClient: httpClient,
98-
modelName: 'user_content_preferences',
99-
fromJson: UserContentPreferences.fromJson,
100-
toJson: (prefs) => prefs.toJson(),
101-
);
102-
final userContentPreferencesRepository =
103-
HtDataRepository<UserContentPreferences>(
104-
dataClient: userContentPreferencesClient,
105-
);
106-
107-
final userAppSettingsClient = HtDataApi<UserAppSettings>(
108-
httpClient: httpClient,
109-
modelName: 'user_app_settings',
110-
fromJson: UserAppSettings.fromJson,
111-
toJson: (settings) => settings.toJson(),
112-
);
113-
final userAppSettingsRepository = HtDataRepository<UserAppSettings>(
114-
dataClient: userAppSettingsClient,
115-
);
116-
117-
final appConfigClient = HtDataApi<AppConfig>(
118-
httpClient: httpClient,
119-
modelName: 'app_config',
120-
fromJson: AppConfig.fromJson,
121-
toJson: (config) => config.toJson(),
122-
);
123-
final appConfigRepository = HtDataRepository<AppConfig>(
124-
dataClient: appConfigClient,
125-
);
126-
127-
// 7. Run the App, injecting repositories
128-
runApp(
129-
App(
130-
htAuthenticationRepository: authenticationRepository,
131-
htHeadlinesRepository: headlinesRepository,
132-
htCategoriesRepository: categoriesRepository,
133-
htCountriesRepository: countriesRepository,
134-
htSourcesRepository: sourcesRepository,
135-
htUserAppSettingsRepository: userAppSettingsRepository,
136-
htUserContentPreferencesRepository: userContentPreferencesRepository,
137-
htAppConfigRepository: appConfigRepository,
138-
kvStorageService: kvStorage,
139-
),
140-
);
9+
void main() async {
10+
final appConfig = switch (currentEnvironment) {
11+
app_config.AppEnvironment.production => app_config.AppConfig.production(),
12+
app_config.AppEnvironment.developmentInMemory =>
13+
app_config.AppConfig.developmentInMemory(),
14+
app_config.AppEnvironment.developmentApi =>
15+
app_config.AppConfig.developmentApi(),
16+
};
17+
await bootstrap(appConfig);
14118
}

0 commit comments

Comments
 (0)