-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #501 from lyskouski/CR-498
[#498] [CR] LocalStorage limitations
- Loading branch information
Showing
10 changed files
with
214 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
"curr", | ||
"lerp", | ||
"LTRB", | ||
"readwrite", | ||
"sublist", | ||
"unfocus", | ||
"writeln" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
lib/_classes/storage/transaction_log/abstract_storage.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2024 The terCAD team. All rights reserved. | ||
// Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file. | ||
|
||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:app_finance/_classes/storage/transaction_log/interface_storage.dart'; | ||
import 'package:app_finance/_ext/int_ext.dart'; | ||
import 'package:path_provider/path_provider.dart'; | ||
|
||
abstract class AbstractStorage implements InterfaceStorage { | ||
static File? _logFile; | ||
static bool _isLocked = false; | ||
|
||
static const filePath = '.terCAD/app-finance.log'; | ||
|
||
static Future<File> get logFle async { | ||
if (_logFile != null) { | ||
return Future.value(_logFile); | ||
} | ||
List<File> scope = [ | ||
await getApplicationDocumentsDirectory(), | ||
await getApplicationSupportDirectory(), | ||
Directory.systemTemp, | ||
await getTemporaryDirectory(), | ||
].map((dir) => File('${dir.absolute.path}/$filePath')).toList(); | ||
File? file = scope.where((f) => f.existsSync()).firstOrNull; | ||
int i = 0; | ||
while (i < scope.length && file == null) { | ||
try { | ||
File tmp = scope[i]; | ||
if (!tmp.existsSync()) { | ||
tmp.createSync(recursive: true); | ||
tmp.writeAsString("\n", mode: FileMode.append); | ||
} | ||
file = tmp; | ||
} catch (e) { | ||
i++; | ||
} | ||
} | ||
if (file == null) { | ||
throw Exception('Write access denied for: $scope.'); | ||
} | ||
return _logFile = file; | ||
} | ||
|
||
static Future<String> getSize() async { | ||
int size = (await logFle).lengthSync(); | ||
return size.toByteSize(); | ||
} | ||
|
||
static void saveRaw(String line) { | ||
int retrial = 1; | ||
while (_isLocked && retrial < 1000) { | ||
sleep(Duration(microseconds: retrial * 10)); | ||
retrial++; | ||
} | ||
_isLocked = true; | ||
_logFile!.writeAsStringSync("$line\n", mode: FileMode.append); | ||
_isLocked = false; | ||
} | ||
|
||
static Stream<String> read() async* { | ||
Stream<String> lines = (await logFle).openRead().transform(utf8.decoder).transform(const LineSplitter()); | ||
|
||
await for (var line in lines) { | ||
yield line; | ||
} | ||
} | ||
|
||
static void clear() { | ||
_logFile?.deleteSync(); | ||
_logFile?.createSync(); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
lib/_classes/storage/transaction_log/abstract_storage_web.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2024 The terCAD team. All rights reserved. | ||
// Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file. | ||
|
||
import 'package:app_finance/_classes/storage/app_preferences.dart'; | ||
import 'package:app_finance/_classes/storage/transaction_log/interface_storage.dart'; | ||
import 'package:app_finance/_ext/int_ext.dart'; | ||
import 'package:idb_shim/idb_browser.dart'; | ||
|
||
abstract class AbstractStorage implements InterfaceStorage { | ||
static int increment = 0; | ||
static const String storeName = 'records'; | ||
static Database? db; | ||
|
||
static Future<void> _initIndexedDB() async { | ||
IdbFactory? idbFactory = getIdbFactory(); | ||
if (idbFactory == null) { | ||
return; | ||
} | ||
db = await idbFactory.open('fingrom.db', version: 1, onUpgradeNeeded: (VersionChangeEvent event) { | ||
event.database.createObjectStore(storeName, keyPath: 'id'); | ||
}); | ||
} | ||
|
||
static Future<String> getSize() async { | ||
int size = increment * 256; | ||
return size.toByteSize(); | ||
} | ||
|
||
static void saveRaw(String line) { | ||
if (db != null) { | ||
var store = db!.transaction(storeName, 'readwrite').objectStore(storeName); | ||
store.put({'id': 'log$increment', 'line': line}); | ||
} else { | ||
AppPreferences.set('log$increment', line); | ||
} | ||
increment++; | ||
} | ||
|
||
static Stream<String> readRaw(Function callback) async* { | ||
int attempts = 0; | ||
do { | ||
int i = increment + attempts; | ||
String? line = await callback(i); | ||
if (line == null) { | ||
attempts++; | ||
} else { | ||
increment += attempts + 1; | ||
attempts = 0; | ||
} | ||
yield line ?? ''; | ||
} while (attempts < 10); | ||
} | ||
|
||
static Stream<String> read() async* { | ||
increment = 0; | ||
await _initIndexedDB(); | ||
await for (var line in readRaw((i) => AppPreferences.get('log$i'))) { | ||
yield line; | ||
} | ||
if (db != null) { | ||
var store = db!.transaction(storeName, 'readonly').objectStore(storeName); | ||
await for (var line in readRaw((i) async => ((await store.getObject('log$i')) as Map?)?['line'])) { | ||
yield line; | ||
} | ||
} | ||
} | ||
|
||
static void clear() { | ||
while (increment > 0) { | ||
AppPreferences.clear('log$increment'); | ||
if (db != null) { | ||
db!.transaction(storeName, 'readwrite').objectStore(storeName).delete('log$increment'); | ||
} | ||
increment--; | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
lib/_classes/storage/transaction_log/interface_storage.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright 2024 The terCAD team. All rights reserved. | ||
// Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file. | ||
|
||
abstract interface class InterfaceStorage { | ||
static Future<String> getSize() async => Future.value(''); | ||
|
||
static void saveRaw(String line) {} | ||
|
||
static Stream<String> read() async* {} | ||
|
||
static void clear() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.