Files
cherrypick/cherrypick/example/bin/main.dart

123 lines
3.0 KiB
Dart
Raw Normal View History

2020-07-03 10:00:25 +03:00
import 'dart:async';
2025-05-13 23:36:44 +03:00
import 'package:cherrypick/cherrypick.dart';
2021-04-26 09:43:57 +03:00
class AppModule extends Module {
@override
void builder(Scope currentScope) {
2025-05-13 23:36:44 +03:00
bind<ApiClient>().withName("apiClientMock").toInstance(ApiClientMock());
bind<ApiClient>().withName("apiClientImpl").toInstance(ApiClientImpl());
2021-04-26 09:43:57 +03:00
}
}
class FeatureModule extends Module {
final bool isMock;
2021-04-26 09:43:57 +03:00
FeatureModule({required this.isMock});
@override
void builder(Scope currentScope) {
2025-05-13 23:36:44 +03:00
// Using toProvideAsync for async initialization
2025-05-19 21:02:33 +03:00
bind<DataRepository>()
.withName("networkRepo")
.toProvideWithParams((params) async {
print('REPO PARAMS: $params');
2025-05-13 23:36:44 +03:00
final client = await Future.delayed(
2025-05-19 21:02:33 +03:00
Duration(milliseconds: 1000),
() => currentScope.resolve<ApiClient>(
named: isMock ? "apiClientMock" : "apiClientImpl",
),
);
2025-05-13 23:36:44 +03:00
return NetworkDataRepository(client);
}).singleton();
// Asynchronous initialization of DataBloc
2025-05-19 21:02:33 +03:00
bind<DataBloc>().toProvide(
() async {
2025-05-13 23:36:44 +03:00
final repo = await currentScope.resolveAsync<DataRepository>(
2025-05-19 21:02:33 +03:00
named: "networkRepo",
params: 'Some params',
);
2025-05-13 23:36:44 +03:00
return DataBloc(repo);
},
2021-04-26 09:43:57 +03:00
);
}
}
2020-07-03 10:00:25 +03:00
Future<void> main() async {
try {
2025-05-19 21:02:33 +03:00
final scope = openRootScope().installModules([AppModule()]);
2021-04-26 09:43:57 +03:00
final subScope = scope
.openSubScope("featureScope")
.installModules([FeatureModule(isMock: true)]);
2020-07-03 10:00:25 +03:00
2025-05-19 21:02:33 +03:00
// Asynchronous instance resolution
final dataBloc = await subScope.resolveAsync<DataBloc>();
dataBloc.data.listen(
(d) => print('Received data: $d'),
onError: (e) => print('Error: $e'),
onDone: () => print('DONE'),
);
2020-07-03 10:00:25 +03:00
await dataBloc.fetchData();
} catch (e) {
print('Error resolving dependency: $e');
}
2020-07-03 10:00:25 +03:00
}
class DataBloc {
final DataRepository _dataRepository;
2025-05-13 23:36:44 +03:00
final StreamController<String> _dataController = StreamController.broadcast();
Stream<String> get data => _dataController.stream;
2020-07-03 10:00:25 +03:00
2025-05-13 23:36:44 +03:00
DataBloc(this._dataRepository);
2020-07-03 10:00:25 +03:00
Future<void> fetchData() async {
try {
2025-05-13 23:36:44 +03:00
_dataController.sink.add(await _dataRepository.getData());
2020-07-03 10:00:25 +03:00
} catch (e) {
_dataController.sink.addError(e);
}
}
void dispose() {
_dataController.close();
}
}
abstract class DataRepository {
2025-05-13 23:36:44 +03:00
Future<String> getData();
2020-07-03 10:00:25 +03:00
}
class NetworkDataRepository implements DataRepository {
final ApiClient _apiClient;
final _token = 'token';
NetworkDataRepository(this._apiClient);
@override
2025-05-13 23:36:44 +03:00
Future<String> getData() async => await _apiClient.sendRequest(
url: 'www.google.com', token: _token, requestBody: {'type': 'data'});
2020-07-03 10:00:25 +03:00
}
abstract class ApiClient {
2025-05-22 23:52:02 +03:00
Future sendRequest({String url, String token, Map requestBody});
2020-07-03 10:00:25 +03:00
}
class ApiClientMock implements ApiClient {
2025-05-13 23:12:17 +03:00
@override
2025-05-22 23:52:02 +03:00
Future sendRequest({String? url, String? token, Map? requestBody}) async {
2025-05-13 23:36:44 +03:00
return 'Local Data';
2021-04-26 09:43:57 +03:00
}
}
class ApiClientImpl implements ApiClient {
2025-05-13 23:12:17 +03:00
@override
2025-05-22 23:52:02 +03:00
Future sendRequest({String? url, String? token, Map? requestBody}) async {
2025-05-13 23:36:44 +03:00
return 'Network data';
2020-07-03 10:00:25 +03:00
}
2020-06-27 06:44:46 +03:00
}