/
call_container.dart
186 lines (157 loc) · 5.14 KB
/
call_container.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import 'dart:async';
import 'package:flutter/material.dart';
import '../../stream_video_flutter.dart';
/// Builder used to create a custom incoming call widget.
typedef IncomingCallBuilder = Widget Function(
BuildContext context,
Call call,
CallState callState,
);
/// Builder used to create a custom outgoing call widget.
typedef OutgoingCallBuilder = Widget Function(
BuildContext context,
Call call,
CallState callState,
);
/// Builder used to create a custom call content widget.
typedef CallContentBuilder = Widget Function(
BuildContext context,
Call call,
CallState callState,
);
/// Builder used to create a custom widget for participants avatars.
typedef ParticipantsAvatarBuilder = Widget Function(
BuildContext context,
Call call,
CallState callState,
List<UserInfo> participants,
);
/// Builder used to create a custom widget for participants display names.
typedef ParticipantsDisplayNameBuilder = Widget Function(
BuildContext context,
Call call,
CallState callState,
List<UserInfo> participants,
);
/// Represents different call content based on the call state.
class StreamCallContainer extends StatefulWidget {
/// Creates a new instance of [StreamCallContainer].
const StreamCallContainer({
super.key,
required this.call,
this.callConnectOptions,
this.onBackPressed,
this.onLeaveCallTap,
this.onAcceptCallTap,
this.onDeclineCallTap,
this.onCancelCallTap,
this.incomingCallBuilder,
this.outgoingCallBuilder,
this.callContentBuilder,
});
/// Represents a call.
final Call call;
/// Options used while connecting to the call.
final CallConnectOptions? callConnectOptions;
/// The action to perform when the back button is pressed.
final VoidCallback? onBackPressed;
/// The action to perform when the leave call button is tapped.
final VoidCallback? onLeaveCallTap;
/// The action to perform when the accept call button is tapped.
final VoidCallback? onAcceptCallTap;
/// The action to perform when the decline call button is tapped.
final VoidCallback? onDeclineCallTap;
/// The action to perform when the cancel call button is tapped.
final VoidCallback? onCancelCallTap;
/// Builder used to create a custom incoming call widget.
final IncomingCallBuilder? incomingCallBuilder;
/// Builder used to create a custom outgoing call widget.
final OutgoingCallBuilder? outgoingCallBuilder;
/// Builder used to create a custom call content widget.
final CallContentBuilder? callContentBuilder;
@override
State<StreamCallContainer> createState() => _StreamCallContainerState();
}
class _StreamCallContainerState extends State<StreamCallContainer> {
final _logger = taggedLogger(tag: 'SV:CallContainer');
StreamSubscription<CallState>? _callStateSubscription;
/// Represents a call.
Call get call => widget.call;
/// Holds information about the call.
late CallState _callState;
@override
void initState() {
super.initState();
_callStateSubscription = call.state.listen(_setState);
_callState = call.state.value;
_connect();
}
@override
void dispose() {
_callStateSubscription?.cancel();
_callStateSubscription = null;
super.dispose();
}
void _setState(CallState callState) {
_logger.v(() => '[setState] callState.status: ${callState.status}');
setState(() {
_callState = callState;
});
if (callState.status.isDisconnected) {
_leave();
}
}
@override
Widget build(BuildContext context) {
final status = _callState.status;
_logger.v(() => '[build] status: $status');
if (status is CallStatusIncoming && !status.acceptedByMe) {
return widget.incomingCallBuilder?.call(context, call, _callState) ??
StreamIncomingCallContent(
call: call,
callState: _callState,
onAcceptCallTap: widget.onAcceptCallTap,
onDeclineCallTap: widget.onDeclineCallTap,
);
} else if (status is CallStatusOutgoing && !status.acceptedByCallee) {
return widget.outgoingCallBuilder?.call(context, call, _callState) ??
StreamOutgoingCallContent(
call: call,
callState: _callState,
onCancelCallTap: widget.onCancelCallTap,
);
} else {
return widget.callContentBuilder?.call(context, call, _callState) ??
StreamCallContent(
call: call,
callState: _callState,
onBackPressed: widget.onBackPressed,
onLeaveCallTap: widget.onLeaveCallTap,
);
}
}
Future<void> _connect() async {
try {
_logger.d(() => '[connect] no args');
if (widget.callConnectOptions != null) {
call.connectOptions = widget.callConnectOptions!;
}
final result = await call.join();
_logger.v(() => '[connect] completed: $result');
} catch (e) {
_logger.v(() => '[connect] failed: $e');
await _leave();
}
}
Future<void> _leave() async {
_logger.d(() => '[leave] no args');
// play tone
final bool popped;
if (mounted) {
popped = await Navigator.maybePop(context);
} else {
popped = false;
}
_logger.v(() => '[leave] popped: $popped');
}
}