Files
buzz/mobile/lib/features/profile/profile_provider.dart
ff0b7982f1 Polish mobile top navigation (#4778)
## Summary

- Polish mobile Home, Activity, Search, and Settings navigation chrome.
- Add progressive Buzz gradients/frost, aligned theme colors, dividers,
typography, and section spacing.
- Refine Search and Settings motion, including automatic keyboard focus
on search activation.

<img width="630" height="1368"
alt="C78FA3CE-F2B3-45F2-B9F5-7EA7500778CC"
src="https://github.com/user-attachments/assets/5935514b-d894-4010-80dd-a938363fee93"
/>
<img width="630" height="1368"
alt="5C058A73-1879-476A-881C-531ACC256D84"
src="https://github.com/user-attachments/assets/5266c841-17ee-49e8-9841-b06d84f4195f"
/>
<img width="630" height="1368"
alt="3F50ADB7-9BDA-4A8D-A81E-20560C3B9EA6"
src="https://github.com/user-attachments/assets/f615f61e-9e96-4bce-b261-ae5ec54db872"
/>
<img width="630" height="1368"
alt="35ECE741-01F3-4B79-80C5-1DDD447121A7"
src="https://github.com/user-attachments/assets/b5145186-9884-44eb-8ebc-f3303831c0a4"
/>

## Validation


- `flutter analyze`
- Focused Home, Activity, Channels, Search, theme, and footer widget
tests
- Full pre-push checks, including mobile tests, desktop checks, and
Tauri checks
- On-device iPhone review during the visual polish pass

---------

Signed-off-by: kenny lopez <klopez4212@gmail.com>
Signed-off-by: npub1glqcqfjxdens59scl477pmejh8lht4hqkhx0y4w38jxr6e6w6y2sm29y4e <47c18026466e670a1618fd7de0ef32b9ff75d6e0b5ccf255d13c8c3d674ed115@buzz.block.builderlab.xyz>
Signed-off-by: Code Reviewer <037593536284cf40e221c96c931e9877d4166d54f6bb84e5341a86d7fd5d05a4@buzz.block.builderlab.xyz>
Signed-off-by: Kenny Lopez <klopez4212@gmail.com>
Co-authored-by: npub1glqcqfjxdens59scl477pmejh8lht4hqkhx0y4w38jxr6e6w6y2sm29y4e <47c18026466e670a1618fd7de0ef32b9ff75d6e0b5ccf255d13c8c3d674ed115@buzz.block.builderlab.xyz>
Co-authored-by: Code Reviewer <037593536284cf40e221c96c931e9877d4166d54f6bb84e5341a86d7fd5d05a4@buzz.block.builderlab.xyz>
2026-08-05 19:01:20 +01:00

169 lines
5.0 KiB
Dart

import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import '../../shared/relay/relay.dart';
import '../../shared/theme/theme.dart';
import 'user_profile.dart';
/// The current user's profile (kind:0 metadata) loaded over the relay
/// WebSocket. Returns null when no nsec is configured or when the user has
/// not yet published a profile.
class ProfileNotifier extends AsyncNotifier<UserProfile?> {
@override
Future<UserProfile?> build() {
ref.watch(relayConfigProvider);
ref.watch(relaySessionProvider);
return _fetch();
}
Future<UserProfile?> _fetch() async {
final myPk = ref.read(myPubkeyProvider);
if (myPk == null) return null;
final session = ref.read(relaySessionProvider.notifier);
final events = await session.fetchHistory(NostrFilters.profile(myPk));
if (events.isEmpty) return null;
final data = ProfileData.fromEvent(events.first);
return UserProfile(
pubkey: data.pubkey,
displayName: data.displayName,
avatarUrl: data.avatarUrl,
about: data.about,
nip05Handle: data.nip05,
);
}
Future<void> refresh() async {
state = await AsyncValue.guard(_fetch);
}
}
final profileProvider = AsyncNotifierProvider<ProfileNotifier, UserProfile?>(
ProfileNotifier.new,
);
/// Presence status for the current user.
///
/// Sends a heartbeat every 60s while the app is active by publishing a
/// kind:20001 presence event over the relay WebSocket. Watches
/// [appLifecycleProvider] to send "away" when backgrounded.
class PresenceNotifier extends AsyncNotifier<String> {
static const _heartbeatInterval = Duration(seconds: 60);
static const _preferenceKeyPrefix = 'buzz_presence_preference_';
Timer? _heartbeatTimer;
String? _preferencePubkey;
String? _manualPresence;
@override
Future<String> build() {
ref.watch(relaySessionProvider);
final pubkey = ref.watch(myPubkeyProvider)?.toLowerCase();
if (_preferencePubkey != pubkey) {
_preferencePubkey = pubkey;
final stored = pubkey == null
? null
: ref
.read(savedPrefsProvider)
.getString('$_preferenceKeyPrefix$pubkey');
_manualPresence = stored == 'away' || stored == 'offline' ? stored : null;
}
final lifecycle = ref.watch(appLifecycleProvider);
ref.onDispose(() {
_heartbeatTimer?.cancel();
_heartbeatTimer = null;
});
final manualPresence = _manualPresence;
if (manualPresence != null) {
_heartbeatTimer?.cancel();
_heartbeatTimer = null;
return _setPresence(manualPresence);
}
if (lifecycle == AppLifecycleState.resumed) {
_startHeartbeat();
return _setPresence('online');
} else if (lifecycle == AppLifecycleState.paused ||
lifecycle == AppLifecycleState.detached) {
_heartbeatTimer?.cancel();
_heartbeatTimer = null;
return _setPresence('away');
}
// Default: we don't know. Reflect the most recent state we set, or
// 'offline' if never set.
return Future.value('offline');
}
void _startHeartbeat() {
_heartbeatTimer?.cancel();
_heartbeatTimer = Timer.periodic(_heartbeatInterval, (_) {
_setPresence('online');
});
}
/// Updates the current user's presence preference and publishes it.
///
/// Online restores automatic lifecycle-driven presence. Away and Offline
/// remain selected until the user chooses another value.
Future<void> setPresence(String status) async {
if (status != 'online' && status != 'away' && status != 'offline') return;
_manualPresence = status == 'online' ? null : status;
final pubkey = ref.read(myPubkeyProvider)?.toLowerCase();
if (pubkey != null) {
await ref
.read(savedPrefsProvider)
.setString('$_preferenceKeyPrefix$pubkey', _manualPresence ?? 'auto');
}
if (_manualPresence == null &&
ref.read(appLifecycleProvider) == AppLifecycleState.resumed) {
_startHeartbeat();
} else {
_heartbeatTimer?.cancel();
_heartbeatTimer = null;
}
state = AsyncData(status);
await _setPresence(status);
}
/// Publish a kind:20001 presence event. Returns the requested status
/// optimistically — failures are silently absorbed and the next heartbeat
/// will retry.
Future<String> _setPresence(String status) async {
final sessionState = ref.read(relaySessionProvider);
if (sessionState.status != SessionStatus.connected) return status;
final config = ref.read(relayConfigProvider);
final relay = SignedEventRelay(
session: ref.read(relaySessionProvider.notifier),
nsec: config.nsec,
);
try {
await relay.submit(
kind: EventKind.presenceUpdate,
content: status,
tags: const [],
);
} catch (_) {
// Heartbeat will retry.
}
return status;
}
Future<void> refresh() async {
// No-op: presence is driven by heartbeats and lifecycle, not pulled.
}
}
final presenceProvider = AsyncNotifierProvider<PresenceNotifier, String>(
PresenceNotifier.new,
);