import 'package:comunic/helpers/calls_helper.dart'; import 'package:comunic/helpers/conversations_helper.dart'; import 'package:comunic/models/conversation.dart'; import 'package:comunic/ui/widgets/comunic_back_button_widget.dart'; import 'package:comunic/ui/widgets/safe_state.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; import 'package:flutter/material.dart'; /// Call screen /// /// @author Pierre Hubert class CallScreen extends StatefulWidget { final int convID; const CallScreen({Key key, @required this.convID}) : assert(convID != null), assert(convID > 0), super(key: key); @override _CallScreenState createState() => _CallScreenState(); } class _CallScreenState extends SafeState { // Widget properties int get convID => widget.convID; // State properties Conversation _conversation; String _convName; var _error = false; @override void initState() { super.initState(); _initCall(); } @override void dispose() { super.dispose(); _endCall(); } void _initCall() async { try { setState(() => _error = false); // First, load information about the conversation _conversation = await ConversationsHelper().getSingleOrThrow(convID); _convName = await ConversationsHelper.getConversationNameAsync(_conversation); assert(_convName != null); setState(() {}); // Join the call await CallsHelper.join(convID); } catch (e, stack) { print("Could not initialize call! $e\n$stack"); setState(() => _error = true); } } void _endCall() async { try { // Leave the call await CallsHelper.leave(convID); } catch (e, stack) { print("Could not end call properly! $e\n$stack"); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( leading: ComunicBackButton(), title: _convName == null ? CircularProgressIndicator() : Text(_convName), ), body: _buildBody(), ); } /// Build widget body Widget _buildBody() { if (_error) // Handle errors return buildErrorCard(tr("Could not initialize call!"), actions: [ MaterialButton( onPressed: () => _initCall(), child: Text(tr("Try again").toUpperCase()), ) ]); } }