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 0 : PrivatBankSettings({List<String>? accounts, Map<String, String>? links, Map<String, DateTime>? imported})
16 0 : : accounts = accounts ?? [],
17 0 : links = links ?? {},
18 0 : imported = imported ?? {};
19 :
20 0 : Map<String, dynamic> toJson() => {
21 0 : 'accounts': accounts,
22 0 : 'links': links,
23 0 : 'imported': imported.map((key, value) => MapEntry(key, value.toIso8601String())),
24 : };
25 :
26 0 : factory PrivatBankSettings.fromJson(Map<String, dynamic> json) => PrivatBankSettings(
27 0 : accounts: (json['accounts'] as List<dynamic>? ?? []).map((e) => e.toString()).toList(),
28 0 : links: (json['links'] as Map<String, dynamic>? ?? {}).map((key, value) => MapEntry(key, value.toString())),
29 0 : imported: (json['imported'] as Map<String, dynamic>? ?? {}).map(
30 0 : (key, value) => MapEntry(key, DateTime.parse(value.toString())),
31 : ),
32 : );
33 :
34 0 : static PrivatBankSettings load() {
35 0 : final value = AppPreferences.get(AppPreferences.prefPrivatBank);
36 0 : if (value == null || value.isEmpty) {
37 0 : return PrivatBankSettings();
38 : }
39 : try {
40 0 : return PrivatBankSettings.fromJson(jsonDecode(value));
41 0 : } on FormatException {
42 0 : return PrivatBankSettings();
43 : }
44 : }
45 :
46 0 : Future<void> save() => AppPreferences.set(AppPreferences.prefPrivatBank, jsonEncode(toJson()));
47 :
48 0 : static String? loadToken() {
49 0 : final value = AppPreferences.get(AppPreferences.prefPrivatBankToken);
50 0 : if (value == null || value.isEmpty) {
51 : return null;
52 : }
53 : try {
54 0 : return EncryptionHandler.decrypt(value);
55 : } catch (_) {
56 : return null;
57 : }
58 : }
59 :
60 0 : static Future<void> saveToken(String token) =>
61 0 : AppPreferences.set(AppPreferences.prefPrivatBankToken, EncryptionHandler.encrypt(token));
62 :
63 0 : static Future<void> clearToken() => AppPreferences.clear(AppPreferences.prefPrivatBankToken);
64 : }
|