Files
buzz/mobile/lib/features/profile/user_status_provider.dart
klopez4212 b30f1f6129 Polish mobile profiles, DMs, and sheets (#5401)
## Summary
- add poster-first, tap-to-toggle animated avatars on profile surfaces
while preserving transparent/static behavior elsewhere
- align mobile DM headers, membership actions, and invisible agent
recipient addressing with established desktop semantics
- polish titled sheets and status editing, preserve native iOS sheet
corners, and batch relay reads to improve review-build responsiveness

## Snapshots

<table>
  <tr>
    <th>Profile avatar</th>
    <th>Agent DM header and composer</th>
  </tr>
  <tr>
<td><img
src="https://raw.githubusercontent.com/block/buzz/e8de7495451dbe1a393ab43c5e204ca7425f2ba5/pr-5401--profile-avatar.png"
width="360" alt="Mobile profile settings with animated avatar
surface"></td>
<td><img
src="https://raw.githubusercontent.com/block/buzz/e8de7495451dbe1a393ab43c5e204ca7425f2ba5/pr-5401--agent-dm.png"
width="360" alt="Agent direct message with masked presence and normal
composer"></td>
  </tr>
  <tr>
    <th>Members sheet</th>
    <th>Status editor</th>
  </tr>
  <tr>
<td><img
src="https://raw.githubusercontent.com/block/buzz/e8de7495451dbe1a393ab43c5e204ca7425f2ba5/pr-5401--members-sheet.png"
width="360" alt="Members bottom sheet with centered title and padded
content"></td>
<td><img
src="https://raw.githubusercontent.com/block/buzz/e8de7495451dbe1a393ab43c5e204ca7425f2ba5/pr-5401--status-sheet.png"
width="360" alt="Status editor bottom sheet with duration and quick
statuses"></td>
  </tr>
  <tr>
    <th colspan="2">Switch Community</th>
  </tr>
  <tr>
<td colspan="2" align="center"><img
src="https://raw.githubusercontent.com/block/buzz/e8de7495451dbe1a393ab43c5e204ca7425f2ba5/pr-5401--switch-community.png"
width="720" alt="Switch Community bottom sheet with centered title and
aligned Edit action"></td>
  </tr>
</table>

## Validation
- `just mobile-check`
- `just mobile-test` (1,283 tests)
- installed and reviewed isolated debug builds on iPhone and Pixel

---------

Signed-off-by: kenny lopez <klopez4212@gmail.com>
Signed-off-by: Princess Donut <b238ea756dee4d98afa5883fc7f1de61eeabe65bf700e3a5a5a80db5e42e2c2b@buzz.block.builderlab.xyz>
Signed-off-by: Kenny Lopez <klopez4212@gmail.com>
Signed-off-by: Wes <wesbillman@users.noreply.github.com>
Co-authored-by: Princess Donut <b238ea756dee4d98afa5883fc7f1de61eeabe65bf700e3a5a5a80db5e42e2c2b@buzz.block.builderlab.xyz>
Co-authored-by: Wes <wesbillman@users.noreply.github.com>
Co-authored-by: Carl <c7ebe626f000404285d3686e1dc74cc07cc60a9754a150041ba132e14bd3e2ec@buzz.block.builderlab.xyz>
2026-08-13 20:16:04 -07:00

179 lines
5.2 KiB
Dart

import 'dart:async';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:nostr/nostr.dart' as nostr;
import '../../shared/relay/relay.dart';
import 'user_status.dart';
import 'user_status_cache_provider.dart';
/// Manages the current user's own NIP-38 status (kind:30315, d=general).
///
/// Fetches the existing status on build and provides [setStatus] / [clearStatus]
/// for publishing. Publishes via WebSocket (triggers fan-out). No heartbeat
/// needed — user status events are parameterised replaceable, not ephemeral.
class UserStatusNotifier extends AsyncNotifier<UserStatus?> {
Timer? _expirationTimer;
@override
Future<UserStatus?> build() async {
ref.watch(relayClientProvider);
ref.watch(relaySessionProvider);
ref.onDispose(() {
_expirationTimer?.cancel();
_expirationTimer = null;
});
final status = await _fetch();
_scheduleExpiration(status);
return status;
}
Future<UserStatus?> _fetch() async {
final config = ref.read(relayConfigProvider);
final nsec = config.nsec;
if (nsec == null || nsec.isEmpty) return null;
String pubkey;
try {
final privkeyHex = nostr.Nip19.decode(payload: nsec).data;
final keyPair = nostr.Keys(privkeyHex);
pubkey = keyPair.public.toLowerCase();
} catch (_) {
return null;
}
final sessionState = ref.read(relaySessionProvider);
if (sessionState.status != SessionStatus.connected) return null;
try {
final session = ref.read(relaySessionProvider.notifier);
final events = await session.fetchHistory(
NostrFilter(
kinds: const [EventKind.userStatus],
authors: [pubkey],
tags: const {
'#d': ['general'],
},
limit: 1,
),
);
if (events.isEmpty) return null;
// Pick the most recent event.
final latest = events.reduce(
(a, b) => a.createdAt >= b.createdAt ? a : b,
);
final status = UserStatus.fromEvent(latest);
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
return status.isEmpty || status.isExpiredAt(now) ? null : status;
} catch (_) {
return null;
}
}
Future<void> setStatus(
String text,
String emoji, {
DateTime? expiresAt,
}) async {
final trimmed = text.trim();
final config = ref.read(relayConfigProvider);
final nsec = config.nsec;
if (nsec == null || nsec.isEmpty) return;
final tags = <List<String>>[
['d', 'general'],
];
if (emoji.isNotEmpty) {
tags.add(['emoji', emoji]);
}
if (expiresAt != null) {
tags.add(['expiration', '${expiresAt.millisecondsSinceEpoch ~/ 1000}']);
}
final privkeyHex = nostr.Nip19.decode(payload: nsec).data;
final event = nostr.Event.from(
kind: EventKind.userStatus,
content: trimmed,
tags: tags,
secretKey: privkeyHex,
verify: false,
);
final session = ref.read(relaySessionProvider.notifier);
await session.publish(NostrEvent.fromJson(event.toMap()));
// Optimistic update: update own state immediately.
final newStatus = (trimmed.isNotEmpty || emoji.isNotEmpty)
? UserStatus(
text: trimmed,
emoji: emoji,
updatedAt: DateTime.now().millisecondsSinceEpoch ~/ 1000,
expiresAt: expiresAt == null
? null
: expiresAt.millisecondsSinceEpoch ~/ 1000,
)
: null;
state = AsyncValue.data(newStatus);
_scheduleExpiration(newStatus);
// Also update the shared cache so other UI reads stay consistent.
final keyPair = nostr.Keys(privkeyHex);
final pubkey = keyPair.public.toLowerCase();
ref.read(userStatusCacheProvider.notifier).updateStatus(pubkey, newStatus);
}
Future<void> clearStatus() => setStatus('', '');
void _scheduleExpiration(UserStatus? status) {
_expirationTimer?.cancel();
_expirationTimer = null;
final deadline = status?.expirationDateTime;
if (deadline == null) return;
final remaining = deadline.difference(DateTime.now());
_expirationTimer = Timer(
remaining.isNegative ? Duration.zero : remaining,
_expireCurrentStatus,
);
}
void _expireCurrentStatus() {
final current = state.asData?.value;
if (current == null) return;
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
if (!current.isExpiredAt(now)) {
// Timer callbacks can land just before the Unix-second boundary.
_expirationTimer = Timer(
const Duration(milliseconds: 50),
_expireCurrentStatus,
);
return;
}
state = const AsyncValue.data(null);
final pubkey = _currentPubkey();
if (pubkey != null) {
ref.read(userStatusCacheProvider.notifier).updateStatus(pubkey, null);
}
}
String? _currentPubkey() {
final nsec = ref.read(relayConfigProvider).nsec;
if (nsec == null || nsec.isEmpty) return null;
try {
final privkeyHex = nostr.Nip19.decode(payload: nsec).data;
return nostr.Keys(privkeyHex).public.toLowerCase();
} catch (_) {
return null;
}
}
}
final userStatusProvider =
AsyncNotifierProvider<UserStatusNotifier, UserStatus?>(
UserStatusNotifier.new,
);