Skip to content

Commit

Permalink
support async setup of ndkDb
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-lox committed Dec 10, 2024
1 parent acedfb5 commit f220fa1
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 39 deletions.
46 changes: 23 additions & 23 deletions lib/data_layer/db/object_box_ndk/db_object_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -29,7 +29,7 @@ class DbObjectBox implements CacheManager {

@override
Future<ContactList?> loadContactList(String pubKey) async {
await _dbRdy;
await dbRdy;
final contactListBox = _objectBox.store.box<DbContactList>();
final existingContact = contactListBox
.query(DbContactList_.pubKey.equals(pubKey))
Expand All @@ -44,7 +44,7 @@ class DbObjectBox implements CacheManager {

@override
Future<Nip01Event?> loadEvent(String id) async {
await _dbRdy;
await dbRdy;
final eventBox = _objectBox.store.box<DbNip01Event>();
final existingEvent =
eventBox.query(DbNip01Event_.nostrId.equals(id)).build().findFirst();
Expand All @@ -62,7 +62,7 @@ class DbObjectBox implements CacheManager {
int? since,
int? until,
}) async {
await _dbRdy;
await dbRdy;
final eventBox = _objectBox.store.box<DbNip01Event>();

var query = eventBox.query(DbNip01Event_.pubKey
Expand Down Expand Up @@ -94,7 +94,7 @@ class DbObjectBox implements CacheManager {

@override
Future<Metadata?> loadMetadata(String pubKey) async {
await _dbRdy;
await dbRdy;
final metadataBox = _objectBox.store.box<DbMetadata>();
final existingMetadata = metadataBox
.query(DbMetadata_.pubKey.equals(pubKey))
Expand All @@ -109,7 +109,7 @@ class DbObjectBox implements CacheManager {

@override
Future<List<Metadata?>> loadMetadatas(List<String> pubKeys) async {
await _dbRdy;
await dbRdy;
final metadataBox = _objectBox.store.box<DbMetadata>();
final existingMetadatas = metadataBox
.query(DbMetadata_.pubKey.oneOf(pubKeys))
Expand All @@ -121,21 +121,21 @@ class DbObjectBox implements CacheManager {

@override
Future<void> removeAllContactLists() async {
await _dbRdy;
await dbRdy;
final contactListBox = _objectBox.store.box<DbContactList>();
contactListBox.removeAll();
}

@override
Future<void> removeAllEvents() async {
await _dbRdy;
await dbRdy;
final eventBox = _objectBox.store.box<DbNip01Event>();
eventBox.removeAll();
}

@override
Future<void> removeAllEventsByPubKey(String pubKey) async {
await _dbRdy;
await dbRdy;
final eventBox = _objectBox.store.box<DbNip01Event>();
final events =
eventBox.query(DbNip01Event_.pubKey.equals(pubKey)).build().find();
Expand All @@ -144,14 +144,14 @@ class DbObjectBox implements CacheManager {

@override
Future<void> removeAllMetadatas() async {
await _dbRdy;
await dbRdy;
final metadataBox = _objectBox.store.box<DbMetadata>();
metadataBox.removeAll();
}

@override
Future<void> saveContactList(ContactList contactList) async {
await _dbRdy;
await dbRdy;
final contactListBox = _objectBox.store.box<DbContactList>();
final existingContact = contactListBox
.query(DbContactList_.pubKey.equals(contactList.pubKey))
Expand All @@ -166,15 +166,15 @@ class DbObjectBox implements CacheManager {

@override
Future<void> saveContactLists(List<ContactList> contactLists) async {
await _dbRdy;
await dbRdy;
final contactListBox = _objectBox.store.box<DbContactList>();
contactListBox
.putMany(contactLists.map((cl) => DbContactList.fromNdk(cl)).toList());
}

@override
Future<void> saveEvent(Nip01Event event) async {
await _dbRdy;
await dbRdy;
final eventBox = _objectBox.store.box<DbNip01Event>();
final existingEvent = eventBox
.query(DbNip01Event_.nostrId.equals(event.id))
Expand All @@ -188,14 +188,14 @@ class DbObjectBox implements CacheManager {

@override
Future<void> saveEvents(List<Nip01Event> events) async {
await _dbRdy;
await dbRdy;
final eventBox = _objectBox.store.box<DbNip01Event>();
eventBox.putMany(events.map((e) => DbNip01Event.fromNdk(e)).toList());
}

@override
Future<void> saveMetadata(Metadata metadata) async {
await _dbRdy;
await dbRdy;
final metadataBox = _objectBox.store.box<DbMetadata>();
final existingMetadatas = metadataBox
.query(DbMetadata_.pubKey.equals(metadata.pubKey))
Expand All @@ -214,15 +214,15 @@ class DbObjectBox implements CacheManager {

@override
Future<void> saveMetadatas(List<Metadata> metadatas) async {
await _dbRdy;
await dbRdy;
for (final metadata in metadatas) {
await saveMetadata(metadata);
}
}

@override
Future<Nip05?> loadNip05(String pubKey) async {
await _dbRdy;
await dbRdy;
final nip05Box = _objectBox.store.box<DbNip05>();
final existingNip05 = nip05Box
.query(DbNip05_.pubKey.equals(pubKey))
Expand All @@ -239,7 +239,7 @@ class DbObjectBox implements CacheManager {

@override
Future<List<Nip05?>> loadNip05s(List<String> pubKeys) async {
await _dbRdy;
await dbRdy;
final nip05Box = _objectBox.store.box<DbNip05>();
final existingNip05s = nip05Box
.query(DbNip05_.pubKey.oneOf(pubKeys))
Expand All @@ -258,7 +258,7 @@ class DbObjectBox implements CacheManager {

@override
Future<UserRelayList?> loadUserRelayList(String pubKey) async {
await _dbRdy;
await dbRdy;
final userRelayListBox = _objectBox.store.box<DbUserRelayList>();
final existingUserRelayList = userRelayListBox
.query(DbUserRelayList_.pubKey.equals(pubKey))
Expand All @@ -273,7 +273,7 @@ class DbObjectBox implements CacheManager {

@override
Future<void> removeAllNip05s() async {
await _dbRdy;
await dbRdy;
final nip05Box = _objectBox.store.box<DbNip05>();
nip05Box.removeAll();
}
Expand Down Expand Up @@ -328,7 +328,7 @@ class DbObjectBox implements CacheManager {

@override
Future<void> saveNip05(Nip05 nip05) async {
await _dbRdy;
await dbRdy;
final nip05Box = _objectBox.store.box<DbNip05>();
final existingNip05 = nip05Box
.query(DbNip05_.pubKey.equals(nip05.pubKey))
Expand All @@ -343,7 +343,7 @@ class DbObjectBox implements CacheManager {

@override
Future<void> saveNip05s(List<Nip05> nip05s) async {
await _dbRdy;
await dbRdy;
final nip05Box = _objectBox.store.box<DbNip05>();
nip05Box.putMany(nip05s.map((n) => DbNip05.fromNdk(n)).toList());
}
Expand All @@ -356,7 +356,7 @@ class DbObjectBox implements CacheManager {

@override
Future<void> saveUserRelayList(UserRelayList userRelayList) async {
await _dbRdy;
await dbRdy;
final userRelayListBox = _objectBox.store.box<DbUserRelayList>();
final existingUserRelayList = userRelayListBox
.query(DbUserRelayList_.pubKey.equals(userRelayList.pubKey))
Expand Down
13 changes: 11 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -57,6 +59,13 @@ Future<void> 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);
Expand Down
15 changes: 15 additions & 0 deletions lib/presentation_layer/providers/db_ndk_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:ndk/ndk.dart';
import 'package:riverpod/riverpod.dart';

final dbNdkProvider =
StateNotifierProvider<DbProviderNotifier, CacheManager?>((ref) {
return DbProviderNotifier();
});

class DbProviderNotifier extends StateNotifier<CacheManager?> {
DbProviderNotifier() : super(null);

void setDB(CacheManager newDb) {
state = newDb;
}
}
8 changes: 0 additions & 8 deletions lib/presentation_layer/providers/db_object_box_provider.dart

This file was deleted.

9 changes: 3 additions & 6 deletions lib/presentation_layer/providers/ndk_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ndk>((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,
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit f220fa1

Please sign in to comment.