feat: Phase 2 complete — calls, media, spaces, persistence, chat improvements

- LiveKit/MatrixRTC voice+video calls with full call screen UI
- Incoming call overlay (accept/decline)
- Media upload/download — file picker, image rendering, file download
- Spaces navigation — space list + expandable child rooms
- Drift persistence — rooms + messages written on every sync
- Sync persistence auto-starts on login and session restore
- Chat: typing indicators, long-press menu, reply, emoji reactions
- User search dialog + start DM from rooms screen
- Android: INTERNET + CAMERA + RECORD_AUDIO permissions in main manifest
- Emoji picker for reactions

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-02 06:48:03 +10:00
parent 8f13c725a4
commit f12a7ac1fd
20 changed files with 2458 additions and 191 deletions

View File

@@ -1,25 +1,109 @@
// Version: 1.0.0 | Created: 2026-04-01
// Call controller stub. LiveKit integration deferred to Phase 2.
// Version: 1.1.0 | Created: 2026-04-01
// Call controller — manages LiveKit room connection lifecycle.
// Transitions through idle → connecting → active → ended states.
import 'dart:async';
import 'package:livekit_client/livekit_client.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../data/livekit_service.dart';
import '../domain/call_state.dart';
part 'call_controller.g.dart';
@Riverpod(keepAlive: false)
class CallController extends _$CallController {
Timer? _durationTimer;
Duration _elapsed = Duration.zero;
@override
CallState build() => const CallState.idle();
/// Phase 2: join a LiveKit room via MatrixRTC JWT endpoint.
Future<void> joinCall(String roomId) async {
/// Join a LiveKit room via MatrixRTC JWT endpoint.
///
/// On success, starts a timer to track call duration and transitions to
/// [CallActive]. On failure, transitions to [CallEnded] with a reason.
Future<void> joinCall(String roomId, {bool withVideo = true}) async {
state = CallState.connecting(roomId: roomId);
// TODO(phase2): fetch JWT from AppConfig.livekitJwtUrl and connect LiveKit client.
state = const CallState.ended(reason: 'Calls not yet implemented.');
final service = ref.read(liveKitServiceProvider);
final result = await service.connect(roomId);
switch (result) {
case LiveKitConnected(:final room):
// Enable camera and microphone on the local participant.
final local = room.localParticipant;
if (local != null) {
if (withVideo) {
await local.setCameraEnabled(true);
}
await local.setMicrophoneEnabled(true);
}
_startTimer(roomId, room, isVideo: withVideo);
case LiveKitFailed(:final failure):
state = CallEnded(
reason: switch (failure) {
LiveKitNotAuthenticated() => 'Not authenticated.',
LiveKitJwtFetchFailed(:final message) =>
'Could not connect: $message',
LiveKitConnectFailed(:final message) => 'Call failed: $message',
},
);
}
}
/// Toggle microphone on/off during an active call.
Future<void> toggleAudio() async {
final current = state;
if (current is! CallActive) return;
final room = ref.read(liveKitServiceProvider).activeRoom;
final local = room?.localParticipant;
if (local == null) return;
final newEnabled = !current.isAudioEnabled;
await local.setMicrophoneEnabled(newEnabled);
state = current.copyWith(isAudioEnabled: newEnabled);
}
/// Toggle camera on/off during an active call.
Future<void> toggleVideo() async {
final current = state;
if (current is! CallActive) return;
final room = ref.read(liveKitServiceProvider).activeRoom;
final local = room?.localParticipant;
if (local == null) return;
final newEnabled = !current.isVideoEnabled;
await local.setCameraEnabled(newEnabled);
state = current.copyWith(isVideoEnabled: newEnabled);
}
/// Hang up — disconnect from LiveKit and transition to [CallEnded].
Future<void> endCall() async {
_durationTimer?.cancel();
_durationTimer = null;
await ref.read(liveKitServiceProvider).disconnect();
state = const CallState.ended();
}
void _startTimer(String roomId, Room room, {required bool isVideo}) {
_elapsed = Duration.zero;
state = CallActive(
roomId: roomId,
duration: _elapsed,
isVideoEnabled: isVideo,
isAudioEnabled: true,
);
_durationTimer = Timer.periodic(const Duration(seconds: 1), (_) {
_elapsed += const Duration(seconds: 1);
final current = state;
if (current is CallActive) {
state = current.copyWith(duration: _elapsed);
}
});
}
}

View File

@@ -1,68 +1,190 @@
// Version: 1.0.0 | Created: 2026-04-01
// Call screen skeleton. Phase 2 will wire in LiveKit video/audio.
// Version: 1.1.0 | Created: 2026-04-01
// Full call screen with LiveKit video/audio.
// - Remote video: full screen background
// - Local video: picture-in-picture overlay (bottom right)
// - Controls: mute, toggle video, end call
// - Duration timer and participant name
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart' show RTCVideoViewObjectFit;
import 'package:go_router/go_router.dart';
import 'package:livekit_client/livekit_client.dart';
import '../data/livekit_service.dart';
import '../domain/call_state.dart';
import 'call_controller.dart';
class CallScreen extends ConsumerWidget {
class CallScreen extends ConsumerStatefulWidget {
const CallScreen({super.key, required this.roomId});
final String roomId;
@override
Widget build(BuildContext context, WidgetRef ref) {
ConsumerState<CallScreen> createState() => _CallScreenState();
}
class _CallScreenState extends ConsumerState<CallScreen> {
@override
void initState() {
super.initState();
// Start the call as soon as the screen opens.
// Using addPostFrameCallback so the provider is ready.
WidgetsBinding.instance.addPostFrameCallback((_) {
ref
.read(callControllerProvider.notifier)
.joinCall(widget.roomId, withVideo: true);
});
}
@override
Widget build(BuildContext context) {
final callState = ref.watch(callControllerProvider);
// Pop back automatically when call ends.
ref.listen<CallState>(callControllerProvider, (_, next) {
if (next is CallEnded && context.canPop()) {
context.pop();
}
});
return Scaffold(
backgroundColor: Colors.black,
body: SafeArea(
child: Column(
child: Stack(
children: [
Expanded(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.videocam_off_outlined,
size: 80,
color: Colors.white.withAlpha(153),
),
const SizedBox(height: 16),
Text(
switch (callState) {
CallConnecting() => 'Connecting...',
CallEnded(:final reason) => reason ?? 'Call ended.',
_ => 'Call (Phase 2)',
},
style: const TextStyle(color: Colors.white, fontSize: 18),
),
const SizedBox(height: 8),
Text(
'Video calls will be available in the next release.',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white.withAlpha(153),
fontSize: 14,
),
),
],
),
),
// Remote video — full screen
const _RemoteVideoView(),
// Local PiP — bottom right
if (callState is CallActive && callState.isVideoEnabled)
const _LocalVideoPip(),
// Connecting / connecting overlay
if (callState is CallConnecting) const _ConnectingOverlay(),
// Call controls — pinned to bottom
Positioned(
left: 0,
right: 0,
bottom: 24,
child: _CallControls(roomId: widget.roomId),
),
Padding(
padding: const EdgeInsets.all(32),
child: FloatingActionButton(
backgroundColor: Colors.red,
onPressed: () {
ref.read(callControllerProvider.notifier).endCall();
context.pop();
},
child: const Icon(Icons.call_end, color: Colors.white),
// Participant info — top left
Positioned(
top: 16,
left: 16,
child: _CallInfo(callState: callState),
),
],
),
),
);
}
}
// ---------------------------------------------------------------------------
// Remote video view
// ---------------------------------------------------------------------------
class _RemoteVideoView extends ConsumerWidget {
const _RemoteVideoView();
@override
Widget build(BuildContext context, WidgetRef ref) {
final room = ref.watch(liveKitServiceProvider).activeRoom;
if (room == null) {
return const _NoVideoPlaceholder();
}
final remoteParticipants = room.remoteParticipants.values.toList();
if (remoteParticipants.isEmpty) {
return const _NoVideoPlaceholder();
}
// Show the first remote participant's first video track.
final firstParticipant = remoteParticipants.first;
final videoPubs = firstParticipant.videoTrackPublications;
if (videoPubs.isEmpty || videoPubs.first.track == null) {
return const _NoVideoPlaceholder();
}
// safe: checked non-null above
final videoTrack = videoPubs.first.track!;
return VideoTrackRenderer(
videoTrack,
fit: RTCVideoViewObjectFit.RTCVideoViewObjectFitCover,
);
}
}
// ---------------------------------------------------------------------------
// Local PiP
// ---------------------------------------------------------------------------
class _LocalVideoPip extends ConsumerWidget {
const _LocalVideoPip();
@override
Widget build(BuildContext context, WidgetRef ref) {
final room = ref.watch(liveKitServiceProvider).activeRoom;
final local = room?.localParticipant;
if (local == null) return const SizedBox.shrink();
final videoPubs = local.videoTrackPublications;
if (videoPubs.isEmpty || videoPubs.first.track == null) {
return const SizedBox.shrink();
}
// safe: checked non-null above
final localTrack = videoPubs.first.track!;
return Positioned(
right: 16,
bottom: 120,
child: Container(
width: 100,
height: 150,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
border: Border.all(color: Colors.white38, width: 1),
),
clipBehavior: Clip.hardEdge,
child: VideoTrackRenderer(
localTrack,
mirrorMode: VideoViewMirrorMode.mirror,
),
),
);
}
}
// ---------------------------------------------------------------------------
// Placeholders and overlays
// ---------------------------------------------------------------------------
class _NoVideoPlaceholder extends StatelessWidget {
const _NoVideoPlaceholder();
@override
Widget build(BuildContext context) {
return Container(
color: const Color(0xFF1A1A2E),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.person_outline,
size: 96,
color: Colors.white.withAlpha(77),
),
const SizedBox(height: 12),
Text(
'Waiting for video...',
style: TextStyle(
color: Colors.white.withAlpha(153),
fontSize: 16,
),
),
],
@@ -71,3 +193,175 @@ class CallScreen extends ConsumerWidget {
);
}
}
class _ConnectingOverlay extends StatelessWidget {
const _ConnectingOverlay();
@override
Widget build(BuildContext context) {
return Container(
color: Colors.black.withAlpha(153),
child: const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(color: Colors.white),
SizedBox(height: 16),
Text(
'Connecting...',
style: TextStyle(color: Colors.white, fontSize: 18),
),
],
),
),
);
}
}
// ---------------------------------------------------------------------------
// Call info (top)
// ---------------------------------------------------------------------------
class _CallInfo extends StatelessWidget {
const _CallInfo({required this.callState});
final CallState callState;
@override
Widget build(BuildContext context) {
final durationText = switch (callState) {
CallActive(:final duration) => _formatDuration(duration),
CallConnecting() => 'Connecting…',
_ => '',
};
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (durationText.isNotEmpty)
Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
decoration: BoxDecoration(
color: Colors.black54,
borderRadius: BorderRadius.circular(8),
),
child: Text(
durationText,
style: const TextStyle(
color: Colors.white,
fontSize: 14,
fontFeatures: [FontFeature.tabularFigures()],
),
),
),
],
);
}
String _formatDuration(Duration d) {
final minutes = d.inMinutes.remainder(60).toString().padLeft(2, '0');
final seconds = d.inSeconds.remainder(60).toString().padLeft(2, '0');
return '${d.inHours > 0 ? '${d.inHours}:' : ''}$minutes:$seconds';
}
}
// ---------------------------------------------------------------------------
// Call controls (bottom)
// ---------------------------------------------------------------------------
class _CallControls extends ConsumerWidget {
const _CallControls({required this.roomId});
final String roomId;
@override
Widget build(BuildContext context, WidgetRef ref) {
final callState = ref.watch(callControllerProvider);
final notifier = ref.read(callControllerProvider.notifier);
final isAudioEnabled = callState is CallActive
? callState.isAudioEnabled
: true;
final isVideoEnabled = callState is CallActive
? callState.isVideoEnabled
: true;
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
// Mute button
_ControlButton(
icon: isAudioEnabled ? Icons.mic : Icons.mic_off,
label: isAudioEnabled ? 'Mute' : 'Unmute',
onTap: () => notifier.toggleAudio(),
active: isAudioEnabled,
),
// End call button — prominent red
GestureDetector(
onTap: () async {
await notifier.endCall();
if (context.mounted && context.canPop()) context.pop();
},
child: Container(
width: 72,
height: 72,
decoration: const BoxDecoration(
color: Colors.red,
shape: BoxShape.circle,
),
child: const Icon(Icons.call_end, color: Colors.white, size: 32),
),
),
// Video toggle
_ControlButton(
icon: isVideoEnabled ? Icons.videocam : Icons.videocam_off,
label: isVideoEnabled ? 'Hide video' : 'Show video',
onTap: () => notifier.toggleVideo(),
active: isVideoEnabled,
),
],
);
}
}
class _ControlButton extends StatelessWidget {
const _ControlButton({
required this.icon,
required this.label,
required this.onTap,
required this.active,
});
final IconData icon;
final String label;
final VoidCallback onTap;
final bool active;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 56,
height: 56,
decoration: BoxDecoration(
color: active
? Colors.white.withAlpha(51)
: Colors.white.withAlpha(26),
shape: BoxShape.circle,
),
child: Icon(icon, color: Colors.white, size: 24),
),
const SizedBox(height: 6),
Text(
label,
style: const TextStyle(color: Colors.white70, fontSize: 11),
),
],
),
);
}
}

