LCOV - code coverage report
Current view: top level - lib/_classes/integration/privatbank - privatbank_settings.dart Coverage Total Hit
Test: lcov.info Lines: 92.9 % 28 26
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 PrivatBankSettings {
      10              :   // IBANs entered by a user (the API provides no list of accounts)
      11              :   final List<String> accounts;
      12              :   final Map<String, String> links;
      13              :   final Map<String, DateTime> imported;
      14              : 
      15            1 :   PrivatBankSettings({List<String>? accounts, Map<String, String>? links, Map<String, DateTime>? imported})
      16            1 :       : accounts = accounts ?? [],
      17            1 :         links = links ?? {},
      18            1 :         imported = imported ?? {};
      19              : 
      20            2 :   Map<String, dynamic> toJson() => {
      21            1 :         'accounts': accounts,
      22            1 :         'links': links,
      23            5 :         'imported': imported.map((key, value) => MapEntry(key, value.toIso8601String())),
      24              :       };
      25              : 
      26            2 :   factory PrivatBankSettings.fromJson(Map<String, dynamic> json) => PrivatBankSettings(
      27            5 :         accounts: (json['accounts'] as List<dynamic>? ?? []).map((e) => e.toString()).toList(),
      28            5 :         links: (json['links'] as Map<String, dynamic>? ?? {}).map((key, value) => MapEntry(key, value.toString())),
      29            2 :         imported: (json['imported'] as Map<String, dynamic>? ?? {}).map(
      30            4 :           (key, value) => MapEntry(key, DateTime.parse(value.toString())),
      31              :         ),
      32              :       );
      33              : 
      34            1 :   static PrivatBankSettings load() {
      35            1 :     final value = AppPreferences.get(AppPreferences.prefPrivatBank);
      36            1 :     if (value == null || value.isEmpty) {
      37            1 :       return PrivatBankSettings();
      38              :     }
      39              :     try {
      40            2 :       return PrivatBankSettings.fromJson(jsonDecode(value));
      41            0 :     } on FormatException {
      42            0 :       return PrivatBankSettings();
      43              :     }
      44              :   }
      45              : 
      46            4 :   Future<void> save() => AppPreferences.set(AppPreferences.prefPrivatBank, jsonEncode(toJson()));
      47              : 
      48            1 :   static String? loadToken() {
      49            1 :     final value = AppPreferences.get(AppPreferences.prefPrivatBankToken);
      50            1 :     if (value == null || value.isEmpty) {
      51              :       return null;
      52              :     }
      53              :     try {
      54            1 :       return EncryptionHandler.decrypt(value);
      55              :     } catch (_) {
      56              :       return null;
      57              :     }
      58              :   }
      59              : 
      60            1 :   static Future<void> saveToken(String token) =>
      61            2 :       AppPreferences.set(AppPreferences.prefPrivatBankToken, EncryptionHandler.encrypt(token));
      62              : 
      63            2 :   static Future<void> clearToken() => AppPreferences.clear(AppPreferences.prefPrivatBankToken);
      64              : }
        

Generated by: LCOV version 2.0-1