LCOV - code coverage report
Current view: top level - src/rx - rx_getbuilder.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 42 44 95.5 %
Date: 2020-07-01 03:00:01 Functions: 0 0 -

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

Generated by: LCOV version 1.14