2020-04-20 11:24:40 +00:00
|
|
|
import 'package:comunic/helpers/calls_helper.dart';
|
2020-04-20 11:19:49 +00:00
|
|
|
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';
|
2020-04-20 08:53:25 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-04-20 11:19:49 +00:00
|
|
|
class _CallScreenState extends SafeState<CallScreen> {
|
|
|
|
// 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(() {});
|
2020-04-20 11:24:40 +00:00
|
|
|
|
|
|
|
// Join the call
|
|
|
|
await CallsHelper.join(convID);
|
2020-04-20 11:19:49 +00:00
|
|
|
} catch (e, stack) {
|
|
|
|
print("Could not initialize call! $e\n$stack");
|
|
|
|
setState(() => _error = true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 11:24:40 +00:00
|
|
|
void _endCall() async {
|
|
|
|
try {
|
|
|
|
// Leave the call
|
|
|
|
await CallsHelper.leave(convID);
|
|
|
|
} catch (e, stack) {
|
|
|
|
print("Could not end call properly! $e\n$stack");
|
|
|
|
}
|
|
|
|
}
|
2020-04-20 11:19:49 +00:00
|
|
|
|
2020-04-20 08:53:25 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-04-20 11:19:49 +00:00
|
|
|
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()),
|
|
|
|
)
|
|
|
|
]);
|
2020-04-20 08:53:25 +00:00
|
|
|
}
|
|
|
|
}
|