LCOV - code coverage report
Current view: top level - _classes/herald - app_locale.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 14 21 66.7 %
Date: 2024-10-04 11:08:31 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 'package:app_finance/_classes/herald/app_design.dart';
       5             : import 'package:app_finance/_classes/storage/app_preferences.dart';
       6             : import 'package:flutter/material.dart';
       7             : import 'package:flutter_currency_picker/flutter_currency_picker.dart';
       8             : import 'package:flutter_gen/gen_l10n/app_localization.dart';
       9             : import 'package:flutter_gen/gen_l10n/app_localization_en.dart';
      10             : 
      11             : class AppLocale extends ValueNotifier<Locale?> {
      12           6 :   static String code = AppPreferences.get(AppPreferences.prefLocale) ?? 'en';
      13           0 :   static AppLocalizations labels = AppLocalizationsEn();
      14             : 
      15           4 :   AppLocale() : super(AppLocale.fromCode(code));
      16             : 
      17           1 :   static Locale? fromCode(String value) {
      18             :     try {
      19           1 :       final keys = value.split('_');
      20           5 :       return Locale.fromSubtags(languageCode: keys.first, countryCode: keys.length > 1 ? keys.last : null);
      21             :     } catch (e) {
      22             :       return null;
      23             :     }
      24             :   }
      25             : 
      26           1 :   Future<void> set(String newValue, [Function? callback]) async {
      27           1 :     final loc = fromCode(newValue);
      28           3 :     if (loc != null && loc != value && AppLocalizations.supportedLocales.contains(loc)) {
      29           1 :       value = loc;
      30           2 :       code = value.toString();
      31           1 :       await AppPreferences.set(AppPreferences.prefLocale, newValue);
      32             :       CurrencyDefaults.defaultLocale = loc;
      33             :       if (callback != null) {
      34           0 :         callback();
      35             :       }
      36           1 :       notifyListeners();
      37             :     }
      38             :   }
      39             : 
      40           1 :   void updateState(BuildContext context) {
      41           1 :     final value = Localizations.localeOf(context).toString();
      42           0 :     labels = AppLocalizations.of(context) ?? AppLocalizationsEn();
      43           0 :     CurrencyProvider.initFromContext(context, locale: super.value);
      44           0 :     if (AppPreferences.get(AppPreferences.prefDesign) == null) {
      45           0 :       AppPreferences.set(AppPreferences.prefDesign, AppDesign.fromLocale(fromCode(value)));
      46             :     }
      47           0 :     set(value);
      48             :   }
      49             : }

Generated by: LCOV version 1.14