Ahmed Fwela
Committed by GitHub

Merge branch 'jonataslaw:master' into fix-router-outlet

@@ -6,12 +6,13 @@ @@ -6,12 +6,13 @@
6 - [Get.putAsync](#getputasync) 6 - [Get.putAsync](#getputasync)
7 - [Get.create](#getcreate) 7 - [Get.create](#getcreate)
8 - [Using instantiated methods/classes](#using-instantiated-methodsclasses) 8 - [Using instantiated methods/classes](#using-instantiated-methodsclasses)
  9 + - [Specifying an alternate instance](#specifying-an-alternate-instance)
9 - [Differences between methods](#differences-between-methods) 10 - [Differences between methods](#differences-between-methods)
10 - [Bindings](#bindings) 11 - [Bindings](#bindings)
11 - - [How to use](#how-to-use) 12 + - [Bindings class](#bindings-class)
12 - [BindingsBuilder](#bindingsbuilder) 13 - [BindingsBuilder](#bindingsbuilder)
13 - [SmartManagement](#smartmanagement) 14 - [SmartManagement](#smartmanagement)
14 - - [How to change](#How-to-change) 15 + - [How to change](#how-to-change)
15 - [SmartManagement.full](#smartmanagementfull) 16 - [SmartManagement.full](#smartmanagementfull)
16 - [SmartManagement.onlyBuilders](#smartmanagementonlybuilders) 17 - [SmartManagement.onlyBuilders](#smartmanagementonlybuilders)
17 - [SmartManagement.keepFactory](#smartmanagementkeepfactory) 18 - [SmartManagement.keepFactory](#smartmanagementkeepfactory)
@@ -202,6 +203,25 @@ To remove an instance of Get: @@ -202,6 +203,25 @@ To remove an instance of Get:
202 Get.delete<Controller>(); //usually you don't need to do this because GetX already delete unused controllers 203 Get.delete<Controller>(); //usually you don't need to do this because GetX already delete unused controllers
203 ``` 204 ```
204 205
  206 +## Specifying an alternate instance
  207 +
  208 +A currently inserted instance can be replaced with a similar or extended class instance by using the `replace` method. This can then be retrieved by using the original class.
  209 +
  210 +```dart
  211 +class ParentClass {}
  212 +
  213 +class ChildClass extends ParentClass {
  214 + bool isChild = true;
  215 +}
  216 +
  217 +Get.put(ParentClass());
  218 +
  219 +Get.replace<ParentClass, ChildClass>(ChildClass());
  220 +
  221 +final instance = Get.find<ParentClass>();
  222 +print(instance is ChildClass); //true
  223 +```
  224 +
205 ## Differences between methods 225 ## Differences between methods
206 226
207 First, let's of the `fenix` of Get.lazyPut and the `permanent` of the other methods. 227 First, let's of the `fenix` of Get.lazyPut and the `permanent` of the other methods.
@@ -128,4 +128,29 @@ extension Inst on GetInterface { @@ -128,4 +128,29 @@ extension Inst on GetInterface {
128 /// `Get.lazyPut()`, is registered in memory. 128 /// `Get.lazyPut()`, is registered in memory.
129 /// - [tag] optional, if you use a [tag] to register the Instance. 129 /// - [tag] optional, if you use a [tag] to register the Instance.
130 bool isPrepared<S>({String? tag}) => GetInstance().isPrepared<S>(tag: tag); 130 bool isPrepared<S>({String? tag}) => GetInstance().isPrepared<S>(tag: tag);
  131 +
  132 + /// Replace a parent instance of a class in dependency management
  133 + /// with a [child] instance
  134 + /// - [tag] optional, if you use a [tag] to register the Instance.
  135 + void replace<P>(P child, {String? tag}) {
  136 + final info = GetInstance().getInstanceInfo<P>(tag: tag);
  137 + final permanent = (info.isPermanent ?? false);
  138 + delete<P>(tag: tag, force: permanent);
  139 + put(child, tag: tag, permanent: permanent);
  140 + }
  141 +
  142 + /// Replaces a parent instance with a new Instance<P> lazily from the
  143 + /// [<P>builder()] callback.
  144 + /// - [tag] optional, if you use a [tag] to register the Instance.
  145 + /// - [fenix] optional
  146 + ///
  147 + /// Note: if fenix is not provided it will be set to true if
  148 + /// the parent instance was permanent
  149 + void lazyReplace<P>(InstanceBuilderCallback<P> builder,
  150 + {String? tag, bool? fenix}) {
  151 + final info = GetInstance().getInstanceInfo<P>(tag: tag);
  152 + final permanent = (info.isPermanent ?? false);
  153 + delete<P>(tag: tag, force: permanent);
  154 + lazyPut(builder, tag: tag, fenix: fenix ?? permanent);
  155 + }
131 } 156 }
1 import 'package:flutter_test/flutter_test.dart'; 1 import 'package:flutter_test/flutter_test.dart';
2 import 'package:get/get.dart'; 2 import 'package:get/get.dart';
  3 +
3 import 'util/matcher.dart' as m; 4 import 'util/matcher.dart' as m;
4 5
5 class Mock { 6 class Mock {
@@ -9,7 +10,9 @@ class Mock { @@ -9,7 +10,9 @@ class Mock {
9 } 10 }
10 } 11 }
11 12
12 -class DisposableController extends GetLifeCycle {} 13 +abstract class MyController extends GetLifeCycle {}
  14 +
  15 +class DisposableController extends MyController {}
13 16
14 // ignore: one_member_abstracts 17 // ignore: one_member_abstracts
15 abstract class Service { 18 abstract class Service {
@@ -168,6 +171,86 @@ void main() { @@ -168,6 +171,86 @@ void main() {
168 expect(instance.initialized, true); 171 expect(instance.initialized, true);
169 }); 172 });
170 }); 173 });
  174 +
  175 + group('Get.replace test for replacing parent instance that is', () {
  176 + tearDown(Get.reset);
  177 + test('temporary', () async {
  178 + Get.put(DisposableController());
  179 + Get.replace<DisposableController>(Controller());
  180 + final instance = Get.find<DisposableController>();
  181 + expect(instance is Controller, isTrue);
  182 + expect((instance as Controller).init, greaterThan(0));
  183 + });
  184 +
  185 + test('permanent', () async {
  186 + Get.put(DisposableController(), permanent: true);
  187 + Get.replace<DisposableController>(Controller());
  188 + final instance = Get.find<DisposableController>();
  189 + expect(instance is Controller, isTrue);
  190 + expect((instance as Controller).init, greaterThan(0));
  191 + });
  192 +
  193 + test('tagged temporary', () async {
  194 + final tag = 'tag';
  195 + Get.put(DisposableController(), tag: tag);
  196 + Get.replace<DisposableController>(Controller(), tag: tag);
  197 + final instance = Get.find<DisposableController>(tag: tag);
  198 + expect(instance is Controller, isTrue);
  199 + expect((instance as Controller).init, greaterThan(0));
  200 + });
  201 +
  202 + test('tagged permanent', () async {
  203 + final tag = 'tag';
  204 + Get.put(DisposableController(), permanent: true, tag: tag);
  205 + Get.replace<DisposableController>(Controller(), tag: tag);
  206 + final instance = Get.find<DisposableController>(tag: tag);
  207 + expect(instance is Controller, isTrue);
  208 + expect((instance as Controller).init, greaterThan(0));
  209 + });
  210 +
  211 + test('a generic parent type', () async {
  212 + final tag = 'tag';
  213 + Get.put<MyController>(DisposableController(), permanent: true, tag: tag);
  214 + Get.replace<MyController>(Controller(), tag: tag);
  215 + final instance = Get.find<MyController>(tag: tag);
  216 + expect(instance is Controller, isTrue);
  217 + expect((instance as Controller).init, greaterThan(0));
  218 + });
  219 + });
  220 +
  221 + group('Get.lazyReplace replaces parent instance', () {
  222 + tearDown(Get.reset);
  223 + test('without fenix', () async {
  224 + Get.put(DisposableController());
  225 + Get.lazyReplace<DisposableController>(() => Controller());
  226 + final instance = Get.find<DisposableController>();
  227 + expect(instance, isA<Controller>());
  228 + expect((instance as Controller).init, greaterThan(0));
  229 + });
  230 +
  231 + test('with fenix', () async {
  232 + Get.put(DisposableController());
  233 + Get.lazyReplace<DisposableController>(() => Controller(), fenix: true);
  234 + expect(Get.find<DisposableController>(), isA<Controller>());
  235 + (Get.find<DisposableController>() as Controller).increment();
  236 +
  237 + expect((Get.find<DisposableController>() as Controller).count, 1);
  238 + Get.delete<DisposableController>();
  239 + expect((Get.find<DisposableController>() as Controller).count, 0);
  240 + });
  241 +
  242 + test('with fenix when parent is permanent', () async {
  243 + Get.put(DisposableController(), permanent: true);
  244 + Get.lazyReplace<DisposableController>(() => Controller());
  245 + final instance = Get.find<DisposableController>();
  246 + expect(instance, isA<Controller>());
  247 + (instance as Controller).increment();
  248 +
  249 + expect((Get.find<DisposableController>() as Controller).count, 1);
  250 + Get.delete<DisposableController>();
  251 + expect((Get.find<DisposableController>() as Controller).count, 0);
  252 + });
  253 + });
171 } 254 }
172 255
173 class PermanentService extends GetxService {} 256 class PermanentService extends GetxService {}