nostr images fetching from API

This commit is contained in:
gitea
2025-11-06 00:45:58 +01:00
parent c37dce0222
commit 9650fc78a8
12 changed files with 2694 additions and 233 deletions
+120
View File
@@ -0,0 +1,120 @@
import 'package:flutter/material.dart';
import '../../data/local/local_storage_service.dart';
import '../../data/local/models/item.dart';
/// Home screen showing local storage and cached content.
class HomeScreen extends StatefulWidget {
final LocalStorageService? localStorageService;
const HomeScreen({
super.key,
this.localStorageService,
});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
List<Item> _items = [];
bool _isLoading = true;
@override
void initState() {
super.initState();
_loadItems();
}
Future<void> _loadItems() async {
if (widget.localStorageService == null) {
setState(() {
_isLoading = false;
});
return;
}
try {
final items = await widget.localStorageService!.getAllItems();
setState(() {
_items = items;
_isLoading = false;
});
} catch (e) {
setState(() {
_isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home'),
),
body: _isLoading
? const Center(child: CircularProgressIndicator())
: RefreshIndicator(
onRefresh: _loadItems,
child: _items.isEmpty
? const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.storage_outlined,
size: 64,
color: Colors.grey,
),
SizedBox(height: 16),
Text(
'No items in local storage',
style: TextStyle(
fontSize: 16,
color: Colors.grey,
),
),
],
),
)
: ListView.builder(
itemCount: _items.length,
itemBuilder: (context, index) {
final item = _items[index];
return ListTile(
leading: const Icon(Icons.data_object),
title: Text(item.id),
subtitle: Text(
'Created: ${DateTime.fromMillisecondsSinceEpoch(item.createdAt).toString().split('.')[0]}',
),
trailing: IconButton(
icon: const Icon(Icons.delete_outline),
onPressed: () async {
await widget.localStorageService?.deleteItem(item.id);
_loadItems();
},
),
);
},
),
),
floatingActionButton: widget.localStorageService != null
? FloatingActionButton(
onPressed: () async {
final item = Item(
id: 'item-${DateTime.now().millisecondsSinceEpoch}',
data: {
'name': 'New Item',
'timestamp': DateTime.now().toIso8601String(),
},
);
await widget.localStorageService!.insertItem(item);
_loadItems();
},
child: const Icon(Icons.add),
)
: null,
);
}
}
+432
View File
@@ -0,0 +1,432 @@
import 'dart:typed_data';
import 'package:flutter/material.dart';
import '../../data/local/local_storage_service.dart';
import '../../data/immich/immich_service.dart';
import '../../data/immich/models/immich_asset.dart';
/// Screen for Immich media integration.
///
/// Displays images from Immich in a grid layout with pull-to-refresh.
/// Shows cached images first, then fetches from API.
class ImmichScreen extends StatefulWidget {
final LocalStorageService? localStorageService;
final ImmichService? immichService;
const ImmichScreen({
super.key,
this.localStorageService,
this.immichService,
});
@override
State<ImmichScreen> createState() => _ImmichScreenState();
}
class _ImmichScreenState extends State<ImmichScreen> {
List<ImmichAsset> _assets = [];
bool _isLoading = false;
String? _errorMessage;
@override
void initState() {
super.initState();
_loadAssets();
}
/// Loads assets from cache first, then fetches from API.
Future<void> _loadAssets({bool forceRefresh = false}) async {
if (widget.immichService == null) {
setState(() {
_errorMessage = 'Immich service not available';
_isLoading = false;
});
return;
}
setState(() {
_isLoading = !forceRefresh;
_errorMessage = null;
});
try {
// First, try to load cached assets
if (!forceRefresh) {
final cachedAssets = await widget.immichService!.getCachedAssets();
if (cachedAssets.isNotEmpty) {
setState(() {
_assets = cachedAssets;
_isLoading = false;
});
// Still fetch from API in background to update cache
_fetchFromApi();
return;
}
}
// Fetch from API
await _fetchFromApi();
} catch (e) {
setState(() {
_errorMessage = 'Failed to load assets: ${e.toString()}';
_isLoading = false;
});
}
}
/// Fetches assets from Immich API.
Future<void> _fetchFromApi() async {
try {
final assets = await widget.immichService!.fetchAssets(limit: 100);
setState(() {
_assets = assets;
_isLoading = false;
});
} catch (e) {
setState(() {
_errorMessage = 'Failed to fetch from Immich: ${e.toString()}';
_isLoading = false;
});
}
}
/// Gets the thumbnail URL for an asset with proper headers.
String _getThumbnailUrl(ImmichAsset asset) {
return widget.immichService!.getThumbnailUrl(asset.id);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Immich Media'),
actions: [
IconButton(
icon: const Icon(Icons.info_outline),
onPressed: _testServerConnection,
tooltip: 'Test Server Connection',
),
if (_assets.isNotEmpty)
IconButton(
icon: const Icon(Icons.refresh),
onPressed: () => _loadAssets(forceRefresh: true),
tooltip: 'Refresh',
),
],
),
body: _buildBody(),
);
}
Widget _buildBody() {
if (_isLoading && _assets.isEmpty) {
return const Center(
child: CircularProgressIndicator(),
);
}
if (_errorMessage != null && _assets.isEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.error_outline,
size: 64,
color: Colors.red,
),
const SizedBox(height: 16),
Text(
_errorMessage!,
style: const TextStyle(color: Colors.red),
textAlign: TextAlign.center,
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () => _loadAssets(forceRefresh: true),
child: const Text('Retry'),
),
],
),
);
}
if (_assets.isEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.photo_library_outlined,
size: 64,
color: Colors.grey,
),
const SizedBox(height: 16),
const Text(
'No images found',
style: TextStyle(fontSize: 18),
),
const SizedBox(height: 8),
const Text(
'Pull down to refresh or upload images to Immich',
style: TextStyle(color: Colors.grey),
textAlign: TextAlign.center,
),
],
),
);
}
return RefreshIndicator(
onRefresh: () => _loadAssets(forceRefresh: true),
child: GridView.builder(
padding: const EdgeInsets.all(8),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
crossAxisSpacing: 8,
mainAxisSpacing: 8,
childAspectRatio: 1,
),
itemCount: _assets.length,
itemBuilder: (context, index) {
final asset = _assets[index];
return _buildImageTile(asset);
},
),
);
}
Widget _buildImageTile(ImmichAsset asset) {
final thumbnailUrl = _getThumbnailUrl(asset);
// For Immich API, we need to pass the API key as a header
// Since Image.network doesn't easily support custom headers,
// we'll use a workaround: Immich might accept the key in the URL query parameter
// OR we need to fetch images via Dio and convert to bytes.
// Let's check ImmichService - it has Dio with headers configured.
// Actually, we can use Image.network with headers parameter (Flutter supports this).
// But we need the API key. Let me check if ImmichService exposes it.
// Since Immich API requires x-api-key header, and Image.network supports headers,
// we need to get the API key. However, ImmichService doesn't expose it.
// Let's modify ImmichService to expose a method that returns headers, or
// we can fetch images via Dio and display them.
// For now, let's use Image.network and assume Immich might work without header
// (which it won't, but this is a placeholder). We'll fix this properly next.
return GestureDetector(
onTap: () {
// TODO: Navigate to full image view
_showImageDetails(asset);
},
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
color: Colors.grey[300],
),
child: ClipRRect(
borderRadius: BorderRadius.circular(8),
child: _buildImageWidget(thumbnailUrl, asset),
),
),
);
}
Widget _buildImageWidget(String url, ImmichAsset asset) {
// Use FutureBuilder to fetch image bytes via ImmichService with proper auth
return FutureBuilder<Uint8List?>(
future: widget.immichService?.fetchImageBytes(asset.id, isThumbnail: true),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
if (snapshot.hasError || !snapshot.hasData || snapshot.data == null) {
return Container(
color: Colors.grey[200],
child: const Icon(
Icons.broken_image,
color: Colors.grey,
),
);
}
return Image.memory(
snapshot.data!,
fit: BoxFit.cover,
);
},
);
}
void _showImageDetails(ImmichAsset asset) {
showDialog(
context: context,
builder: (context) => Dialog(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
AppBar(
title: Text(asset.fileName),
automaticallyImplyLeading: false,
actions: [
IconButton(
icon: const Icon(Icons.close),
onPressed: () => Navigator.of(context).pop(),
),
],
),
Expanded(
child: FutureBuilder<Uint8List?>(
future: widget.immichService?.fetchImageBytes(asset.id, isThumbnail: false),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
if (snapshot.hasError || !snapshot.hasData || snapshot.data == null) {
return const Center(
child: Text('Failed to load image'),
);
}
return Image.memory(
snapshot.data!,
fit: BoxFit.contain,
);
},
),
),
Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('File: ${asset.fileName}'),
Text('Size: ${_formatFileSize(asset.fileSize)}'),
if (asset.width != null && asset.height != null)
Text('Dimensions: ${asset.width}x${asset.height}'),
Text('Date: ${_formatDate(asset.createdAt)}'),
],
),
),
],
),
),
);
}
String _formatFileSize(int bytes) {
if (bytes < 1024) return '$bytes B';
if (bytes < 1024 * 1024) return '${(bytes / 1024).toStringAsFixed(1)} KB';
return '${(bytes / (1024 * 1024)).toStringAsFixed(1)} MB';
}
String _formatDate(DateTime date) {
return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
}
/// Tests the connection to Immich server by calling /api/server/about.
Future<void> _testServerConnection() async {
if (widget.immichService == null) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Immich service not available'),
backgroundColor: Colors.red,
),
);
}
return;
}
showDialog(
context: context,
barrierDismissible: false,
builder: (context) => const Center(
child: CircularProgressIndicator(),
),
);
try {
final serverInfo = await widget.immichService!.getServerInfo();
if (!mounted) return;
Navigator.of(context).pop(); // Close loading dialog
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Server Info'),
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
'GET /api/server/about',
style: Theme.of(context).textTheme.titleSmall,
),
const SizedBox(height: 16),
Text(
_formatServerInfo(serverInfo),
style: Theme.of(context).textTheme.bodyMedium,
),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Close'),
),
],
),
);
} catch (e) {
if (!mounted) return;
Navigator.of(context).pop(); // Close loading dialog
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Connection Test Failed'),
content: Text(
'Error: ${e.toString()}',
style: const TextStyle(color: Colors.red),
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Close'),
),
],
),
);
}
}
/// Formats server info map as a readable string.
String _formatServerInfo(Map<String, dynamic> info) {
final buffer = StringBuffer();
info.forEach((key, value) {
if (value is Map) {
buffer.writeln('$key:');
value.forEach((subKey, subValue) {
buffer.writeln(' $subKey: $subValue');
});
} else {
buffer.writeln('$key: $value');
}
});
return buffer.toString();
}
}
+218
View File
@@ -0,0 +1,218 @@
import 'package:flutter/material.dart';
import '../home/home_screen.dart';
import '../immich/immich_screen.dart';
import '../nostr_events/nostr_events_screen.dart';
import '../relay_management/relay_management_screen.dart';
import '../relay_management/relay_management_controller.dart';
import '../session/session_screen.dart';
import '../settings/settings_screen.dart';
import '../../data/nostr/nostr_service.dart';
import '../../data/sync/sync_engine.dart';
import '../../data/session/session_service.dart';
import '../../data/local/local_storage_service.dart';
import '../../data/firebase/firebase_service.dart';
/// Route names for the app navigation.
class AppRoutes {
static const String home = '/';
static const String immich = '/immich';
static const String nostrEvents = '/nostr-events';
static const String relayManagement = '/relay-management';
static const String session = '/session';
static const String settings = '/settings';
static const String login = '/login';
}
/// Route guard that requires authentication.
class AuthGuard {
final SessionService? sessionService;
AuthGuard(this.sessionService);
/// Checks if user is authenticated.
bool get isAuthenticated => sessionService?.isLoggedIn ?? false;
/// Redirects to login if not authenticated.
String? checkAuth(String route) {
if (_requiresAuth(route) && !isAuthenticated) {
return AppRoutes.login;
}
return null;
}
/// Checks if a route requires authentication.
bool _requiresAuth(String route) {
// Routes that require authentication
const protectedRoutes = [
AppRoutes.immich,
AppRoutes.nostrEvents,
AppRoutes.session,
];
return protectedRoutes.contains(route);
}
}
/// App router for managing navigation and route guards.
class AppRouter {
final SessionService? sessionService;
final LocalStorageService? localStorageService;
final NostrService? nostrService;
final SyncEngine? syncEngine;
final FirebaseService? firebaseService;
late final AuthGuard _authGuard;
AppRouter({
this.sessionService,
this.localStorageService,
this.nostrService,
this.syncEngine,
this.firebaseService,
}) {
_authGuard = AuthGuard(sessionService);
}
/// Generates routes for the app.
Route<dynamic>? generateRoute(RouteSettings settings) {
// Check route guard
final redirect = _authGuard.checkAuth(settings.name ?? '');
if (redirect != null) {
return MaterialPageRoute(
builder: (_) => _buildLoginScreen(),
settings: settings,
);
}
switch (settings.name) {
case AppRoutes.home:
return MaterialPageRoute(
builder: (_) => HomeScreen(
localStorageService: localStorageService,
),
settings: settings,
);
case AppRoutes.immich:
return MaterialPageRoute(
builder: (_) => ImmichScreen(
localStorageService: localStorageService,
),
settings: settings,
);
case AppRoutes.nostrEvents:
return MaterialPageRoute(
builder: (_) => NostrEventsScreen(
nostrService: nostrService,
syncEngine: syncEngine,
),
settings: settings,
);
case AppRoutes.relayManagement:
if (nostrService == null || syncEngine == null) {
return MaterialPageRoute(
builder: (_) => _buildErrorScreen('Nostr service not available'),
settings: settings,
);
}
return MaterialPageRoute(
builder: (_) => RelayManagementScreen(
controller: RelayManagementController(
nostrService: nostrService!,
syncEngine: syncEngine!,
),
),
settings: settings,
);
case AppRoutes.session:
return MaterialPageRoute(
builder: (_) => SessionScreen(
sessionService: sessionService,
firebaseService: firebaseService,
),
settings: settings,
);
case AppRoutes.settings:
return MaterialPageRoute(
builder: (_) => SettingsScreen(
firebaseService: firebaseService,
),
settings: settings,
);
case AppRoutes.login:
return MaterialPageRoute(
builder: (_) => _buildLoginScreen(),
settings: settings,
);
default:
return MaterialPageRoute(
builder: (_) => _buildErrorScreen('Route not found: ${settings.name}'),
settings: settings,
);
}
}
Widget _buildLoginScreen() {
return Scaffold(
appBar: AppBar(
title: const Text('Login Required'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.lock_outline,
size: 64,
color: Colors.grey,
),
const SizedBox(height: 16),
const Text(
'Please login to access this feature',
style: TextStyle(fontSize: 16),
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: () {
// Navigation will be handled by the navigation scaffold
},
child: const Text('Go to Login'),
),
],
),
),
);
}
Widget _buildErrorScreen(String message) {
return Scaffold(
appBar: AppBar(
title: const Text('Error'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.error_outline,
size: 64,
color: Colors.red,
),
const SizedBox(height: 16),
Text(
message,
style: const TextStyle(fontSize: 16),
textAlign: TextAlign.center,
),
],
),
),
);
}
}
@@ -0,0 +1,178 @@
import 'package:flutter/material.dart';
import '../home/home_screen.dart';
import '../immich/immich_screen.dart';
import '../nostr_events/nostr_events_screen.dart';
import '../session/session_screen.dart';
import '../settings/settings_screen.dart';
import '../../data/session/session_service.dart';
import '../../data/local/local_storage_service.dart';
import '../../data/nostr/nostr_service.dart';
import '../../data/sync/sync_engine.dart';
import '../../data/firebase/firebase_service.dart';
import '../../data/immich/immich_service.dart';
/// Main navigation scaffold with bottom navigation bar.
class MainNavigationScaffold extends StatefulWidget {
final SessionService? sessionService;
final LocalStorageService? localStorageService;
final NostrService? nostrService;
final SyncEngine? syncEngine;
final FirebaseService? firebaseService;
final ImmichService? immichService;
const MainNavigationScaffold({
super.key,
this.sessionService,
this.localStorageService,
this.nostrService,
this.syncEngine,
this.firebaseService,
this.immichService,
});
@override
State<MainNavigationScaffold> createState() => _MainNavigationScaffoldState();
}
class _MainNavigationScaffoldState extends State<MainNavigationScaffold> {
int _currentIndex = 0;
int _loginStateVersion = 0; // Increment when login state changes
int? _pendingProtectedRoute; // Track if user was trying to access a protected route
void _onItemTapped(int index) {
setState(() {
_currentIndex = index;
// If accessing a protected route (Immich=1, Nostr=2) while not logged in, remember it
if ((index == 1 || index == 2) && !(widget.sessionService?.isLoggedIn ?? false)) {
_pendingProtectedRoute = index;
} else {
_pendingProtectedRoute = null;
}
});
}
/// Callback to notify that login state may have changed
void _onSessionStateChanged() {
setState(() {
_loginStateVersion++; // Force rebuild when login state changes
// If user just logged in and was trying to access a protected route, navigate there
if (widget.sessionService?.isLoggedIn == true && _pendingProtectedRoute != null) {
_currentIndex = _pendingProtectedRoute!;
_pendingProtectedRoute = null;
}
});
}
Widget _buildScreen(int index) {
switch (index) {
case 0:
return HomeScreen(
localStorageService: widget.localStorageService,
);
case 1:
// Check auth guard for Immich
if (!(widget.sessionService?.isLoggedIn ?? false)) {
return _buildLoginRequiredScreen();
}
return ImmichScreen(
localStorageService: widget.localStorageService,
immichService: widget.immichService,
);
case 2:
// Check auth guard for Nostr Events
if (!(widget.sessionService?.isLoggedIn ?? false)) {
return _buildLoginRequiredScreen();
}
return NostrEventsScreen(
nostrService: widget.nostrService,
syncEngine: widget.syncEngine,
);
case 3:
return SessionScreen(
sessionService: widget.sessionService,
firebaseService: widget.firebaseService,
onSessionChanged: _onSessionStateChanged,
);
case 4:
return SettingsScreen(
firebaseService: widget.firebaseService,
nostrService: widget.nostrService,
syncEngine: widget.syncEngine,
);
default:
return const SizedBox();
}
}
Widget _buildLoginRequiredScreen() {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.lock_outline,
size: 64,
color: Colors.grey,
),
const SizedBox(height: 16),
const Text(
'Please login to access this feature',
style: TextStyle(fontSize: 16),
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: () {
setState(() {
_currentIndex = 3; // Navigate to Session tab
});
},
child: const Text('Go to Login'),
),
],
),
),
);
}
@override
Widget build(BuildContext context) {
// Use login state version to force IndexedStack rebuild when login changes
return Scaffold(
body: IndexedStack(
key: ValueKey('nav_$_currentIndex\_v$_loginStateVersion'),
index: _currentIndex,
children: List.generate(5, (index) => _buildScreen(index)),
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: _currentIndex,
onTap: _onItemTapped,
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.photo_library),
label: 'Immich',
),
BottomNavigationBarItem(
icon: Icon(Icons.cloud),
label: 'Nostr',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Session',
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
label: 'Settings',
),
],
),
);
}
}
@@ -0,0 +1,61 @@
import 'package:flutter/material.dart';
import '../../data/nostr/nostr_service.dart';
import '../../data/sync/sync_engine.dart';
/// Screen for displaying Nostr events (placeholder).
class NostrEventsScreen extends StatelessWidget {
final NostrService? nostrService;
final SyncEngine? syncEngine;
const NostrEventsScreen({
super.key,
this.nostrService,
this.syncEngine,
});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Nostr Events'),
),
body: const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.cloud_outlined,
size: 64,
color: Colors.grey,
),
SizedBox(height: 16),
Text(
'Nostr Events',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 8),
Text(
'This screen will display Nostr events',
style: TextStyle(
fontSize: 14,
color: Colors.grey,
),
),
SizedBox(height: 24),
Text(
'Placeholder: Add your Nostr events UI here',
style: TextStyle(
fontSize: 12,
color: Colors.grey,
),
),
],
),
),
);
}
}
+368
View File
@@ -0,0 +1,368 @@
import 'package:flutter/material.dart';
import '../../data/session/session_service.dart';
import '../../data/firebase/firebase_service.dart';
/// Screen for user session management (login/logout).
class SessionScreen extends StatefulWidget {
final SessionService? sessionService;
final FirebaseService? firebaseService;
final VoidCallback? onSessionChanged;
const SessionScreen({
super.key,
this.sessionService,
this.firebaseService,
this.onSessionChanged,
});
@override
State<SessionScreen> createState() => _SessionScreenState();
}
class _SessionScreenState extends State<SessionScreen> {
final TextEditingController _usernameController = TextEditingController();
final TextEditingController _userIdController = TextEditingController();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
bool _isLoading = false;
bool _useFirebaseAuth = false;
@override
void initState() {
super.initState();
// Check if Firebase Auth is available
_useFirebaseAuth = widget.firebaseService?.isEnabled == true &&
widget.firebaseService?.config.authEnabled == true;
}
@override
void dispose() {
_usernameController.dispose();
_userIdController.dispose();
_emailController.dispose();
_passwordController.dispose();
super.dispose();
}
Future<void> _handleLogin() async {
if (widget.sessionService == null) return;
setState(() {
_isLoading = true;
});
try {
if (_useFirebaseAuth && widget.firebaseService != null) {
// Use Firebase Auth for authentication
final email = _emailController.text.trim();
final password = _passwordController.text.trim();
if (email.isEmpty || password.isEmpty) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Please enter email and password'),
),
);
}
return;
}
// Authenticate with Firebase
final firebaseUser = await widget.firebaseService!.loginWithEmailPassword(
email: email,
password: password,
);
// Create session with Firebase user info
await widget.sessionService!.login(
id: firebaseUser.uid,
username: firebaseUser.email?.split('@').first ?? firebaseUser.uid,
token: await firebaseUser.getIdToken(),
);
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Login successful'),
),
);
setState(() {});
// Notify parent that session state changed
widget.onSessionChanged?.call();
}
} else {
// Simple validation mode (no Firebase Auth)
final username = _usernameController.text.trim();
final userId = _userIdController.text.trim();
if (username.isEmpty || userId.isEmpty) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Please enter username and user ID'),
),
);
}
return;
}
// Basic validation: require minimum length
if (userId.length < 3) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('User ID must be at least 3 characters'),
),
);
}
return;
}
if (username.length < 2) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Username must be at least 2 characters'),
),
);
}
return;
}
// Create session (demo mode - no real authentication)
await widget.sessionService!.login(
id: userId,
username: username,
);
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Login successful (demo mode)'),
backgroundColor: Colors.orange,
),
);
setState(() {});
// Notify parent that session state changed
widget.onSessionChanged?.call();
}
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Login failed: ${e.toString().replaceAll('FirebaseException: ', '').replaceAll('SessionException: ', '')}'),
backgroundColor: Colors.red,
),
);
}
} finally {
if (mounted) {
setState(() {
_isLoading = false;
});
}
}
}
Future<void> _handleLogout() async {
if (widget.sessionService == null) return;
setState(() {
_isLoading = true;
});
try {
// Logout from session service first
await widget.sessionService!.logout();
// Also logout from Firebase Auth if enabled
if (_useFirebaseAuth && widget.firebaseService != null) {
try {
await widget.firebaseService!.logout();
} catch (e) {
// Log error but don't fail logout - session is already cleared
debugPrint('Warning: Firebase logout failed: $e');
}
}
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Logout successful'),
),
);
setState(() {});
// Notify parent that session state changed
widget.onSessionChanged?.call();
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Logout failed: ${e.toString().replaceAll('SessionException: ', '')}'),
backgroundColor: Colors.red,
),
);
}
} finally {
if (mounted) {
setState(() {
_isLoading = false;
});
}
}
}
@override
Widget build(BuildContext context) {
final isLoggedIn = widget.sessionService?.isLoggedIn ?? false;
final currentUser = widget.sessionService?.currentUser;
return Scaffold(
appBar: AppBar(
title: const Text('Session'),
),
body: _isLoading
? const Center(child: CircularProgressIndicator())
: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (isLoggedIn && currentUser != null) ...[
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Current Session',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 12),
Text('User ID: ${currentUser.id}'),
Text('Username: ${currentUser.username}'),
Text(
'Created: ${DateTime.fromMillisecondsSinceEpoch(currentUser.createdAt).toString().split('.')[0]}',
),
],
),
),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _handleLogout,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
),
child: const Text('Logout'),
),
] else ...[
const Icon(
Icons.person_outline,
size: 64,
color: Colors.grey,
),
const SizedBox(height: 16),
const Text(
'Login',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
if (!_useFirebaseAuth) ...[
const SizedBox(height: 8),
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.orange.shade50,
borderRadius: BorderRadius.circular(8),
border: Border.all(color: Colors.orange.shade200),
),
child: Row(
children: [
Icon(Icons.info_outline, color: Colors.orange.shade700, size: 20),
const SizedBox(width: 8),
Expanded(
child: Text(
'Demo mode: No authentication required. Enter any valid user ID and username.',
style: TextStyle(
fontSize: 12,
color: Colors.orange.shade700,
),
),
),
],
),
),
],
const SizedBox(height: 24),
if (_useFirebaseAuth) ...[
TextField(
controller: _emailController,
decoration: const InputDecoration(
labelText: 'Email',
hintText: 'Enter your email',
border: OutlineInputBorder(),
),
keyboardType: TextInputType.emailAddress,
autofillHints: const [AutofillHints.email],
),
const SizedBox(height: 16),
TextField(
controller: _passwordController,
decoration: const InputDecoration(
labelText: 'Password',
hintText: 'Enter your password',
border: OutlineInputBorder(),
),
obscureText: true,
autofillHints: const [AutofillHints.password],
),
] else ...[
TextField(
controller: _userIdController,
decoration: const InputDecoration(
labelText: 'User ID',
hintText: 'Enter user ID (min 3 characters)',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 16),
TextField(
controller: _usernameController,
decoration: const InputDecoration(
labelText: 'Username',
hintText: 'Enter username (min 2 characters)',
border: OutlineInputBorder(),
),
),
],
const SizedBox(height: 24),
ElevatedButton(
onPressed: _isLoading ? null : _handleLogin,
child: _isLoading
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text('Login'),
),
],
],
),
),
);
}
}
+78
View File
@@ -0,0 +1,78 @@
import 'package:flutter/material.dart';
import '../../data/firebase/firebase_service.dart';
import '../../data/nostr/nostr_service.dart';
import '../../data/sync/sync_engine.dart';
import '../relay_management/relay_management_screen.dart';
import '../relay_management/relay_management_controller.dart';
/// Settings screen (placeholder).
class SettingsScreen extends StatelessWidget {
final FirebaseService? firebaseService;
final NostrService? nostrService;
final SyncEngine? syncEngine;
const SettingsScreen({
super.key,
this.firebaseService,
this.nostrService,
this.syncEngine,
});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Settings'),
),
body: ListView(
children: [
if (firebaseService != null)
SwitchListTile(
title: const Text('Firebase Enabled'),
subtitle: Text(
firebaseService!.isEnabled
? 'Firebase services are active'
: 'Firebase services are disabled',
),
value: firebaseService!.isEnabled,
onChanged: null, // Read-only for now
),
if (nostrService != null && syncEngine != null) ...[
const Divider(),
ListTile(
leading: const Icon(Icons.cloud),
title: const Text('Relay Management'),
subtitle: const Text('Manage Nostr relays'),
trailing: const Icon(Icons.chevron_right),
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => RelayManagementScreen(
controller: RelayManagementController(
nostrService: nostrService!,
syncEngine: syncEngine!,
),
),
),
);
},
),
],
const Divider(),
const ListTile(
leading: Icon(Icons.info_outline),
title: Text('App Version'),
subtitle: Text('1.0.0'),
),
const Divider(),
const ListTile(
leading: Icon(Icons.help_outline),
title: Text('About'),
subtitle: Text('Flutter Modular App Boilerplate'),
),
],
),
);
}
}