pull refresh on session screen

master
gitea 2 months ago
parent 947fb667cf
commit 49dd0fdaf1

@ -382,6 +382,50 @@ class SessionService {
await _loadPreferredRelaysIfAvailable(); await _loadPreferredRelaysIfAvailable();
} }
/// Refreshes the current user's Nostr profile and preferred relays.
///
/// Re-fetches the profile from relays and reloads preferred relays from NIP-05.
///
/// Throws [SessionException] if refresh fails or if no user is logged in.
Future<void> refreshNostrProfile() async {
if (_currentUser == null) {
throw SessionException('No user logged in.');
}
if (_nostrService == null) {
throw SessionException('Nostr service not available');
}
// Only refresh if user has Nostr profile (logged in with Nostr)
if (_currentUser!.nostrProfile == null && _currentUser!.nostrPrivateKey == null) {
throw SessionException('User is not logged in with Nostr');
}
try {
// Re-fetch profile from relays
NostrProfile? profile;
try {
profile = await _nostrService!.fetchProfile(_currentUser!.id);
} catch (e) {
debugPrint('Warning: Failed to refresh Nostr profile: $e');
// Continue without profile update - offline-first behavior
}
// Update user with refreshed profile
if (profile != null) {
_currentUser = _currentUser!.copyWith(nostrProfile: profile);
}
// Reload preferred relays from NIP-05 if available
await _loadPreferredRelaysIfAvailable();
} catch (e) {
if (e is SessionException) {
rethrow;
}
throw SessionException('Failed to refresh Nostr profile: $e');
}
}
/// Internal method to load preferred relays from NIP-05 if available. /// Internal method to load preferred relays from NIP-05 if available.
Future<void> _loadPreferredRelaysIfAvailable() async { Future<void> _loadPreferredRelaysIfAvailable() async {
if (_currentUser == null || _nostrService == null) { if (_currentUser == null || _nostrService == null) {

@ -267,6 +267,33 @@ class _SessionScreenState extends State<SessionScreen> {
} }
} }
Future<void> _handleRefresh() async {
if (widget.sessionService == null) return;
try {
await widget.sessionService!.refreshNostrProfile();
if (mounted) {
setState(() {});
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Session data refreshed'),
duration: Duration(seconds: 2),
),
);
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Failed to refresh: ${e.toString().replaceAll('SessionException: ', '')}'),
backgroundColor: Colors.red,
duration: const Duration(seconds: 3),
),
);
}
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final isLoggedIn = widget.sessionService?.isLoggedIn ?? false; final isLoggedIn = widget.sessionService?.isLoggedIn ?? false;
@ -278,11 +305,20 @@ class _SessionScreenState extends State<SessionScreen> {
), ),
body: _isLoading body: _isLoading
? const Center(child: CircularProgressIndicator()) ? const Center(child: CircularProgressIndicator())
: SingleChildScrollView( : _buildRefreshableContent(isLoggedIn, currentUser),
padding: const EdgeInsets.all(16), );
child: Column( }
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [ Widget _buildRefreshableContent(bool isLoggedIn, currentUser) {
final canRefresh = isLoggedIn &&
(currentUser?.nostrProfile != null || currentUser?.nostrPrivateKey != null);
final content = SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (isLoggedIn && currentUser != null) ...[ if (isLoggedIn && currentUser != null) ...[
Card( Card(
child: Padding( child: Padding(
@ -613,9 +649,17 @@ class _SessionScreenState extends State<SessionScreen> {
), ),
], ],
], ],
), ),
), );
);
if (canRefresh) {
return RefreshIndicator(
onRefresh: _handleRefresh,
child: content,
);
} else {
return content;
}
} }
} }

Loading…
Cancel
Save

Powered by TurnKey Linux.