Line data Source code
1 : import 'package:at_client/src/preference/at_client_preference.dart';
2 : import 'package:at_persistence_secondary_server/at_persistence_secondary_server.dart';
3 :
4 : /// Manager to create local storage
5 : class StorageManager {
6 0 : static final StorageManager _singleton = StorageManager._internal();
7 :
8 0 : StorageManager._internal();
9 :
10 0 : factory StorageManager.getInstance() {
11 0 : return _singleton;
12 : }
13 :
14 : bool isStorageInitialized = false;
15 :
16 : AtClientPreference? preferences;
17 :
18 0 : StorageManager(this.preferences);
19 :
20 0 : Future<void> init(String currentAtSign, List<int>? keyStoreSecret) async {
21 0 : if (!isStorageInitialized) {
22 0 : await _initStorage(currentAtSign, keyStoreSecret);
23 : }
24 : }
25 :
26 0 : Future<void> _initStorage(
27 : String currentAtSign, List<int>? keyStoreSecret) async {
28 0 : print('initializing storage');
29 0 : var storagePath = preferences!.hiveStoragePath;
30 0 : var commitLogPath = preferences!.commitLogPath;
31 :
32 : if (storagePath == null || commitLogPath == null) {
33 0 : throw Exception('Please set local storage paths');
34 : }
35 0 : var atCommitLog = await AtCommitLogManagerImpl.getInstance().getCommitLog(
36 : currentAtSign,
37 : commitLogPath: commitLogPath,
38 : enableCommitId: false);
39 : // Initialize Persistence
40 0 : var manager = SecondaryPersistenceStoreFactory.getInstance()
41 0 : .getSecondaryPersistenceStore(currentAtSign)!
42 0 : .getHivePersistenceManager()!;
43 0 : await manager.init(storagePath);
44 0 : var hiveKeyStore = SecondaryPersistenceStoreFactory.getInstance()
45 0 : .getSecondaryPersistenceStore(currentAtSign)!
46 0 : .getSecondaryKeyStore()!;
47 0 : hiveKeyStore.commitLog = atCommitLog;
48 0 : var keyStoreManager = SecondaryPersistenceStoreFactory.getInstance()
49 0 : .getSecondaryPersistenceStore(currentAtSign)!
50 0 : .getSecondaryKeyStoreManager()!;
51 0 : keyStoreManager.keyStore = hiveKeyStore;
52 0 : isStorageInitialized = true;
53 : }
54 : }
|