LCOV - code coverage report
Current view: top level - src/rx - rx_getbuilder.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 38 38 100.0 %
Date: 2020-06-03 02:02:49 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:async';
       2             : import 'package:flutter/widgets.dart';
       3             : import 'package:get/src/get_main.dart';
       4             : import 'rx_impl.dart';
       5             : import 'rx_interface.dart';
       6             : 
       7             : class GetX<T extends RxController> 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 :   _GetXState<T> createState() => _GetXState<T>();
      29             : }
      30             : 
      31             : class _GetXState<T extends RxController> extends State<GetX<T>> {
      32             :   RxInterface _observer;
      33             :   StreamSubscription _listenSubscription;
      34             :   T controller;
      35             :   bool isCreator = false;
      36             : 
      37           1 :   _GetXState() {
      38           2 :     _observer = Rx();
      39             :   }
      40             : 
      41           1 :   @override
      42             :   void initState() {
      43           1 :     bool isPrepared = Get.isPrepared<T>();
      44           1 :     bool isRegistred = Get.isRegistred<T>();
      45           2 :     if (widget.global) {
      46             :       // if (Get().smartManagement == SmartManagement.full) {
      47             :       //   Get.isDependencyInit<T>();
      48             :       // }
      49             :       if (isPrepared) {
      50           1 :         isCreator = true;
      51           2 :         controller = Get.find<T>();
      52             :       } else if (isRegistred) {
      53           2 :         controller = Get.find<T>();
      54           1 :         isCreator = false;
      55             :       } else {
      56           3 :         controller = widget.init;
      57           1 :         isCreator = true;
      58           2 :         Get.put<T>(controller);
      59             :       }
      60             :     } else {
      61           3 :       controller = widget.init;
      62           1 :       isCreator = true;
      63             :     }
      64           2 :     if (widget.initState != null) widget.initState(this);
      65           1 :     if (isCreator) {
      66           2 :       controller?.onInit();
      67             :     }
      68             : 
      69           6 :     _listenSubscription = _observer.subject.stream.listen((data) {
      70           2 :       setState(() {});
      71             :     });
      72           1 :     super.initState();
      73             :   }
      74             : 
      75           1 :   @override
      76             :   void dispose() {
      77           2 :     if (widget.dispose != null) widget.dispose(this);
      78             : 
      79           3 :     if (isCreator || widget.assignId) {
      80           3 :       if (widget.autoRemove && Get.isRegistred<T>()) {
      81             :         // controller.onClose();
      82           1 :         Get.delete<T>();
      83             :       }
      84             :       // } else {
      85             :       //   controller.onClose();
      86             :     }
      87             :     // controller.onClose();
      88           2 :     _observer.close();
      89           2 :     _listenSubscription?.cancel();
      90             : 
      91           1 :     controller = null;
      92           1 :     isCreator = null;
      93           1 :     super.dispose();
      94             :   }
      95             : 
      96           1 :   @override
      97             :   Widget build(BuildContext context) {
      98           1 :     final observer = Get.obs;
      99           2 :     Get.obs = this._observer;
     100           4 :     final result = widget.builder(controller);
     101           1 :     Get.obs = observer;
     102             :     return result;
     103             :   }
     104             : }

Generated by: LCOV version 1.14