LCOV - code coverage report
Current view: top level - src - redirector.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 24 32 75.0 %
Date: 2021-04-29 14:25:52 Functions: 0 0 -

          Line data    Source code
       1             : part of 'main.dart';
       2             : 
       3             : /// A class which helps you in beforeLeave or beforeEnter functions
       4             : /// This class contain 2 main functionality:
       5             : ///   1. It allows you to redirect using [VRedirector.push], [VRedirector.pushNamed], ...
       6             : ///   2. It gives you access to information about the previous route and the new route
       7             : ///
       8             : /// Note that you should use this object to redirect in beforeLeave and beforeEnter. Never
       9             : /// use VRouterData to do so.
      10             : class VRedirector {
      11          12 :   VRedirector({
      12             :     required BuildContext context,
      13             :     required this.from,
      14             :     required this.to,
      15             :     required this.previousVRouterData,
      16             :     required this.newVRouterData,
      17             :   }) : _context = context;
      18             : 
      19             :   /// If [_shouldUpdate] is set to false, the current url updating is stopped
      20             :   ///
      21             :   /// You should NOT modify this, instead use [stopRedirection], or other methods
      22             :   /// such as [push], [pushNamed], ...
      23             :   bool _shouldUpdate = true;
      24             : 
      25          20 :   bool get shouldUpdate => _shouldUpdate;
      26             : 
      27             :   /// The url we are coming from
      28             :   final String? from;
      29             : 
      30             :   /// The url we are going to
      31             :   final String? to;
      32             : 
      33             :   /// The [VRouterData] of the previous route
      34             :   /// Useful information is:
      35             :   ///   * [VRouterData.pathParameters]
      36             :   ///   * [VRouterData.queryParameters]
      37             :   ///   * [VRouterData.historyState]
      38             :   ///
      39             :   /// Note that you should NOT call [newVRouterData.replaceHistoryState]
      40             :   ///   If you are in beforeLeave, call [saveHistoryState] instead
      41             :   ///   If you are in beforeEnter, you can't save an history state here
      42             :   final RootVRouterData? previousVRouterData;
      43             : 
      44             :   /// The [VRouterData] of the new route
      45             :   /// Useful information is:
      46             :   ///   * [VRouterData.pathParameters]
      47             :   ///   * [VRouterData.queryParameters]
      48             :   ///   * [VRouterData.historyState]
      49             :   ///
      50             :   /// Note that you should NOT call [newVRouterData.replaceHistoryState]
      51             :   ///   If you are in beforeLeave, call [saveHistoryState] instead
      52             :   ///   If you are in beforeEnter, you can't save an history state here
      53             :   final RootVRouterData? newVRouterData;
      54             : 
      55             :   /// A context which gives us access to VRouter and the current VRoute
      56             :   /// This is local because we don't want developers to use VRouterData to redirect
      57             :   final BuildContext _context;
      58             : 
      59             :   /// Function which will be executed after stopping the redirection
      60             :   /// if [push], [pushNamed], ... have been used.
      61             :   void Function(VRouteElementNode vRouteElementNode)? _redirectFunction;
      62             : 
      63             :   /// Stops the redirection
      64             :   ///
      65             :   /// This also checks that only one method which stops the redirection is used
      66           5 :   void stopRedirection() {
      67           5 :     if (!shouldUpdate) {
      68             :       throw 'You already stopped the redirection. You can only use one such action on VRedirector.';
      69             :     }
      70           5 :     _shouldUpdate = false;
      71             :   }
      72             : 
      73             :   /// Prevent the current redirection and push a route instead
      74             :   ///
      75             :   /// See [VRouter.push] for more information on push
      76           1 :   void push(
      77             :     String newUrl, {
      78             :     Map<String, String> queryParameters = const {},
      79             :     Map<String, String> historyState = const {},
      80             :   }) {
      81           1 :     stopRedirection();
      82           5 :     _redirectFunction = (_) => RootVRouterData.of(_context).push(newUrl,
      83             :         queryParameters: queryParameters, historyState: historyState);
      84             :   }
      85             : 
      86             :   /// Prevent the current redirection and pushNamed a route instead
      87             :   ///
      88             :   /// See [VRouter.push] for more information on push
      89           1 :   void pushNamed(
      90             :     String name, {
      91             :     Map<String, String> pathParameters = const {},
      92             :     Map<String, String> queryParameters = const {},
      93             :     Map<String, String> historyState = const {},
      94             :   }) {
      95           1 :     stopRedirection();
      96           5 :     _redirectFunction = (_) => RootVRouterData.of(_context).pushNamed(name,
      97             :         pathParameters: pathParameters,
      98             :         queryParameters: queryParameters,
      99             :         historyState: historyState);
     100             :   }
     101             : 
     102             :   /// Prevent the current redirection and pushReplacement a route instead
     103             :   ///
     104             :   /// See [VRouter.push] for more information on push
     105           1 :   void pushReplacement(
     106             :     String newUrl, {
     107             :     Map<String, String> queryParameters = const {},
     108             :     Map<String, String> historyState = const {},
     109             :   }) {
     110           1 :     stopRedirection();
     111           5 :     _redirectFunction = (_) => RootVRouterData.of(_context).pushReplacement(
     112             :         newUrl,
     113             :         queryParameters: queryParameters,
     114             :         historyState: historyState);
     115             :   }
     116             : 
     117             :   /// Prevent the current redirection and pushReplacementNamed a route instead
     118             :   ///
     119             :   /// See [VRouter.push] for more information on push
     120           0 :   void pushReplacementNamed(
     121             :     String name, {
     122             :     Map<String, String> pathParameters = const {},
     123             :     Map<String, String> queryParameters = const {},
     124             :     Map<String, String> historyState = const {},
     125             :   }) {
     126           0 :     stopRedirection();
     127           0 :     _redirectFunction =
     128           0 :         (_) => RootVRouterData.of(_context).pushReplacementNamed(
     129             :               name,
     130             :               pathParameters: pathParameters,
     131             :               queryParameters: queryParameters,
     132             :               historyState: historyState,
     133             :             );
     134             :   }
     135             : 
     136             :   /// Prevent the current redirection and pushExternal instead
     137             :   ///
     138             :   /// See [VRouter.push] for more information on push
     139           0 :   void pushExternal(String newUrl, {bool openNewTab = false}) {
     140           0 :     stopRedirection();
     141           0 :     _redirectFunction = (_) => RootVRouterData.of(_context)
     142           0 :         .pushExternal(newUrl, openNewTab: openNewTab);
     143             :   }
     144             : 
     145             :   /// Prevent the current redirection and call pop instead
     146             :   ///
     147             :   /// See [VRouter.pop] for more information on push
     148           1 :   void pop({
     149             :     Map<String, String> pathParameters = const {},
     150             :     Map<String, String> queryParameters = const {},
     151             :     Map<String, String> newHistoryState = const {},
     152             :   }) {
     153           1 :     stopRedirection();
     154           2 :     _redirectFunction = (VRouteElementNode vRouteElementNode) =>
     155           3 :         RootVRouterData.of(_context).popFromElement(
     156           1 :           vRouteElementNode.getVRouteElementToPop(),
     157             :           pathParameters: pathParameters,
     158             :           queryParameters: queryParameters,
     159             :           newHistoryState: newHistoryState,
     160             :         );
     161             :   }
     162             : 
     163             :   /// Prevent the current redirection and call systemPop instead
     164             :   ///
     165             :   /// See [VRouter.systemPop] for more information on push
     166           1 :   Future<void> systemPop({
     167             :     Map<String, String> pathParameters = const {},
     168             :     Map<String, String> queryParameters = const {},
     169             :     Map<String, String> newHistoryState = const {},
     170             :   }) async {
     171           1 :     stopRedirection();
     172           2 :     _redirectFunction = (VRouteElementNode vRouteElementNode) =>
     173           3 :         RootVRouterData.of(_context).systemPopFromElement(
     174           1 :           vRouteElementNode.getVRouteElementToPop(),
     175             :           pathParameters: pathParameters,
     176             :           queryParameters: queryParameters,
     177             :           newHistoryState: newHistoryState,
     178             :         );
     179             :   }
     180             : }

Generated by: LCOV version 1.14