LCOV - code coverage report
Current view: top level - lib/notifiers - state_notifier.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 20 20 100.0 %
Date: 2023-10-11 10:27:27 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:async';
       2             : 
       3             : import 'package:meta/meta.dart';
       4             : 
       5             : import 'base_notifier.dart';
       6             : 
       7             : class StateNotifier<S> extends BaseNotifier<S> with ListeneableNotifier<S> {
       8           6 :   StateNotifier(S initialState) {
       9           6 :     _state = initialState;
      10          12 :     _oldState = _state;
      11             :   }
      12             : 
      13             :   late S _state, _oldState;
      14          12 :   S get state => _state;
      15           6 :   S get oldState => _oldState;
      16             : 
      17             :   /// Updates the State and notify to listeners and rebuild the widgets
      18             :   ///
      19             :   /// [state] must be different of the current state
      20           6 :   @protected
      21             :   set state(S newState) {
      22           6 :     _update(newState);
      23             :   }
      24             : 
      25             :   /// updates the state but does not notify to the listeners
      26           1 :   @protected
      27             :   void onlyUpdate(S newState) {
      28           1 :     _update(newState, false);
      29             :   }
      30             : 
      31             :   /// changes the state value
      32             :   /// if [notify] is false it doesn't notify to all listeners
      33           6 :   void _update(S newState, [bool notify = true]) {
      34          18 :     if (!disposed && onStateWillChange(_state, newState)) {
      35          12 :       _oldState = _state;
      36           6 :       _state = newState;
      37          18 :       onStateChanged(_oldState, _state);
      38             :       if (notify) {
      39          12 :         notifyListeners(_state);
      40             :       }
      41             :     }
      42             :   }
      43             : 
      44             :   /// this method is called when the state is going to be updated
      45             :   /// By default this method returns true, you can use this method to intercept the [newState]
      46             :   /// and check if the new state is valid.
      47             :   /// If this method returns false the new state will be igonored
      48           8 :   bool onStateWillChange(S oldState, S newState) => oldState != newState;
      49             : 
      50             :   /// this method is called when the state has been changed
      51           3 :   void onStateChanged(S oldState, S currentState) {}
      52             : 
      53           4 :   @protected
      54             :   @mustCallSuper
      55             :   @override
      56             :   FutureOr<void> dispose() {
      57           4 :     super.dispose();
      58           4 :     clearListeners();
      59             :   }
      60             : }

Generated by: LCOV version 1.16