Line data Source code
1 : // Copyright 2023 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 'package:app_finance/_classes/herald/app_locale.dart';
5 : import 'package:app_finance/_classes/storage/transaction_log/abstract_protocol.dart';
6 : import 'package:app_finance/_classes/storage/transaction_log/interface_protocol.dart';
7 : import 'package:app_finance/_mixins/file/file_import_mixin.dart';
8 : import 'package:app_finance/_mixins/file/file_export_mixin_web.dart'
9 : if (dart.library.io) 'package:app_finance/_mixins/file/file_export_mixin.dart';
10 :
11 : class FileProtocol extends AbstractProtocol with FileImportMixin, FileExportMixin implements InterfaceProtocol {
12 : @override
13 : final Function callbackMessage;
14 : @override
15 : final Function callbackProgress;
16 : String message = '';
17 : bool inProgress = false;
18 :
19 0 : FileProtocol({
20 : required this.callbackMessage,
21 : required this.callbackProgress,
22 : });
23 :
24 0 : @override
25 : Future<void> save(dynamic data) async {
26 0 : if (data.isEmpty) {
27 0 : callbackMessage(message = AppLocale.labels.isRequired);
28 : return;
29 : }
30 0 : callbackProgress(inProgress = true);
31 0 : final codeUnits = await exportTransactions();
32 0 : await exportFile(codeUnits, data);
33 0 : callbackProgress(inProgress = false);
34 0 : callbackMessage(message = AppLocale.labels.success);
35 : }
36 :
37 0 : @override
38 : Future<void> load(dynamic data, [bool isEncrypted = true, bool isCleaned = false]) async {
39 0 : callbackProgress(inProgress = true);
40 0 : final content = await importFile(['log']);
41 : if (content != null) {
42 : if (isCleaned) {
43 0 : clearTransactions();
44 : }
45 0 : importTransactions(content.codeUnits, isEncrypted);
46 0 : callbackMessage(message = AppLocale.labels.success);
47 : } else {
48 0 : callbackMessage(message = AppLocale.labels.missingContent);
49 : }
50 0 : callbackProgress(inProgress = false);
51 : }
52 : }
|