Skip to content

Commit

Permalink
Merge pull request #2 from FilledStacks/master
Browse files Browse the repository at this point in the history
Merged from FilledStacks
  • Loading branch information
bats64mgutsi authored Mar 21, 2021
2 parents 68d9a85 + d7a0a9f commit 717ab3d
Show file tree
Hide file tree
Showing 44 changed files with 414 additions and 241 deletions.
24 changes: 24 additions & 0 deletions packages/stacked/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
## 1.9.7

- Adds `resolveUsing` function that can be used for singleton type dependency registrations.

## 1.9.6

- Exports the `ExtendedNavigator` for nested navigation
- Update provider in stacked to 5.0.0
- Revert nonReactive update update for stacked
- Update get_it to 6.0.0
- Will revert fix for issue that came with disposing a nonReactive Viewmodel.

## 1.9.5

- Adds the asType property into the locator types

## 1.9.4

- Adds fix for [issue #171](https://github.com/FilledStacks/stacked/issues/171)

## 1.9.3

- Adds onDispose functionality

## 1.9.2

### MAJOR UPDATE: FORMS!!!
Expand Down
49 changes: 46 additions & 3 deletions packages/stacked/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,41 @@ class MyApp extends StatelessWidget {

Now you can perform navigations using the `NavigationService` if it's been registered as a dependency on your `locator`.

### Nested Navigation

Declaring your nested routes inside of the parent route's children property will generate a nested router class. The name will be the page name provided to the parent + Route. In this example: `OtherNavigatorRouter`.

```dart
@StackedApp(routes: [
MaterialRoute(page: HomeView, initial: true),
MaterialRoute(page: OtherNavigator, children: [
MaterialRoute(page: OtherView, initial: true),
MaterialRoute(page: OtherNestedView),
]),
],
)
```

Now we need to render these nested routes inside of their parent `OtherNavigator` and for that we use an `ExtendedNavigator()`.

```dart
class OtherNavigator extends StatelessWidget {
...
@override
Widget build(BuildContext context) {
return Scaffold(
body: ExtendedNavigator(router: OtherNavigatorRouter(), navigatorKey: StackedService.nestedNavigationKey(1)));
}
}
```
Now we can navigate to the nested route using the `NavigationService`, making sure to match the `id` to the `nestedNavigationKey` supplied when creating the `ExtendedNavigator()`.

```dart
_navigationService.navigateTo(OtherNavigatorRoutes.otherNestedView, id: 1);
```

### Router Arguments

View argument serialisation is automatic when using the generated router. Lets take the following view
Expand Down Expand Up @@ -1033,12 +1068,14 @@ _Note_: When your view arguments change you have to run the code generation comm

### Dependency Registration

The other major piece of boilerplate that was required was setting up get_it and making use of it on its own. This is still a very valid approach but with this new changes I wanted to introduce a quicker way of setting all that up and remove the boilerplate. This is also done using the `StackedApp` annotation. The class takes in a list of `DependencyRegistration`'s into a property called `dependencies`.
The other major piece of boilerplate that was required was setting up get_it and making use of it on its own. This is still a very valid approach but with these new changes I wanted to introduce a quicker way of setting all that up and remove the boilerplate. This is also done using the `StackedApp` annotation. The class takes in a list of `DependencyRegistration`'s into a property called `dependencies`.

```dart
@StackedApp(
dependencies: [
LazySingleton(classType: DialogService),
LazySingleton(classType: ThemeService, resolveUsing: ThemeService.getInstance),
// abstracted class type support
LazySingleton(classType: FirebaseAuthService, asType: AuthService),
Singleton(classType: NavigationService),
Expand Down Expand Up @@ -1069,7 +1106,13 @@ static Future<SharedPreferencesService> getInstance() async {
}
```

Once you've defined your dependencies then you can run
You can also pass in a `resolveFunction` for singleton registrations which takes a static `Function`. This would produce something like this

```dart
locator.registerLazySingleton(() => ThemeService.getInstance());
```

When looking at the `ThemeService` dependency registration. Once you've defined your dependencies then you can run

```
flutter pub run build_runner build --delete-conflicting-outputs
Expand Down
9 changes: 8 additions & 1 deletion packages/stacked/example/lib/app/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import 'package:new_architecture/ui/bottom_nav/history/history_viewmodel.dart';
import 'package:new_architecture/ui/details/details_view.dart';
import 'package:new_architecture/ui/form/example_form_view.dart';
import 'package:new_architecture/ui/home/home_view.dart';
import 'package:new_architecture/ui/nonreactive/nonreactive_view.dart';
import 'package:new_architecture/ui/stream_view/stream_counter_view.dart';
import 'package:stacked/stacked_annotations.dart';
import 'package:stacked_services/stacked_services.dart';
import 'package:stacked_themes/stacked_themes.dart';

@StackedApp(
routes: [
Expand All @@ -17,7 +19,8 @@ import 'package:stacked_services/stacked_services.dart';
MaterialRoute(page: StreamCounterView),
CupertinoRoute(page: DetailsView),
// TODO: Change the name of the FormView to avoid type clashing
MaterialRoute(page: ExampleFormView, initial: true),
MaterialRoute(page: ExampleFormView),
MaterialRoute(page: NonReactiveView, initial: true),
],
dependencies: [
// Lazy singletons
Expand All @@ -26,6 +29,10 @@ import 'package:stacked_services/stacked_services.dart';
LazySingleton(classType: InformationService),
LazySingleton(classType: NavigationService),
LazySingleton(classType: EpochService),
LazySingleton(
classType: ThemeService,
resolveUsing: ThemeService.getInstance,
),

// singletons
Singleton(classType: HistoryViewModel),
Expand Down
2 changes: 2 additions & 0 deletions packages/stacked/example/lib/app/app.locator.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion packages/stacked/example/lib/app/app.router.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/stacked/example/lib/services/epoch_service.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:injectable/injectable.dart';
import 'package:new_architecture/services/iepoch_service.dart';

@lazySingleton
class EpochService {
Stream<int> epochUpdatesNumbers() async* {
while (true) {
Expand Down
1 change: 1 addition & 0 deletions packages/stacked/example/lib/services/iepoch_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abstract class IEpochService {}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'package:injectable/injectable.dart';
import 'package:observable_ish/value/value.dart';
import 'package:stacked/stacked.dart';

@lazySingleton
class InformationService with ReactiveServiceMixin {
InformationService() {
listenToReactiveValues([_postCount]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'package:injectable/injectable.dart';
import 'package:stacked/stacked.dart';

@singleton
class FavoritesViewModel extends BaseViewModel {


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'package:injectable/injectable.dart';
import 'package:stacked/stacked.dart';

@singleton
class HistoryViewModel extends FutureViewModel<int> {
@override
Future<int> futureToRun() async {
Expand Down
25 changes: 25 additions & 0 deletions packages/stacked/example/lib/ui/nonreactive/nonreactive_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';
import 'package:stacked/stacked.dart';

import 'nonreactive_viewmodel.dart';

class NonReactiveView extends StatelessWidget {
const NonReactiveView({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return ViewModelBuilder<NonReactiveViewModel>.nonReactive(
builder: (context, model, child) => Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
model.updateTitle();
},
),
body: Center(
child: Text(model.title),
),
),
viewModelBuilder: () => NonReactiveViewModel(),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:stacked/stacked.dart';

class NonReactiveViewModel extends BaseViewModel {
String title = 'This should not change';

void updateTitle() {
title += '. This has changed';
notifyListeners();
}
}
11 changes: 7 additions & 4 deletions packages/stacked/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,26 @@ dependencies:

# state management
stacked:
path: ../
# path: ../
stacked_services:
stacked_hooks:
flutter_hooks:
stacked_themes:

# navigation
get:

# logging
logger:

dev_dependencies:
flutter_test:
sdk: flutter

# dependency injection
injectable_generator:
build_runner:
stacked_generator:
path: ../../stacked_generator
stacked_generator: ^0.2.6
# path: ../../stacked_generator

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,38 @@ class DependencyRegistration {
/// The type of the service to register
final Type classType;

const DependencyRegistration({this.classType});
/// An abstracted class type of service to register
final Type asType;

final Function resolveUsing;

const DependencyRegistration(
{this.classType, this.asType, this.resolveUsing});
}

/// Registers the type passed in as a singleton instance in the get_it locator
class Singleton extends DependencyRegistration {
const Singleton({Type classType}) : super(classType: classType);
const Singleton({Type classType, Type asType, Function resolveUsing})
: super(classType: classType, asType: asType, resolveUsing: resolveUsing);
}

/// Registers the type passed in as a LazySingleton instance in the get_it locator
class LazySingleton extends DependencyRegistration {
const LazySingleton({Type classType}) : super(classType: classType);
const LazySingleton({Type classType, Type asType, Function resolveUsing})
: super(classType: classType, asType: asType, resolveUsing: resolveUsing);
}

/// Registers the type passed in as a Factory in the get_it locator
class Factory extends DependencyRegistration {
const Factory({Type classType}) : super(classType: classType);
const Factory({Type classType, Type asType})
: super(classType: classType, asType: asType);
}

/// Registers the type passed in to be presolved using the function passed in
class Presolve extends DependencyRegistration {
/// The static instance Future function to use for resolving the type registered
final Future Function() presolveUsing;

const Presolve({Type classType, this.presolveUsing})
: super(classType: classType);
const Presolve({Type classType, this.presolveUsing, Type asType})
: super(classType: classType, asType: asType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ mixin ReactiveServiceMixin {
reactiveValue.onChange.listen((event) => notifyListeners());
} else if (reactiveValue is RxSet) {
reactiveValue.onChange.listen((event) => notifyListeners());
} else if (reactiveValue is ChangeNotifier) {
(reactiveValue as ChangeNotifier).notifyListeners();
}
}
}
Expand Down
Loading

0 comments on commit 717ab3d

Please # to comment.