LCOV - code coverage report
Current view: top level - lib/_classes/integration/monobank - monobank_settings.dart Coverage Total Hit
Test: lcov.info Lines: 91.9 % 37 34
Test Date: 2026-12-05 10:50:38 Functions: - 0 0

            Line data    Source code
       1              : // Copyright 2026 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:convert';
       5              : 
       6              : import 'package:app_finance/_classes/controller/encryption_handler.dart';
       7              : import 'package:app_finance/_classes/storage/app_preferences.dart';
       8              : 
       9              : class MonobankSettings {
      10              :   final Map<String, String> links;
      11              :   final Map<String, DateTime> imported;
      12              :   final Map<String, int> records;
      13              :   static const recordsLimit = 1000;
      14              : 
      15            1 :   MonobankSettings({Map<String, String>? links, Map<String, DateTime>? imported, Map<String, int>? records})
      16            1 :       : links = links ?? {},
      17            1 :         imported = imported ?? {},
      18            1 :         records = records ?? {};
      19              : 
      20            1 :   void addRecords(Map<String, int> value) {
      21            2 :     for (final entry in value.entries) {
      22            3 :       records.remove(entry.key);
      23            4 :       records[entry.key] = entry.value;
      24              :     }
      25            3 :     final extra = records.length - recordsLimit;
      26            1 :     if (extra > 0) {
      27            0 :       records.keys.take(extra).toList().forEach(records.remove);
      28              :     }
      29              :   }
      30              : 
      31            2 :   Map<String, dynamic> toJson() => {
      32            1 :         'links': links,
      33            5 :         'imported': imported.map((key, value) => MapEntry(key, value.toIso8601String())),
      34            1 :         'records': records,
      35              :       };
      36              : 
      37            2 :   factory MonobankSettings.fromJson(Map<String, dynamic> json) => MonobankSettings(
      38            5 :         links: (json['links'] as Map<String, dynamic>? ?? {}).map((key, value) => MapEntry(key, value.toString())),
      39            2 :         imported: (json['imported'] as Map<String, dynamic>? ?? {}).map(
      40            4 :           (key, value) => MapEntry(key, DateTime.parse(value.toString())),
      41              :         ),
      42            1 :         records: {
      43            2 :           for (final entry in (json['records'] as Map<String, dynamic>? ?? {}).entries)
      44            6 :             if (entry.value is num) entry.key: (entry.value as num).toInt(),
      45              :         },
      46              :       );
      47              : 
      48            1 :   static MonobankSettings load() {
      49            1 :     final value = AppPreferences.get(AppPreferences.prefMonobank);
      50            1 :     if (value == null || value.isEmpty) {
      51            1 :       return MonobankSettings();
      52              :     }
      53              :     try {
      54            2 :       return MonobankSettings.fromJson(jsonDecode(value));
      55            0 :     } on FormatException {
      56            0 :       return MonobankSettings();
      57              :     }
      58              :   }
      59              : 
      60            4 :   Future<void> save() => AppPreferences.set(AppPreferences.prefMonobank, jsonEncode(toJson()));
      61              : 
      62            1 :   static String? loadToken() {
      63            1 :     final value = AppPreferences.get(AppPreferences.prefMonobankToken);
      64            1 :     if (value == null || value.isEmpty) {
      65              :       return null;
      66              :     }
      67              :     try {
      68            1 :       return EncryptionHandler.decrypt(value);
      69              :     } catch (_) {
      70              :       return null;
      71              :     }
      72              :   }
      73              : 
      74            1 :   static Future<void> saveToken(String token) =>
      75            2 :       AppPreferences.set(AppPreferences.prefMonobankToken, EncryptionHandler.encrypt(token));
      76              : 
      77            2 :   static Future<void> clearToken() => AppPreferences.clear(AppPreferences.prefMonobankToken);
      78              : }
        

Generated by: LCOV version 2.0-1