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 SimpleFinSettings {
10 : // SimpleFIN account id to the uuid of the application's account
11 : final Map<String, String> links;
12 :
13 : // SimpleFIN account id to the last day transactions were imported for
14 : final Map<String, DateTime> imported;
15 :
16 0 : SimpleFinSettings({Map<String, String>? links, Map<String, DateTime>? imported})
17 0 : : links = links ?? {},
18 0 : imported = imported ?? {};
19 :
20 0 : Map<String, dynamic> toJson() => {
21 0 : 'links': links,
22 0 : 'imported': imported.map((key, value) => MapEntry(key, value.toIso8601String())),
23 : };
24 :
25 0 : factory SimpleFinSettings.fromJson(Map<String, dynamic> json) => SimpleFinSettings(
26 0 : links: (json['links'] as Map<String, dynamic>? ?? {}).map((key, value) => MapEntry(key, value.toString())),
27 0 : imported: (json['imported'] as Map<String, dynamic>? ?? {}).map(
28 0 : (key, value) => MapEntry(key, DateTime.parse(value.toString())),
29 : ),
30 : );
31 :
32 0 : static SimpleFinSettings load() {
33 0 : final value = AppPreferences.get(AppPreferences.prefSimpleFin);
34 0 : if (value == null || value.isEmpty) {
35 0 : return SimpleFinSettings();
36 : }
37 : try {
38 0 : return SimpleFinSettings.fromJson(jsonDecode(value));
39 0 : } on FormatException {
40 0 : return SimpleFinSettings();
41 : }
42 : }
43 :
44 0 : Future<void> save() => AppPreferences.set(AppPreferences.prefSimpleFin, jsonEncode(toJson()));
45 :
46 0 : static Uri? loadAccess() {
47 0 : final value = AppPreferences.get(AppPreferences.prefSimpleFinAccess);
48 0 : if (value == null || value.isEmpty) {
49 : return null;
50 : }
51 : try {
52 0 : return Uri.parse(EncryptionHandler.decrypt(value));
53 : } catch (_) {
54 : return null;
55 : }
56 : }
57 :
58 0 : static Future<void> saveAccess(Uri access) =>
59 0 : AppPreferences.set(AppPreferences.prefSimpleFinAccess, EncryptionHandler.encrypt(access.toString()));
60 :
61 0 : static Future<void> clearAccess() => AppPreferences.clear(AppPreferences.prefSimpleFinAccess);
62 : }
|