2020-07-03 10:00:25 +03:00
|
|
|
import 'dart:async';
|
|
|
|
|
import 'package:meta/meta.dart';
|
2021-04-26 14:56:09 +03:00
|
|
|
import 'package:cherrypick/scope.dart';
|
|
|
|
|
import 'package:cherrypick/module.dart';
|
2021-04-26 09:43:57 +03:00
|
|
|
|
|
|
|
|
class AppModule extends Module {
|
|
|
|
|
@override
|
|
|
|
|
void builder(Scope currentScope) {
|
2021-04-29 10:02:32 +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 {
|
|
|
|
|
bool isMock;
|
|
|
|
|
|
|
|
|
|
FeatureModule({required this.isMock});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void builder(Scope currentScope) {
|
|
|
|
|
bind<DataRepository>()
|
2021-04-29 10:02:32 +03:00
|
|
|
.withName('networkRepo')
|
2021-04-26 09:43:57 +03:00
|
|
|
.toProvide(
|
|
|
|
|
() => NetworkDataRepository(
|
|
|
|
|
currentScope.resolve<ApiClient>(
|
2021-04-29 10:02:32 +03:00
|
|
|
named: isMock ? 'apiClientMock' : 'apiClientImpl',
|
2021-04-26 09:43:57 +03:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.singeltone();
|
|
|
|
|
bind<DataBloc>().toProvide(
|
|
|
|
|
() => DataBloc(
|
2021-04-29 10:02:32 +03:00
|
|
|
currentScope.resolve<DataRepository>(named: 'networkRepo'),
|
2021-04-26 09:43:57 +03:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-03 10:00:25 +03:00
|
|
|
|
|
|
|
|
void main() async {
|
2021-04-26 09:43:57 +03:00
|
|
|
final scope = openRootScope().installModules([
|
|
|
|
|
AppModule(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
final subScope = scope
|
2021-04-29 10:02:32 +03:00
|
|
|
.openSubScope('featureScope')
|
2021-04-26 09:43:57 +03:00
|
|
|
.installModules([FeatureModule(isMock: true)]);
|
2020-07-03 10:00:25 +03:00
|
|
|
|
2021-04-26 09:43:57 +03:00
|
|
|
final dataBloc = subScope.resolve<DataBloc>();
|
2020-07-03 10:00:25 +03:00
|
|
|
dataBloc.data.listen((d) => print('Received data: $d'),
|
|
|
|
|
onError: (e) => print('Error: $e'), onDone: () => print('DONE'));
|
|
|
|
|
|
|
|
|
|
await dataBloc.fetchData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DataBloc {
|
|
|
|
|
final DataRepository _dataRepository;
|
|
|
|
|
|
|
|
|
|
Stream<String> get data => _dataController.stream;
|
2021-04-29 10:02:32 +03:00
|
|
|
final StreamController<String> _dataController = StreamController.broadcast();
|
2020-07-03 10:00:25 +03:00
|
|
|
|
|
|
|
|
DataBloc(this._dataRepository);
|
|
|
|
|
|
|
|
|
|
Future<void> fetchData() async {
|
|
|
|
|
try {
|
|
|
|
|
_dataController.sink.add(await _dataRepository.getData());
|
|
|
|
|
} catch (e) {
|
|
|
|
|
_dataController.sink.addError(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void dispose() {
|
|
|
|
|
_dataController.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
abstract class DataRepository {
|
|
|
|
|
Future<String> getData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class NetworkDataRepository implements DataRepository {
|
|
|
|
|
final ApiClient _apiClient;
|
|
|
|
|
final _token = 'token';
|
|
|
|
|
|
|
|
|
|
NetworkDataRepository(this._apiClient);
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<String> getData() async => await _apiClient.sendRequest(
|
|
|
|
|
url: 'www.google.com', token: _token, requestBody: {'type': 'data'});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
abstract class ApiClient {
|
|
|
|
|
Future sendRequest({@required String url, String token, Map requestBody});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ApiClientMock implements ApiClient {
|
|
|
|
|
@override
|
|
|
|
|
Future sendRequest(
|
2021-03-27 19:48:03 +03:00
|
|
|
{@required String? url, String? token, Map? requestBody}) async {
|
2021-04-26 09:43:57 +03:00
|
|
|
return 'Local Data';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ApiClientImpl implements ApiClient {
|
|
|
|
|
@override
|
|
|
|
|
Future sendRequest(
|
|
|
|
|
{@required String? url, String? token, Map? requestBody}) async {
|
|
|
|
|
return 'Network data';
|
2020-07-03 10:00:25 +03:00
|
|
|
}
|
2020-06-27 06:44:46 +03:00
|
|
|
}
|