Files
m8chat-app2/lib/features/calls/presentation/incoming_call_overlay.dart
help4bis a36df1c961 feat: encrypted-history prompt, incoming-call alerts, message edit, panic button (v1.7.0+11)
P1 key_restore_prompt: offer recovery-key restore right after login
P2 incoming calls: mount overlay in app.dart + detect MSC3401 call.member
   (Element X signalling) — calls were previously invisible
P3 message edit: editMessage/EditMessage + Edit dialog (reactions/delete
   were already wired)
P5 panic button: new feature/panic module — hold-to-send alert text +
   m.location to a configured room; alert sent before geolocation so a
   denied permission never blocks it

All verified end-to-end in headless Chromium against production build.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-04 07:30:57 +10:00

194 lines
6.0 KiB
Dart

// Version: 1.3.0 | Created: 2026-04-01 | Updated: 2026-07-04
// IncomingCallOverlay — full-screen overlay shown when an m.call.invite
// arrives. Displays caller name/avatar, and Accept / Decline buttons.
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../app/router.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../../../shared/widgets/matrix_avatar.dart';
import '../data/matrixrtc_repository.dart';
import '../domain/incoming_call.dart';
part 'incoming_call_overlay.g.dart';
// ---------------------------------------------------------------------------
// Provider that surfaces the latest incoming call (or null when idle)
// ---------------------------------------------------------------------------
@riverpod
Stream<IncomingCall?> incomingCallStream(Ref ref) async* {
yield null; // idle initial state
final repo = ref.watch(matrixRtcRepositoryProvider);
await for (final call in repo.incomingCallStream) {
yield call;
}
}
// ---------------------------------------------------------------------------
// Widget
// ---------------------------------------------------------------------------
/// Wrap this around the top-level router widget to detect and display incoming
/// calls. Listens to [incomingCallStreamProvider] and shows the overlay when
/// a call arrives.
class IncomingCallOverlayHost extends ConsumerWidget {
const IncomingCallOverlayHost({super.key, required this.child});
final Widget child;
@override
Widget build(BuildContext context, WidgetRef ref) {
final callAsync = ref.watch(incomingCallStreamProvider);
return Stack(
children: [
child,
callAsync.when(
loading: () => const SizedBox.shrink(),
error: (_, __) => const SizedBox.shrink(),
data: (call) {
if (call == null) return const SizedBox.shrink();
return _IncomingCallOverlay(call: call);
},
),
],
);
}
}
class _IncomingCallOverlay extends ConsumerStatefulWidget {
const _IncomingCallOverlay({required this.call});
final IncomingCall call;
@override
ConsumerState<_IncomingCallOverlay> createState() =>
_IncomingCallOverlayState();
}
class _IncomingCallOverlayState extends ConsumerState<_IncomingCallOverlay> {
bool _dismissed = false;
String? _dismissedForCallId;
@override
Widget build(BuildContext context) {
// A new call resets the dismissal so the overlay can show again.
if (_dismissedForCallId != widget.call.callId) {
_dismissed = false;
_dismissedForCallId = widget.call.callId;
}
if (_dismissed) return const SizedBox.shrink();
final theme = Theme.of(context);
final call = widget.call;
return Positioned.fill(
child: Material(
color: Colors.black.withAlpha(220),
child: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Caller avatar
MatrixAvatar(
name: call.callerDisplayName.isNotEmpty
? call.callerDisplayName
: call.callerId,
avatarUrl: call.callerAvatarUrl,
radius: 56,
),
const SizedBox(height: 24),
// Caller name
Text(
call.callerDisplayName.isNotEmpty
? call.callerDisplayName
: call.callerId,
style: theme.textTheme.headlineMedium?.copyWith(
color: Colors.white,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 8),
Text(
call.isVideo ? 'Incoming video call' : 'Incoming voice call',
style: TextStyle(
color: Colors.white.withAlpha(179),
fontSize: 16,
),
),
const SizedBox(height: 64),
// Accept / Decline
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
_CallActionButton(
icon: Icons.call_end,
label: 'Decline',
colour: Colors.red,
onTap: () => setState(() => _dismissed = true),
),
_CallActionButton(
icon: call.isVideo ? Icons.videocam : Icons.call,
label: 'Accept',
colour: Colors.green,
onTap: () {
// The overlay lives above the router's navigator, so
// context.push would not find GoRouter — use the
// router instance directly, then dismiss the overlay.
setState(() => _dismissed = true);
ref
.read(routerProvider)
.push('/calls/${Uri.encodeComponent(call.roomId)}');
},
),
],
),
],
),
),
),
);
}
}
class _CallActionButton extends StatelessWidget {
const _CallActionButton({
required this.icon,
required this.label,
required this.colour,
required this.onTap,
});
final IconData icon;
final String label;
final Color colour;
final VoidCallback onTap;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 72,
height: 72,
decoration: BoxDecoration(color: colour, shape: BoxShape.circle),
child: Icon(icon, color: Colors.white, size: 32),
),
const SizedBox(height: 8),
Text(
label,
style: const TextStyle(color: Colors.white, fontSize: 14),
),
],
),
);
}
}