View File

@@ -0,0 +1,202 @@
// Version: 1.1.0 | Created: 2026-04-01
// IncomingCallOverlay — full-screen overlay shown when an m.call.invite
// arrives. Displays caller name/avatar, and Accept / Decline buttons.
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:riverpod_annotation/riverpod_annotation.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 ConsumerWidget {
const _IncomingCallOverlay({required this.call});
final IncomingCall call;
@override
Widget build(BuildContext context, WidgetRef ref) {
final theme = Theme.of(context);
return Positioned.fill(
child: Material(
color: Colors.black.withAlpha(220),
child: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Caller avatar
_CallerAvatar(call: call),
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: () {
// Dismiss the overlay by navigating away; the repository
// stream will emit null on the next event cycle.
},
),
_CallActionButton(
icon: call.isVideo ? Icons.videocam : Icons.call,
label: 'Accept',
colour: Colors.green,
onTap: () {
context.push(
'/calls/${Uri.encodeComponent(call.roomId)}',
);
},
),
],
),
],
),
),
),
);
}
}
class _CallerAvatar extends StatelessWidget {
const _CallerAvatar({required this.call});
final IncomingCall call;
@override
Widget build(BuildContext context) {
final initials = call.callerDisplayName.isNotEmpty
? call.callerDisplayName[0].toUpperCase()
: '?';
if (call.callerAvatarUrl != null) {
return CircleAvatar(
radius: 56,
backgroundImage: CachedNetworkImageProvider(call.callerAvatarUrl!),
);
}
return CircleAvatar(
radius: 56,
backgroundColor: Theme.of(context).colorScheme.primary.withAlpha(77),
child: Text(
initials,
style: const TextStyle(
fontSize: 40,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
);
}
}
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),
),
],
),
);
}
}