LCOV - code coverage report
Current view: top level - src/rx - rx_getbuilder.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 36 37 97.3 %
Date: 2020-06-12 23:08:29 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:flutter/widgets.dart';
       2             : import 'package:get_core/src/root/smart_management.dart';
       3             : import '../get_instance.dart';
       4             : import 'rx_impl.dart';
       5             : import 'rx_interface.dart';
       6             : 
       7             : class GetX<T extends DisposableInterface> extends StatefulWidget {
       8             :   final Widget Function(T) builder;
       9             :   final bool global;
      10             :   // final Stream Function(T) stream;
      11             :   // final StreamController Function(T) streamController;
      12             :   final bool autoRemove;
      13             :   final bool assignId;
      14             :   final void Function(State state) initState, dispose, didChangeDependencies;
      15             :   final T init;
      16           1 :   const GetX({
      17             :     this.builder,
      18             :     this.global = true,
      19             :     this.autoRemove = true,
      20             :     this.initState,
      21             :     this.assignId = false,
      22             :     //  this.stream,
      23             :     this.dispose,
      24             :     this.didChangeDependencies,
      25             :     this.init,
      26             :     // this.streamController
      27             :   });
      28           2 :   GetImplXState<T> createState() => GetImplXState<T>();
      29             : }
      30             : 
      31             : class GetImplXState<T extends DisposableInterface> extends State<GetX<T>> {
      32             :   RxInterface _observer;
      33             :   T controller;
      34             :   bool isCreator = false;
      35             : 
      36           1 :   @override
      37             :   void initState() {
      38           2 :     _observer = Rx();
      39           2 :     bool isPrepared = GetInstance().isPrepared<T>();
      40           2 :     bool isRegistred = GetInstance().isRegistred<T>();
      41           2 :     if (widget.global) {
      42             :       if (isPrepared) {
      43           2 :         if (GetConfig.smartManagement != SmartManagement.keepFactory) {
      44           1 :           isCreator = true;
      45             :         }
      46           3 :         controller = GetInstance().find<T>();
      47             :       } else if (isRegistred) {
      48           3 :         controller = GetInstance().find<T>();
      49           1 :         isCreator = false;
      50             :       } else {
      51           3 :         controller = widget.init;
      52           1 :         isCreator = true;
      53           3 :         GetInstance().put<T>(controller);
      54             :       }
      55             :     } else {
      56           3 :       controller = widget.init;
      57           1 :       isCreator = true;
      58           2 :       controller?.onStart();
      59             :     }
      60           2 :     if (widget.initState != null) widget.initState(this);
      61           3 :     if (isCreator && GetConfig.smartManagement == SmartManagement.onlyBuilder) {
      62           0 :       controller?.onStart();
      63             :     }
      64           7 :     _observer.subject.stream.listen((data) => setState(() {}));
      65           1 :     super.initState();
      66             :   }
      67             : 
      68           1 :   @override
      69             :   void dispose() {
      70           2 :     if (widget.dispose != null) widget.dispose(this);
      71           3 :     if (isCreator || widget.assignId) {
      72           4 :       if (widget.autoRemove && GetInstance().isRegistred<T>()) {
      73           2 :         GetInstance().delete<T>();
      74             :       }
      75             :     }
      76             : 
      77           2 :     _observer.close();
      78           1 :     controller = null;
      79           1 :     isCreator = null;
      80           1 :     super.dispose();
      81             :   }
      82             : 
      83           1 :   Widget get notifyChilds {
      84             :     final observer = getObs;
      85           1 :     getObs = _observer;
      86           4 :     final result = widget.builder(controller);
      87             :     getObs = observer;
      88             :     return result;
      89             :   }
      90             : 
      91           1 :   @override
      92           1 :   Widget build(BuildContext context) => notifyChilds;
      93             : }

Generated by: LCOV version 1.14