mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +00:00
init di
This commit is contained in:
21
examples/postly/lib/di/app_module.dart
Normal file
21
examples/postly/lib/di/app_module.dart
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:cherrypick/cherrypick.dart';
|
||||||
|
import '../data/network/json_placeholder_api.dart';
|
||||||
|
import '../data/post_repository_impl.dart';
|
||||||
|
import '../domain/repository/post_repository.dart';
|
||||||
|
|
||||||
|
class AppModule extends Module {
|
||||||
|
@override
|
||||||
|
void builder(Scope currentScope) {
|
||||||
|
bind<Dio>().toProvide(() => Dio()).singleton();
|
||||||
|
|
||||||
|
bind<JsonPlaceholderApi>()
|
||||||
|
.toProvide(() => JsonPlaceholderApi(currentScope.resolve<Dio>()))
|
||||||
|
.singleton();
|
||||||
|
|
||||||
|
bind<PostRepository>()
|
||||||
|
.toProvide(() =>
|
||||||
|
PostRepositoryImpl(currentScope.resolve<JsonPlaceholderApi>()))
|
||||||
|
.singleton();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,28 +1,29 @@
|
|||||||
|
import 'package:cherrypick/cherrypick.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:dio/dio.dart';
|
import 'di/app_module.dart';
|
||||||
import 'data/network/json_placeholder_api.dart';
|
|
||||||
import 'data/post_repository_impl.dart';
|
|
||||||
import 'domain/repository/post_repository.dart';
|
import 'domain/repository/post_repository.dart';
|
||||||
import 'presentation/bloc/post_bloc.dart';
|
import 'presentation/bloc/post_bloc.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'router/app_router.dart';
|
import 'router/app_router.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
final dio = Dio();
|
final scope = CherryPick.openRootScope();
|
||||||
final api = JsonPlaceholderApi(dio);
|
scope.installModules([AppModule()]);
|
||||||
final repository = PostRepositoryImpl(api);
|
|
||||||
|
|
||||||
runApp(MyApp(repository: repository));
|
runApp(MyApp(scope: scope));
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyApp extends StatelessWidget {
|
class MyApp extends StatelessWidget {
|
||||||
final PostRepository repository;
|
final Scope scope;
|
||||||
final _appRouter = AppRouter();
|
final _appRouter = AppRouter();
|
||||||
|
|
||||||
MyApp({super.key, required this.repository});
|
MyApp({super.key, required this.scope});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
// Получаем репозиторий через injector
|
||||||
|
final repository = scope.resolve<PostRepository>();
|
||||||
|
|
||||||
return BlocProvider(
|
return BlocProvider(
|
||||||
create: (_) => PostBloc(repository),
|
create: (_) => PostBloc(repository),
|
||||||
child: MaterialApp.router(
|
child: MaterialApp.router(
|
||||||
|
|||||||
@@ -150,6 +150,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.3"
|
version: "2.0.3"
|
||||||
|
cherrypick:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: cherrypick
|
||||||
|
sha256: e1e2b4f3a70cbe7760e479e6ddb7dce2fc85a1bbb2fba6c398efe235ed111dfe
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.0.2"
|
||||||
clock:
|
clock:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -11,6 +11,9 @@ environment:
|
|||||||
dependencies:
|
dependencies:
|
||||||
flutter:
|
flutter:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
|
||||||
|
cherrypick: any
|
||||||
|
|
||||||
dio: ^5.4.0
|
dio: ^5.4.0
|
||||||
retrofit: ^4.0.3
|
retrofit: ^4.0.3
|
||||||
freezed_annotation: ^2.0.0
|
freezed_annotation: ^2.0.0
|
||||||
|
|||||||
@@ -5,29 +5,24 @@
|
|||||||
// gestures. You can also use WidgetTester to find child widgets in the widget
|
// gestures. You can also use WidgetTester to find child widgets in the widget
|
||||||
// tree, read text, and verify that the values of widget properties are correct.
|
// tree, read text, and verify that the values of widget properties are correct.
|
||||||
|
|
||||||
import 'package:dio/dio.dart';
|
import 'package:cherrypick/cherrypick.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:postly/data/network/json_placeholder_api.dart';
|
import 'package:postly/di/app_module.dart';
|
||||||
import 'package:postly/data/post_repository_impl.dart';
|
|
||||||
import 'package:postly/domain/repository/post_repository.dart';
|
|
||||||
|
|
||||||
import 'package:postly/main.dart';
|
import 'package:postly/main.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
late Dio dio;
|
late Scope scope;
|
||||||
late JsonPlaceholderApi api;
|
|
||||||
late PostRepository repository;
|
|
||||||
|
|
||||||
setUp(() {
|
setUp(() {
|
||||||
dio = Dio();
|
scope = CherryPick.openRootScope();
|
||||||
api = JsonPlaceholderApi(dio);
|
scope.installModules([AppModule()]);
|
||||||
repository = PostRepositoryImpl(api);
|
|
||||||
});
|
});
|
||||||
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
|
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
|
||||||
// Build our app and trigger a frame.
|
// Build our app and trigger a frame.
|
||||||
await tester.pumpWidget(MyApp(
|
await tester.pumpWidget(MyApp(
|
||||||
repository: repository,
|
scope: scope,
|
||||||
));
|
));
|
||||||
|
|
||||||
// Verify that our counter starts at 0.
|
// Verify that our counter starts at 0.
|
||||||
|
|||||||
Reference in New Issue
Block a user