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 0 : NotificationApp({this.account, this.category, this.currency});
16 :
17 0 : bool get isLinked => account != null && category != null;
18 :
19 0 : Map<String, dynamic> toJson() => {
20 0 : if (account != null) 'account': account,
21 0 : if (category != null) 'category': category,
22 0 : if (currency != null) 'currency': currency,
23 : };
24 :
25 0 : factory NotificationApp.fromJson(Map<String, dynamic> json) => NotificationApp(
26 0 : account: json['account']?.toString(),
27 0 : category: json['category']?.toString(),
28 0 : 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 1 : 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 1 : }) : apps = apps ?? {},
49 1 : accounts = accounts ?? {},
50 1 : income = income ?? [...NotificationParser.incomeKeywords],
51 1 : balance = balance ?? [...NotificationParser.balanceKeywords],
52 1 : merchant = merchant ?? [...NotificationParser.merchantLabels];
53 :
54 0 : NotificationApp get(String package) => apps.putIfAbsent(package, NotificationApp.new);
55 :
56 0 : Map<String, dynamic> toJson() => {
57 0 : 'apps': apps.map((key, value) => MapEntry(key, value.toJson())),
58 0 : 'income': income,
59 0 : 'balance': balance,
60 0 : 'merchant': merchant,
61 0 : 'accounts': {
62 0 : for (final entry in accounts.entries)
63 0 : if (entry.value.isNotEmpty) entry.key: entry.value,
64 : },
65 : };
66 :
67 0 : static List<String>? _toKeywords(dynamic value) =>
68 0 : value is List ? NotificationParser.toKeywords(value.map((e) => e.toString())) : null;
69 :
70 0 : factory NotificationSettings.fromJson(Map<String, dynamic> json) => NotificationSettings(
71 0 : apps: (json['apps'] as Map<String, dynamic>? ?? {}).map(
72 0 : (key, value) => MapEntry(key, NotificationApp.fromJson(value as Map<String, dynamic>)),
73 : ),
74 0 : income: _toKeywords(json['income']),
75 0 : balance: _toKeywords(json['balance']),
76 0 : merchant: _toKeywords(json['merchant']),
77 0 : accounts: (json['accounts'] as Map<String, dynamic>? ?? {})
78 0 : .map((key, value) => MapEntry(key, _toKeywords(value) ?? <String>[])),
79 : );
80 :
81 1 : static NotificationSettings load() {
82 1 : final value = AppPreferences.get(AppPreferences.prefNotification);
83 0 : if (value == null || value.isEmpty) {
84 1 : return NotificationSettings();
85 : }
86 : try {
87 0 : 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 0 : Future<void> save() => AppPreferences.set(AppPreferences.prefNotification, jsonEncode(toJson()));
96 : }
|