-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathregister_dependencies.dart
62 lines (58 loc) · 2.02 KB
/
register_dependencies.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
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:open_weather_client/open_weather.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:watch_it/watch_it.dart';
import 'app/app_model.dart';
import 'locations/locations_service.dart';
import 'settings/settings_service.dart';
import 'weather/weather_model.dart';
void registerDependencies() {
di
..registerSingletonAsync<FlutterSecureStorage>(
() async {
const flutterSecureStorage = FlutterSecureStorage(
aOptions: AndroidOptions(encryptedSharedPreferences: true),
);
const apiKey = String.fromEnvironment('API_KEY', defaultValue: '');
if (apiKey.trim().isNotEmpty) {
flutterSecureStorage.write(key: SettingKeys.apiKey, value: apiKey);
}
return flutterSecureStorage;
},
)
..registerSingletonAsync<SharedPreferences>(
() async => SharedPreferences.getInstance(),
)
..registerSingletonWithDependencies<SettingsService>(
() => SettingsService(
sharedPreferences: di<SharedPreferences>(),
flutterSecureStorage: di<FlutterSecureStorage>(),
),
dependsOn: [FlutterSecureStorage, SharedPreferences],
dispose: (s) => s.dispose(),
)
..registerSingletonWithDependencies<LocationsService>(
() => LocationsService(
settingsService: di<SettingsService>(),
),
dependsOn: [SettingsService],
)
..registerSingletonAsync<OpenWeather>(
() async => OpenWeather(
apiKey:
await di<FlutterSecureStorage>().read(key: SettingKeys.apiKey) ??
'',
),
dependsOn: [SettingsService],
)
..registerSingletonWithDependencies(
() => WeatherModel(
settingsService: di<SettingsService>(),
locationsService: di<LocationsService>(),
openWeather: di<OpenWeather>(),
),
dependsOn: [OpenWeather, SettingsService],
dispose: (s) => s.dispose(),
)
..registerLazySingleton(AppModel.new);
}