1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-10-23 15:03:22 +00:00
comunicmobile/lib/ui/screens/call_screen.dart

179 lines
4.8 KiB
Dart
Raw Normal View History

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';
2020-04-20 12:24:35 +00:00
import 'package:comunic/helpers/events_helper.dart';
import 'package:comunic/helpers/users_helper.dart';
2020-04-20 12:02:32 +00:00
import 'package:comunic/lists/call_members_list.dart';
import 'package:comunic/lists/users_list.dart';
2020-04-20 11:43:17 +00:00
import 'package:comunic/models/call_config.dart';
import 'package:comunic/models/call_member.dart';
2020-04-20 11:19:49 +00:00
import 'package:comunic/models/conversation.dart';
2020-04-20 12:32:57 +00:00
import 'package:comunic/ui/routes/main_route.dart';
2020-04-20 11:19:49 +00:00
import 'package:comunic/ui/widgets/safe_state.dart';
import 'package:comunic/utils/account_utils.dart';
2020-04-20 11:19:49 +00:00
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;
2020-04-20 11:43:17 +00:00
CallConfig _conf;
2020-04-20 11:19:49 +00:00
var _error = false;
2020-04-20 12:02:32 +00:00
CallMembersList _membersList;
UsersList _usersList;
2020-04-20 11:19:49 +00:00
@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:43:17 +00:00
// Get call configuration
_conf = await CallsHelper.getConfig();
2020-04-20 12:02:32 +00:00
// Get current members of the call
final membersList = await CallsHelper.getMembers(convID);
membersList.removeUser(userID());
_usersList = await UsersHelper().getListWithThrow(membersList.usersID);
_membersList = membersList;
setState(() {});
2020-04-20 12:24:35 +00:00
// Register to events
this.listenChangeState<UserJoinedCallEvent>((e) {
if (e.callID == convID) _membersList.add(CallMember(id: e.userID));
});
this.listen<UserLeftCallEvent>((e) {
if (e.callID == convID) _removeMember(e.userID);
});
2020-04-20 12:32:57 +00:00
this.listen<CallClosedEvent>((e) {
2020-04-20 12:41:09 +00:00
if (e.callID == convID) _leaveCall(needConfirm: false);
2020-04-20 12:32:57 +00:00
});
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 12:32:57 +00:00
// Do clean up operations when call screen is destroyed
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 12:32:57 +00:00
// Make us leave the call
2020-04-20 12:41:09 +00:00
void _leaveCall({bool needConfirm = true}) async {
if (needConfirm &&
!await showConfirmDialog(
context: context,
message: tr("Do you really want to leave this call ?"))) return;
2020-04-20 12:32:57 +00:00
MainController.of(context).popPage();
}
2020-04-20 12:24:35 +00:00
void _removeMember(int memberID) {
_membersList.removeUser(memberID);
setState(() {});
}
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(
2020-04-20 12:41:09 +00:00
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () => _leaveCall(),
),
2020-04-20 11:19:49 +00:00
title:
_convName == null ? CircularProgressIndicator() : Text(_convName),
),
body: _buildBody(),
);
}
/// Build widget body
Widget _buildBody() {
2020-04-20 12:02:32 +00:00
// Handle errors
2020-04-20 11:19:49 +00:00
if (_error)
return buildErrorCard(tr("Could not initialize call!"), actions: [
MaterialButton(
onPressed: () => _initCall(),
child: Text(tr("Try again").toUpperCase()),
)
]);
// Check if are not ready to show call UI
if (_membersList == null) return buildCenteredProgressBar();
return Column(
children: <Widget>[_buildMembersArea()],
);
}
/// Build members area
Widget _buildMembersArea() {
return Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: RichText(
text: TextSpan(
children: _membersList
.map((f) => TextSpan(
text: _usersList.getUser(f.id).displayName + " ",
style: TextStyle(
color: f.status == MemberStatus.JOINED
? null
: Colors.green)))
.toList())),
),
);
2020-04-20 08:53:25 +00:00
}
}