diff --git a/lib/data_layer/db/object_box_ndk/db_object_box.dart b/lib/data_layer/db/object_box_ndk/db_object_box.dart index bf1bd9ff..0a0b606b 100644 --- a/lib/data_layer/db/object_box_ndk/db_object_box.dart +++ b/lib/data_layer/db/object_box_ndk/db_object_box.dart @@ -14,7 +14,7 @@ import 'schema/db_user_relay_list.dart'; class DbObjectBox implements CacheManager { final Completer _initCompleter = Completer(); - Future get _dbRdy => _initCompleter.future; + Future get dbRdy => _initCompleter.future; late ObjectBoxInit _objectBox; DbObjectBox() { @@ -29,7 +29,7 @@ class DbObjectBox implements CacheManager { @override Future loadContactList(String pubKey) async { - await _dbRdy; + await dbRdy; final contactListBox = _objectBox.store.box(); final existingContact = contactListBox .query(DbContactList_.pubKey.equals(pubKey)) @@ -44,7 +44,7 @@ class DbObjectBox implements CacheManager { @override Future loadEvent(String id) async { - await _dbRdy; + await dbRdy; final eventBox = _objectBox.store.box(); final existingEvent = eventBox.query(DbNip01Event_.nostrId.equals(id)).build().findFirst(); @@ -62,7 +62,7 @@ class DbObjectBox implements CacheManager { int? since, int? until, }) async { - await _dbRdy; + await dbRdy; final eventBox = _objectBox.store.box(); var query = eventBox.query(DbNip01Event_.pubKey @@ -94,7 +94,7 @@ class DbObjectBox implements CacheManager { @override Future loadMetadata(String pubKey) async { - await _dbRdy; + await dbRdy; final metadataBox = _objectBox.store.box(); final existingMetadata = metadataBox .query(DbMetadata_.pubKey.equals(pubKey)) @@ -109,7 +109,7 @@ class DbObjectBox implements CacheManager { @override Future> loadMetadatas(List pubKeys) async { - await _dbRdy; + await dbRdy; final metadataBox = _objectBox.store.box(); final existingMetadatas = metadataBox .query(DbMetadata_.pubKey.oneOf(pubKeys)) @@ -121,21 +121,21 @@ class DbObjectBox implements CacheManager { @override Future removeAllContactLists() async { - await _dbRdy; + await dbRdy; final contactListBox = _objectBox.store.box(); contactListBox.removeAll(); } @override Future removeAllEvents() async { - await _dbRdy; + await dbRdy; final eventBox = _objectBox.store.box(); eventBox.removeAll(); } @override Future removeAllEventsByPubKey(String pubKey) async { - await _dbRdy; + await dbRdy; final eventBox = _objectBox.store.box(); final events = eventBox.query(DbNip01Event_.pubKey.equals(pubKey)).build().find(); @@ -144,14 +144,14 @@ class DbObjectBox implements CacheManager { @override Future removeAllMetadatas() async { - await _dbRdy; + await dbRdy; final metadataBox = _objectBox.store.box(); metadataBox.removeAll(); } @override Future saveContactList(ContactList contactList) async { - await _dbRdy; + await dbRdy; final contactListBox = _objectBox.store.box(); final existingContact = contactListBox .query(DbContactList_.pubKey.equals(contactList.pubKey)) @@ -166,7 +166,7 @@ class DbObjectBox implements CacheManager { @override Future saveContactLists(List contactLists) async { - await _dbRdy; + await dbRdy; final contactListBox = _objectBox.store.box(); contactListBox .putMany(contactLists.map((cl) => DbContactList.fromNdk(cl)).toList()); @@ -174,7 +174,7 @@ class DbObjectBox implements CacheManager { @override Future saveEvent(Nip01Event event) async { - await _dbRdy; + await dbRdy; final eventBox = _objectBox.store.box(); final existingEvent = eventBox .query(DbNip01Event_.nostrId.equals(event.id)) @@ -188,14 +188,14 @@ class DbObjectBox implements CacheManager { @override Future saveEvents(List events) async { - await _dbRdy; + await dbRdy; final eventBox = _objectBox.store.box(); eventBox.putMany(events.map((e) => DbNip01Event.fromNdk(e)).toList()); } @override Future saveMetadata(Metadata metadata) async { - await _dbRdy; + await dbRdy; final metadataBox = _objectBox.store.box(); final existingMetadatas = metadataBox .query(DbMetadata_.pubKey.equals(metadata.pubKey)) @@ -214,7 +214,7 @@ class DbObjectBox implements CacheManager { @override Future saveMetadatas(List metadatas) async { - await _dbRdy; + await dbRdy; for (final metadata in metadatas) { await saveMetadata(metadata); } @@ -222,7 +222,7 @@ class DbObjectBox implements CacheManager { @override Future loadNip05(String pubKey) async { - await _dbRdy; + await dbRdy; final nip05Box = _objectBox.store.box(); final existingNip05 = nip05Box .query(DbNip05_.pubKey.equals(pubKey)) @@ -239,7 +239,7 @@ class DbObjectBox implements CacheManager { @override Future> loadNip05s(List pubKeys) async { - await _dbRdy; + await dbRdy; final nip05Box = _objectBox.store.box(); final existingNip05s = nip05Box .query(DbNip05_.pubKey.oneOf(pubKeys)) @@ -258,7 +258,7 @@ class DbObjectBox implements CacheManager { @override Future loadUserRelayList(String pubKey) async { - await _dbRdy; + await dbRdy; final userRelayListBox = _objectBox.store.box(); final existingUserRelayList = userRelayListBox .query(DbUserRelayList_.pubKey.equals(pubKey)) @@ -273,7 +273,7 @@ class DbObjectBox implements CacheManager { @override Future removeAllNip05s() async { - await _dbRdy; + await dbRdy; final nip05Box = _objectBox.store.box(); nip05Box.removeAll(); } @@ -328,7 +328,7 @@ class DbObjectBox implements CacheManager { @override Future saveNip05(Nip05 nip05) async { - await _dbRdy; + await dbRdy; final nip05Box = _objectBox.store.box(); final existingNip05 = nip05Box .query(DbNip05_.pubKey.equals(nip05.pubKey)) @@ -343,7 +343,7 @@ class DbObjectBox implements CacheManager { @override Future saveNip05s(List nip05s) async { - await _dbRdy; + await dbRdy; final nip05Box = _objectBox.store.box(); nip05Box.putMany(nip05s.map((n) => DbNip05.fromNdk(n)).toList()); } @@ -356,7 +356,7 @@ class DbObjectBox implements CacheManager { @override Future saveUserRelayList(UserRelayList userRelayList) async { - await _dbRdy; + await dbRdy; final userRelayListBox = _objectBox.store.box(); final existingUserRelayList = userRelayListBox .query(DbUserRelayList_.pubKey.equals(userRelayList.pubKey)) diff --git a/lib/main.dart b/lib/main.dart index 1d669094..b9ed7827 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,14 +1,16 @@ +import 'dart:io'; import 'dart:ui'; +import 'package:camelus/presentation_layer/providers/db_ndk_provider.dart'; import 'package:camelus/presentation_layer/providers/inbox_outbox_provider.dart'; import 'package:flutter/cupertino.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_mentions/flutter_mentions.dart'; - import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:ndk/ndk.dart'; //import 'package:device_preview/device_preview.dart'; - +import 'data_layer/db/object_box_ndk/db_object_box.dart'; import 'deep_links.dart'; import 'domain_layer/usecases/app_auth.dart'; import 'presentation_layer/providers/event_signer_provider.dart'; @@ -57,6 +59,13 @@ Future main() async { // Create a ProviderContainer final providerContainer = ProviderContainer(); + // init ndk db + DbObjectBox dbCacheManager = DbObjectBox(); + await dbCacheManager.dbRdy; + final CacheManager cacheManager = dbCacheManager; + + providerContainer.read(dbNdkProvider.notifier).setDB(cacheManager); + // we have a signer, so we can set it if (mySigner != null) { providerContainer.read(eventSignerProvider.notifier).setSigner(mySigner); diff --git a/lib/presentation_layer/providers/db_ndk_provider.dart b/lib/presentation_layer/providers/db_ndk_provider.dart new file mode 100644 index 00000000..e7dea58a --- /dev/null +++ b/lib/presentation_layer/providers/db_ndk_provider.dart @@ -0,0 +1,15 @@ +import 'package:ndk/ndk.dart'; +import 'package:riverpod/riverpod.dart'; + +final dbNdkProvider = + StateNotifierProvider((ref) { + return DbProviderNotifier(); +}); + +class DbProviderNotifier extends StateNotifier { + DbProviderNotifier() : super(null); + + void setDB(CacheManager newDb) { + state = newDb; + } +} diff --git a/lib/presentation_layer/providers/db_object_box_provider.dart b/lib/presentation_layer/providers/db_object_box_provider.dart deleted file mode 100644 index 02b48e5d..00000000 --- a/lib/presentation_layer/providers/db_object_box_provider.dart +++ /dev/null @@ -1,8 +0,0 @@ -import 'package:riverpod/riverpod.dart'; - -import '../../data_layer/db/object_box_ndk/db_object_box.dart'; - -final dbObjectBoxProvider = Provider((ref) { - final db = DbObjectBox(); - return db; -}); diff --git a/lib/presentation_layer/providers/ndk_provider.dart b/lib/presentation_layer/providers/ndk_provider.dart index b59279b9..24cb1917 100644 --- a/lib/presentation_layer/providers/ndk_provider.dart +++ b/lib/presentation_layer/providers/ndk_provider.dart @@ -2,21 +2,18 @@ import 'package:ndk/ndk.dart'; import 'package:riverpod/riverpod.dart'; import '../../config/default_relays.dart'; -import 'db_object_box_provider.dart'; +import 'db_ndk_provider.dart'; import 'event_signer_provider.dart'; import 'event_verifier.dart'; final ndkProvider = Provider((ref) { final eventSigner = ref.watch(eventSignerProvider); final eventVerifier = ref.watch(eventVerifierProvider); - - final dbObjectBox = ref.watch(dbObjectBoxProvider); - - final CacheManager memDb = MemCacheManager(); + final db = ref.watch(dbNdkProvider); final NdkConfig ndkConfig = NdkConfig( engine: NdkEngine.JIT, - cache: dbObjectBox, + cache: db!, eventSigner: eventSigner, eventVerifier: eventVerifier, bootstrapRelays: CAMELUS_BOOTSTRAP_RELAYS, diff --git a/pubspec.lock b/pubspec.lock index f6b1443d..44fa456d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -438,6 +438,14 @@ packages: url: "https://pub.dev" source: hosted version: "5.0.3" + equatable: + dependency: transitive + description: + name: equatable + sha256: "567c64b3cb4cf82397aac55f4f0cbd3ca20d77c6c03bedbc4ceaddc08904aef7" + url: "https://pub.dev" + source: hosted + version: "2.0.7" fake_async: dependency: transitive description: