LCOV - code coverage report
Current view: top level - lib/_classes/controller - focus_controller.dart Coverage Total Hit
Test: lcov.info Lines: 98.7 % 76 75
Test Date: 2026-12-05 10:50:38 Functions: - 0 0

            Line data    Source code
       1              : // Copyright 2023 The terCAD team. All rights reserved.
       2              : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.
       3              : 
       4              : import 'dart:collection';
       5              : import 'dart:math';
       6              : 
       7              : import 'package:app_finance/_classes/controller/delayed_call.dart';
       8              : import 'package:app_finance/_configs/theme_helper.dart';
       9              : import 'package:material_ui/material_ui.dart';
      10              : 
      11              : class FocusController {
      12              :   final DelayedCall _scrollTo = DelayedCall(300);
      13              :   final DelayedCall _focus = DelayedCall(300);
      14              :   final _controller = ScrollController();
      15              : 
      16              :   final scope = <int?, FocusNode>{};
      17              :   final values = <int?, dynamic>{};
      18              :   final order = SplayTreeMap<int, int?>();
      19              :   int? focus;
      20              :   int? idButton;
      21              :   bool _isInitialized = false;
      22              :   static bool force = false;
      23              : 
      24            8 :   FocusController();
      25              : 
      26            6 :   ScrollController get controller {
      27           12 :     order.clear();
      28           12 :     values.clear();
      29            6 :     return _controller;
      30              :   }
      31              : 
      32           14 :   int key(dynamic item) => item?.hashCode ?? 0;
      33              : 
      34            2 :   FocusNode last(dynamic item) {
      35            4 :     idButton = key(item);
      36            8 :     scope[idButton] ??= FocusNode();
      37            6 :     return scope[idButton]!;
      38              :   }
      39              : 
      40            7 :   FocusNode bind(dynamic item, {required BuildContext context, dynamic value}) {
      41            7 :     final id = key(item);
      42           14 :     if (!order.containsValue(id)) {
      43           28 :       order[order.length] = id;
      44              :     }
      45           21 :     scope[id] ??= FocusNode();
      46           14 :     values[id] = value;
      47            7 :     if (!force && !_isInitialized) {
      48           21 :       _focus.run(onEditingComplete);
      49              :     }
      50           14 :     return scope[id]!;
      51              :   }
      52              : 
      53            6 :   bool _isControllerActive() => _controller.hasClients;
      54              : 
      55            2 :   double _getPosition(BuildContext context) {
      56            2 :     RenderObject? obj = context.findRenderObject();
      57            6 :     return obj is RenderBox ? obj.localToGlobal(Offset.zero).dy : 0;
      58              :   }
      59              : 
      60            6 :   double _getMinPosition() => order.values
      61           10 :       .map((id) => scope[id]?.context)
      62            4 :       .where((element) => element != null)
      63            6 :       .map((e) => _getPosition(e!))
      64            2 :       .reduce(min);
      65              : 
      66           30 :   bool _isLast(int? idx) => idx == order[order.lastKey()];
      67              : 
      68           15 :   TextInputAction getAction(dynamic item) => _isLast(key(item)) ? TextInputAction.done : TextInputAction.next;
      69              : 
      70            2 :   void scrollToFocusedElement(dynamic item, [int? idx]) {
      71            0 :     idx ??= key(item!);
      72            6 :     if (!_isControllerActive() || scope.isEmpty) {
      73              :       return;
      74              :     }
      75           14 :     if (order.containsValue(idx) && scope[idx] != null && scope[idx]!.context != null) {
      76           14 :       double position = _getPosition(scope[idx]!.context!) - _getMinPosition() - ThemeHelper.barHeight;
      77            2 :       if (position < 0) {
      78              :         position = 0;
      79              :       }
      80            4 :       _controller.animateTo(
      81              :         position,
      82              :         duration: const Duration(milliseconds: 600),
      83              :         curve: Curves.easeInOut,
      84              :       );
      85              :     }
      86              :   }
      87              : 
      88           42 :   bool get hasFocus => scope.values.any((node) => node.hasFocus);
      89              : 
      90            7 :   void onEditingComplete([dynamic item]) {
      91              :     if (item == null) {
      92            7 :       if (_isInitialized) {
      93              :         return;
      94              :       }
      95            7 :       _isInitialized = true;
      96            7 :       if (hasFocus) {
      97              :         return;
      98              :       }
      99              :     }
     100           28 :     int? targetKey = item == null ? focus ?? order.values.firstOrNull : null;
     101              :     int? idx;
     102              :     force = false;
     103              :     if (item != null) {
     104            2 :       idx = key(item);
     105            4 :       values[idx] = true;
     106              :     }
     107           21 :     List<int?> check = order.values.toList();
     108            7 :     if (idButton != null) {
     109            6 :       values[idButton] = null;
     110            4 :       check.add(idButton);
     111              :     }
     112           14 :     for (int? key in check) {
     113           26 :       if (targetKey != null && key != targetKey && (values[key] == '' || values[key] == null)) {
     114            5 :         onFocus(null, key, false);
     115              :         targetKey = null;
     116              :       }
     117            7 :       if (key == idx) {
     118              :         targetKey = key;
     119              :       }
     120              :     }
     121              :   }
     122              : 
     123            7 :   bool isFocused(dynamic item, [BuildContext? context]) =>
     124           23 :       key(item) == focus && (context == null || (ModalRoute.isCurrentOf(context) ?? true));
     125              : 
     126           25 :   void blur() => scope.forEach((_, value) => value.unfocus());
     127              : 
     128            5 :   void onFocus(dynamic item, [int? idx, bool isForced = true]) {
     129              :     if (isForced) {
     130            4 :       _focus.abort();
     131              :     }
     132            2 :     idx ??= key(item);
     133            5 :     focus = idx;
     134           10 :     if ((order.containsValue(idx) || idx == idButton) &&
     135           10 :         scope[idx] != null &&
     136           15 :         scope[idx]!.context != null &&
     137           20 :         scope[idx]!.context!.mounted) {
     138            5 :       blur();
     139           15 :       scope[idx]?.requestFocus();
     140              :       if (item != null) {
     141            8 :         _scrollTo.run(() => scrollToFocusedElement(item, idx));
     142              :       }
     143              :     }
     144              :     force = isForced;
     145              :   }
     146              : 
     147            6 :   void dispose() {
     148           12 :     _focus.abort();
     149           12 :     _scrollTo.abort();
     150           22 :     scope.forEach((_, node) => node.dispose());
     151           12 :     scope.clear();
     152           12 :     values.clear();
     153           12 :     order.clear();
     154           12 :     _controller.dispose();
     155            6 :     focus = null;
     156              :   }
     157              : }
        

Generated by: LCOV version 2.0-1