Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .pubignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ reports/

analysis_options.yaml
Makefile
doc/
doc/
example/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.1

* Add dispose for DependencyLibrary

## 2.0.0

* Fix bugs, Add new feature, parent dependency
Expand Down
246 changes: 246 additions & 0 deletions EXAMPLE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@

# depend

![Pub Version](https://img.shields.io/pub/v/depend)
![License](https://img.shields.io/github/license/AlexHCJP/depend)
![Issues](https://img.shields.io/github/issues/AlexHCJP/depend)
![Coverage](https://img.shields.io/codecov/c/github/contributors-company/depend)
![Stars](https://img.shields.io/github/stars/AlexHCJP/depend)
![Contributors](https://img.shields.io/github/contributors/AlexHCJP/depend)
![Watchers](https://img.shields.io/github/watchers/AlexHCJP/depend)
![Forks](https://img.shields.io/github/forks/AlexHCJP/depend)

`depend` is a library for managing dependencies in Flutter applications. It provides a convenient way to initialize and access services or repositories via an `InheritedWidget`.

---

## Why it Rocks 🚀

- Initialize dependencies before launching the app
- Access dependencies from anywhere in the widget tree
- Log initialization times for each dependency
- Clean and extensible way to manage dependencies
- Easy to use and integrate with existing codebases

---

- [dependencies](#dependencies)
- [Why it Rocks 🚀](#why-it-rocks-)
- [Installation](#installation)
- [Example Usage](#example-usage)
- [Example 1: Define Dependencies](#example-1-define-dependencies)
- [Step 2: Initialize Dependencies](#step-2-initialize-dependencies)
- [Step 3: Access Dependencies with `InheritedWidget`](#step-3-access-dependencies-with-inheritedwidget)
- [Example 2: Use Parent Dependencies](#example-2-use-parent-dependencies)
- [Step 1: Define Parent Dependencies](#step-1-define-parent-dependencies)
- [Logging and Debugging](#logging-and-debugging)

## Installation

Add the package to your `pubspec.yaml`:

```yaml
dependencies:
depend: ^0.0.1
```

Then run:

```bash
$ flutter pub get
```
---

## Example Usage

### Example 1: Define Dependencies

#### Step 1: Extends `DependenciesLibrary`

Create a `DependenciesLibrary` that extends `DependenciesLibrary` and initializes your dependencies:

```dart
class RootLibrary extends DependenciesLibrary {
late final ApiService apiService;

@override
Future<void> init() async {
await log(() async => apiService = await ApiService().init());
}
}
```

#### Step 2: Initialize Dependencies

Use `DependenciesInit` to initialize your dependencies before launching the app:

```dart
void main() {
runApp(
Dependencies<RootLibrary>(
library: RootLibrary(),
placeholder: const ColoredBox(
color: Colors.white,
child: Center(child: CircularProgressIndicator()),
),
child: const MyApp(),
),
);
}
```

#### Step 3: Access Dependencies with `InheritedWidget`

Once initialized, dependencies can be accessed from anywhere in the widget tree using `Dependencies.of(context).authRepository`:

```dart

/// The repository for the example
final class AuthRepository {
final AuthDataSource dataSource;

AuthRepository({required this.dataSource});

Future<String> login() => dataSource.login();

void dispose() {
// stream.close();
}
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Dependencies<ModuleLibrary>(
library: ModuleLibrary(
parent: Dependencies.of<RootLibrary>(context),
),
child: BlocProvider(
create: (context) => DefaultBloc(
Dependencies.of<ModuleLibrary>(context).authRepository,
),
child: const MyHomePage(),
),
),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});

@override
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
void _login() {
context.read<DefaultBloc>().add(DefaultEvent());
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Column(
children: [
BlocBuilder<DefaultBloc, DefaultState>(
builder: (context, state) {
return Text('Login: ${state.authorized}');
},
),
Builder(
builder: (context) {
return ElevatedButton(
onPressed: _login,
child: const Text('Login'),
);
},
)
],
),
),
),
);
}
}

```

### Example 2: Use Parent Dependencies

#### Step 1: Define Parent Dependencies

```dart

class RootLibrary extends DependenciesLibrary {
late final ApiService apiService;

@override
Future<void> init() async {
apiService = await ApiService().init();
}
}

class ModuleLibrary extends DependenciesLibrary<RootLibrary> {
late final AuthRepository authRepository;

ModuleLibrary({required super.parent});

@override
Future<void> init() async {
// initialize dependencies
authRepository = AuthRepository(
dataSource: AuthDataSource(
apiService: parent.apiService, // parent - RootLibrary
),
);
}

@override
void dispose() {
authRepository.dispose();
}
}



```

## Logging and Debugging

During initialization, each dependency logs the time it took to initialize:

```dart
class ModuleLibrary extends DependenciesLibrary<RootLibrary> {
late final AuthRepository authRepository;

ModuleLibrary({required super.parent});

@override
Future<void> init() async {
await log(() async => authRepository = AuthRepository(
dataSource: AuthDataSource(
apiService: parent.apiService,
),
),
);
}
}
```

```dart
💡 ApiService: initialized successfully for 10 ms
💡 AuthRepository: initialized successfully for 0 ms
```

This is useful for tracking performance and initialization times.

## Codecov

![Codecov](https://codecov.io/gh/contributors-company/depend/graphs/sunburst.svg?token=DITZJ9E9OM)
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ final class AuthRepository {
AuthRepository({required this.dataSource});

Future<String> login() => dataSource.login();

void dispose() {
// stream.close();
}
}

class MyApp extends StatelessWidget {
Expand Down Expand Up @@ -191,13 +195,17 @@ class ModuleLibrary extends DependenciesLibrary<RootLibrary> {
@override
Future<void> init() async {
// initialize dependencies

authRepository = AuthRepository(
dataSource: AuthDataSource(
apiService: parent.apiService, // parent - RootLibrary
),
);
}

@override
void dispose() {
authRepository.dispose();
}
}


Expand All @@ -220,7 +228,8 @@ class ModuleLibrary extends DependenciesLibrary<RootLibrary> {
dataSource: AuthDataSource(
apiService: parent.apiService,
),
));
),
);
}
}
```
Expand Down
52 changes: 32 additions & 20 deletions coverage/lcov.info
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,47 @@ LH:5
end_of_record
SF:lib/src/dependencies.dart
DA:49,1
DA:56,4
DA:57,3
DA:58,2
DA:59,3
DA:63,0
DA:93,1
DA:99,0
DA:100,0
DA:101,2
DA:119,1
DA:123,2
DA:55,4
DA:56,3
DA:57,2
DA:58,3
DA:89,1
DA:95,0
DA:96,0
DA:97,2
DA:115,1
DA:119,2
DA:121,1
DA:122,1
DA:123,1
DA:125,1
DA:126,1
DA:127,1
DA:127,2
DA:128,1
DA:129,1
DA:130,1
DA:131,2
DA:130,2
DA:132,1
DA:133,1
DA:134,2
DA:134,1
DA:135,1
DA:136,1
DA:137,1
DA:138,1
DA:139,1
DA:140,2
DA:141,1
DA:142,1
DA:147,0
DA:149,0
LF:30
LH:25
DA:143,1
DA:144,1
DA:150,0
DA:152,0
DA:157,1
DA:163,1
DA:164,1
DA:169,1
DA:171,1
DA:172,3
DA:175,1
DA:176,2
LF:42
LH:38
end_of_record
7 changes: 7 additions & 0 deletions example/.pubignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ios/
linux/
windows/
web/
macos/
android/
lib/src/
Loading