tane/apps/app_seeds/lib/main.dart
vjrj 73ee98206f feat(chat): app-wide inbox listener so messages arrive without opening the chat
Reception only ran inside an open ChatScreen for a known peer, so a first
message from a new peer was invisible: nothing in local storage -> nothing
in the inbox list -> you never opened the chat -> you never subscribed.

Add InboxService: one long-lived NIP-17 inbox subscription for the whole
app (foreground), persisting every incoming message to MessageStore and
firing a 'changes' signal the inbox list live-reloads on. Reconnects when
the network returns; degrades to nothing offline. Started from main when a
social identity exists.

Make MessageStore.append idempotent (dedup by sender+timestamp+text) and
serialized behind a write lock — the global listener and an open chat's own
subscription now write the same conversation concurrently and relays
re-deliver stored gift wraps on every resubscribe. Tests for both.

Known trade-offs (follow-ups): foreground-only (no push yet); each of
InboxService/ChatScreen/MarketScreen opens its own RelayPool (a shared
connection is a later optimization).
2026-07-10 16:53:03 +02:00

60 lines
2.2 KiB
Dart

import 'dart:async';
import 'package:flutter/widgets.dart';
import 'app.dart';
import 'data/species_repository.dart';
import 'data/variety_repository.dart';
import 'di/injector.dart';
import 'i18n/strings.g.dart';
import 'services/auto_backup_service.dart';
import 'services/coarse_location.dart';
import 'services/inbox_service.dart';
import 'services/locale_store.dart';
import 'services/message_store.dart';
import 'services/offer_outbox.dart';
import 'services/onboarding_store.dart';
import 'services/profile_cache.dart';
import 'services/profile_store.dart';
import 'services/social_service.dart';
import 'services/social_settings.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// Start from the device language, then let an explicit in-app choice win —
// it's the only reliable way to reach languages the OS picker doesn't offer
// (e.g. Asturian). Restored after DI, since the store lives in the keystore.
LocaleSettings.useDeviceLocaleSync();
await configureDependencies();
final savedLocale = await getIt<LocaleStore>().saved();
if (savedLocale != null) LocaleSettings.setLocaleSync(savedLocale);
final onboarding = getIt<OnboardingStore>();
final inbox =
getIt.isRegistered<InboxService>() ? getIt<InboxService>() : null;
// Start listening for incoming private messages app-wide (foreground), so
// they arrive and populate the inbox list even before their chat is opened.
if (inbox != null) unawaited(inbox.start());
runApp(
TranslationProvider(
child: TaneApp(
repository: getIt<VarietyRepository>(),
species: getIt<SpeciesRepository>(),
onboarding: onboarding,
social: getIt.isRegistered<SocialService>()
? getIt<SocialService>()
: null,
socialSettings: getIt<SocialSettings>(),
location: const GeolocatorCoarseLocation(),
outbox: getIt<OfferOutbox>(),
messageStore: getIt<MessageStore>(),
profileStore: getIt<ProfileStore>(),
profileCache: getIt<ProfileCache>(),
inbox: inbox,
showIntro: !await onboarding.introSeen(),
autoBackup: getIt.isRegistered<AutoBackupService>()
? getIt<AutoBackupService>()
: null,
),
),
);
}