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 WiseSettings {
10 : int? profile;
11 : final Map<String, String> links;
12 : final Map<String, DateTime> imported;
13 :
14 1 : WiseSettings({this.profile, Map<String, String>? links, Map<String, DateTime>? imported})
15 0 : : links = links ?? {},
16 0 : imported = imported ?? {};
17 :
18 2 : Map<String, dynamic> toJson() => {
19 1 : 'profile': profile,
20 1 : 'links': links,
21 5 : 'imported': imported.map((key, value) => MapEntry(key, value.toIso8601String())),
22 : };
23 :
24 2 : factory WiseSettings.fromJson(Map<String, dynamic> json) => WiseSettings(
25 2 : profile: (json['profile'] as num?)?.toInt(),
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 WiseSettings load() {
33 1 : final value = AppPreferences.get(AppPreferences.prefWise);
34 1 : if (value == null || value.isEmpty) {
35 0 : return WiseSettings();
36 : }
37 : try {
38 2 : return WiseSettings.fromJson(jsonDecode(value));
39 0 : } on FormatException {
40 0 : return WiseSettings();
41 : }
42 : }
43 :
44 4 : Future<void> save() => AppPreferences.set(AppPreferences.prefWise, jsonEncode(toJson()));
45 :
46 1 : static String? loadToken() {
47 1 : final value = AppPreferences.get(AppPreferences.prefWiseToken);
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.prefWiseToken, EncryptionHandler.encrypt(token));
60 :
61 2 : static Future<void> clearToken() => AppPreferences.clear(AppPreferences.prefWiseToken);
62 : }
|