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