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 'dart:convert'; 5 : import 'dart:async'; 6 : import 'package:app_finance/_classes/controller/encryption_handler.dart'; 7 : import 'package:app_finance/_classes/storage/app_data.dart'; 8 : import 'package:app_finance/_classes/storage/transaction_log/abstract_storage_web.dart' 9 : if (dart.library.io) 'package:app_finance/_classes/storage/transaction_log/abstract_storage.dart'; 10 : import 'package:app_finance/_classes/storage/transaction_log/interface_storage.dart'; 11 : import 'package:app_finance/_ext/data_ext.dart'; 12 : 13 : class TransactionLog extends AbstractStorage implements InterfaceStorage { 14 : static int amount = 0; 15 : 16 0 : static Future<String> getSize() => AbstractStorage.getSize(); 17 : 18 0 : static void clear() => AbstractStorage.clear(); 19 : 20 6 : static Stream<String> read() => AbstractStorage.read(); 21 : 22 0 : static void saveRaw(String line) => AbstractStorage.saveRaw(line); 23 : 24 0 : static void save(dynamic content) { 25 0 : String line = content.toString(); 26 0 : if (EncryptionHandler.doEncrypt()) { 27 0 : line = EncryptionHandler.encrypt(line); 28 : } 29 0 : saveRaw(line); 30 0 : amount++; 31 : } 32 : 33 0 : static void init(AppData store, String type, Map<String, dynamic> data) { 34 0 : final obj = type.toDataObject(data, store); 35 : if (obj != null) { 36 0 : store.update(obj.uuid ?? '', obj, true); 37 : } 38 : } 39 : 40 3 : static Future<bool> load(AppData store) async { 41 3 : bool isEncrypted = EncryptionHandler.doEncrypt(); 42 : bool isOK = true; 43 : amount = 0; 44 6 : await for (var line in read()) { 45 0 : isOK &= add(store, line, isEncrypted); 46 0 : amount++; 47 : } 48 : return isOK; 49 : } 50 : 51 0 : static bool add(AppData store, String line, bool isEncrypted, [bool onlyNew = false]) { 52 : bool isOK = true; 53 0 : if (line == '') { 54 : return isOK; 55 : } 56 : try { 57 : if (isEncrypted) { 58 0 : line = EncryptionHandler.decrypt(line); 59 : } 60 0 : var obj = json.decode(line); 61 : // FIXME: hash is not always the same for Web 62 : if ( // EncryptionHandler.getHash(obj['data']) == obj['type']['hash'] && 63 0 : (!onlyNew || store.getByUuid(obj['data']['uuid']) == null)) { 64 0 : init(store, obj['type']['name'], obj['data']); 65 : } else { 66 : // Corrupted data... skip 67 : } 68 : // ignore: unused_catch_stack 69 : } catch (e, stackTrace) { 70 : // print([e, stackTrace]); 71 : isOK = false; 72 : } 73 : return isOK; 74 : } 75 : }