LCOV - code coverage report
Current view: top level - src/state - get_state.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 62 62 100.0 %
Date: 2020-06-02 18:55:34 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:flutter/material.dart';
       2             : import '../get_main.dart';
       3             : 
       4             : class RealState {
       5             :   final State state;
       6             :   final String id;
       7             :   final bool isCreator;
       8           5 :   const RealState({this.state, this.id, this.isCreator = false});
       9             : }
      10             : 
      11             : class GetController extends State {
      12             :   List<RealState> _allStates = [];
      13             : 
      14             :   /// Update GetBuilder with update(this)
      15           1 :   void update(GetController controller,
      16             :       [List<String> ids, bool condition = true]) {
      17             :     if (controller == null || !condition) return;
      18             : 
      19             :     if (ids == null) {
      20             :       // _allStates[controller.hashCode];
      21           3 :       _allStates.forEach((rs) {
      22           6 :         if (rs.state != null && rs.state.mounted) rs.state.setState(() {});
      23             :       });
      24             :     } else {
      25           1 :       ids.forEach(
      26           1 :         (s) {
      27             :           //  var all = _allStates[controller.hashCode];
      28           3 :           _allStates.forEach((rs) {
      29           5 :             if (rs.state != null && rs.state.mounted && rs.id == s)
      30           3 :               rs.state.setState(() {});
      31             :           });
      32             :         },
      33             :       );
      34             :     }
      35             :   }
      36             : 
      37           5 :   void onClose() async {}
      38           5 :   void onInit() async {}
      39             : 
      40           1 :   @override
      41             :   Widget build(_) => throw ("build method can't be called");
      42             : }
      43             : 
      44             : class GetBuilder<T extends GetController> extends StatefulWidget {
      45             :   @required
      46             :   final Widget Function(T) builder;
      47             :   final bool global;
      48             :   final String id;
      49             :   final bool autoRemove;
      50             :   final bool assignId;
      51             :   final void Function(State state) initState, dispose, didChangeDependencies;
      52             :   final void Function(GetBuilder oldWidget, State state) didUpdateWidget;
      53             :   final T init;
      54           5 :   const GetBuilder({
      55             :     Key key,
      56             :     this.init,
      57             :     this.global = true,
      58             :     this.builder,
      59             :     this.autoRemove = true,
      60             :     this.assignId = false,
      61             :     this.initState,
      62             :     this.dispose,
      63             :     this.id,
      64             :     this.didChangeDependencies,
      65             :     this.didUpdateWidget,
      66           1 :   })  : assert(builder != null),
      67           5 :         super(key: key);
      68           5 :   @override
      69           5 :   _GetBuilderState<T> createState() => _GetBuilderState<T>();
      70             : }
      71             : 
      72             : class _GetBuilderState<T extends GetController> extends State<GetBuilder<T>> {
      73             :   T controller;
      74             :   RealState real;
      75             :   bool isCreator = false;
      76           5 :   @override
      77             :   void initState() {
      78           5 :     super.initState();
      79             : 
      80          10 :     if (widget.global) {
      81           5 :       if (Get.isPrepared<T>()) {
      82           1 :         isCreator = true;
      83           2 :         controller = Get.find<T>();
      84             : 
      85           5 :         real = RealState(state: this, id: widget.id, isCreator: isCreator);
      86           4 :         controller._allStates.add(real);
      87           6 :       } else if (Get.isRegistred<T>() && !Get.isPrepared<T>()) {
      88           2 :         controller = Get.find<T>();
      89           1 :         isCreator = false;
      90           5 :         real = RealState(state: this, id: widget.id, isCreator: isCreator);
      91           4 :         controller._allStates.add(real);
      92             :       } else {
      93          15 :         controller = widget.init;
      94           5 :         isCreator = true;
      95             : 
      96          25 :         real = RealState(state: this, id: widget.id, isCreator: isCreator);
      97          20 :         controller._allStates.add(real);
      98          10 :         Get.put<T>(controller);
      99             :       }
     100             :     } else {
     101           3 :       controller = widget.init;
     102             : 
     103           1 :       isCreator = true;
     104           5 :       real = RealState(state: this, id: widget.id, isCreator: isCreator);
     105           4 :       controller._allStates.add(real);
     106             :     }
     107          19 :     if (widget.initState != null) widget.initState(this);
     108           5 :     if (isCreator) {
     109          10 :       controller?.onInit();
     110             :     }
     111             :   }
     112             : 
     113           5 :   @override
     114             :   void dispose() {
     115           5 :     super.dispose();
     116             : 
     117          19 :     if (widget.dispose != null) widget.dispose(this);
     118             : 
     119           7 :     if (isCreator || widget.assignId) {
     120          15 :       if (widget.autoRemove && Get.isRegistred<T>()) {
     121             :         // controller.onClose();
     122          20 :         controller._allStates.remove(real);
     123           5 :         Get.delete<T>();
     124             :       }
     125             :     } else {
     126             :       // controller._allStates[controller].remove(this);
     127           4 :       controller._allStates.remove(real);
     128             :     }
     129             : 
     130             :     /// force GC remove this
     131           5 :     controller = null;
     132           5 :     real = null;
     133           5 :     isCreator = null;
     134             :   }
     135             : 
     136           5 :   @override
     137             :   void didChangeDependencies() {
     138           5 :     super.didChangeDependencies();
     139          10 :     if (widget.didChangeDependencies != null)
     140           3 :       widget.didChangeDependencies(this);
     141             :   }
     142             : 
     143           2 :   @override
     144             :   void didUpdateWidget(GetBuilder oldWidget) {
     145           2 :     super.didUpdateWidget(oldWidget);
     146           4 :     if (widget.didUpdateWidget != null) widget.didUpdateWidget(oldWidget, this);
     147             :   }
     148             : 
     149           5 :   @override
     150             :   Widget build(BuildContext context) {
     151          20 :     return widget.builder(controller);
     152             :   }
     153             : }

Generated by: LCOV version 1.14