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:54:13 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            1 :   FocusController();
      25              : 
      26            1 :   ScrollController get controller {
      27            2 :     order.clear();
      28            2 :     values.clear();
      29            1 :     return _controller;
      30              :   }
      31              : 
      32            2 :   int key(dynamic item) => item?.hashCode ?? 0;
      33              : 
      34            1 :   FocusNode last(dynamic item) {
      35            2 :     idButton = key(item);
      36            4 :     scope[idButton] ??= FocusNode();
      37            3 :     return scope[idButton]!;
      38              :   }
      39              : 
      40            1 :   FocusNode bind(dynamic item, {required BuildContext context, dynamic value}) {
      41            1 :     final id = key(item);
      42            2 :     if (!order.containsValue(id)) {
      43            4 :       order[order.length] = id;
      44              :     }
      45            3 :     scope[id] ??= FocusNode();
      46            2 :     values[id] = value;
      47            1 :     if (!force && !_isInitialized) {
      48            3 :       _focus.run(onEditingComplete);
      49              :     }
      50            2 :     return scope[id]!;
      51              :   }
      52              : 
      53            3 :   bool _isControllerActive() => _controller.hasClients;
      54              : 
      55            1 :   double _getPosition(BuildContext context) {
      56            1 :     RenderObject? obj = context.findRenderObject();
      57            3 :     return obj is RenderBox ? obj.localToGlobal(Offset.zero).dy : 0;
      58              :   }
      59              : 
      60            3 :   double _getMinPosition() => order.values
      61            5 :       .map((id) => scope[id]?.context)
      62            2 :       .where((element) => element != null)
      63            3 :       .map((e) => _getPosition(e!))
      64            1 :       .reduce(min);
      65              : 
      66            6 :   bool _isLast(int? idx) => idx == order[order.lastKey()];
      67              : 
      68            3 :   TextInputAction getAction(dynamic item) => _isLast(key(item)) ? TextInputAction.done : TextInputAction.next;
      69              : 
      70            1 :   void scrollToFocusedElement(dynamic item, [int? idx]) {
      71            0 :     idx ??= key(item!);
      72            3 :     if (!_isControllerActive() || scope.isEmpty) {
      73              :       return;
      74              :     }
      75            7 :     if (order.containsValue(idx) && scope[idx] != null && scope[idx]!.context != null) {
      76            7 :       double position = _getPosition(scope[idx]!.context!) - _getMinPosition() - ThemeHelper.barHeight;
      77            1 :       if (position < 0) {
      78              :         position = 0;
      79              :       }
      80            2 :       _controller.animateTo(
      81              :         position,
      82              :         duration: const Duration(milliseconds: 600),
      83              :         curve: Curves.easeInOut,
      84              :       );
      85              :     }
      86              :   }
      87              : 
      88            6 :   bool get hasFocus => scope.values.any((node) => node.hasFocus);
      89              : 
      90            1 :   void onEditingComplete([dynamic item]) {
      91              :     if (item == null) {
      92            1 :       if (_isInitialized) {
      93              :         return;
      94              :       }
      95            1 :       _isInitialized = true;
      96            1 :       if (hasFocus) {
      97              :         return;
      98              :       }
      99              :     }
     100            4 :     int? targetKey = item == null ? focus ?? order.values.firstOrNull : null;
     101              :     int? idx;
     102              :     force = false;
     103              :     if (item != null) {
     104            1 :       idx = key(item);
     105            2 :       values[idx] = true;
     106              :     }
     107            3 :     List<int?> check = order.values.toList();
     108            1 :     if (idButton != null) {
     109            3 :       values[idButton] = null;
     110            2 :       check.add(idButton);
     111              :     }
     112            2 :     for (int? key in check) {
     113            6 :       if (targetKey != null && key != targetKey && (values[key] == '' || values[key] == null)) {
     114            1 :         onFocus(null, key, false);
     115              :         targetKey = null;
     116              :       }
     117            1 :       if (key == idx) {
     118              :         targetKey = key;
     119              :       }
     120              :     }
     121              :   }
     122              : 
     123            1 :   bool isFocused(dynamic item, [BuildContext? context]) =>
     124            4 :       key(item) == focus && (context == null || (ModalRoute.isCurrentOf(context) ?? true));
     125              : 
     126            5 :   void blur() => scope.forEach((_, value) => value.unfocus());
     127              : 
     128            1 :   void onFocus(dynamic item, [int? idx, bool isForced = true]) {
     129              :     if (isForced) {
     130            2 :       _focus.abort();
     131              :     }
     132            1 :     idx ??= key(item);
     133            1 :     focus = idx;
     134            4 :     if ((order.containsValue(idx) || idx == idButton) &&
     135            2 :         scope[idx] != null &&
     136            3 :         scope[idx]!.context != null &&
     137            4 :         scope[idx]!.context!.mounted) {
     138            1 :       blur();
     139            3 :       scope[idx]?.requestFocus();
     140              :       if (item != null) {
     141            4 :         _scrollTo.run(() => scrollToFocusedElement(item, idx));
     142              :       }
     143              :     }
     144              :     force = isForced;
     145              :   }
     146              : 
     147            1 :   void dispose() {
     148            2 :     _focus.abort();
     149            2 :     _scrollTo.abort();
     150            4 :     scope.forEach((_, node) => node.dispose());
     151            2 :     scope.clear();
     152            2 :     values.clear();
     153            2 :     order.clear();
     154            2 :     _controller.dispose();
     155            1 :     focus = null;
     156              :   }
     157              : }
        

Generated by: LCOV version 2.0-1