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