import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:flutter/services.dart'; import '../l10n/app_localizations.dart'; import '../providers/auth_provider.dart'; import '../providers/settings_provider.dart'; import '../services/api_service.dart'; import '../widgets/android_quick_stream_panel.dart'; class MyStreamPage extends StatefulWidget { const MyStreamPage({super.key}); @override State createState() => _MyStreamPageState(); } class _MyStreamPageState extends State { Map? _roomInfo; bool _isLoading = false; @override void initState() { super.initState(); _fetchMyRoom(); } Future _fetchMyRoom() async { setState(() => _isLoading = true); final settings = context.read(); final auth = context.read(); final api = ApiService(settings, auth.token); try { final response = await api.getMyRoom(); if (!mounted) { return; } if (response.statusCode == 200) { setState(() => _roomInfo = jsonDecode(response.body)); } } catch (e) { if (!mounted) { return; } final l10n = AppLocalizations.of(context); ScaffoldMessenger.of( context, ).showSnackBar(SnackBar(content: Text(l10n?.failedToFetchRoomInfo ?? "Failed to fetch room info"))); } finally { if (mounted) { setState(() => _isLoading = false); } } } @override Widget build(BuildContext context) { final settings = context.watch(); final l10n = AppLocalizations.of(context)!; return Scaffold( appBar: AppBar(title: Text(l10n.myStreamConsole)), body: _isLoading ? const Center(child: CircularProgressIndicator()) : _roomInfo == null ? Center(child: Text(l10n.noRoomInfo)) : SingleChildScrollView( padding: const EdgeInsets.all(20), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _buildInfoCard( title: l10n.roomTitle, value: _roomInfo!['title'], icon: Icons.edit, onTap: () { // TODO: Implement title update API later ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text("Title editing coming soon!")), ); }, ), const SizedBox(height: 20), _buildInfoCard( title: l10n.rtmpServerUrl, value: settings.rtmpUrl, icon: Icons.copy, onTap: () { Clipboard.setData(ClipboardData(text: settings.rtmpUrl)); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(l10n.copiedToClipboard), ), ); }, ), const SizedBox(height: 20), _buildInfoCard( title: l10n.streamKey, value: _roomInfo!['stream_key'], icon: Icons.copy, isSecret: true, onTap: () { Clipboard.setData( ClipboardData(text: _roomInfo!['stream_key']), ); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(l10n.copiedToClipboard), ), ); }, ), const SizedBox(height: 24), AndroidQuickStreamPanel( rtmpBaseUrl: settings.rtmpUrl, streamKey: _roomInfo!['stream_key'], ), const SizedBox(height: 30), const Center( child: Column( children: [ Icon(Icons.info_outline, color: Colors.grey), SizedBox(height: 8), Text( "Use OBS or other tools to stream to this address.", textAlign: TextAlign.center, style: TextStyle(color: Colors.grey, fontSize: 12), ), ], ), ), ], ), ), ); } Widget _buildInfoCard({ required String title, required String value, required IconData icon, bool isSecret = false, VoidCallback? onTap, }) { return Card( child: ListTile( title: Text(title, style: const TextStyle(fontSize: 12, color: Colors.grey)), subtitle: Text( isSecret ? "••••••••••••••••" : value, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16), ), trailing: IconButton(icon: Icon(icon), onPressed: onTap), ), ); } }