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/integration/notification/notification_parser.dart';
7 : import 'package:app_finance/_classes/storage/app_preferences.dart';
8 :
9 : class NotificationApp {
10 : String? account;
11 : String? category;
12 : // Currency (code) of amounts without one, and the preferred one for ambiguous symbols ($, kr)
13 : String? currency;
14 :
15 1 : NotificationApp({this.account, this.category, this.currency});
16 :
17 3 : bool get isLinked => account != null && category != null;
18 :
19 2 : Map<String, dynamic> toJson() => {
20 3 : if (account != null) 'account': account,
21 3 : if (category != null) 'category': category,
22 1 : if (currency != null) 'currency': currency,
23 : };
24 :
25 2 : factory NotificationApp.fromJson(Map<String, dynamic> json) => NotificationApp(
26 2 : account: json['account']?.toString(),
27 2 : category: json['category']?.toString(),
28 1 : currency: json['currency']?.toString(),
29 : );
30 : }
31 :
32 : class NotificationSettings {
33 : // Package name of the application => its link to the account
34 : final Map<String, NotificationApp> apps;
35 : // Keywords to detect income, to skip balance amounts and to find a merchant (for all apps), the built-in ones by default
36 : List<String> income;
37 : List<String> balance;
38 : List<String> merchant;
39 : // Keywords to recognize the account (uuid) of a transaction, otherwise the default one of the app is taken
40 : Map<String, List<String>> accounts;
41 :
42 6 : NotificationSettings({
43 : Map<String, NotificationApp>? apps,
44 : List<String>? income,
45 : List<String>? balance,
46 : List<String>? merchant,
47 : Map<String, List<String>>? accounts,
48 6 : }) : apps = apps ?? {},
49 6 : accounts = accounts ?? {},
50 6 : income = income ?? [...NotificationParser.incomeKeywords],
51 6 : balance = balance ?? [...NotificationParser.balanceKeywords],
52 6 : merchant = merchant ?? [...NotificationParser.merchantLabels];
53 :
54 3 : NotificationApp get(String package) => apps.putIfAbsent(package, NotificationApp.new);
55 :
56 2 : Map<String, dynamic> toJson() => {
57 5 : 'apps': apps.map((key, value) => MapEntry(key, value.toJson())),
58 1 : 'income': income,
59 1 : 'balance': balance,
60 1 : 'merchant': merchant,
61 1 : 'accounts': {
62 2 : for (final entry in accounts.entries)
63 5 : if (entry.value.isNotEmpty) entry.key: entry.value,
64 : },
65 : };
66 :
67 1 : static List<String>? _toKeywords(dynamic value) =>
68 5 : value is List ? NotificationParser.toKeywords(value.map((e) => e.toString())) : null;
69 :
70 2 : factory NotificationSettings.fromJson(Map<String, dynamic> json) => NotificationSettings(
71 2 : apps: (json['apps'] as Map<String, dynamic>? ?? {}).map(
72 3 : (key, value) => MapEntry(key, NotificationApp.fromJson(value as Map<String, dynamic>)),
73 : ),
74 2 : income: _toKeywords(json['income']),
75 2 : balance: _toKeywords(json['balance']),
76 2 : merchant: _toKeywords(json['merchant']),
77 2 : accounts: (json['accounts'] as Map<String, dynamic>? ?? {})
78 4 : .map((key, value) => MapEntry(key, _toKeywords(value) ?? <String>[])),
79 : );
80 :
81 6 : static NotificationSettings load() {
82 6 : final value = AppPreferences.get(AppPreferences.prefNotification);
83 1 : if (value == null || value.isEmpty) {
84 6 : return NotificationSettings();
85 : }
86 : try {
87 2 : return NotificationSettings.fromJson(jsonDecode(value));
88 0 : } on FormatException {
89 0 : return NotificationSettings();
90 0 : } on TypeError {
91 0 : return NotificationSettings();
92 : }
93 : }
94 :
95 4 : Future<void> save() => AppPreferences.set(AppPreferences.prefNotification, jsonEncode(toJson()));
96 : }
|